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?

2
  • Find a bigger fish. Outdoing Leonid may require an AI that passes the Turing test. :-)
    – Mr.Wizard
    Aug 15, 2011 at 21:12
  • 2
    @Mr.Wizard 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). Aug 15, 2011 at 22:21

2 Answers 2

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

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/… Jun 28, 2011 at 20:33
  • 2
    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. Jun 28, 2011 at 20:47
  • 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)
    – Simon
    Dec 12, 2011 at 1:13
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.

10
  • 1
    @bzlm It belongs to the above program. The name 'key' is a misnomer. It's actually an app-id for usage statistics Jun 28, 2011 at 11:35
  • 1
    I'll post another question: "How to do this for @Mr ... err Mma 7?" Jun 28, 2011 at 11:42
  • 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...
    – acl
    Jun 28, 2011 at 11:43
  • 4
    @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. Jun 28, 2011 at 12:00
  • 3
    @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+west Jun 28, 2011 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.