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?

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • Find a bigger fish. Outdoing Leonid may require an AI that passes the Turing test. :-) – Mr.Wizard Aug 15 '11 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). – Sjoerd C. de Vries Aug 15 '11 at 22:21

2 Answers2

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.

Community
  • 1
  • 1
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • 1
    @bzlm It belongs to the above program. The name 'key' is a misnomer. It's actually an app-id for usage statistics – Sjoerd C. de Vries Jun 28 '11 at 11:35
  • 1
    I'll post another question: "How to do this for @Mr ... err Mma 7?" – Dr. belisarius Jun 28 '11 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 '11 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. – Leonid Shifrin Jun 28 '11 at 12:00
  • @belisarius 5000: Thanks! I've been slowly crawling towards it the last day. Thought I'd ask a question just to push it over the boundary. MMA7 version: LOL, Immediate close request. – Sjoerd C. de Vries Jun 28 '11 at 12:02
  • 3
    @Leonid Just in case you are not aware, "The fastest gun in the West" problem has been extensively discussed. See for example http://meta.stackoverflow.com/search?q=fastest+gun+in+the+west – Dr. belisarius Jun 28 '11 at 12:04
  • @acl Too bad. BTW Did you ever go to chat? I tried to continue our conversation there but the @acl there probably only is send to you if you participated there before. – Sjoerd C. de Vries Jun 28 '11 at 12:05
  • @belisarius Thanks for the link! I think you mentioned this to me before, but without an explicit link. I also think that this is a real problem, in the sense that there is no obvious and easy solution. – Leonid Shifrin Jun 28 '11 at 12:13
  • @Leonid: The fastest gun is a problem _only_ if you consider rep to be of any value. Irrespective of whose answer gets "accepted", we can still have discussions and collaborate. Also, if you've observed, your long and well thought out answers are always immune from this problem :) So, although the SO model might be broken, quality does win in the end... – abcd Jun 28 '11 at 13:38
  • @yoda I think that most people here consider rep of at least *some* value, so it remains a problem (remove the rep mechanism from SO and its face would change). I also think that the quality does win as long as the community stays small and coherent (as is so far the case here), but not so sure about larger ones - there might be a "phase transition" at some community scale, after which the social aspect (answerers' reps, etc) and positive feedback advantage of earlier answers may overweight the quality. It might be a better model if vote weights were themselves determined by the rep. – Leonid Shifrin Jun 28 '11 at 14:17
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

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
Brett Champion
  • 8,497
  • 1
  • 27
  • 44
  • +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]: http://blog.wolfram.com/2011/06/23/mission-mathematica-making-a-photo-booth-app-in-three-steps/#more-6374 – Sjoerd C. de Vries Jun 28 '11 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. – Brett Champion Jun 28 '11 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 '11 at 01:13