-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
91 lines (66 loc) · 2.11 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
import feedparser
def get_all_tags(RSS_URL):
feed = feedparser.parse(RSS_URL)
tags = []
for post in feed.entries:
tags.append(post.tags[0].term)
return set(tags)
import json
from collections import Counter
def getuserdata():
with open('data.json') as json_file:
data = json.load(json_file)
return data
def save_and_update_data(userdata:dict):
try:
with open('data.json') as f:
data = json.load(f)
f.close()
except Exception:
with open('data.json','w') as f:
data = []
json.dump(data, f)
f.close()
categories = []
for item in userdata['categories']:
categories.append(item)
userdata['categories'] = categories
data.append(userdata)
with open('data.json', 'w') as f:
print(data)
json.dump(data, f)
f.close()
def show_statistics():
with open('data.json') as f:
data = json.load(f)
f.close()
users_count = len(data)
categories = []
times = []
for item in data:
times.append(item['time'])
for cat in item['categories']:
categories.append(cat)
categories_str = ""
for k,v in Counter(categories).most_common(100):
categories_str+="{}: {}\n".format(k,v)
times_str = ""
for k,v in Counter(times).most_common(100):
times_str+="{}: {}\n".format(k,v)
return """Кількість підписок: {}\n\nПідписка на категорії у розрізі популярності:\n{}\nБажаний час дайджесту:\n{}""".format(users_count, categories_str, times_str)
import pickle
def save_intercations(data):
try:
import cPickle as pickle
except ImportError: # python 3.x
import pickle
with open('data.p', 'wb') as fp:
pickle.dump(data, fp)
def load_interaction():
with open('data.p', 'rb') as fp:
data = pickle.load(fp)
return data