def main(): set_from_db.get_songs_from_file() set_from_db.get_users_from_file() # Users.initiate_users() # SongsList.initiate_songs() Users.add_user("eytan", "123") e = Users.get_user("eytan") e.change_password("123", "456") e.add_playlist("lala") e.add_playlist("lala2") p = e.get_playlist("lala") s1 = Song("hamilton", 2010, "rap", "lin manuel miranda") s2 = Song("hamilton_1", 2010, "rap", "lin manuel miranda") SongsList.add_song(s1) SongsList.add_song(s2) e.add_song_to_playlist("lala", "hamilton") e.add_song_to_playlist("lala", "hamilton_1") e.add_song_to_playlist("lala2", "hamilton_1") set_from_db.write_users_to_file() set_from_db.write_songs_to_file() print("h")
def get_songs_from_source(songs): SongsList.initiate_songs() for current in songs: song = Song(current['title'], current['year'], current['genre'], current['performer']) song.rating = current['rating'] SongsList.add_song(song)
def write_songs_to_file(): all_songs = [] for s in SongsList.get_all_songs(): song = SongsList.get_song(s) to_write = { "title": song.title, "year": song.year, "genre": song.genre, "performer": song.performer, "rating": song.rating } all_songs.append(to_write) with open(songs_file_path, 'w') as f: f.write(json.dumps(all_songs, indent=4))
def get_ranked_songs(): rank = int(request.args.get("rank")) op = request.args.get("op") valid = valid_parameters(rank=rank, op=op) if valid != True: return valid all_songs = SongsList.get_all_songs_obj() returned_songs = [] if op == "less": returned_songs = [ v.title for v in all_songs.values() if v.rating < rank ] elif op == "eq": returned_songs = [ v.title for v in all_songs.values() if v.rating == rank ] elif op == "greater": returned_songs = [ v.title for v in all_songs.values() if v.rating > rank ] else: return {"error": f"no such operation {op}"} return {"message": "OK", "data": returned_songs}
def downvote_song(): user_name = request.json.get("user_name") user_password = request.json.get("user_password") song_title = request.json.get("song_title") valid = valid_parameters(user_name=user_name, user_password=user_password, song_title=song_title) if valid != True: return valid user = Users.get_user(user_name) if user is None: return {"error": f"the user {user_name} does not exist"} if user.password != user.parse_password(user_password): return {"error": f"either the user name or the password are wrong"} s = SongsList.get_song(song_title) if s is None: return {"error": "no such song in the songs collection"} s.downvote() set_from_db.write_songs_to_file() return { "message": "OK", "data": { "song_title": song_title, "rating": s.rating } }
def add_song(self, song_title): if song_title in self.get_songs(): return None song = SongsList.get_song(song_title) if song is None: return None else: self.songs.append(song) return song_title
def add_song(): song_title = request.json.get("song_title") song_genre = request.json.get("song_genre") song_year = request.json.get("song_year") song_performer = request.json.get("song_performer") valid = valid_parameters(song_title=song_title, song_genre=song_genre, song_year=song_year, song_performer=song_performer) if valid != True: return valid s = Song(song_title, song_year, song_genre, song_performer) add_song = SongsList.add_song(s) if add_song is None: return {"error": "this song already exist in the collection"} set_from_db.write_songs_to_file() return {"message": "OK", "data": song_title}