This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_last_edit_array.py
68 lines (55 loc) · 1.65 KB
/
get_last_edit_array.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
import pymongo
import time
import json
client = pymongo.MongoClient()
usersCollection = client.wikimedia_user_metrics.users
THRESHOLD = 10
def get_months():
months = []
for year in range(2002, 2021):
for month in (range(1, 13) if year < 2020 else range(1, 10)):
months.append(str(year) + "/" + str(month))
return months
def get_obj():
print('Starting get_obj', time.time())
obj = {}
print('Getting parsed users', time.time())
parsedUsers = list(usersCollection.aggregate([
{
'$match': {
'is_bot': False,
'lastMonth': { '$ne': None }
}
}, {
'$project': {
'id': True,
'lastMonth': True,
'events_tot': {
'$sum': [
'$n_pages_created', '$n_pages_deleted', '$n_pages_edited', '$n_pages_moved', '$n_pages_restored'
]
}
}
}, {
'$match': {
'events_tot': {
'$gte': THRESHOLD
}
}
}
]))
print('Got parsed users ' + str(len(parsedUsers)), time.time())
print('Generating object', time.time())
for parsedUser in parsedUsers:
month = parsedUser['lastMonth']
if month not in obj:
obj[month] = 0
else:
obj[month] += 1
print('Generated object', time.time())
print('Done get_obj', time.time())
return obj
obj = get_obj()
text = json.dumps(obj)
with open("last_edit_object_more_than_" + str(THRESHOLD) + ".json", 'w') as outfile:
outfile.write(text)