Пример #1
0
    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()
Пример #2
0
    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
Пример #3
0
 def make_dt(mt: MTrack):
     return Track.make_Track_obj(mt)
Пример #4
0
 def mtracks_to_tracks(mtrks):
     from app.models import Track
     from app.m3u import MTrack
     if (len(mtrks) > 0) and (isinstance(mtrks[0], MTrack)):
         trks = [Track.make_Track_obj(mt) for mt in mtrks]
     return trks