Skip to content

Instantly share code, notes, and snippets.

@minhlab
Last active October 5, 2017 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhlab/6aed1cbaab28041f629bda020f21bd9a to your computer and use it in GitHub Desktop.
Save minhlab/6aed1cbaab28041f629bda020f21bd9a to your computer and use it in GitHub Desktop.
Tuanminh01 (thảo luận · đóng góp): 1191 điểm, Điểm thi tháng 9 năm 2017
DanGong (thảo luận · đóng góp): 1030 điểm, Điểm thi tháng 9 2017
Buileducanh (thảo luận · đóng góp): 1023 điểm, Điểm thi tháng 9/2017
Nguyenphanuyennhi (thảo luận · đóng góp): 1018 điểm, Điểm thi tháng 9 2017
Phương Huy (thảo luận · đóng góp): 945 điểm, Điểm thi tháng 9 năm 2017
P.T.Đ (thảo luận · đóng góp): 843 điểm, Điểm thi tháng 9 năm 2017
Bluetpp (thảo luận · đóng góp): 746 điểm, Điểm thi tháng 9/2017
Mintu Martin (thảo luận · đóng góp): 455 điểm, Điểm thi tháng 9 năm 2017
Anhkhang234 (thảo luận · đóng góp): 223 điểm, Điểm thi tháng 9 năm 2017
A.Einstein05 (thảo luận · đóng góp): 156 điểm, Điểm thi tháng 9 năm 2017
import pywikibot
import re
import csv
import codecs
from collections import OrderedDict
from time import time
def get_revision_with_content(page, rev_id):
if rev_id == 0:
return 0, None
content = p.getOldVersion(rev_id)
if content is None:
print('Found a difficult case. Looking at previous revision to find some content...')
p.getVersionHistory()
prev_revs = [rev for rev in p._revisions.values() if rev['revid'] < rev_id]
prev_revs.sort(key=lambda rev: rev['revid'], reverse=True)
i = 0
while content is None and i < len(prev_revs):
rev_id = prev_revs[i]['revid']
content = p.getOldVersion(rev_id)
i += 1
return rev_id, content
if __name__ == '__main__':
start_sec = time()
try:
users = open('finalists.txt').readlines()
users2 = []
for u in users:
p = u.index('(')
users2.append(u[:p-1])
site = pywikibot.Site()
reported_scores = [re.search(r':\s*(\d+)', u).group(1) for u in users]
user2titles = {}
user_title2score = {}
for uname in users2:
rows = [('Title', 'Categories', 'Is new?', 'Bytes added', 'Raw score', 'Initial revision', 'Last revision', 'Is redirect?')]
contribs = site.usercontribs(uname, namespaces=[0], start='20170901000000', end='20171001000000', reverse=True)
print('Looking for all titles that user %s touched ...' %uname)
titles = OrderedDict()
for contrib in contribs:
comment = contrib.get('comment', '')
if not (comment.startswith('Đã lùi') or comment.startswith('Đã thay đổi mức khóa')
or comment.startswith('Đã khóa')):
# print(contrib.get('comment'))
if contrib['title'] not in titles:
titles[contrib['title']] = {'init_id': contrib['parentid']}
titles[contrib['title']]['last_id'] = contrib['revid']
# titles = dict((t, titles[t]) for t in list(titles)[:3]) # for debugging
for title, info in titles.items():
print('Looking at edits by %s on page %s ...' %(uname, title))
p = pywikibot.Page(site, title)
cats = [c.title() for c in p.categories()]
is_new = (info['init_id'] == 0)
info['init_id'], old_content = get_revision_with_content(p, info['init_id'])
init_bytes = len((old_content or '').encode('utf-8'))
info['last_id'], last_content = get_revision_with_content(p, info['last_id'])
last_bytes = len((last_content or '').encode('utf-8'))
bytes_added = (last_bytes - init_bytes)
score = max((3 if is_new else 0) + (bytes_added // 1000), 0)
is_redirect = site.page_isredirect(p)
rows.append((title, ','.join(cats), int(is_new), bytes_added, score,
info['init_id'], info['last_id'], int(is_redirect)))
user_title2score[(uname,title)] = score
user_csv_path = uname + '.csv'
with codecs.open(user_csv_path, 'w', 'utf-8') as f:
cw = csv.writer(f)
cw.writerows(rows)
print('Written to %s' %user_csv_path)
user2titles[uname] = set(titles)
collisions = [('Title', 'User 1', 'Score 1', 'User 2', 'Score 2')]
for i in range(len(users2)-1):
for j in range(i+1, len(users2)):
u1, u2 = users2[i], users2[j]
shared_titles = user2titles[u1].intersection(user2titles[u2])
if u1 != u2 and shared_titles:
for title in shared_titles:
collisions.append((title, u1, user_title2score[(u1, title)], u2, user_title2score[(u2, title)]))
with codecs.open('collisions.csv', 'w', 'utf-8') as f:
cw = csv.writer(f)
cw.writerows(collisions)
print('Written to collisions.csv')
except KeyboardInterrupt:
print('Interrupted. Goodbye!')
print('Time elapsed: %.2f minutes' %((time()-start_sec)/60))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment