Watching for new Mathematica questions using Mathematica and the StackOverflow API

前端 未结 2 2037
醉酒成梦
醉酒成梦 2020-12-05 08:53

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 Mathem

2条回答
  •  难免孤独
    2020-12-05 09:02

    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.

提交回复
热议问题