使用Mathematica和StackOverflow API观察新的Mathematica问题

时间:2011-06-28 11:26:00

标签: wolfram-mathematica stackexchange-api

除非韦纳德先生正在度假,否则很难打败这种看似无处不在和无所不知的现象。我们如何使用Mathematica和StackOverflow API超越他?

2 个答案:

答案 0 :(得分:15)

实际上很容易。您只需要以下内容。

定义监视任务:

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

开始这个:

StartScheduledTask[mySOWatchTask];

停止:

 StopScheduledTask[mySOWatchTask];

看看正在运行的是什么:

 ScheduledTasks[] // Shallow

删除任务:

 RemoveScheduledTask[mySOWatchTask];

或所有任务:

RemoveScheduledTask[ScheduledTasks[]];

每分钟轮询一次(the minimum that is not seen as abusive),并显示一个对话框,每当有新的Mathematica问题到达时播放声音。

enter image description here

它的美妙之处在于:它使用Mathematica 8,我们都知道Mr.Wizard没有那个(还); - )

请注意,SO API正在大量缓存,因此可能无法直接响应。我也没有对此进行过广泛的测试。

修改
请注意,上面使用的密钥(app-id)仅供这个小型Mathematica应用程序使用。如果您需要一个用于不同应用程序的应用程序,您可以快速轻松地完成一个程序here。我花了不到一分钟。

答案 1 :(得分:15)

以下是Sjoerd解决方案的变体。

主要区别在于使用停靠的单元格而不是弹出对话框。单元格有一个指向新帖子的链接,以及一个将停靠的单元格清除到先前状态的按钮。

另一个区别是使用问题ID而不是标题来确定新帖子。我知道标题有时会被编辑,因此在这种情况下,这会触发新帖。

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