Last active
December 31, 2016 09:25
-
-
Save dongweiming/ea0803913296ab36c1f039e2b9eb6812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import re | |
from collections import defaultdict | |
from datetime import datetime | |
import requests | |
# https://github.com/livid/v2ex/blob/master/api.py#L247 | |
url = 'https://www.v2ex.com/api/replies/show.json' | |
rs = requests.get(url, params={'topic_id': 313225, 'page': 11}) | |
regex = re.compile(r'(\d){2}') | |
number_map = defaultdict(list) | |
members = [] | |
COUNT = 4 | |
END_TIMESTAMP =int(datetime(2016, 10, 20).strftime('%s')) | |
for index, r in enumerate(rs.json()): | |
created = r['created'] | |
if created >= END_TIMESTAMP: | |
break | |
content = r['content'] | |
match = regex.search(content) | |
if not match: | |
continue | |
number = match.group() | |
content = content.replace(number, '', 1) | |
if regex.search(content): | |
continue | |
member_id = r['member']['id'] | |
if member_id not in members: | |
members.append(member_id) | |
floor = index + 1 | |
number_map[int(number)].append((member_id, r['member']['username'], | |
floor, number, | |
str(datetime.fromtimestamp(created)))) | |
number = int(raw_input('输入幸运数字❯ ')) | |
values = number_map.get(number, []) | |
if not len(values) >= COUNT: | |
offset = 1 | |
while 1: | |
values += sorted([number_map.get(number + o, []) | |
for o in (offset, -offset)], | |
lambda x: x[1]) | |
if len(values) > COUNT: | |
break | |
offset += 1 | |
values = values[:COUNT] | |
print '\n如下{}位同学获得《Python Web开发实战》:\n'.format(COUNT) | |
for val in values: | |
print ('用户ID: {:>6}\t昵称: {:>13}\t楼层: {:>4}\t参与数字: {:>2}\t' | |
'参与时间:{}').format(*val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
执行结果: