Last active
March 26, 2018 10:32
-
-
Save dongweiming/df2573f153d4cfdda88c2699b9032582 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 | |
# 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/34142963/comments?limit=2000&offset=0' | |
TOKEN_FILE = 'token.pkl' | |
END_TIMESTAMP = datetime(2018, 3, 5, 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'] | |
name = comment['author']['name'] | |
if url in seen_profiles: | |
continue | |
seen_profiles.add(url) | |
NUMBER_MAP[int(number)].append(('{}({})'.format(name, 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 3学习笔记(上卷)》:\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