This repository has been archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
ebooks.py
272 lines (252 loc) · 10.7 KB
/
ebooks.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import random
import re
import sys
import twitter
import tweepy
from mastodon import Mastodon
import markov
from bs4 import BeautifulSoup
try:
# Python 3
from html.entities import name2codepoint as n2c
from urllib.request import urlopen
except ImportError:
# Python 2
from htmlentitydefs import name2codepoint as n2c
from urllib2 import urlopen
chr = unichr
from local_settings import *
def connect(type='twitter'):
if type == 'twitter':
if TWITTER_API_VERSION == 'v2':
return tweepy.Client(bearer_token=MY_BEARER_TOKEN,
consumer_key=MY_CONSUMER_KEY,
consumer_secret=MY_CONSUMER_SECRET,
access_token=MY_ACCESS_TOKEN_KEY,
access_token_secret=MY_ACCESS_TOKEN_SECRET)
else:
return twitter.Api(consumer_key=MY_CONSUMER_KEY,
consumer_secret=MY_CONSUMER_SECRET,
access_token_key=MY_ACCESS_TOKEN_KEY,
access_token_secret=MY_ACCESS_TOKEN_SECRET,
tweet_mode='extended')
elif type == 'mastodon':
return Mastodon(client_id=CLIENT_CRED_FILENAME, api_base_url=MASTODON_API_BASE_URL, access_token=USER_ACCESS_FILENAME)
return None
def entity(text):
if text[:2] == "&#":
try:
if text[:3] == "&#x":
return chr(int(text[3:-1], 16))
else:
return chr(int(text[2:-1]))
except ValueError:
pass
else:
guess = text[1:-1]
if guess == "apos":
guess = "lsquo"
numero = n2c[guess]
try:
text = chr(numero)
except KeyError:
pass
return text
def filter_status(text):
text = re.sub(r'\b(RT|MT) .+', '', text) # take out anything after RT or MT
text = re.sub(r'(\#|@|(h\/t)|(http))\S+', '', text) # Take out URLs, hashtags, hts, etc.
text = re.sub('\s+', ' ', text) # collaspse consecutive whitespace to single spaces.
text = re.sub(r'\"|\(|\)', '', text) # take out quotes.
text = re.sub(r'\s+\(?(via|says)\s@\w+\)?', '', text) # remove attribution
text = re.sub(r'<[^>]*>','', text) #strip out html tags from mastodon posts
htmlsents = re.findall(r'&\w+;', text)
for item in htmlsents:
text = text.replace(item, entity(item))
text = re.sub(r'\xe9', 'e', text) # take out accented e
return text
def scrape_page(src_url, web_context, web_attributes):
tweets = []
last_url = ""
for i in range(len(src_url)):
if src_url[i] != last_url:
last_url = src_url[i]
print(">>> Scraping {0}".format(src_url[i]))
try:
page = urlopen(src_url[i])
except Exception:
last_url = "ERROR"
import traceback
print(">>> Error scraping {0}:".format(src_url[i]))
print(traceback.format_exc())
continue
soup = BeautifulSoup(page, 'html.parser')
hits = soup.find_all(web_context[i], attrs=web_attributes[i])
if not hits:
print(">>> No results found!")
continue
else:
errors = 0
for hit in hits:
try:
tweet = str(hit.text).strip()
except (UnicodeEncodeError, UnicodeDecodeError):
errors += 1
continue
if tweet:
tweets.append(tweet)
if errors > 0:
print(">>> We had trouble reading {} result{}.".format(errors, "s" if errors > 1 else ""))
return(tweets)
def grab_tweets(api, max_id=None):
source_tweets = []
user_tweets = api.GetUserTimeline(screen_name=user, count=200, max_id=max_id, include_rts=True, trim_user=True, exclude_replies=True)
if user_tweets:
max_id = user_tweets[-1].id - 1
for tweet in user_tweets:
if tweet.full_text:
tweet.text = filter_status(tweet.full_text)
else:
tweet.text = filter_status(tweet.full_text)
if re.search(SOURCE_EXCLUDE, tweet.text):
continue
if tweet.text:
source_tweets.append(tweet.text)
else:
pass
return source_tweets, max_id
def grab_tweets_v2(api, handle):
twitter_tweets = []
id = api.get_user(username=handle).data.id
for tweet in tweepy.Paginator(api.get_users_tweets, id, exclude=['retweets', 'replies'],
max_results=100).flatten(limit=TWEETS_TO_GRAB):
tweet_text = filter_status(tweet.text)
if re.search(SOURCE_EXCLUDE, tweet_text):
continue
if tweet_text:
twitter_tweets.append(tweet_text)
return twitter_tweets
def grab_toots(api, account_id=None,max_id=None):
if account_id:
source_toots = []
user_toots = api.account_statuses(account_id)
max_id = user_toots[len(user_toots)-1]['id']-1
for toot in user_toots:
if toot['in_reply_to_id'] or toot['reblog']:
pass #skip this one
else:
toot['content'] = filter_status(toot['content'])
if len(toot['content']) != 0:
source_toots.append(toot['content'])
return source_toots, max_id
if __name__ == "__main__":
order = ORDER
guess = 0
if ODDS and not DEBUG:
guess = random.randint(0, ODDS - 1)
if guess:
print(str(guess) + " No, sorry, not this time.") # message if the random number fails.
sys.exit()
else:
api = connect()
if ENABLE_MASTODON_SOURCES or ENABLE_MASTODON_POSTING:
mastoapi = connect(type='mastodon')
source_statuses = []
if STATIC_TEST:
file = TEST_SOURCE
print(">>> Generating from {0}".format(file))
string_list = open(file).readlines()
for item in string_list:
source_statuses += item.split(",")
if SCRAPE_URL:
source_statuses += scrape_page(SRC_URL, WEB_CONTEXT, WEB_ATTRIBUTES)
if ENABLE_TWITTER_SOURCES and TWITTER_SOURCE_ACCOUNTS and len(TWITTER_SOURCE_ACCOUNTS[0]) > 0 and TWITTER_API_VERSION == 'v2':
for handle in TWITTER_SOURCE_ACCOUNTS:
source_statuses += grab_tweets_v2(api,handle)
elif ENABLE_TWITTER_SOURCES and TWITTER_SOURCE_ACCOUNTS and len(TWITTER_SOURCE_ACCOUNTS[0]) > 0:
twitter_tweets = []
for handle in TWITTER_SOURCE_ACCOUNTS:
user = handle
handle_stats = api.GetUser(screen_name=user)
status_count = handle_stats.statuses_count
max_id = None
my_range = min(17, int((status_count/200) + 1))
for x in range(1, my_range):
twitter_tweets_iter, max_id = grab_tweets(api, max_id)
twitter_tweets += twitter_tweets_iter
print("{0} tweets found in {1}".format(len(twitter_tweets), handle))
if not twitter_tweets:
print("Error fetching tweets from Twitter. Aborting.")
sys.exit()
else:
source_statuses += twitter_tweets
if ENABLE_MASTODON_SOURCES and len(MASTODON_SOURCE_ACCOUNTS) > 0:
source_toots = []
max_id=None
for handle in MASTODON_SOURCE_ACCOUNTS:
accounts = mastoapi.account_search(handle)
if len(accounts) != 1:
pass # Ambiguous search
else:
account_id = accounts[0]['id']
num_toots = accounts[0]['statuses_count']
if num_toots < 3200:
my_range = int((num_toots/200)+1)
else:
my_range = 17
for x in range(my_range)[1:]:
source_toots_iter, max_id = grab_toots(mastoapi,account_id, max_id=max_id)
source_toots += source_toots_iter
print("{0} toots found from {1}".format(len(source_toots), handle))
if len(source_toots) == 0:
print("Error fetching toots for %s. Aborting." % handle)
sys.exit()
source_statuses += source_toots
if len(source_statuses) == 0:
print("No statuses found!")
sys.exit()
mine = markov.MarkovChainer(order)
for status in source_statuses:
if not re.search('([\.\!\?\"\']$)', status):
status += "."
mine.add_text(status)
for x in range(0, 10):
ebook_status = mine.generate_sentence()
# randomly drop the last word, as Horse_ebooks appears to do.
if random.randint(0, 4) == 0 and re.search(r'(in|to|from|for|with|by|our|of|your|around|under|beyond)\s\w+$', ebook_status) is not None:
print("Losing last word randomly")
ebook_status = re.sub(r'\s\w+.$', '', ebook_status)
print(ebook_status)
# if a tweet is very short, this will randomly add a second sentence to it.
if ebook_status is not None and len(ebook_status) < 40:
rando = random.randint(0, 10)
if rando == 0 or rando == 7:
print("Short tweet. Adding another sentence randomly")
newer_status = mine.generate_sentence()
if newer_status is not None:
ebook_status += " " + mine.generate_sentence()
else:
ebook_status = ebook_status
elif rando == 1:
# say something crazy/prophetic in all caps
print("ALL THE THINGS")
ebook_status = ebook_status.upper()
# throw out tweets that match anything from the source account.
if ebook_status is not None and len(ebook_status) < 210:
for status in source_statuses:
if ebook_status[:-1] not in status:
continue
else:
print("TOO SIMILAR: " + ebook_status)
sys.exit()
if not DEBUG:
if ENABLE_TWITTER_POSTING and TWITTER_API_VERSION=='v2':
status = api.create_tweet(text=ebook_status)
elif ENABLE_TWITTER_POSTING:
status = api.PostUpdate(ebook_status)
if ENABLE_MASTODON_POSTING:
status = mastoapi.toot(ebook_status)
print(ebook_status)
elif not ebook_status:
print("Status is empty, sorry.")
else:
print("TOO LONG: " + ebook_status)