def add_playlist(m3u: M3UList): # Create Playlist obj from M3UList obj, and Insert Playlist into DB if it does not already exist pl = Playlist.make_playlist_obj(m3u) if not Playlist.db_pl_exists(pl): try: Playlist.add_playlist(pl) except Exception as inst: print(str(inst)) else: print('Playlist {} already exists in DB'.format(pl.playlist_name)) # Insert tracks from m3u into DB if they do not already exist addable_tracks = [] for mt in m3u.tracks: dt = Track.make_Track_obj(mt) if not Track.db_trk_exists(dt): try: Track.add_track(t=dt, com=False) addable_tracks.append(dt) except Exception: raise Exception('add {} track fails'.format( dt.artist_title)) else: print('Track {} already exists in DB'.format(dt.artist_title)) addable_tracks.append(dt) db.session.commit() # add tracks to db playlist npl = Playlist.query.filter_by(playlist_name=pl.playlist_name).first() for dt in addable_tracks: npl.add_track(dt, com=False) db.session.commit()
def add_track_to_pls(trk: Track, pl: Playlist): if Track.db_trk_exists(trk): try: pl.tracks.append(trk) db.session.commit() except Exception: print(' Failed to add {} track to {} playlist'.format( trk.artist_title, pl.playlist_name))
def add_tracks_to_pls(trks: [], pl: Playlist): for t in trks: if Track.db_trk_exists(t): try: pl.tracks.append(t) except Exception: print(' Failed to add {} track to {} playlist'.format( t.artist_title, pl.playlist_name)) try: db.session.commit() except Exception: print('submit fails for added tracks to {} playlist'.format( pl.playlist_name))
def add_tracks_from_dir(param): count_added = 0 count_tot = 0 exts = ['.mp3', '.m4a', '.flac', '.m4v', '.wmv'] for root, dirs, files in os.walk(Config.MEDIA_SRC_DIRS[param], topdown=True): for name in files: nname = PurePath(os.path.join(root, name)) suf = nname.suffix if suf in exts: print('loading file as Track into db: {}'.format( os.path.join(root, name))) mt = MTrack.mtrack_factory(os.path.join(root, name)) if not mt: print('MTrack {} is None'.format(str(nname))) continue t = Track.make_Track_obj(mt) if not t: print('Track {} is None'.format(str(nname))) continue try: if not Track.db_trk_exists(t): Track.add_track(t, com=True) count_added += 1 count_tot += 1 else: count_tot += 1 except Exception as inst: print(inst) print( 'rolling back and commit after failing db add {}'. format(inst)) db.session.rollback() db.session.commit() raise inst return count_tot, count_added