Skip to content

Instantly share code, notes, and snippets.

@dongweiming
Last active December 31, 2016 09:25
Show Gist options
  • Save dongweiming/ea0803913296ab36c1f039e2b9eb6812 to your computer and use it in GitHub Desktop.
Save dongweiming/ea0803913296ab36c1f039e2b9eb6812 to your computer and use it in GitHub Desktop.
#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)
@dongweiming
Copy link
Author

执行结果:

❯ python v2ex.py
输入幸运数字❯ 84

如下3位同学获得《Python Web开发实战》:

用户ID: 125050    昵称:     zjb861107   楼层:  682    参与数字: 84    参与时间:2016-10-17 13:58:40
用户ID: 171753    昵称:       juyulin   楼层:  908    参与数字: 84    参与时间:2016-10-17 21:21:26
用户ID: 163669    昵称:  DarkAlexWang   楼层:  982    参与数字: 84    参与时间:2016-10-18 01:57:31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment