Repository navigation
Expand file tree
/
Copy pathdbupdate.py
More file actions
174 lines (142 loc) · 7.1 KB
/
Copy pathdbupdate.py
File metadata and controls
174 lines (142 loc) · 7.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
#-*-coding:utf-8-*-
import dbFunctions as fn
import os.path, traceback, time, datetime, logging, os
import sqlite3
import sh
sh.init()
retry = 10;
baseScale = 100
today = lambda: str(datetime.datetime.now()).split(' ')[0]
timenow = lambda: str(datetime.datetime.now()).split('.')[0]
################## Create Logger ##################
logger = logging.getLogger("Update Log")
logger.setLevel(logging.INFO)
if os.name == 'nt':
log_file = r"C:\tmp\dbupdate-{}.log".format(today())
else:
log_file = "/home/pi/Desktop/olo/log_dbupdate/dbupdate-{}.log".format(today())
directory = "/home/pi/Desktop/olo/log_dbupdate/"
if not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
open(log_file, 'a')
handler = logging.FileHandler(log_file)
logger.addHandler(handler)
###################################################
startTime = datetime.datetime.now()
print("[{}]: @@ Running DB update at: {}".format(timenow(), startTime))
logger.info("[{}]: @@ Running DB update at: {}".format(timenow(), startTime))
start_time = time.time();
# create a database connection and a cursor that navigates/retrieves the data
conn = sqlite3.connect(fn.dbPath(sh.dbname_eds));
cur = conn.cursor()
fn.addDailyStats(cur, conn, startTime)
cur.execute("SELECT * FROM lastUpdatedTimestamp");
res = cur.fetchone()
lastUpdatedDate = datetime.datetime.strptime(res[1], "%Y-%m-%d %H:%M:%S.%f")
tracks_dict = {};
# do not run the script if the last updated date is within a day
timeDiff = (startTime - lastUpdatedDate)
timeDiff_mins = int(timeDiff.total_seconds()/60)
logger.info("[{}]: ** Time difference in minutes is {}".format(timenow(), timeDiff_mins))
# allow max 10 minutes of update time when checking freshness
if (timeDiff_mins > 1430):
print("[{}]: @@ DB is outdated. Starts updating..".format(timenow()))
logger.info("[{}]: @@ DB is outdated. Starts updating..".format(timenow()))
limitScale = timeDiff.days * baseScale * 1.5
if (limitScale > 1000):
limitScale = 1000
if (sh.OLO_ID != 1):
tracks = None
for _ in range(int(retry)):
try:
tracks = fn.getLastFmHistroy(username=sh.lastFM_username, limit=limitScale);
except KeyboardInterrupt:
exit()
except:
print("[{}]: @@ Caught an exception while getting LastFM Histroy,,".format(timenow()))
print(traceback.format_exc())
print("[{}]: retrying.. {} out of {}".format(timenow(), str(_+1), str(retry)))
logger.info("[{}]: @@ Caught an exception while getting LastFM Histroy,,".format(timenow()))
logger.info(traceback.format_exc())
logger.info("[{}]: retrying.. {} out of {}".format(timenow(), str(_+1), str(retry)))
continue;
else:
c = 0;
for _ in range(int(retry)):
try:
print("[{}]: ## Getting Tracks from {}".format(timenow(), sh.lastFM_username_eds[c]))
logger.info("[{}]: ## Getting Tracks from {}".format(timenow(), sh.lastFM_username_eds[c]))
# tracks = fn.getLastFmHistroy(username=sh.lastFM_username);
tracks = fn.getLastFmHistroy(username = sh.lastFM_username_eds[c], limit=limitScale)
except KeyboardInterrupt:
quit()
except:
_ += 1;
print("[{}]: @@ Caught an exception while getting LastFM Histroy,,".format(timenow()))
print(traceback.format_exc())
print("[{}]: retrying.. {}".format(timenow(), _))
logger.info("[{}]: @@ Caught an exception while getting LastFM Histroy,,".format(timenow()))
logger.info(traceback.format_exc())
logger.info("[{}]: retrying.. {}".format(timenow(), _))
continue;
if (tracks is not None):
print("[{}]: @@ got {} tracks from {}".format(timenow(), len(tracks), sh.lastFM_username_eds[c]))
logger.info("[{}]: @@ got {} tracks from {}".format(timenow(), len(tracks), sh.lastFM_username_eds[c]))
for track in tracks:
skip = False;
key = int(track[0])
# if another entry is found at the same timestamp
if key in tracks_dict:
# try searching for an empty timestamp for the next 5 slots
skip = True
for i in range(6):
# if an empty slot is found, update the timestamp
if (key+i) not in tracks_dict:
key = key+i
track[0] = str(key)
skip = False
break;
if (not skip):
tracks_dict[key] = track
tracks = None
c += 1;
if (c >= len(sh.lastFM_username_eds)):
break;
if (sh.OLO_ID == 1):
print("\nTotal dictionary size: {}".format(len(tracks_dict)))
logger.info("\nTotal dictionary size: {}".format(len(tracks_dict)))
tracks = list(sorted(tracks_dict.values(), key=lambda x: int(x[0]), reverse=True))
print("Total tracks length: {}".format(len(tracks)))
logger.info("Total tracks length: {}".format(len(tracks)))
if (tracks is not None):
print("[{}]: ### tracks: {}, length: {}".format(timenow(), type(tracks), len(tracks)))
logger.info("[{}]: ### tracks: {}, length: {}".format(timenow(), type(tracks), len(tracks)))
for _ in range(int(retry)):
try:
# insert tracks
fn.insertTracks(cur, username=sh.lastFM_username, conn=conn, logger=logger, update=True, tracksToInsert=tracks);
except KeyboardInterrupt:
exit()
except:
print("[{}]: @@ Caught an exception while initializing DB,,".format(timenow()))
print(traceback.format_exc())
print("[{}]: @@ retrying.. {} out of {}".format(timenow(), str(_+1), str(retry)))
logger.info("[{}]: @@ Caught an exception while initializing DB,,".format(timenow()))
logger.info(traceback.format_exc())
logger.info("[{}]: retrying.. {} out of {}".format(timenow(), str(_+1), str(retry)))
continue;
# insert a timestamp
cur.execute("INSERT OR REPLACE INTO lastUpdatedTimestamp VALUES(?,?)", (1, datetime.datetime.now()));
conn.commit();
break;
else:
print("[{}]: @@ DB is still fresh!".format(timenow()))
logger.info("[{}]: @@ DB is still fresh!".format(timenow()))
# save and reset bucket counters
fn.initBucketCounters(cur, conn=conn, update=True);
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
print("[{}]: --- ### Executed in [{}] seconds ---".format(timenow(), time.time() - start_time));
logger.info("[{}]: --- ### Executed in [{}] seconds ---".format(timenow(), time.time() - start_time))