Watching for new Mathematica questions using Mathematica and the StackOverflow API
Asked Answered
D

2

11

Unless Mr.Wizard is on vacation, it is pretty difficult to beat this phenomenon which seems to be gifted with omnipresence and omniscience. How can we outdo him using Mathematica and the StackOverflow API?

Detriment answered 28/6, 2011 at 11:25 Comment(2)
Find a bigger fish. Outdoing Leonid may require an AI that passes the Turing test. :-)Gunas
@Gunas I'm almost always able to identify Leonid's posts as his long before I see his tag. I think my unconscious heuristic is If[PostLength>1 page, Poster==Leonid], that and probably a lot of CS-ish stuff (amazingly he just told you he doesn't have a CS background).Detriment
S
15

Here's a variation on Sjoerd's solution.

The main difference is to use a docked cell instead of a popup dialog. The cell has a link to the new post, as well as a button that will clear the docked cell to the previous state.

The other difference is to use the question ID instead of the title for determining new posts. I know that titles get edited sometimes, so this trigger as a new post in that case.

storedTitle = "";
storedID = 0;
mySOWatchTask = 
  CreateScheduledTask[{lastTitle, 
     lastID} = {"title", 
      "question_id"} /. ("questions" /. 
        Import["http://api.stackoverflow.com/1.1/questions?key=       \
         QEpjCjsXYE6s_kZEkFr4Lw&page=1&pagesize=1&sort=creation&\
tagged=                mathematica", "JSON"])[[1]];
   If[lastID != storedID,
    storedTitle = lastTitle;
    storedID = lastID;
    SetOptions[$FrontEndSession, 
     DockedCells -> 
      Cell[BoxData[
        ToBoxes[Style[
          With[{dock = Options[$FrontEndSession, DockedCells]}, 
           Grid[{{Button[Style["\[CircleTimes]", 16], 
               SetOptions[$FrontEndSession, dock], 
               Appearance -> None], "New StackOverflow question: ", 
              Hyperlink[lastTitle, 
               "http://stackoverflow.com/questions/" <> 
                ToString[lastID]]}}, 
            Alignment -> {{Left, Left, Left}}, 
            ItemSize -> {{2, 14, Scaled[0.7]}}]], 
          FontFamily -> "Times"]]], "DockedCell", 
       Background -> Orange]]; EmitSound[Sound[SoundNote[]]]];, 60];

enter image description here

Seaborg answered 28/6, 2011 at 15:55 Comment(3)
+1 Nice change. I like the bit with the DockedCell setting and how you restore it to the original values. For this question and app, I got my inspiration from [Yu-Sung Chang's 'Photo booth' blog][1]. Previously, I thought of timed evaluation as nothing more than cron tasks for batch jobs, but I learned that it does a fantastic job in GUI's and apps like this. [1]: blog.wolfram.com/2011/06/23/…Detriment
I think docked cells are underused, probably because there is a bit of effort involved to create one. I have several copies of Mathematica installed, and usually at least two open, and use a custom docked cell that tells me which notebook is connected to which version. For this reason it was important to me to have a way to get back to my normal docked cell.Seaborg
I just saw this edit. It looks nice. It reminds me of the notifications that SO itself gives for new answers etc... (Aside: For a course I taught, I used a docked cell in the assignment template. It contained info about the assignment, a next question button and a submit via FTP button)Ramsay
D
15

Pretty easy actually. All you need is the following.

Define a watch task:

storedTitle = "";

mySOWatchTask =
  CreateScheduledTask[
   {
    lastTitle = 
    "title" /. ("questions" /. 
        Import["http://api.stackoverflow.com/1.1/questions?key=\
                QEpjCjsXYE6s_kZEkFr4Lw&page=1&pagesize=1&sort=creation&tagged=\
                mathematica", "JSON"])[[1]];
    If[lastTitle != storedTitle, 
      storedTitle = lastTitle; 
      EmitSound[Sound[SoundNote[]]]; 
      MessageDialog["New question: " <> lastTitle]
    ];
    },
   60
   ];

And to start this:

StartScheduledTask[mySOWatchTask];

Stop it with:

 StopScheduledTask[mySOWatchTask];

Look what's running:

 ScheduledTasks[] // Shallow

Remove the task:

 RemoveScheduledTask[mySOWatchTask];

or all tasks:

RemoveScheduledTask[ScheduledTasks[]];

This polls SO every minute (the minimum that is not seen as abusive), and displays a dialog box and a plays a sound whenever a new Mathematica question arrives.

enter image description here

The beauty of this is: it uses Mathematica 8, and we all know Mr.Wizard doesn't have that (yet) ;-)

Note that the SO API is being cached heavily, so response may not come directly. I also haven't tested this extensively.

EDIT
Please note that the key (app-id) used above is intended to be used by this small Mathematica application only. If you need one for a different application you can get one yourself in a fast and painless procedure here. It took me less than a minute.

Detriment answered 28/6, 2011 at 11:33 Comment(10)
@bzlm It belongs to the above program. The name 'key' is a misnomer. It's actually an app-id for usage statisticsDetriment
I'll post another question: "How to do this for @Mr ... err Mma 7?"United
I wrote something very similar yesterday after our discussion but I guess you're right about the caching: it didn't tell me about this question! Oh well...Adalineadall
@Sjoerd I would vote for this more than once if I could. But you could have used this to your advantage not only w.r.t. @Mr. Now that you published this, we are all equal again :) But seriously, this confirms what I was saying in one of our past discussions: the rush to be the first, stimulated by the current SO model, is an artifact and reflects its weakness - it does not encourage collaboration to the right extent.Notability
@belisarius 5000: Thanks! I've been slowly crawling towards it the last day. Thought I'd ask a question just to push it over the boundary. MMA7 version: LOL, Immediate close request.Detriment
@Leonid Just in case you are not aware, "The fastest gun in the West" problem has been extensively discussed. See for example meta.stackoverflow.com/search?q=fastest+gun+in+the+westUnited
@Adalineadall Too bad. BTW Did you ever go to chat? I tried to continue our conversation there but the @Adalineadall there probably only is send to you if you participated there before.Detriment
@belisarius Thanks for the link! I think you mentioned this to me before, but without an explicit link. I also think that this is a real problem, in the sense that there is no obvious and easy solution.Notability
@Leonid: The fastest gun is a problem only if you consider rep to be of any value. Irrespective of whose answer gets "accepted", we can still have discussions and collaborate. Also, if you've observed, your long and well thought out answers are always immune from this problem :) So, although the SO model might be broken, quality does win in the end...Doubletime
@yoda I think that most people here consider rep of at least some value, so it remains a problem (remove the rep mechanism from SO and its face would change). I also think that the quality does win as long as the community stays small and coherent (as is so far the case here), but not so sure about larger ones - there might be a "phase transition" at some community scale, after which the social aspect (answerers' reps, etc) and positive feedback advantage of earlier answers may overweight the quality. It might be a better model if vote weights were themselves determined by the rep.Notability
S
15

Here's a variation on Sjoerd's solution.

The main difference is to use a docked cell instead of a popup dialog. The cell has a link to the new post, as well as a button that will clear the docked cell to the previous state.

The other difference is to use the question ID instead of the title for determining new posts. I know that titles get edited sometimes, so this trigger as a new post in that case.

storedTitle = "";
storedID = 0;
mySOWatchTask = 
  CreateScheduledTask[{lastTitle, 
     lastID} = {"title", 
      "question_id"} /. ("questions" /. 
        Import["http://api.stackoverflow.com/1.1/questions?key=       \
         QEpjCjsXYE6s_kZEkFr4Lw&page=1&pagesize=1&sort=creation&\
tagged=                mathematica", "JSON"])[[1]];
   If[lastID != storedID,
    storedTitle = lastTitle;
    storedID = lastID;
    SetOptions[$FrontEndSession, 
     DockedCells -> 
      Cell[BoxData[
        ToBoxes[Style[
          With[{dock = Options[$FrontEndSession, DockedCells]}, 
           Grid[{{Button[Style["\[CircleTimes]", 16], 
               SetOptions[$FrontEndSession, dock], 
               Appearance -> None], "New StackOverflow question: ", 
              Hyperlink[lastTitle, 
               "http://stackoverflow.com/questions/" <> 
                ToString[lastID]]}}, 
            Alignment -> {{Left, Left, Left}}, 
            ItemSize -> {{2, 14, Scaled[0.7]}}]], 
          FontFamily -> "Times"]]], "DockedCell", 
       Background -> Orange]]; EmitSound[Sound[SoundNote[]]]];, 60];

enter image description here

Seaborg answered 28/6, 2011 at 15:55 Comment(3)
+1 Nice change. I like the bit with the DockedCell setting and how you restore it to the original values. For this question and app, I got my inspiration from [Yu-Sung Chang's 'Photo booth' blog][1]. Previously, I thought of timed evaluation as nothing more than cron tasks for batch jobs, but I learned that it does a fantastic job in GUI's and apps like this. [1]: blog.wolfram.com/2011/06/23/…Detriment
I think docked cells are underused, probably because there is a bit of effort involved to create one. I have several copies of Mathematica installed, and usually at least two open, and use a custom docked cell that tells me which notebook is connected to which version. For this reason it was important to me to have a way to get back to my normal docked cell.Seaborg
I just saw this edit. It looks nice. It reminds me of the notifications that SO itself gives for new answers etc... (Aside: For a course I taught, I used a docked cell in the assignment template. It contained info about the assignment, a next question button and a submit via FTP button)Ramsay

© 2022 - 2024 — McMap. All rights reserved.