forked from RainwXY/zhihu-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer_topic.py
58 lines (48 loc) · 1.84 KB
/
answer_topic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
import sys
import json
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
from zhihu_oauth import ZhihuClient
from neo4j import Database
client = ZhihuClient()
client.load_token('token.pkl')
database = Database()
# me = client.me()
def user_bestanswers():
j = 0
while True:
answerIDs = database.graph.data("match(u:User)-[:AUTHOR]->(a:Answer) where a.answer_topic_corresponded is null return a.answerId as answerId skip 800 limit 100")
for answerID in answerIDs:
try:
# flag = is_coresspoded(answerID)
# if flag is 1:
# print("抓过了"+str(answerID))
# continue
answer = client.answer(int(answerID["answerId"]))
topics = answer.question.topics
content = json.dumps(answer.content)
tx = database.graph.begin()
for topic in topics:
cypher = "merge(a:Answer{answerId: '"+str(answerID["answerId"])+"'}) set a.answer_topic_corresponded = true, a.content = "+content+" with a merge(t:Topic{name:"+json.dumps(topic.name)+"}) merge (a)-[:BELONGED]->(t)"
tx.run(cypher)
# database.graph.data(cypher)
tx.commit()
j += 1
print("对应了"+str(j)+"answer->topic")
except Exception, e:
print(e)
continue
print("it is over")
def is_coresspoded(answerID):
flag = database.graph.data("match(a:Answer{answerId: '"+str(answerID["answerId"])+"'}) where a.xxx = 1 return count(a) as num ")
if flag[0]["num"] == 1:
return 1
else:
return 2
def main():
user_bestanswers()
if __name__ == '__main__':
main()