-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsong_finder.py
executable file
·60 lines (49 loc) · 1.64 KB
/
song_finder.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
import sqlite3
import re
def getArtists(cursor, search):
artists =[]
query = "SELECT artist,year,genre FROM lyrics WHERE song LIKE '%s';" %(search)
cursor.execute(query)
rows=cursor.fetchall()
for row in rows:
#print(row[0])
artists.append(row[0])
return artists
def compareArtists(artists, search):
for artist in artists:
if search == artist:
return artist
def searchStringLogic(cursor, search_string):
words = search_string.split(' ')
for i in range(len(words) - 1):
artist = '-'.join(words[0:i])
song = '-'.join(words[i+1:])
print('Artist', artist)
print('Song', song)
artists = getArtists(cursor, song)
artist_result = compareArtists(artists, artist)
if artist_result is not None:
print(artist_result)
return (artist_result, song)
return (None, None)
def getAllSongs(cursor, artist, song):
songs = []
query = "SELECT song, year FROM lyrics WHERE artist LIKE '%s' AND song NOT LIKE '%s';" %(song, artist)
cursor.execute(query)
rows=cursor.fetchall()
for row in rows:
print(row[0], row[1])
songs.append(str(row[0]) + str(row[1]))
return songs
def populateSongs(search):
try:
conn=sqlite3.connect('ps.db')
except sqlite3.Error as e:
print(e)
cursor = conn.cursor()
(artist, song) = searchStringLogic(cursor, search)
if artist is not None:
songs = getAllSongs(cursor, artist, song)
else:
songs = [None]
return songs