-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (107 loc) · 4.51 KB
/
app.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
from flask import Flask, request, render_template, jsonify
import database
app = Flask("__name__")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/search")
def search():
# Get the searched lyrics and artist from ajax get request
lyrics = request.args.get("lyrics")
artist = request.args.get("artist")
start_date = request.args.get("start_date")
end_date = request.args.get("end_date")
# If nothing searched, return blank html
if not any([lyrics, artist, start_date, end_date]):
return jsonify({"html": ""})
# Connect to database and get cursor
conn, cursor = database.connect_to_db()
# Check if an artist name was searched,
# If so, then try to find an associated artist_id
if artist:
artist_query = "SELECT id FROM artists WHERE artist_name LIKE '%' || ? || '%'"
cursor.execute(artist_query, (artist,))
artist_ids = cursor.fetchall()
artist_ids = tuple(i[0] for i in artist_ids)
# SQLITE has a max limit for wildcards at 999
if len(artist_ids) > 999:
artist_ids = artist_ids[:998]
# If only artist searched,
# Then select songs based on artists
if artist and not any([lyrics, start_date, end_date]):
song_query = (
"SELECT song_name, artist1_id, first_played FROM songs WHERE artist1_id IN ("
+ ",".join("?" * len(artist_ids))
+ ")"
)
song_parameters = artist_ids
# If only lyrics searched,
# Then select songs based on lyrics
elif lyrics and not any([artist, start_date, end_date]):
song_query = """SELECT songs.song_name, artists.artist_name, songs.first_played FROM songs INNER JOIN artists ON songs.artist1_id = artists.ID
WHERE lyrics LIKE '%' || ? || '%'"""
song_parameters = (lyrics,)
# If only start date set,
# Then select all songs
elif start_date and not any([artist, lyrics, end_date]):
song_query = "SELECT song_name, artist1_id, first_played FROM songs INNER JOIN artists ON songs.artist1_id = artists.ID WHERE first_played > ?"
song_parameters = (start_date,)
# If only end date set,
# Then select all songs
elif end_date and not any([artist, lyrics, start_date]):
song_query = "SELECT song_name, artist1_id, first_played FROM songs WHERE first_played < ?"
song_parameters = (end_date,)
# If both artist and lyrics searched, but no timerange set
elif artist and lyrics and not any([start_date, end_date]):
song_query = (
"""SELECT song_name, artist1_id, first_played FROM songs WHERE lyrics LIKE '%' || ? || '%'
AND artist1_id IN ("""
+ ",".join("?" * len(artist_ids))
+ ")"
)
song_parameters = [lyrics]
for i in artist_ids:
song_parameters.append(i)
song_parameters = tuple(song_parameters)
# If both artist and start date
elif artist and start_date and not any([lyrics, end_date]):
song_query = (
"""SELECT song_name, artist1_id, first_played FROM songs WHERE first_played > ?
AND artist1_id IN ("""
+ ",".join("?" * len(artist_ids))
+ ")"
)
song_parameters = [start_date]
for i in artist_ids:
song_parameters.append(i)
song_parameters = tuple(song_parameters)
# If both artist and end date
elif artist and end_date and not any([lyrics, start_date]):
song_query = (
"""SELECT song_name, artist1_id, first_played FROM songs WHERE first_played < ?
AND artist1_id IN ("""
+ ",".join("?" * len(artist_ids))
+ ")"
)
song_parameters = [end_date]
for i in artist_ids:
song_parameters.append(i)
song_parameters = tuple(song_parameters)
elif lyrics and start_date and not any([artist, end_date]):
song_query = """SELECT song_name, artist_id, first_played FROM songs WHERE lyrics LIKE '%' || ? || '%'
AND first_played > ?"""
song_parameters = (lyrics, start_date)
# Execute search and get all results
cursor.execute(song_query, song_parameters)
songs = cursor.fetchall()
# Close database connection and cursor
database.close_db_cursor_and_conn(conn, cursor)
return jsonify(
{
"html": "<ul>\n{}</ul>".format(
"\n".join("<li>{}</li>".format(s) for s in songs)
)
}
)
if __name__ == "__main__":
app.run(debug=True)