-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapp.py
122 lines (106 loc) · 4.08 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import time
from random import random
from twython import Twython
CONSUMER_KEY = os.environ['TWITTER_CONSUMER_KEY']
CONSUMER_SECRET = os.environ['TWITTER_CONSUMER_SECRET']
OAUTH_TOKEN = os.environ['TWITTER_OAUTH_TOKEN']
OAUTH_TOKEN_SECRET = os.environ['TWITTER_OAUTH_TOKEN_SECRET']
TWEET_LENGTH = 140
TWEET_URL_LENGTH = 21
RUN_EVERY_N_SECONDS = 60*5 # e.g. 60*5 = tweets every five minutes
USERS_TO_IGNORE = []
DO_NOT_FAVORITE_USERS_AGAIN = True
def twitter_handle():
return Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
def favorite_tweet(tweet, handle):
handle.create_favorite(id=tweet['id'])
def random_favoriting(keywords, handle, favorite_probability=0.2):
"""
keywords is a list, like ['bananas', 'apples', 'oranges']
n.b. if this function is called every N seconds
then you can expect to favorite a tweet
once every N/favorite_probability seconds
"""
for keyword in keywords:
xs = handle.search(q=keyword)
ts = [x for x in xs['statuses']]
if DO_NOT_FAVORITE_USERS_AGAIN:
ts = [x for x in ts if x['user']['id'] not in USERS_TO_IGNORE]
if ts:
if random() < favorite_probability:
print 'Favoriting: ' + ts[0]['text']
favorite_tweet(ts[0], handle)
if DO_NOT_FAVORITE_USERS_AGAIN:
USERS_TO_IGNORE.append(ts[0]['user']['id'])
return
def get_urls_of_media_in_tweet(tweet):
"""
get the urls of media contained in a tweet
"""
if 'entities' not in tweet or 'media' not in tweet['entities']:
return []
return [x['media_url'] for x in tweet['entities']['media']]
def get_mentions(handle, include_entities=False):
"""
returns iterator of tweets mentioning us
if you want to get media in tweets, include_entities must be True
"""
return handle.cursor(handle.get_mentions_timeline,
include_entities=include_entities)
def get_images_in_mentions(handle):
"""
check for tweets that mention you, and get the urls of media in those tweets
e.g., this might be helpful if you make a twitter bot where users can mention you in tweets containing photos, and your bot replies with an altered version of that photo
"""
for tweet in get_mentions(handle, include_entities=True):
urls = get_urls_of_media_in_tweet(tweet)
yield urls
def submit_tweet_with_media(message, mediafile, tweet_to_reply=None, handle=None):
"""
imfile is the path to an media
tweet_to_reply is a tweet that you're replying to, if not None
"""
if not handle:
handle = twitter_handle()
media_ids = handle.upload_media(media=open(mediafile))
if tweet_to_reply is None:
handle.update_status(status=message,
media_ids=media_ids['media_id'])
else:
# must mention user's name for it to be a reply
message += ' @' + tweet_to_reply['user']['screen_name']
handle.update_status(status=message,
in_reply_to_status_id=tweet_to_reply['id'],
media_ids=media_ids['media_id'])
def submit_tweet(message, tweet_to_reply=None, handle=None):
"""
tweet_to_reply is a tweet that you're replying to, if not None
"""
if not handle:
handle = twitter_handle()
if tweet_to_reply is None:
handle.update_status(status=message)
else:
# must mention user's name for it to be a reply
message += ' @' + tweet_to_reply['user']['screen_name']
handle.update_status(status=message,
in_reply_to_status_id=tweet_to_reply['id'])
def get_message(handle):
"""
Your code goes here!
"""
message = 'I TWEET THIS.'
assert len(message) <= TWEET_LENGTH
return message
def main():
handle = twitter_handle()
USERS_TO_IGNORE.extend([x['user']['id'] for x in handle.get_favorites()])
while True:
message = get_message(handle)
print message
submit_tweet(message, handle)
# random_favoriting(['apples', 'oranges'], handle)
time.sleep(RUN_EVERY_N_SECONDS)
if __name__ == '__main__':
main()