예제 #1
0
def rescanLibrary(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = strip_tags(response['LIBRARY_ID'])
            if Library.objects.filter(id=library).count() == 1:
                library = Library.objects.get(id=library)

                # Check if the library is not used somewhere else
                if library.playlist.isScanned:
                    # Delete all the old tracks
                    library.playlist.delete()

                    # Recreating playlist
                    playlist = Playlist()
                    playlist.name = library.name
                    playlist.user = request.user
                    playlist.isLibrary = True
                    playlist.save()
                    library.playlist = playlist
                    library.save()

                    # Scan library
                    data = scanLibrary(library, playlist, library.convertID3)
                else:
                    data = errorCheckMessage(False, "rescanError")
            else:
                data = errorCheckMessage(False, "dbError")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
예제 #2
0
def load_playlist(fname):
    try:
        with open(fname + ".playlist") as f:
            playlist = Playlist()
            data = json.load(f)
            playlist.name = data['name']
            playlist.thumbnail = data['thumbnail']
            db.session.add(playlist)
            db.session.commit()
            for s in data['songs']:
                song_obj = Song.query.filter_by(
                    itunes_resource_id=s['itunes_resource_id']).first()
                if song_obj is None:
                    song_obj = Song()
                    song_obj.name = s['name']
                    song_obj.artist = s['artist']
                    song_obj.album = s['album']
                    song_obj.thumbnail_url = s['thumbnail_url']
                    song_obj.preview_url = s['preview_url']
                    song_obj.external_url = s['external_url']
                    song_obj.itunes_resource_id = s['itunes_resource_id']
                    db.session.add(song_obj)
                    db.session.commit()
                playlist.songs.append(song_obj)
                db.session.commit()
            return True
    except IOError as e:
        return False

    return False
예제 #3
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'LIBRARY_ID': library.id,
                        'LIBRARY_NAME': library.playlist.name,
                    }
                    data = {**data, **errorCheckMessage(True, None)}
                else:
                    data = errorCheckMessage(False, "dirNotFound")
            else:
                data = errorCheckMessage(False, "badFormat")
        else:
            data = errorCheckMessage(False, "permissionError")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
예제 #4
0
    def get(self):
        global threads
        global threadid
        parser = reqparse.RequestParser()
        parser.add_argument('spotifyid',
                            type=str,
                            required=True,
                            help='spotify playlist URI')
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help='name for playlist')
        parser.add_argument('auth',
                            type=str,
                            required=True,
                            help='auth token defined in ..env')
        args = parser.parse_args()
        args['spotifyid'] = args['spotifyid'].split(':')[-1]
        if args['auth'] != Config.PLAYLIST_API_SECRET:
            return {"message": "unauthorized"}, 401

        # create and populate the metadata of the new playlist
        playlist_object = Playlist()
        playlist_object.name = args['name']
        playlist_object.thumbnail = "/resource/placeholder.png"
        playlist_object.spotifyid = args['spotifyid']
        db.session.add(playlist_object)
        db.session.commit()
        # create the thread to populate it
        threadid += 1
        thread = GetSpotifyPlaylistWithITunesThread(playlist_object.id,
                                                    args['spotifyid'],
                                                    threadid,
                                                    name="Playlist-thread-%i" %
                                                    threadid)
        # add new thread to queue and start it if no threads running
        threads.append(thread)
        if not thread_running:
            threads.pop().start()
            return {
                'threadid': threadid,
                'queue_length': len(threads),
                'started': True
            }
        return {
            'threadid': threadid,
            'queue_length': len(threads),
            'started': False
        }
예제 #5
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'NAME' in response:
            playlist = Playlist()
            playlist.name = strip_tags(response['NAME'])
            playlist.user = request.user
            playlist.save()
            data = {
                'PLAYLIST_ID': playlist.id,
                'NAME': playlist.name,
            }
            data = {**data, **errorCheckMessage(True, None)}
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
예제 #6
0
def newLibrary(request):
    if request.method == 'POST':
        user = request.user
        if checkPermission(["LIBR"], user):
            response = json.loads(request.body)
            if 'URL' in response and 'NAME' in response:
                dirPath = response['URL']
                if os.path.isdir(dirPath):
                    # Removing / at the end of the dir path if present
                    if dirPath.endswith("/"):
                        dirPath = dirPath[:-1]
                    library = Library()
                    library.path = dirPath
                    playlist = Playlist()
                    playlist.user = user
                    playlist.isLibrary = True
                    playlist.name = strip_tags(response['NAME'])
                    playlist.save()
                    library.playlist = playlist
                    library.save()
                    data = {
                        'INFO': {
                            'ID': library.id,
                            'NAME': library.playlist.name,
                            'DESCRIPTION': library.playlist.description,
                            'IS_PUBLIC': library.playlist.isPublic,
                            'IS_LIBRARY': library.playlist.isLibrary,
                            'TOTAL_TRACK': "TO BE IMPLEMENTED",
                            'TOTAL_DURATION': "TO BE IMPLEMENTED",
                            'AVERAGE_BITRATE': "TO BE IMPLEMENTED",
                            'OWNER': library.playlist.user.username
                        }
                    }
                    data = {**data, **errorCheckMessage(True, None, newLibrary)}
                else:
                    data = errorCheckMessage(False, ErrorEnum.DIR_NOT_FOUND, newLibrary)
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newLibrary, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newLibrary, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newLibrary)
    return JsonResponse(data)
예제 #7
0
def initialScan(request):
    print("Asked for initial scan")
    if request.method == 'POST':
        response = json.loads(request.body)
        if 'LIBRARY_ID' in response:
            library = Library.objects.get(id=response['LIBRARY_ID'])
            if os.path.isdir(library.path):
                playlist = Playlist()
                playlist.name = library.name
                playlist.user = request.user
                playlist.isLibrary = True
                playlist.save()
                data = scanLibrary(library, playlist, library.convertID3)
            else:
                data = errorCheckMessage(False, "dirNotFound")
        else:
            data = errorCheckMessage(False, "badFormat")
    else:
        data = errorCheckMessage(False, "badRequest")
    return JsonResponse(data)
예제 #8
0
def newPlaylist(request):
    if request.method == 'POST':
        response = json.loads(request.body)
        user = request.user
        if checkPermission(["PLST"], user):
            if 'PLAYLIST_NAME' in response:
                playlist = Playlist()
                playlist.name = strip_tags(response['PLAYLIST_NAME'])
                playlist.user = request.user
                playlist.save()
                data = {
                    'PLAYLIST_ID': playlist.id,
                    'PLAYLIST_NAME': playlist.name,
                }
                data = {**data, **errorCheckMessage(True, None, newPlaylist)}
            else:
                data = errorCheckMessage(False, ErrorEnum.BAD_FORMAT, newPlaylist, user)
        else:
            data = errorCheckMessage(False, ErrorEnum.PERMISSION_ERROR, newPlaylist, user)
    else:
        data = errorCheckMessage(False, ErrorEnum.BAD_REQUEST, newPlaylist)
    return JsonResponse(data)