-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.py
More file actions
121 lines (90 loc) · 3.34 KB
/
cache.py
File metadata and controls
121 lines (90 loc) · 3.34 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import datetime
import json
import logging
import click
def timestamp():
now = datetime.datetime.now()
return ('['+str(now.hour)+':'+str(now.minute)+':'+str(now.second)+'] ')
def clear(age):
'''Clears cache older than age(days)'''
d_count = 0
with open('cache/playlist.json') as f:
data = json.load(f)
for item in data:
if get_age_in_days(data[item]) > age:
del data[item]
d_count += 1
with open('cache/playlist.json', 'w') as f:
json.dump(data, f, indent=4)
with open('cache/search.json') as f:
data = json.load(f)
for item in data:
if get_age_in_days(data[item]) > age:
del data[item]
d_count += 1
with open('cache/search.json', 'w') as f:
json.dump(data, f, indent=4)
click.echo(timestamp()+"cache.clear() - Cleared "+str(d_count)+" items from cache")
def get_age_in_days(data):
now = datetime.datetime.now()
created_at = datetime.datetime.strptime(data["created_at"], "%Y-%m-%d %H:%M:%S")
age_in_days = (now - created_at).days
return age_in_days
def save(x,url,title,t):
logging.info("Writing to cache")
d = {}
if t == 0:
path = 'cache/search.json'
elif t == 1:
path = 'cache/playlist.json'
elif t == 2:
path = 'cache/url.json'
try:
with open(path) as f:
data = json.load(f)
except:
data = {}
d["url"] = url
d["title"] = title
d["created_at"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data[x] = d
with open(path, 'w') as f:
json.dump(data, f, indent=4)
def load(x,t):
'''Loads cache from file'''
if t == 0:
path = 'cache/search.json'
elif t == 1:
path = 'cache/playlist.json'
elif t == 2:
path = 'cache/url.json'
with open(path) as f:
data = json.load(f)
if x in data:
jsn = []
logging.info("Reading from cache")
if t == 1:
for i in range(len(data[x]['url'])):
jsn.append({
"source": data[x]['url'][i],
"title": data[x]['title'][i]
})
else:
jsn.append({
"source": data[x]['url'],
"title": data[x]['title']
})
return jsn
else:
return None
# u = "https://www.youtube.com/watch?v=sck4_Vhr-H4"
# t = "gumdrops"
# save("nelward gumdrops",{"url":u,"title":t},0)
# i = "PLmeBqWgbwZgi48Y-VnhmCJbFob06Rc1p8"
# url = [
# "https://www.youtube.com/watch?v=68LWlLgHzAI","https://www.youtube.com/watch?v=i_-wXT0zERc","https://www.youtube.com/watch?v=sDLsSQf3Hc0","https://www.youtube.com/watch?v=MgXDWIYEV3Y","https://www.youtube.com/watch?v=4Q_qAwTiJ-w","https://www.youtube.com/watch?v=zJKiBZqHO5o"
# ]
# title = [
# "smle - It'll Be Okay","smle - Haunted (feat Seann Bowe) (Official Music Video)","SMLE - 2 Me (feat. Kiddo Ai & Nick Smith) [Monstercat Release]","SMLE - Halo (feat. Helen Tess) [Monstercat Release]","WRLD x SMLE - Stranded (feat. Kiddo AI)","smle - Just 5 More Minutes"
# ]
# save(i,{"url":url,"title":title},1)