forked from timepilot/the_shrinkbot
/
the_shrink.py
75 lines (52 loc) · 2.17 KB
/
the_shrink.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
68
69
70
71
72
from twitter.api import Twitter, TwitterError
from twitter.oauth import OAuth, write_token_file, read_token_file
from twitter.oauth_dance import oauth_dance
import os
import time
import sys
import eliza
CONSUMER_KEY='uS6hO2sV6tDKIOeVjhnFnQ'
CONSUMER_SECRET='MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk'
# The very first arg, if present, is the last id replied to.
if __name__ == '__main__':
oauth_filename = os.environ.get('HOME', '') + os.sep + '.twitter_oauth'
oauth_token, oauth_token_secret = read_token_file(oauth_filename)
# We use two twitter clients, one to search, another to update. Just
# easier that way...
twitter = Twitter(domain='search.twitter.com')
twitter.uriparts=()
last_id_replied = ''
print '###### args = ', sys.argv
if len(sys.argv) > 1:
last_id_replied = sys.argv[1]
last_id_file = 'last_id_replied'
if (os.path.exists(last_id_file)):
last_id_replied = int(open(last_id_file).read())
print 'Using last id from file ', last_id_replied
doctor = eliza.eliza()
poster = Twitter(
auth=OAuth(
oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
secure=True,
api_version='1',
domain='api.twitter.com')
while True:
results = twitter.search(q="@PMStevenHarper", since_id=last_id_replied)['results']
if not results:
print 'No results this time...'
for result in results:
# Remove my name from the question.
question = result['text'].replace('@PMStevenHarper', '')
asker = result['from_user']
id = str(result['id'])
print " <<< " + asker + ": " + question
doctor_response = doctor.respond(question.strip())
# We append part of the ID to avoid duplicates.
msg = '@%s %s (%s)' % (asker, doctor_response, id[-4:])
print '====> Resp = %s' % msg
last_id_replied = id
poster.statuses.update(status=msg)
print 'Last id replied = ', last_id_replied
open(last_id_file, 'w').write(str(last_id_replied))
print 'Now sleeping... \n\n'
time.sleep(30)