def _cache_albums_async(app, refresh_thumbnails=False, refresh_dates=False): with app.app_context(): album_manager = GPhotosAlbum(service) current_ids = list() for a in album_manager.list(): album = Album.query.filter_by(gphotos_id=a.get("id")).first() if not album: album = Album() album.gphotos_id = a.get("id") if not album.end_date or refresh_dates: start_date, end_date = _get_album_date_range(album.gphotos_id) album.start_date = start_date album.end_date = end_date current_ids.append(a.get("id")) album.title = a.get("title") album.url_title = normalize_for_url(a.get("title")) album.items_count = a.get("mediaItemsCount") db.session.add(album) thumbnail = os.path.join(app.config["ALBUM_THUMB_PATH"], a.get("id") + ".jpg") if not os.path.exists(thumbnail) or refresh_thumbnails: urllib.request.urlretrieve( a.get("coverPhotoBaseUrl") + "=w300-h200-c", os.path.join(app.config["ALBUM_THUMB_PATH"], a.get("id") + ".jpg"), ) db.session.commit() # delete from db albums no longer in google photos stmt = delete(Album).where( Album.gphotos_id.notin_(current_ids)).execution_options( synchronize_session="fetch") db.session.execute(stmt) db.session.commit()
def add_tl_to_fav(id): """ Add an album from tolisten to favorites """ toadd = ToListen.query.get(id) form = AlbumForm(request.form) # the suggested rank should be the next available rank # e.g. if there are 200 albums currently in the favorites list, # the suggested rank should be 201 rank = db.session.query(func.max(Album.rank).label("rank")).scalar() + 1 if request.method == 'POST' and form.validate_on_submit(): album = Album() album.rank = form.rank.data album.title = form.title.data album.artist = form.artist.data album.year = form.year.data album.last_played = datetime.strptime(form.last_played.data, '%Y-%m-%d') album.user_id = current_user.id # make sure the added album's ranking is no lower than what makes sense # e.g. if there are currently 200 albums in favorites, # the lowest that makes sense is 201 if int(album.rank) > rank: album.rank = rank elif int(album.rank) < rank: # get every album lower *numerically higher) in current ranking # than the ranking we are trying to add to # and increment ranking by 1 # e.g. there are 200 albums and we assign a ranking of 195 to new album # album currently ranked 195 will become 196, # album currently ranked 196 will becomes 197... # ...album current ranked 200 will become 201 to_move = (Album.query.filter_by(user_id=current_user.id).filter( Album.rank >= int(form.rank.data)).filter( Album.rank < rank).order_by(Album.rank.desc()).all()) for a in to_move: a.rank += 1 db.session.commit() db.session.add(album) title, artist = toadd.title, toadd.artist ToListen.query.filter(ToListen.id == id).delete() db.session.commit() flash(f'Successfully added album {album.title} by {album.artist}') flash(f'Deleted {title} by {artist} from albums to listen') return redirect(url_for('favorites')) # addt'l variables curr_dt = datetime.now().strftime('%Y-%m-%d') curr_dt = datetime.now().strftime('%Y-%m-%d') return render_template('addalbum.html', form=form, last_played=curr_dt, rank=rank, toadd=toadd)
def add_favorite(): """ Add an album to favorites """ form = AlbumForm(request.form) rankrow = (Album.query.filter_by(user_id=current_user.id).order_by( Album.rank.desc()).limit(1).all()) rank = rankrow[0].rank + 1 if request.method == 'POST' and form.validate_on_submit(): album = Album() album.rank = form.rank.data album.title = form.title.data album.artist = form.artist.data album.year = form.year.data album.last_played = datetime.strptime(form.last_played.data, '%Y-%m-%d') album.user_id = current_user.id # make sure the added album's ranking is no lower than what makes sense # e.g. if there are currently 200 albums in favorites, # the lowest that makes sense is 201 if int(album.rank) > rank: album.rank = rank elif int(album.rank) < rank: # get every album lower *numerically higher) in current ranking # than the ranking we are trying to add to # and increment ranking by 1 # e.g. there are 200 albums and we assign a ranking of 195 to new album # album currently ranked 195 will become 196, # album currently ranked 196 will becomes 197... # ...album current ranked 200 will become 201 to_move = (Album.query.filter_by(user_id=current_user.id).filter( Album.rank >= int(form.rank.data)).filter( Album.rank < rank).order_by(Album.rank.desc()).all()) for a in to_move: a.rank += 1 db.session.commit() db.session.add(album) db.session.commit() flash(f'Successfully added album {album.title} by {album.artist}') return redirect(url_for('favorites')) # addt'l variables curr_dt = datetime.now().strftime('%Y-%m-%d') return render_template('addalbum.html', form=form, last_played=curr_dt, rank=rank, toadd=None)
def updateDBInfo(response, track): tags = Information() # Changing tags in the database if 'TITLE' in response and response['TITLE'] != '': tags.trackTitle = strip_tags(response['TITLE']).lstrip().rstrip() track.title = tags.trackTitle if 'ARTISTS' in response and response['ARTISTS'] != '': tags.trackArtist = strip_tags( response['ARTISTS']).lstrip().rstrip().split(',') artists = [] for artist in tags.trackArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() artists.append(Artist.objects.get(name=artist)) track.artist.clear() for artist in artists: track.artist.add(artist) if 'PERFORMER' in response and response['PERFORMER'] != '': tags.trackPerformer = strip_tags( response['PERFORMER']).lstrip().rstrip() track.performer = tags.trackPerformer if 'COMPOSER' in response and response['COMPOSER'] != '': tags.trackComposer = strip_tags(response['COMPOSER']).lstrip().rstrip() track.composer = tags.trackComposer if 'YEAR' in response and response['YEAR'] != '': tags.trackYear = checkIntValueError(response['YEAR']) track.year = tags.trackYear if 'TRACK_NUMBER' in response and response['TRACK_NUMBER'] != '': tags.trackNumber = checkIntValueError(response['TRACK_NUMBER']) track.number = tags.trackNumber if 'BPM' in response and response['BPM'] != '': track.bpm = checkIntValueError(response['BPM']) if 'LYRICS' in response and response['LYRICS'] != '': tags.lyrics = strip_tags(response['LYRICS']).lstrip().rstrip() track.lyrics = tags.lyrics if 'COMMENT' in response and response['COMMENT'] != '': tags.comment = strip_tags(response['COMMENT']).lstrip().rstrip() track.comment = tags.comment if 'GENRE' in response and response['GENRE'] != '': tags.trackGenre = strip_tags(response['GENRE']).lstrip().rstrip() if Genre.objects.filter(name=tags.trackGenre).count() == 0: genre = Genre() genre.name = tags.trackGenre genre.save() genre = Genre.objects.get(name=tags.trackGenre) track.genre = genre if 'COVER' in response: md5Name = hashlib.md5() if str(response['COVER'].split(",")[0]) == "image/png": extension = "png" else: extension = "jpg" md5Name.update(base64.b64decode(str(response['COVER'].split(",")[1]))) # Check if the folder exists filePath = "/ManaZeak/static/img/covers/" if not os.path.isdir(filePath): os.mkdir(filePath) # Create the folder filePath += +md5Name.hexdigest() + extension # if the filePath is the same, then the md5 hash of the image is # the same, therefore the images are the same, therefore do nothing if not os.path.isfile(filePath): with open(filePath, 'wb+') as destination: # Split the header with MIME type tags.cover = base64.b64decode( str(response['COVER'].split(",")[1])) destination.write(tags.cover) track.coverLocation = md5Name.hexdigest() + extension if 'ALBUM_TITLE' in response and 'ALBUM_ARTISTS' in response and response['ALBUM_TITLE'] != '' \ and response['ALBUM_ARTISTS'] != '': tags.albumTitle = strip_tags(response['ALBUM_TITLE']).lstrip().rstrip() tags.albumArtist = strip_tags( response['ALBUM_ARTISTS']).lstrip().rstrip().split(',') if Album.objects.filter(title=tags.albumTitle).count() == 0: album = Album() album.title = tags.albumTitle album.save() album = Album.objects.get(title=tags.albumTitle) album.artist.clear() for artist in tags.albumArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() album.artist.add(Artist.objects.get(name=artist)) if 'ALBUM_TOTAL_DISC' in response and response[ 'ALBUM_TOTAL_DISC'] != '': tags.albumTotalDisc = checkIntValueError( response['ALBUM_TOTAL_DISC']) album.totalDisc = tags.albumTotalDisc if 'DISC_NUMBER' in response and response['DISC_NUMBER'] != '': tags.albumDiscNumber = checkIntValueError(response['DISC_NUMBER']) track.discNumber = tags.albumDiscNumber if 'ALBUM_TOTAL_TRACK' in response and response[ 'ALBUM_TOTAL_TRACK'] != '': tags.albumTotalTrack = checkIntValueError( response['ALBUM_TOTAL_TRACK']) album.totalTrack = tags.albumTotalTrack album.save() track.album = album track.save() return tags