-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitterbot.py
56 lines (41 loc) · 1.51 KB
/
twitterbot.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
import tweepy
import time
from credentials import *
import urllib
from pyshorteners import Shortener
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print("Bot started.")
shortener = Shortener("Tinyurl")
recent_id = 0
image = False
url = 'http://34.250.158.151:3000/meals?{}={}'
reply = '@{} here is the environmental impact of your meal: '
while True:
for tweet in tweepy.Cursor(api.search, q='@ratemyplate_', include_entities=True).items():
if(tweet.id>recent_id):
try:
username = tweet.user.screen_name
formatted_reply = reply.format(username)
if 'media' in tweet.entities:
image = True
image = tweet.entities['media'][0]
media = image['media_url']
formatted_reply += shortener.short(url.format('image', media))
else:
text = '+'.join(tweet.text.split()[1:])
location = tweet.user.location
formatted_reply += shortener.short(url.format('recipe', text))
print(formatted_reply)
# urllib.request.urlopen('http://34.250.158.151:3000/meals?recipe=' + text).read()
# api.update_status('@' + username + ' here is your meal: ' + urllib.parse.quote_plus(str(tweet.id)), in_reply_to_status_id=tweet.id)
api.update_status(formatted_reply, in_reply_to_status_id=tweet.id)
print("Tweet received from ", username)
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
recent_id = tweet.id
print("Bot finished.")
time.sleep(30)