This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
twitter_repeater.py
67 lines (57 loc) · 2.14 KB
/
twitter_repeater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import division
from time import sleep, time
import logging
import database
import twitter
def run():
logging.debug('entering run()')
# Get the latest tweet from @FSCoffeeBot
tweet = twitter.get_latest_tweet()
if not tweet:
logging.warning('no tweet found')
return False
last_tweet_id = int(database.get_last_handled_tweet_id())
this_tweet_id = int(tweet['id'])
logging.debug('found tweet. tweet id: %s' % this_tweet_id)
if not last_tweet_id:
# We haven't init-ed the database yet - this must be the first run
logging.info('No last_tweet_id found. Setting it.')
database.set_last_handled_tweet_id(this_tweet_id)
return False
if last_tweet_id >= this_tweet_id:
logging.debug('We already processed this tweet')
return False
# If we've made it here, it must be a new tweet
tweet_text = tweet['text']
logging.info('New tweet found: %s', tweet_text)
for user in database.get_followers():
database.push_message_to_send(database.MessageToSend(user, tweet_text))
database.set_last_handled_tweet_id(this_tweet_id)
def do_deferred_job():
'''Attempts to run a job in the queue. Returns True if a job is found.'''
message_to_send = database.pop_message_to_send()
if not message_to_send:
return False
# If it's been more then 5 minutes, throw away the job
if time() > message_to_send.time + (5 * 60):
logging.error(
"Timing out DM to %s. dm.time: %s, current time: %s. Message: %s" \
% (message_to_send.user, message_to_send.time, time(),
message_to_send.message))
return True
# Job is still valid. Run it!
if twitter.send_dm(message_to_send.user, message_to_send.message):
logging.info('sent DM to user: %s' % message_to_send.user)
else:
logging.warn('DM to user %s failed' % message_to_send.user)
# We couldn't finish the job - push it back into the queue
database.push_message_to_send(message_to_send)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
while True:
run()
run_time = time()
# Process jobs for 30 seconds
while time() < run_time + 30:
do_deferred_job()
sleep(.1)