-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_beread.py
69 lines (62 loc) · 2.76 KB
/
generate_beread.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
59
60
61
62
63
64
65
66
67
68
69
from pymongo import MongoClient
from tqdm import tqdm
import datetime
clients = [
[
MongoClient(host="ddbs_mongo_1", port=27017),
MongoClient(host="ddbs_mongo_2", port=27017),
],
[
MongoClient(host="ddbs_mongo_1_bak", port=27017),
MongoClient(host="ddbs_mongo_2_bak", port=27017),
]
]
for db1_client, db2_client in clients:
db1_client.history.read.create_index([("aid",1)])
db2_client.history.read.create_index([("aid",1)])
for article in tqdm(db2_client.info.article.find({}), total=10000):
aid = article["aid"]
beread = dict(
id=article["id"],
timestamp=list(),
aid=aid,
readNum = 0,
readUidList = list(),
commentNum = 0,
commentUidList = list(),
agreeNum = 0,
agreeUidList = list(),
shareNum = 0,
shareUidList = list(),
)
for read_record in db1_client.history.read.find(dict(aid=aid)):
read_record["timestamp"] = datetime.datetime.fromtimestamp(int(read_record["timestamp"])/1000).isoformat()
beread["timestamp"].append(read_record["timestamp"])
beread["readNum"]+=1
beread["readUidList"].append(read_record["uid"])
if int(read_record["commentOrNot"]):
beread["commentNum"]+=1
beread["commentUidList"].append(read_record["uid"])
if int(read_record["agreeOrNot"]):
beread["agreeNum"]+=1
beread["agreeUidList"].append(read_record["uid"])
if int(read_record["shareOrNot"]):
beread["shareNum"]+=1
beread["shareUidList"].append(read_record["uid"])
for read_record in db2_client.history.read.find(dict(aid=aid)):
read_record["timestamp"] = datetime.datetime.fromtimestamp(int(read_record["timestamp"])/1000).isoformat()
beread["timestamp"].append(read_record["timestamp"])
beread["readNum"]+=1
beread["readUidList"].append(read_record["uid"])
if int(read_record["commentOrNot"]):
beread["commentNum"]+=1
beread["commentUidList"].append(read_record["uid"])
if int(read_record["agreeOrNot"]):
beread["agreeNum"]+=1
beread["agreeUidList"].append(read_record["uid"])
if int(read_record["shareOrNot"]):
beread["shareNum"]+=1
beread["shareUidList"].append(read_record["uid"])
db2_client.history.beread.update_one(dict(aid=aid),{"$set":beread},upsert=True)
if article["category"] == "science":
db1_client.history.beread.update_one(dict(aid=aid),{"$set":beread},upsert=True)