-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
174 lines (151 loc) · 5.56 KB
/
API.py
File metadata and controls
174 lines (151 loc) · 5.56 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
import random
import re
import sqlite3
drop_votes = 'DROP TABLE IF EXISTS votes;'
create_votes = '''
CREATE TABLE votes (
song_id INTEGER,
vote TEXT
);
'''
drop_songs = 'DROP TABLE IF EXISTS songs;'
create_songs = '''
CREATE TABLE songs (
id INTEGER UNIQUE,
location TEXT,
title TEXT,
artist TEXT,
album_cover TEXT
);
'''
insert_songs = r'''INSERT INTO songs VALUES (1, '/static/mp3/Havana.mp3', 'Havana', 'Camilla Cabello', '/static/albumCovers/Havana.png'),
(2, '/static/mp3/Paradise.mp3', 'Paradise', 'Coldplay', '/static/albumCovers/Paradise.jpg'),
(3, '/static/mp3/BlankSpace.mp3', 'Blank Space', 'Taylor Swift', '/static/albumCovers/BlankSpace.png'),
(4, '/static/mp3/AchillesComeDown.mp3', 'Achilles Come Down', 'Gang of Youths', '/static/albumCovers/Achilles.jpg'),
(5, '/static/mp3/TheFinalCountdown.mp3', 'The Final Countdown', 'Europe', '/static/albumCovers/Countdown.jpg'),
(6, '/static/mp3/BohemianRhapsody.mp3', 'Bohemian Rhapsody', 'Queen', '/static/albumCovers/Queen.jpg'),
(7, '/static/mp3/Thunder.mp3', 'Thunder', 'Imagine Dragons', '/static/albumCovers/Thunder.jpg'),
(8, '/static/mp3/ShortCircuit.mp3', 'Short Circuit', 'Daft Punk', '/static/albumCovers/Circuit.png'),
(9, '/static/mp3/HotlineBling.mp3', 'Hotline Bling', 'Drake', '/static/albumCovers/Hotline.png'),
(10, '/static/mp3/TheATeam.mp3', 'The A Team', 'Ed Sheeran', '/static/albumCovers/Ateam.jpg'),
(11, '/static/mp3/NeverGonnaGiveYouUp.mp3', 'Never Gonna Give You Up', 'Rick Astley', '/static/albumCovers/NeverGonnaGiveYouUp.jpg'),
(12, "/static/mp3/HowFarI'llGo.mp3", "How Far I'll Go", 'James Curran', '/static/albumCovers/Moana.jpg');'''
insert_votes = '''INSERT INTO votes VALUES (4, 'up'),
(5, 'down'),
(6, 'up'),
(7, 'down'),
(8, 'down'),
(9, 'up'),
(10, 'down');'''
#cur = sqlite3.cursor()
class Database:
def __init__(self):
self.con = sqlite3.connect("ncssbook.db")
self.cur = self.con.cursor()
self.cur.execute(drop_votes)
self.cur.execute(create_votes)
self.cur.execute(drop_songs)
self.cur.execute(create_songs)
self.cur.execute(insert_songs)
self.cur.execute(insert_votes)
def reboot(self):
self.con.close()
self.con = sqlite3.connect("ncssbook.db")
self.cur = self.con.cursor()
self.cur.execute(drop_votes)
self.cur.execute(create_votes)
self.cur.execute(drop_songs)
self.cur.execute(create_songs)
self.cur.execute(insert_songs)
self.cur.execute(insert_votes)
@staticmethod
def rand_music(musics):
return random.choice(musics)
#return object of random music(that has not been played)
#def name_find(self, name):
#if name is in the database
#return True
#else:
#return True
#pass
#def all_names(self):
#return()#all names in the database
db = Database()
cur = db.cur
class Person:
def __init__(self):
self.name = "Jimmy Neutron" #get the current users username
def get_name(self):
return self.name
def good(self): #returns list of songs a user likes
rows = cur.execute("SELECT title, artist FROM songs s JOIN votes v ON s.id=v.song_id WHERE v.vote = 'up'")
songs = []
for row in rows:
song = {}
song["title"] = row[0]
song["artist"] = row[1]
songs.append(song)
return songs
def bad(self): #returns list of songs a user dislikes
rows = cur.execute("SELECT title, artist FROM songs s JOIN votes v ON s.id=v.song_id WHERE v.vote = 'down'")
songs = []
for row in rows:
song = {}
song["title"] = row[0]
song["artist"] = row[1]
songs.append(song)
return songs
#def add_song(self, vote):
#if vote == "BAD":
#add that vote to the database
#elif vote == "GOOD":
#add that vote to the database
#else:
#raise ValueError("vote should be \"BAD\" or \"GOOD\"")
class Song:
def __init__(self, id, name, artist, location, cover):
self.id = id
self.title = name
self.artist = artist
self.location = location
self.cover = cover
person = Person()
def get_all_songs():
song_details = 'SELECT * FROM songs;'
cur.execute(song_details)
musics = []
for row in cur:
id = row[0]
location = row[1]
name = row[2]
artist = row[3]
cover = row[4]
musics.append(Song(id,name,artist,location,cover))
return musics
def vote(input):
print(input)
if input[2] == '0':
cur.execute('''INSERT INTO votes VALUES ({}, 'down');'''.format(input[1]))
else:
cur.execute('''INSERT INTO votes VALUES ({}, 'up');'''.format(input[1]))
up_votes = '''SELECT id, location, title, artist, album_cover FROM songs s JOIN votes v ON s.id=v.song_id WHERE v.vote = 'up';'''
cur.execute(up_votes)
for row in cur:
id = row[0]
location = row[1]
name = row[2]
artist = row[3]
cover = row[4]
person.good().append(Song(id,name,artist,location,cover))
down_votes ='''SELECT id, location, title, artist, album_cover FROM songs s JOIN votes v ON s.id=v.song_id WHERE v.vote = 'down';'''
cur.execute(down_votes)
for row in cur:
id = row[0]
location = row[1]
name = row[2]
artist = row[3]
cover = row[4]
person.bad().append(Song(id,name,artist,location,cover))
return True #to be fixed with try catch block
def get_person():
return person