import sys
import time
-from dateutil.parser import parse
+from datetime import datetime, timedelta
+from email.utils import parsedate
from ConfigParser import SafeConfigParser
from heapq import heappop, heappush
import traceback
import os
import os.path
-from api import Twitter, TwitterError
-from oauth import OAuth, read_token_file
-from oauth_dance import oauth_dance
-from util import htmlentitydecode
+from .api import Twitter, TwitterError
+from .oauth import OAuth, read_token_file
+from .oauth_dance import oauth_dance
+from .util import htmlentitydecode
PREFIXES = dict(
cats=dict(
def __repr__(self):
return "<SchedTask %s next:%i delta:%i>" %(
- self.task.__name__, self.next, self.delta)
+ self.task.__name__, self.__next__, self.delta)
def __cmp__(self, other):
- return cmp(self.next, other.next)
+ return cmp(self.__next__, other.__next__)
def __call__(self):
return self.task()
def next_task(self):
now = time.time()
task = heappop(self.task_heap)
- wait = task.next - now
+ wait = task.__next__ - now
task.next = now + task.delta
heappush(self.task_heap, task)
if (wait > 0):
self.sched = Scheduler(
(SchedTask(self.process_events, 1),
SchedTask(self.check_statuses, 120)))
- self.lastUpdate = time.gmtime()
+ self.lastUpdate = (datetime.now() - timedelta(minutes=10)).utctimetuple()
def check_statuses(self):
debug("In check_statuses")
try:
- updates = self.twitter.statuses.friends_timeline()
- except Exception, e:
- print >> sys.stderr, "Exception while querying twitter:"
+ updates = reversed(self.twitter.statuses.friends_timeline())
+ except Exception as e:
+ print("Exception while querying twitter:", file=sys.stderr)
traceback.print_exc(file=sys.stderr)
return
nextLastUpdate = self.lastUpdate
debug("self.lastUpdate is %s" % self.lastUpdate)
for update in updates:
- crt = parse(update['created_at']).utctimetuple()
+ crt = parsedate(update['created_at'])
if (crt > nextLastUpdate):
text = (htmlentitydecode(
update['text'].replace('\n', ' '))
# to people who are not on our following list.
if not text.startswith("@"):
self.privmsg_channels(
- u"%s %s%s%s %s" %(
+ "%s %s%s%s %s" %(
get_prefix(),
IRC_BOLD, update['user']['screen_name'],
IRC_BOLD, text.decode('utf-8')))
crt, nextLastUpdate,
))
nextLastUpdate = crt
- else:
- break
+
debug("setting self.lastUpdate to %s" % nextLastUpdate)
self.lastUpdate = nextLastUpdate
if not os.path.exists(configFilename):
raise Exception()
load_config(configFilename)
- except Exception, e:
- print >> sys.stderr, "Error while loading ini file %s" %(
- configFilename)
- print >> sys.stderr, e
- print >> sys.stderr, __doc__
+ except Exception as e:
+ print("Error while loading ini file %s" %(
+ configFilename), file=sys.stderr)
+ print(e, file=sys.stderr)
+ print(__doc__, file=sys.stderr)
sys.exit(1)
bot = TwitterBot(configFilename)