-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbQueries.py
203 lines (150 loc) · 6.12 KB
/
dbQueries.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import dbInserts
import accessDB
import sys
from datetime import datetime
#Attempt converting dateStr to either the full time format with an hour, or just w days
def validateDatetimeFormat(dateStr):
try:
return datetime.strptime(dateStr,"%Y-%m-%d %H:%M:%S")
except ValueError:
try:
return datetime.strptime(dateStr,"%Y-%m-%d")
except ValueError:
return dateStr #COuld return anthing really...
def getKey(tableRow):
return tableRow[0]
def outputQueryFetchall(cur):
header = [i[0] for i in cur.description]
return {'rows': cur.fetchall(), 'header': header}
def runAndOutput(db, query):
cur = db.cursor()
cur.execute(query)
return outputQueryFetchall(cur)
def getNewestEntryInDB(db):
query = '''SELECT * FROM `Plays` WHERE 1
ORDER BY `id` DESC
LIMIT 1'''
return runAndOutput(db, query)
def getOldestEntryInDB(db):
query = '''SELECT * FROM `Plays` WHERE 1
ORDER BY `id` ASC
LIMIT 1'''
return runAndOutput(db, query)
def getEntryforRow(queryOut, entry, row=0):
entryPos = queryOut['header'].index(entry) # find index of anything in the header row
return queryOut['rows'][row][entryPos] if entryPos != -1 else "null" #return that id
def songsByArtistInPeriod(db, artist=0, startTime=0, endTime=0):
query = "SELECT DISTINCT * FROM `Plays`"
filters = []
# Create filters for those that were entered (0 is just a sentinel)
if artist != 0:
filters.append("`artist` = \'" + artist + "\'")
if startTime != 0:
# Strip the microseconds
startTime = startTime.strftime("%Y-%m-%d %H:%M:%S")
filters.append("`time` > \'" + startTime + "\'")
if endTime != 0:
# Strip the microseconds
endTime = endTime.strftime("%Y-%m-%d %H:%M:%S")
filters.append("`time` < \'" + endTime + "\'")
# Add the filters that were there
for i in xrange(0, len(filters)):
if i == 0:
query += " WHERE "
query += filters[i]
query += " AND "
query = query[:query.rfind(" AND ")] # clip the trailing AND
print (">> running this query: ", query)
return runAndOutput(db, query)
def topSongsInPeriod(db, topN=0, startTime=0, endTime=0):
query = """SELECT `artist`, `song`, COUNT(*) count FROM `Plays` """
#"""WHERE `time` > '2015-11-22' AND `time` < '2015-11-29'
filters = []
if startTime != 0:
# Strip the microseconds
startTime = startTime.strftime("%Y-%m-%d %H:%M:%S")
#filters.append("`time` > \'" + startTime + "\'")
query += "`time` > \'" + startTime + "\' AND"
if endTime != 0:
# Strip the microseconds
endTime = endTime.strftime("%Y-%m-%d %H:%M:%S")
#filters.append("`time` < \'" + endTime + "\'")
query += "`time` < \'" + endTime + "\' AND "
# Add the filters that were there
# for i in xrange(0, len(filters)):
# if i == 0:
# query += " WHERE "
# query += filters[i]
# query += " AND "
query = query[:query.rfind(" AND ")] # clip the trailing AND
query += """ GROUP BY `song` ORDER BY count DESC """
if topN != 0:
query += """LIMIT 0,""" + str(topN)
print (">> running this query: ", query)
return runAndOutput(db, query)
# ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** ~~ ** #
def runQuery(query=[]):
if len(query) > 1:
db = accessDB.accessDatabase()
output = []
if query[0] == "ARTIST_ANDOR_TIME":
try:
# Try and find args and values in the argv
try:
artistIdx = query.index("-artist")
artistName = query[artistIdx + 1]
except:
artistName = 0
try:
startTimeIdx = query.index("-start")
startTime = query[startTimeIdx + 1]
except:
startTime = 0
try:
endTimeIdx = query.index("-end")
endTime = query[endTimeIdx + 1]
except:
endTime = 0
output = songsByArtistInPeriod(db, artist=artistName, startTime=startTime, endTime=endTime)
print output
except Exception,e:
print "Query 'ARTIST_ANDOR_TIME' takes -artist -start & -end arguments... have fun!"
print e
if query[0] == "TOP_N_IN_TIME":
try:
# Try and find args and values in the argv
try:
topNIdx = query.index("-topN")
topN = query[topNIdx + 1]
except:
topN = 20 # a default value
try:
startTimeIdx = query.index("-start")
startTime = query[startTimeIdx + 1]
except:
startTime = 0
try:
endTimeIdx = query.index("-end")
endTime = query[endTimeIdx + 1]
except:
endTime = 0
output = topSongsInPeriod(db, count=topN, startTime=startTime, endTime=endTime)
print output
except Exception,e:
print "Query 'TOP_N_IN_TIME' takes -countN -start & -end arguments... get emmmm"
print e
else:
print query[0], " is not a recognised query.."
accessDB.closeConnection(db)
else:
print "I can't do a damn thing if you dont tell me what you want to do"
return output
if __name__ == "__main__":
runQuery(sys.argv[1:]) # Run the argv but strip out the file name in position 0 //SORT THIS OUT
# db = accessDB.accessDatabase()
# lol = topSongsInPeriod(db, 200)
# for row in lol['rows']:
# print row
# # query = "SELECT `artist`, COUNT(*) AS 'Count' FROM `Plays` GROUP BY `artist` SORT BY `Count` LIMIT 30"
# db = accessDB.accessDatabase()
# print runAndOutput(db, query)