-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
98 lines (81 loc) · 2.67 KB
/
tasks.py
File metadata and controls
98 lines (81 loc) · 2.67 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
import os
from io import StringIO
import celery
import telebot
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, select_autoescape
from config import config
from lib.stat import JSONLogParser
app = celery.Celery("celery_app")
app.config_from_object("celeryconfig")
bot = telebot.TeleBot(config["TELEGRAM_TOKEN"])
path = os.path.join(os.getcwd(), "templates")
loader = ChoiceLoader([FileSystemLoader(path)])
env = Environment(
loader=loader,
autoescape=select_autoescape(),
)
def prepare_template_data(report):
hashtags_frequency = {
k: v
for k, v in sorted(
report["hashtags_frequency"].items(), key=lambda item: -item[1]
)
}
hashtags_frequency = {
"chart_labels": [f"'{e}'" for e in hashtags_frequency.keys()],
"chart_values": [e for e in hashtags_frequency.values()],
}
word_stats = {
k: v
for k, v in sorted(report["word_stats"].items(), key=lambda item: -item[1])
if v > 1
}
word_stats = {
"chart_labels": [f"'{e}'" for e in word_stats.keys()][:250],
"chart_values": [e for e in word_stats.values()][:250],
}
most_replies = {
k: v
for k, v in sorted(report["most_replies"].items(), key=lambda item: -item[1])
if v >= 1
}
most_replies = {
"chart_labels": [f"'{e}'" for e in most_replies.keys()][:250],
"chart_values": [e for e in most_replies.values()][:250],
}
unique_hashtags = report["unique_hashtags"]
data = {
"hashtags_frequency": {
"data": hashtags_frequency,
"type": "pie",
"title": "Hashtag frequency (all)",
},
"unique_hashtags": {
"data": unique_hashtags,
"type": "cloud",
"title": "List of unique of hashtags (all)",
},
"word_stats": {
"data": word_stats,
"type": "pie",
"title": "Word stats (top 250)",
},
"most_replies": {
"data": most_replies,
"type": "bar",
"title": "Most replied users (top 250)",
},
}
return data
@app.task
def run_task(reply_to: int, data_dict: dict):
data = JSONLogParser(data_dict=data_dict)
report = data.generate_stats()
template = env.get_template("report.html")
# prepare data and render template
chart_data = prepare_template_data(report=report)
template_data = template.render(chart_data=chart_data, chat_name=data.name)
obj = StringIO(template_data)
obj.name = f"{data.id}.html"
bot.send_document(chat_id=reply_to, document=obj, caption="your stats result file")
# bot.send_message(reply_to, 'Hello!!!')