Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watching for new Mathematica questions using Mathematica and the StackOverflow API

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?

like image 309
Sjoerd C. de Vries Avatar asked Jun 28 '11 11:06

Sjoerd C. de Vries


2 Answers

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

like image 113
Brett Champion Avatar answered Oct 05 '22 04:10

Brett Champion


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.

like image 23
Sjoerd C. de Vries Avatar answered Oct 05 '22 04:10

Sjoerd C. de Vries