-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkov_app.py
70 lines (56 loc) · 2.04 KB
/
markov_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
"""Rough sample app of Markov Generator"""
import os
import re
import twitter
import markovify
TWEET_LIMIT = 200
def generate(text_model, size, bound):
"""Makes 140 character tweets"""
return [text_model.make_short_sentence(size) for i in range(0, bound)]
def get_all_tweets(username):
"""Spawns api and gets last 2000 tweets"""
api = new_api()
tweets = get_tweets(api, username)
for _ in range(0, 20):
tweets += get_tweets(api, username, since=tweets[len(tweets)-1].id)
return tweets
def get_profile_url(api, username):
"""Get a big version of the profile image"""
user = api.GetUser(screen_name=username),
return user[0].profile_image_url.replace("normal", "400x400")
def remove_twitlonger(tweet_list):
"""Removes all tweets that have a twitlonger link in them"""
return [re.sub(r" \S*…[^']*", "", tweet) for tweet in tweet_list]
def make_tweets(username, num_tweets):
"""Produce an array of generated tweets"""
api = new_api()
data = remove_twitlonger([tweet.text for tweet in get_all_tweets(username)])
model = make_markov_model(data)
return {
'username': username,
'profile_url': get_profile_url(api, username),
'tweets': generate(model, 140, num_tweets),
'long': generate(model, 240, 2),
}
# Utility
def new_api():
"""Wrapper around spawning twitter api"""
return twitter.Api(
consumer_key=os.environ['TWITTER_API_KEY'],
consumer_secret=os.environ['TWITTER_API_SECRET'],
access_token_key=os.environ['TWITTER_ACCESS_TOKEN'],
access_token_secret=os.environ['TWITTER_ACCESS_SECRET'])
def get_tweets(api, username, since=None):
"""Wrapper around api request"""
return api.GetUserTimeline(
screen_name=username,
count=TWEET_LIMIT,
include_rts=False,
trim_user=False,
exclude_replies=True,
max_id=since)
def make_markov_model(tweets):
"""Wrapper around making Markov Chain"""
return markovify.Text(" ".join(tweets))
if __name__ == '__main__':
print('butts')