-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbest_parser.py
More file actions
executable file
·106 lines (82 loc) · 2.63 KB
/
best_parser.py
File metadata and controls
executable file
·106 lines (82 loc) · 2.63 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
# coding:utf8
#
# script for creating the popular-set
#
# >> python best_parser.py
#
# 1) import django
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ask_kasatkin.settings")
django.setup()
# 2) import models, etc else
import json
from datetime import timedelta
from django.utils import timezone
from user_profile.models import UserProperties
from core.models import TagName, Question, Answer
FILENAME = "best_data.txt" # for output
# this script loads every 15 min, asks db for updates and save the results
# get TOP10 users !!! which questions and answers are most popular this week !!!
def load_unique_users(amount):
result = []
append = result.append
time_window = timezone.now() - timedelta(days=7)
for best in Question.objects.filter(date__gte=time_window).order_by("-rating").prefetch_related("author")[:amount]:
buf = {
"rating": best.rating,
"nickname": best.author.nickname,
"id": best.author.user.id
}
if buf not in result:
append(buf)
for best in Answer.objects.filter(date__gte=time_window).order_by("-rating").prefetch_related("author")[:amount]:
buf = {
"rating": best.rating,
"nickname": best.author.nickname,
"id": best.author.user.id
}
if buf not in result:
append(buf)
result.sort()
return result
def get_pop_users():
result = load_unique_users(20)
if len(result) == 0:
result = load_unique_users(40)
output = []
append = output.append
for usr in result:
buf = {
"nickname": usr["nickname"],
"id": usr["id"]
}
if buf not in output:
append(buf)
if len(output) == 10:
break
return output
label_color = [
"#DD4814", "#E05A2B", "#E36C43", "#E77E5A", "#EA9172",
"#EEA389", "#EFAC95", "#F1B5A1", "#F3BEAC", "#F4C8B8",
"#F6D1C4", "#F8DAD0", "#F9E3DB", "#FBECE7", "#FBECE7",
"#FBECE7", "#FBECE7", "#FBECE7", "#FBECE7", "#FBECE7",
]
# get TOP20 tags here
def get_pop_tags():
result = []
append = result.append
for tag in TagName.objects.all():
amount = Question.objects.filter(tags=tag).count()
append([amount, tag.name])
result.sort(reverse=True)
return [{"text": result[i][1], "label": label_color[i]} for i in range(len(result[:20]))]
def save_data():
data = {}
data["popular_users"] = get_pop_users()
data["popular_tags"] = get_pop_tags()
with open(FILENAME, "w") as f:
f.write(json.dumps(data))
if __name__ == "__main__":
save_data()