Repository navigation
Expand file tree
/
Copy pathdbInitialize.py
More file actions
178 lines (139 loc) · 6.12 KB
/
Copy pathdbInitialize.py
File metadata and controls
178 lines (139 loc) · 6.12 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
175
176
177
178
#!/usr/bin/env python3
#-*-coding:utf-8-*-
# https://github.com/pylast/pylast/issues/296
import dbFunctions as fn
import traceback, sqlite3, os.path, time, datetime, logging, os, platform
import sh
sh.init()
today = lambda: str(datetime.datetime.now()).split(' ')[0]
timenow = lambda: str(datetime.datetime.now()).split('.')[0]
retry = 10;
################## Create Logger ##################
logger = logging.getLogger("Initialize Log")
logger.setLevel(logging.INFO)
if platform.system() == 'Windows':
log_file = r"C:\tmp\initialize-{}.log".format(today())
elif platform.system() == 'Linux':
log_file = "/home/pi/Desktop/olo/log_initialize/initialize-{}.log".format(today())
directory = "/home/pi/Desktop/olo/log_initialize/"
else:
log_file = "/Users/eds/Desktop/Projects/olo/log_initialize/initialize-{}.log".format(today())
open(log_file, 'a')
handler = logging.FileHandler(log_file)
logger.addHandler(handler)
###################################################
print("[{}]: @@ Initializing DB at: {}".format(timenow(), datetime.datetime.now()))
logger.info("[{}]: @@ Initializing DB at: {}".format(timenow(), datetime.datetime.now()))
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()
# Create tables if not exists
fn.createTable(cur);
# initialize bucket counters
fn.initBucketCounters(cur, conn);
tracks = None
tracks_dict = {};
_ = 0;
if (sh.OLO_ID != 1):
while True:
try:
print("[{}]: ## Get Tracks".format(timenow()))
logger.info("[{}]: ## Get Tracks".format(timenow()))
# tracks = fn.getLastFmHistroy(username=sh.lastFM_username);
tracks = fn.getLastFmHistroy(username = sh.lastFM_username, limit = 5)
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):
break;
else:
c = 0;
while True:
try:
print("[{}]: ## Getting Tracks from {}".format(timenow(), sh.lastFM_username_eds[c]))
logger.info("[{}]: ## Get Tracks".format(timenow()))
# tracks = fn.getLastFmHistroy(username=sh.lastFM_username);
tracks = fn.getLastFmHistroy(username = sh.lastFM_username_eds[c])
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]))
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)))
tracks = list(sorted(tracks_dict.values(), key=lambda x: int(x[0]), reverse=True))
print("Total tracks length: {}".format(len(tracks)))
if (tracks is not None):
print("[{}]: !! Got {} LastFM tracks".format(timenow(), len(tracks)))
logger.info("[{}]: !! Got {} LastFM tracks".format(timenow(), len(tracks)))
__ = 0;
while True:
# for _ in range(int(retry)):
try:
# insert tracks
print("[{}]: ## Inserting Tracks..".format(timenow()))
logger.info("[{}]: ## Inserting Tracks..".format(timenow()))
fn.insertTracks(cur, username=sh.lastFM_username, conn=conn, logger=logger, tracksToInsert=tracks);
except KeyboardInterrupt:
quit()
except:
__ += 1;
print("[{}]: @@ Caught an exception while initializing DB,,".format(timenow()))
print(traceback.format_exc())
print("[{}]: retrying.. {}".format(timenow(), __))
logger.info("[{}]: @@ Caught an exception while initializing DB,,".format(timenow()))
logger.info(traceback.format_exc())
logger.info("[{}]: retrying.. {}".format(timenow(), __))
continue;
cur.execute("INSERT OR REPLACE INTO lastUpdatedTimestamp VALUES(?,?)", (1,datetime.datetime.now()));
conn.commit();
break;
# # clear the data in the table
# clearTable(cur, "musics");
# vacuumTable(cur);
# # drop the table
# dropTable(cur, "musics");
# Save (commit) the changes to the database
conn.commit()
# 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))