def create_album(name, date_created, user_id): """Create new album to be added to db""" album = Album(name=name, date_created=date_created, user_id=user_id) db.session.add(album) db.session.commit() return album
def add_album(obj): with session_scope() as session: session.add( Album(name=obj['name'], spotify_id=obj['spotify_id'], image_url=obj['images'][0]['url'], release_date=obj['release_date'], number_of_tracks=len(obj['tracks']['items']), artist_id=obj['artists']['id'], popularity=obj['popularity']))
def getAlbumList(): key_ = "blog_albumList_key" try: menus = memcache.get(key_) except Exception: menus = None if menus is None: menus = Album.gql('WHERE valid =:1 ORDER BY order desc',True).fetch(100) memcache.add(key=key_, value=menus, time=3600) else: logging.debug("getAlbumList from cache. ") return menus
def getAlbumList(): key_ = "blog_albumList_key" try: menus = memcache.get(key_) except Exception: menus = None if menus is None: menus = Album.all().filter('valid',True).order('-order') memcache.add(key=key_, value=menus, time=3600) else: logging.debug("getAlbumList from cache. ") return menus
def getAlbumList(): key_ = "blog_albumList_key" try: albums = memcache.get(key_) except Exception: albums = None if albums is None: albums = Album.gql('WHERE valid =:1 ORDER BY order desc',True).fetch(100) memcache.add(key=key_, value=albums, time=3600) else: getLogger(__name__).debug("getAlbumList from cache. ") return albums
def get_album_from_context(self, context): """Return an Album from a Context. Args: context (dict): The Context to convert from. Returns: Album: The Album. """ album_id = id_from_uri(context["uri"]) result = self.get_api_v1("albums/{}".format(album_id)) return Album(result or {})
def load_albums(album_filename): """Load albums from albums.txt into database.""" print("Albums") for i, row in enumerate(open(album_filename)): row = row.rstrip() album_id, album_title, cover_art_url, album_release_year_str, album_release_month_str, album_release_day_str = row.split( "|") if not album_release_year_str or album_release_year_str in ('None', 'None"', ''): album_release_year_str = None if not album_release_month_str or album_release_month_str in ('None', 'None"', ''): album_release_month_str = None else: if len(album_release_month_str) == 1: album_release_month_str = "0" + album_release_month_str if not album_release_day_str or album_release_day_str in ('None', 'None"', ''): album_release_day_str = None else: if len(album_release_day_str) == 1: album_release_day_str = "0" + album_release_day_str if album_release_year_str and album_release_month_str and album_release_day_str: album_release_date_str = " ".join([ album_release_year_str, album_release_month_str, album_release_day_str ]) album_release_date = datetime.datetime.strptime( album_release_date_str, "%Y %m %d") album = Album(album_id=album_id, album_title=album_title, cover_art_url=cover_art_url, album_release_date=album_release_date) db.session.add(album) if i % 500 == 0: print(i) db.session.commit() db.session.commit()
def populateAlbum(session, artist_db_obj, album): """Populates the album database table with data from the Spotify API. Keyword arguments: session -- the database session. artist_db_obj -- the artist database object. album -- the album object containing Spotify data. """ # Populate album information based on the album search. album_db_obj = Album(name=album['name'], uri=album['uri'], release_date=determineReleaseDate( album['release_date']), artist_id=artist_db_obj.id, artist=artist_db_obj) session.add(album_db_obj) session.commit() populateSongs(session, album_db_obj, album)
def UpdateAlbum(self,request): album = Album.get_by_id(int(request.get("id"))) editColumn = request.get("editColumn") if album and editColumn: newData = request.get("newData") if editColumn == "album_username": album.album_username = newData if editColumn == "album_type": album.album_type = newData if editColumn == "access": album.access = newData if editColumn == "date": album.date = simplejson.loads(newData) if editColumn == "order": album.order = simplejson.loads(newData) if editColumn == "valid": album.valid = simplejson.loads(newData) album.put() util.flushAlbumList() return True
def get_albums_from_artist(self, artist, type=("album", "single", "appears_on", "compilation"), market=None): """Get Albums from a certain Artist. Args: artist (Artist): The Artist. type (iter): Which types of albums to return. market (str): The market. Default is None which means use the account. Returns: tuple: The Albums. """ q = {"include_groups": ",".join(type), "market": market or self.get_market(), "limit": 50} url = "artists/{}/albums".format(artist['id']) page = self.get_api_v1(url, q) albums = self.extract_page(page) return tuple(Album(album) for album in albums)
def add_song(path): getcontext().prec = 3 tag = tiny.get(path) artist = session.query(Artist).filter_by(name=tag.artist).first() if artist == None: artist = Artist(name=tag.artist) session.add(artist) album = session.query(Album).filter_by(name=tag.album).first() if album == None: album = Album(name=tag.album, num_songs=0, artist=artist) session.add(album) album.num_songs += 1 duration = Decimal(tag.duration) / Decimal(60) song = Song(name=tag.title, release_date=tag.year, genre=tag.genre, length=duration, album=album, path=path) session.add(song) song.artists.append(artist) session.commit()
def DeleteAlbum(self,request): album = Album.get_by_id(int(request.get("id"))) album.delete() util.flushAlbumList() return True
def get(self): albums = Album.all().order("order") template_values = {"albums": albums} self.generate("admin/admin_albums.html", template_values)
def populateDataManually(): """Populates the database tables with dummy data. """ # Populate starter data in the database # Bind the engine to the metadata of the Base class (enables declaratives to be accessed through a DBSession # instance) Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database and represents a "staging zone" # for all the objects loaded into the database session object. Any change made against the objects in the # session won't be persisted into the database until you call session.commit(). If you're not happy about the # changes, you can revert all of them back to the last commit by calling session.rollback() session = DBSession() # Initial dummy data. user1 = User(email="*****@*****.**", password="******") user2 = User(email="*****@*****.**", password="******") user3 = User(email="*****@*****.**", password="******") session.add(user1) session.add(user2) session.add(user3) session.commit() artist1 = Artist(name="Purity Ring", uri="dummy", followers=1) session.add(artist1) session.commit() album1 = Album(name="Another Eternity", uri="dummyuri", release_date=date.today, artist_id=artist1.id, artist=artist1) session.add(album1) session.commit() song_objects = [ Song(uri="dummyuri", track_number=1, name="Heartsigh", popularity=100, duration=189000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=2, name="Bodyache", popularity=100, duration=179000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=3, name="Push Pull", popularity=100, duration=169000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=4, name="Repetition", popularity=100, duration=159000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=5, name="Stranger than Earth", popularity=100, duration=149000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=6, name="Begin Again", popularity=100, duration=139000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=7, name="Dust Hymn", popularity=100, duration=129000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=8, name="Flood on the Floor", popularity=100, duration=119000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=9, name="Sea Castle", popularity=100, duration=144000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1), Song(uri="dummyuri", track_number=10, name="Stillness in Woe", popularity=100, duration=155000, danceability=1.0, explicit=False, tempo=1.0, energy=1.0, instrumentalness=1.0, time_signature=100, valence=1.0, album_id=album1.id, album=album1) ] session.add_all(song_objects) session.commit()
def add_tracks_from_playlist(): with session_scope() as session: pl_obj = get_playlist("Spencer") itterations = 0 # print(dumps(pl_obj['tracks']['items'], indent=4, sort_keys=True)) print(len(pl_obj['tracks']['items'])) for item in pl_obj['tracks']['items']: itterations += 1 print(itterations) # print(dumps(item['track'], indent=4, sort_keys=True)) # Add all artists if they don't exist artists = item['track']['album']['artists'] print(len(artists)) for artist in artists: print(artist) artist_spotify_id = artist['id'] print(artist_spotify_id) cur_artist = Artist.query.filter_by( spotify_id=artist_spotify_id).first() print("after query") if not cur_artist: obj = dict() obj['spotify_id'] = artist_spotify_id artist_res = get_artist(artist_spotify_id) obj['name'] = artist_res['name'] obj['image_url'] = artist_res['images'][0][ 'image_url'] if len(artist_res['images']) > 0 else None obj['followers'] = artist_res['followers']['total'] obj['popularity'] = artist_res['popularity'] cur_artist = Artist(obj) # Create an Album Object album_spotify_id = item['track']['album']['id'] cur_album = Album.query.filter_by( spotify_id=album_spotify_id).first() if not cur_album: obj = dict() obj['spotify_id'] = album_spotify_id album_res = get_album(album_spotify_id) obj['name'] = album_res['name'] obj['image_url'] = artist_res['images'][0][ 'images_url'] if len(album_res['images']) > 0 else None obj['release_date'] = album_res['release_date'] obj['number_of_tracks'] = album_res['tracks']['total'] obj['popularity'] = album_res['popularity'] cur_album = Album(obj) # Create a Track Object track_spotify_id = item['track']['id'] cur_track = Track.query.filter_by( spotify_id=track_spotify_id).first() if not cur_track: obj = dict() obj['spotify_id'] = track_spotify_id track_res = get_track(track_spotify_id) obj['name'] = track_res['name'] obj['spotify_id'] = track_res['spotify_id'] obj['explicit'] = track_res['explicit'] obj['runtime'] = track_res['duration_ms'] obj['popularity'] = track_res['popularity'] obj['preview_url'] = track_res['preview_url'] cur_album.artist.append(cur_artist) cur_artist.albums.append(cur_album) cur_track.artist.append(cur_artist) cur_track.albums.append(cur_album) cur_album.tracks.append(cur_track) cur_artist.tracks.append(cur_track) # Finally add the artist object containing the albums and track print(cur_artist) session.add(cur_artist) session.commit()
def get(self): albums = Album.all().order('order') template_values = { 'albums':albums, } self.generate('admin/admin_albums.html',template_values)