Watching for new Mathematica questions using Mathematica and the StackOverflow API

人盡茶涼 提交于 2019-11-27 19:53:26
Brett Champion

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];

Sjoerd C. de Vries

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!