Created
April 30, 2009 22:41
-
-
Save kashif/104743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
import java.text.SimpleDateFormat; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
import java.util.regex.*; | |
import java.util.*; | |
import thinktank.twitter.*; | |
import org.json.*; | |
import thinktank.twitter.Twitter.Message; | |
import thinktank.twitter.Twitter.Status; | |
import thinktank.twitter.Twitter.User; | |
import com.wolfram.jlink.*; | |
public class MathBot { | |
// shutdown hook delay time in seconds | |
private static final int shutdownTime = 2; | |
private static KernelLink ml = null; | |
private static Date date = new Date(); | |
public static void main (String[] argv) | |
{ | |
log ("MathBot started"); | |
System.err.println (getTimeStamp()+" This is output to StdErr..."); | |
Thread runtimeHookThread = new Thread() | |
{ | |
public void run() | |
{ | |
shutdownHook(); | |
} | |
}; | |
Runtime.getRuntime().addShutdownHook (runtimeHookThread); | |
try { | |
ml = MathLinkFactory.createKernelLink(argv); | |
// Get rid of the initial InputNamePacket the kernel will send | |
// when it is launched. | |
ml.discardAnswer(); | |
} catch (MathLinkException e) { | |
log ("Fatal error opening link: " + e.getMessage()); | |
return; | |
} | |
Twitter tw = new Twitter("mathematica", "password"); | |
try { | |
while (true) { | |
List<Message> r = tw.getReplies(null,null, date); | |
System.out.println("Replies "+r); | |
Iterator < Message > it = r.iterator ( ) ; | |
if (!r.isEmpty()) | |
date = new Date(); | |
while ( it.hasNext ( ) ) { | |
Message m = it.next(); | |
String thePattern = "@mathematica"; | |
Pattern pattern = Pattern.compile(thePattern, Pattern.CASE_INSENSITIVE); | |
String s = m.getText(); | |
Matcher matcher = pattern.matcher(s); | |
User u = m.getSender(); | |
System.out.println("Sender "+u.getScreenName()); | |
String strResult = ml.evaluateToOutputForm(matcher.replaceAll(""), 0); | |
System.out.println ( s ) ; | |
System.out.println("Answer "+strResult); | |
Message sent = tw.sendMessage(u.getScreenName(), strResult); | |
System.out.println(""+sent.getText()); | |
} | |
log ("running"); | |
Thread.sleep (120000); | |
} | |
} | |
catch (Throwable t) { | |
log ("Exception: "+t.toString()); | |
} | |
} | |
private static void shutdownHook() | |
{ | |
log ("ShutdownHook started"); | |
long t0 = System.currentTimeMillis(); | |
while (true) { | |
try { | |
Thread.sleep (500); | |
ml.close(); | |
} | |
catch (Exception e) { | |
log ("Exception: "+e.toString()); | |
break; | |
} | |
if (System.currentTimeMillis() - t0 > shutdownTime*1000) | |
break; | |
log ("shutdown"); | |
} | |
log ("ShutdownHook completed"); | |
} | |
private static void log (String msg) | |
{ | |
System.out.println (getTimeStamp()+" "+msg); | |
} | |
private static String getTimeStamp() | |
{ | |
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
return f.format(new Date()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment