-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikipedia.py
More file actions
305 lines (275 loc) · 9.05 KB
/
wikipedia.py
File metadata and controls
305 lines (275 loc) · 9.05 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
import logging
import time
import random
import socket
import pythonwhois
import urllib
import re
import string
import unicodedata
from bs4 import BeautifulSoup
import requests
import gzip
from collections import Counter
import psycopg2
from psycopg2.extras import execute_values
def tweet(api, domain, entry, count, hashtags):
# special case because twitter doesn't like en dashes
# resource = entry.replace("–", "%E2%80%93")
tweet_text = f"{domain}․com https://en.wikipedia.org/wiki/{entry}{hashtags}" #special . in .com
try:
status = api.update_status(tweet_text)
except Exception as e:
logging.error(f'error tweeting {tweet_text} ({count})')
logging.error(e)
else:
logging.info(f'successfully tweeted {tweet_text} ({count})')
SPECIAL_PAGE_PREFIXES = (
'Main_Page',
'List',
'File:',
'Special:',
'Talk:',
'Help:',
'Category:',
'Wikipedia:',
'Portal:',
'Template:',
'File_talk:',
'User:',
)
def is_title_valid(title):
# no special pages like Talk pages
if title.startswith(SPECIAL_PAGE_PREFIXES):
return False
# Twitter urls don't play well with dashes
elif title == '-' or '–' in title or '—' in title:
return False
# Articles that start with a year make boring domains
elif title[:4].isdigit():
return False
# Article names that are too long make boring domains
elif len(title) >= 42:
return False
# Article names that contain too many words make boring domains
elif len(title.split('_')) > 4:
return False
# Article titles that contain the word 'of' make boring domains
elif 'of' in title.split('_'):
return False
else:
return True
def is_available(domain):
if len(domain) > 63:
return False
try:
whois = pythonwhois.get_whois(domain + '.com')
except UnicodeError:
return False
except (socket.error, pythonwhois.shared.WhoisException) as msg:
logging.info('whois error')
time.sleep(15)
return False
else:
if "No match" in whois['raw'][0]:
return True
else:
return False
def asciify_title(title):
if set(title).difference(set(string.printable)):
asciified = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore')
if asciified:
asciified = asciified.decode('utf-8')
if len(asciified) == len(title):
return asciified
else:
return False
else:
return False
else:
return title
def strip_disambiguation(title):
m = re.search(r"(.*)\([^()]+\)$", title)
if m:
return m.groups(0)[0].strip('_')
else:
return title
def depunctuate(title):
title = title.replace('&', 'and')
return ''.join([c for c in title if c in string.ascii_letters + string.digits + '-' + '_'])
def get_last_hour_pageview_url():
index_url = "https://dumps.wikimedia.org/other/pageviews/"
r = requests.get(index_url)
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.findAll('a'):
if 'readme' not in link.get('href'):
year_url = index_url + link.get('href')
r = requests.get(year_url)
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.findAll('a'):
month_url = year_url + link.get('href')
r = requests.get(month_url)
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.findAll('a'):
if 'pageviews' in link.get('href'):
most_recent_log_url = month_url + link.get('href')
return most_recent_log_url
def download_logfile(most_recent_log_url):
logfile = requests.get(most_recent_log_url)
logfile = gzip.decompress(logfile.content).decode('utf-8')
c = Counter()
for line in logfile.splitlines():
if line.startswith('en ') or line.startswith('en.m '):
try:
domain_code, page_title, count_views, _ = line.split()
if int(count_views) >= 10:
c[page_title] += int(count_views)
except ValueError:
continue
return c
def mark_as_unavailable(title):
sql = (
"INSERT INTO page (title, is_availabity_checked) "
"VALUES (%s, TRUE) "
"ON CONFLICT (title) DO UPDATE "
" SET is_availabity_checked = TRUE; "
)
with db.cursor() as cursor:
cursor.execute(sql, (title,))
def mark_as_tweeted(title):
sql = (
"INSERT INTO page (title, is_tweeted) "
"VALUES (%s, TRUE) "
"ON CONFLICT (title) DO UPDATE "
" SET is_tweeted = TRUE; "
)
with db.cursor() as cursor:
cursor.execute(sql, (title,))
def get_json(title):
try:
r = requests.get("https://en.wikipedia.org/w/api.php",
params={
'action': 'query',
'format': 'json',
'prop': 'extracts|linkshere|categories',
'explaintext': '1',
'exchars': '350',
'exlimit': '1',
'redirects': '1',
'cllimit': '500',
'lhlimit': '500',
'titles' : title,
}
)
except:
return None
try:
json = r.json()
return next(iter(json['query']['pages'].values()))
except:
return None
FORBIDDEN_CATEGORY_WORDS = [
'people',
'team',
'births',
'deaths',
'ships',
'mma',
'flight',
'ufc',
'days of the year'
]
def is_person_or_team(json):
categories = json.get('categories', [])
categories = [c['title'] for c in categories]
categories = " ".join(categories)
categories = categories.lower()
for forbidden_category_word in FORBIDDEN_CATEGORY_WORDS:
if forbidden_category_word in categories:
return True
if 'team' in json.get('extract', ''):
return True
return False
def get_hashtags(json, title, pageviews):
links = []
for link in json.get('linkshere', []):
link['views'] = pageviews.get(link['title'], 0)
links.append(link)
hashtags = ''
lowercase_hashtags = set()
for link in sorted(links, key=lambda x: x['views'], reverse=True):
linktitle = link['title'].replace(' ', '')
if set(linktitle) - set(string.ascii_letters):
continue
elif linktitle.lower().startswith("list"):
continue
elif len(hashtags + f" #{linktitle}") > 72:
return hashtags
else:
if linktitle.lower() in lowercase_hashtags:
continue
else:
hashtags += f" #{linktitle}"
lowercase_hashtags.add(linktitle.lower())
return hashtags
def is_tweeted_or_unavailable(title):
sql = (
"SELECT * FROM page "
"WHERE title = %s "
" AND (is_tweeted = TRUE "
" OR is_availabity_checked = TRUE) "
"LIMIT 1; "
)
with db.cursor() as cursor:
cursor.execute(sql, (title,))
if cursor.fetchone():
return True
else:
return False
db = psycopg2.connect("dbname=itsavailable")
db.autocommit = True
def run(api):
logging.info("Wikipedia thread started")
while True:
most_recent_log_url = get_last_hour_pageview_url()
pageviews = download_logfile(most_recent_log_url)
for title, count in pageviews.most_common():
if is_tweeted_or_unavailable(title):
# Title has already been tweeted or marked as bad
continue
if not is_title_valid(title):
# Title is not a good article name
mark_as_unavailable(title)
continue
_title = asciify_title(title)
if not _title:
# Title is not ascifiiable
mark_as_unavailable(title)
continue
json = get_json(title)
if json is None:
continue
if is_person_or_team(json):
# Title is a person, team, or otherwise uninteresting
mark_as_unavailable(title)
continue
hashtags = get_hashtags(json, title, pageviews)
_title = strip_disambiguation(_title)
_title = depunctuate(_title)
_title_nospace = _title.replace('_', '').replace('-', '')
_title_hyphens = _title.replace('_', '-')
_title_hyphens = _title_hyphens.replace('--', '-')
if is_available(_title_nospace):
mark_as_tweeted(title)
tweet(api, _title_nospace, title, count, hashtags)
break
#elif is_available(_title_hyphens):
# mark_as_tweeted(title)
# tweet(api, _title_hyphens, title, count, hashtags)
# break
else:
mark_as_unavailable(title)
next_run = random.randint(60*60*2, 60*60*3)
logging.info(f'wikipedia thread starting over in {int(next_run/60)} minutes')
time.sleep(next_run)