This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.py
68 lines (50 loc) · 1.84 KB
/
exporter.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 datetime
import json
import config
from models import Tweet
def get_stat_until(datetime_until) -> tuple[int, int]:
hour_from = datetime_until - datetime.timedelta(hours=6)
tweets = Tweet.select().where(
(Tweet.created_at > hour_from)
& (Tweet.created_at < datetime_until)
& (
(Tweet.is_better == True & Tweet.is_worse == False)
| (Tweet.is_worse == True & Tweet.is_better == False)
)
)
is_worse, is_better = 0, 0
# TODO Optimize it into sql query someday
for tweet in tweets:
is_retweet = tweet.text.startswith("RT @")
coeficient = 1
if is_retweet:
coeficient = 0.20
if tweet.is_worse:
is_worse += coeficient
if tweet.is_better:
is_better += coeficient
return is_worse, is_better
def export():
current_time = datetime.datetime.now().replace(minute=0, second=0, microsecond=0)
analytics_by_date = {}
# Last 80 hours with step 4
for i in range(0, 81, 4):
previous_hour = current_time - datetime.timedelta(hours=i)
is_worse, is_better = get_stat_until(previous_hour)
count = is_worse + is_better
if not count:
# Looks like we don't have tweets yet
continue
worse_percentage = round(is_worse * 100 / count)
better_percentage = round(is_better * 100 / count)
hour_str = previous_hour.isoformat()
print(hour_str, better_percentage, worse_percentage)
analytics_by_date[hour_str] = (worse_percentage, better_percentage)
with open(config.ANALYTICS_FILE, "w") as analytics_file:
data = {
"updated": datetime.datetime.now().isoformat(),
"data": analytics_by_date,
}
analytics_file.write(json.dumps(data))
if __name__ == "__main__":
export()