Created
May 31, 2017 08:21
-
-
Save dongweiming/6320817b9d9a8c487751830ab08fd4d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# pip install zhihu_oauth python-dateutil | |
import os | |
import re | |
from datetime import datetime | |
from collections import defaultdict | |
import dateutil.parser | |
from zhihu_oauth import ZhihuClient | |
API_URL = 'https://zhuanlan.zhihu.com/api/posts/27063533/comments?limit=2000&offset=0' | |
TOKEN_FILE = 'token.pkl' | |
END_TIMESTAMP = datetime(2017, 5, 31, tzinfo=dateutil.tz.gettz('Asia/Shanghai')) | |
NUMERIC_REGEX = re.compile(r'(\d){2}') | |
COUNT = 5 | |
NUMBER_MAP = defaultdict(list) | |
client = ZhihuClient() | |
seen_profiles = set() | |
if os.path.isfile(TOKEN_FILE): | |
client.load_token(TOKEN_FILE) | |
else: | |
client.login_in_terminal() | |
client.save_token(TOKEN_FILE) | |
me = client.me() | |
rs = me._session.get(API_URL) | |
for index, comment in enumerate(rs.json(), 1): | |
created = dateutil.parser.parse(comment['createdTime']) | |
if created >= END_TIMESTAMP: | |
break | |
content = re.sub('<[^<]+?>', '', comment['content']) | |
match = NUMERIC_REGEX.search(content) | |
if not match: | |
continue | |
number = match.group() | |
_content = content.replace(number, '', 1) | |
if NUMERIC_REGEX.search(_content): | |
continue | |
url = comment['author']['profileUrl'] | |
slug = comment['author']['slug'] | |
if url in seen_profiles: | |
continue | |
seen_profiles.add(url) | |
NUMBER_MAP[int(number)].append((slug, index, str(created), content)) | |
number = int(input('输入幸运数字❯ ')) | |
values = NUMBER_MAP.get(number, []) | |
if not len(values) >= COUNT: | |
offset = 1 | |
while 1: | |
values += sorted(sum([NUMBER_MAP.get(number + o, []) | |
for o in (offset, -offset)], []), | |
key=lambda x: x[1]) | |
if len(values) > COUNT: | |
break | |
offset += 1 | |
values = values[:COUNT] | |
print('\n如下{}位同学获得《流畅的Python》:\n'.format(COUNT)) | |
for val in values: | |
print('用户: {:<18}\t楼层: {:<4} 参与时间: {:<10} ' | |
'评论内容:{}'.format(*val)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment