Created
February 11, 2010 04:28
-
-
Save colindean/301216 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
#!/usr/bin/env perl | |
print "Loading twitter-contest library for ttytter\n"; | |
##################################################### | |
##################################################### | |
############## CONFIG SECTION ####################### | |
##################################################### | |
##################################################### | |
#space-separated list of terms which must be in the tweet | |
#Examples: | |
#To watch for tweets which contain the words "ubuntu" /and/ "linux" | |
#$track = 'ubuntu linux'; | |
#To watch for tweets which contain hashtags and such, add them on | |
#Ex. looks for tweets which contain the followfriday hashtag and rushimprint | |
#$track = '#followfriday @rushimprint'; | |
$track = '#linux'; | |
#Alternatively, use $tquery and specify the search API query directly | |
#Do not use them together | |
#$tquery = "\#linux"; | |
#$store->{'terms_query'} = '#linux'; | |
#if set to 1, all terms in track must be present | |
#if set to 0, just one of the terms in track must be present | |
#Set this to 0 if using $tquery | |
$store->{'all_terms_must_be_present'} = 0; | |
#if set to 0, do no log tweets from the user being used to watch | |
#if set to 1, log matching tweets regardless of user | |
$store->{'log_self'} = 0; | |
#1 turns off the timeline and only watch the hashtag. this is desirable. | |
$notimeline = 1; | |
#path to the database file, relative to where start_watcher.sh is | |
$store->{'dbpath'} = 'database/contest.db'; | |
#1 to turn on debug output, 0 to turn it off | |
$store->{'debug'} = 1; | |
##################################################### | |
##################################################### | |
############## END CONFIG SECTION ################### | |
##################################################### | |
##################################################### | |
################################################# | |
## ## | |
## Woe unto thee who changeth the following ## | |
## ## | |
################################################# | |
#die("This bot shouldn't be run anonymously.") if ($anonymous); | |
$store->{'dontecho'} = $whoami; | |
#if this fails, split user | |
($store->{'dontecho'}, $store->{'crap'}) = split(/:/, $user, 2) | |
if (!length($whoami)); | |
#turn on $track to watch certain hashtags | |
#space separated list, need not include hashtags | |
#any tweets with /any/ of these terms will be handled | |
$store->{'tracklist'} = split(/ /, $track); | |
#&ucommand("/set tquery {$store->{'terms_query'}}"); | |
#setup the database handle | |
use DBI; | |
$store->{'dbargs'} = {AutoCommit => 0, PrintError => $store->{'debug'}}; | |
$store->{'dbh'} = DBI->connect("dbi:SQLite:dbname=".$store->{'dbpath'}, | |
"","",$store->{'dbargs'} | |
); | |
#handler for tweets | |
$handle = sub { | |
my $ref = shift; #the tweet | |
my $debug = $store->{'debug'}; #debugging? | |
my $dbh = $store->{'dbh'}; #database handle | |
my $all_terms_must_be_present = $store->{'all_terms_must_be_present'}; #check if all terms are present | |
my @tracklist = $store->{'tracklist'}; #$track expanded into a list | |
my $log_self = $store->{'log_self'}; | |
#we need the screenname | |
my $sn = &descape($ref->{'user'}->{'screen_name'}); | |
return if ($log_self && $sn eq $store->{'dontecho'}); #prevent inadvertant self-logging | |
#we need the full text of the tweet for verification | |
my $text = &descape($ref->{'text'}); | |
if($all_terms_must_be_present){ | |
#if desired, check for the existence of the variables in the tweet's text | |
#we already know what at least one of the tracks is in it | |
for $t (@tracklist) { | |
if(index($text, $t) == -1){ | |
if($debug){ print $stdout "DISCARDED TWEET\n[", $sn, "] ", $text, "\n\n"; } | |
return; #we don't want it if a tracked term is missing | |
} | |
} | |
} | |
#grab the info we need to store | |
#we already have the screenname in $sn | |
#we already have the tweet in $text | |
#we need the timestamp | |
my $timestamp = &descape($ref->{'created_at'}); | |
#and we need the tweet's id | |
my $tweet_id = &descape($ref->{'id'}); | |
#we need to know if they are following me | |
#need an api call for this | |
#TODO: implement this | |
#debugging | |
if($debug){ | |
print $stdout 'NEW TWEET', "\n", 'At ', $timestamp, ', ', $sn, ' tweeted: ', $text, "\n"; | |
} | |
#assemble a query | |
my $query = sprintf("insert into contest_entry | |
(timestamp, user, tweet, id) | |
values | |
(%s, %s, %s, %s)", | |
$dbh->quote($timestamp), | |
$dbh->quote($sn), | |
$dbh->quote($text), | |
$dbh->quote($tweet_id) | |
); | |
if($debug) { print $stdout $query, "\n\n" }; | |
#execute the query | |
$dbh->do($query); | |
if ($debug and $dbh->err()) { print $stdout "DB ERROR: $DBI::errstr\n"; } | |
#commit the query if there was no error | |
$dbh->commit(); | |
#&defaulthandler($ref); | |
}; | |
##called at the beginning of checking tweets | |
$heartbeat = sub { | |
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); | |
my $stamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d", | |
$year+1900,$mon+1,$mday,$hour,$min,$sec); | |
print $stdout $stamp.": MARK - Starting cycle.\n"; | |
}; | |
##called at the end of checking tweets | |
$conclude = sub { | |
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); | |
my $stamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d", | |
$year+1900,$mon+1,$mday,$hour,$min,$sec); | |
print $stdout $stamp.": MARK - The cycle is complete.\n"; #the student has become the master | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment