Beispiel #1
0
 def test_download_song_if_song_is_being_downloaded(self, log_info, retry):
     song = songs.get_song('song')
     song['files']['mp3'] = 'downloading'
     songs.cache.set('song', song)
     songs.download_song({'name': 'song'}, format='mp3')
     log_info.assert_called_once_with(
         'Attempt to download a song in the process of downloading')
Beispiel #2
0
 def test_download_song_if_song_is_being_downloaded(self, log_info, retry):
     song = songs.get_song('song')
     song['files']['mp3'] = 'downloading'
     songs.cache.set('song', song)
     songs.download_song({'name': 'song'}, format='mp3')
     log_info.assert_called_once_with(
         'Attempt to download a song in the process of downloading')
Beispiel #3
0
def download_song(song_name, format):
    influxdb.count('mobile_api.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) + 'Format: {}\n'.format(format))
        return jsonify(reason='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider))
            return jsonify(reason='Unsupported provider'), 400

        song = {'name': song_name}
        songs.download_song.delay(song, format=format, provider=provider)
        return jsonify(refresh_after=30,
                       message='Your song has started downloading.')

    influxdb.count('mobile_api.downloaded_songs')
    song = songs.get_song(song_name)

    return send_file(song['files'][format],
                     as_attachment=True,
                     attachment_filename='{}.{}'.format(song['name'], format),
                     mimetype=MIMETYPES[format])
Beispiel #4
0
def get_playlist(playlist_id):
    playlist_data = g.youtube.playlists().list(
        part='snippet',
        id=playlist_id
    ).execute()['items'][0]

    playlist = {
        'id': playlist_id,
        'name': playlist_data['snippet']['title'],
        'description': playlist_data['snippet']['description'],
        'tracks': [],
        'image': playlist_data['snippet']['thumbnails']['medium']['url']
    }

    playlist_songs = g.youtube.playlistItems().list(
        part='snippet,contentDetails',
        playlistId=playlist_id,
        maxResults=50
    ).execute()['items']

    for track in playlist_songs:
        song = songs.get_song(track['snippet']['title'])
        song['youtube'] = track['contentDetails']['videoId']
        playlist['tracks'].append(song)

    return playlist
Beispiel #5
0
def get_playlist(playlist_id):
    """Get a playlist by its id.

    Parameters
    ----------
    playlist_id : str
        The id of the playlist.

    Returns
    -------
    dict
        id - The deezer playlist id.
        name - The name of the playlist.
        description - The description of the playlist.
        image - A url for an image of the playlist.
        tracks - List of the tracks of the playlist.

    Raises
    ------
    RuntimeError
        If deezer API gives an error.
    """

    access_token = get_access_token_from_session()

    playlist_request = requests.get(
        'http://api.deezer.com/playlist/{}'.format(playlist_id) +
        '?access_token={}'.format(access_token)
    )

    if playlist_request.status_code != 200:
        raise RuntimeError('Deezer API error')

    playlist_data = json.loads(playlist_request.text)

    playlist = {
        'id': playlist_id,
        'name': playlist_data['title'],
        'description': playlist_data['description'],
        'tracks': [],
        'image': playlist_data['picture_medium']
    }

    for track in playlist_data['tracks']['data']:
        song = songs.get_song(get_song_name(track))
        playlist['tracks'].append(song)

    return playlist
Beispiel #6
0
def get_playlist(playlist_id):
    """Get a playlist by its id.

    Parameters
    ----------
    playlist_id : str
        The id of the playlist.

    Returns
    -------
    dict
        id - The deezer playlist id.
        name - The name of the playlist.
        description - The description of the playlist.
        image - A url for an image of the playlist.
        tracks - List of the tracks of the playlist.

    Raises
    ------
    RuntimeError
        If deezer API gives an error.
    """

    access_token = get_access_token_from_session()

    playlist_request = requests.get(
        'http://api.deezer.com/playlist/{}'.format(playlist_id) +
        '?access_token={}'.format(access_token))

    if playlist_request.status_code != 200:
        raise RuntimeError('Deezer API error')

    playlist_data = json.loads(playlist_request.text)

    playlist = {
        'id': playlist_id,
        'name': playlist_data['title'],
        'description': playlist_data['description'],
        'tracks': [],
        'image': playlist_data['picture_medium']
    }

    for track in playlist_data['tracks']['data']:
        song = songs.get_song(get_song_name(track))
        playlist['tracks'].append(song)

    return playlist
Beispiel #7
0
def download_song(song_name, format):
    influxdb.count('youtube.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) +
            'Format: {}\n'.format(format)
        )
        return render_template(
            'show_message.html', message='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider)
            )
            return render_template(
                'show_message.html', message='Unsupported provider'), 400

        song = {'name': song_name}

        youtube_id = request.args.get('provider_id', None)
        if youtube_id is not None and youtube_id != '':
            song['youtube'] = youtube_id

        songs.download_song.delay(
            song, format=format, provider=provider)
        return render_template(
            'show_message.html', refresh_after=30,
            message='Your song has started downloading.'
                    'This page will automatically refresh after 30 seconds.')

    influxdb.count('youtube.downloaded_songs')
    song = songs.get_song(song_name)
    return send_file(
        song['files'][format],
        as_attachment=True,
        attachment_filename='{}.{}'.format(song['name'], format),
        mimetype=MIMETYPES[format]
    )
Beispiel #8
0
def download_song(song_name, format):
    influxdb.count('youtube.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) + 'Format: {}\n'.format(format))
        return render_template('show_message.html',
                               message='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider))
            return render_template('show_message.html',
                                   message='Unsupported provider'), 400

        song = {'name': song_name}

        youtube_id = request.args.get('provider_id', None)
        if youtube_id is not None and youtube_id != '':
            song['youtube'] = youtube_id

        songs.download_song.delay(song, format=format, provider=provider)
        return render_template(
            'show_message.html',
            refresh_after=30,
            message='Your song has started downloading.'
            'This page will automatically refresh after 30 seconds.')

    influxdb.count('youtube.downloaded_songs')
    song = songs.get_song(song_name)
    return send_file(song['files'][format],
                     as_attachment=True,
                     attachment_filename='{}.{}'.format(song['name'], format),
                     mimetype=MIMETYPES[format])
Beispiel #9
0
def _get_album(album_id):
    result = g.spotipy.album(album_id)
    playlist = {
        'id': 'album:{}'.format(album_id),
        'name': result['name'],
        'image': result['images'][0]['url'],
        'tracks': []
    }

    for image in result['images']:
        if image['width'] == 300:
            playlist['image'] = image['url']
            break

    tracks = result['tracks']
    while tracks is not None:
        for track in tracks['items']:
            song = songs.get_song(get_song_name(track))
            playlist['tracks'].append(song)
        tracks = g.spotipy.next(tracks) if tracks['next'] else None

    return playlist
Beispiel #10
0
def _get_album(album_id):
    result = g.spotipy.album(album_id)
    playlist = {
        'id': 'album:{}'.format(album_id),
        'name': result['name'],
        'image': result['images'][0]['url'],
        'tracks': []
    }

    for image in result['images']:
        if image['width'] == 300:
            playlist['image'] = image['url']
            break

    tracks = result['tracks']
    while tracks is not None:
        for track in tracks['items']:
            song = songs.get_song(get_song_name(track))
            playlist['tracks'].append(song)
        tracks = g.spotipy.next(tracks) if tracks['next'] else None

    return playlist
Beispiel #11
0
def download_song(song_name, format):
    influxdb.count('mobile_api.download_song_requests')

    if format not in SUPPORTED_FORMATS:
        current_app.logger.warning(
            'User tried to download a song in unsupported format.\n' +
            'Song: {}\n'.format(song_name) +
            'Format: {}\n'.format(format)
        )
        return jsonify(reason='Unsupported format'), 400

    if not songs.has_song_format(song_name, format):
        provider = request.args.get('provider', SUPPORTED_PROVIDERS[0])
        if provider not in SUPPORTED_PROVIDERS:
            current_app.logger.warning(
                'User tried to download a song with unsupported provider.\n' +
                'Song: {}\n'.format(song_name) +
                'Format: {}\n'.format(format) +
                'Provider: {}\n'.format(provider)
            )
            return jsonify(reason='Unsupported provider'), 400

        song = {'name': song_name}
        songs.download_song.delay(song, format=format, provider=provider)
        return jsonify(
            refresh_after=30,
            message='Your song has started downloading.')

    influxdb.count('mobile_api.downloaded_songs')
    song = songs.get_song(song_name)

    return send_file(
        song['files'][format],
        as_attachment=True,
        attachment_filename='{}.{}'.format(song['name'], format),
        mimetype=MIMETYPES[format]
    )
Beispiel #12
0
def _get_playlist(username, playlist_id):
    result = g.spotipy.user_playlist(username, playlist_id,
                                     'name,description,tracks,images')
    playlist = {
        'id': '{}:{}'.format(username, playlist_id),
        'name': result['name'],
        'description': result['description'],
        'image': result['images'][0]['url'],
        'tracks': []
    }

    for image in result['images']:
        if image['width'] == 300:
            playlist['image'] = image['url']
            break

    tracks = result['tracks']
    while tracks is not None:
        for item in tracks['items']:
            song = songs.get_song(get_song_name(item['track']))
            playlist['tracks'].append(song)
        tracks = g.spotipy.next(tracks) if tracks['next'] else None

    return playlist
Beispiel #13
0
def _get_playlist(username, playlist_id):
    result = g.spotipy.user_playlist(username, playlist_id,
                                     'name,description,tracks,images')
    playlist = {
        'id': '{}:{}'.format(username, playlist_id),
        'name': result['name'],
        'description': result['description'],
        'image': result['images'][0]['url'],
        'tracks': []
    }

    for image in result['images']:
        if image['width'] == 300:
            playlist['image'] = image['url']
            break

    tracks = result['tracks']
    while tracks is not None:
        for item in tracks['items']:
            song = songs.get_song(get_song_name(item['track']))
            playlist['tracks'].append(song)
        tracks = g.spotipy.next(tracks) if tracks['next'] else None

    return playlist
Beispiel #14
0
 def test_has_song_format_if_song_is_being_downloaded(self):
     self.assertFalse(songs.has_song_format('some song', 'mp3'))
     song = songs.get_song('some song')
     song['files']['mp3'] = 'downloading'
     songs.cache.set('some song', song)
     self.assertFalse(songs.has_song_format('some song', 'mp3'))
Beispiel #15
0
 def test_download_song_with_soundcloud(self, download_song, *args):
     songs.download_song({'name': 'song'}, provider='soundcloud')
     download_song.assert_called_with('song id', 'mp3')
     song = songs.get_song('song')
     self.assertEqual(song['files']['mp3'], './songs/1234.mp3')
Beispiel #16
0
 def test_download_song_with_youtube(self, download_song, *args):
     songs.download_song({'name': 'song'})
     download_song.assert_called_with('dQw4w9WgXcQ', 'mp3')
     song = songs.get_song('song')
     self.assertEqual(song['files']['mp3'], './songs/dQw4w9WgXcQ.mp3')
Beispiel #17
0
 def test_download_song_with_unsupported_provider(self):
     with self.assertRaisesRegex(ValueError, 'Provider not found: zamunda'):
         songs.download_song({'name': 'song'}, provider='zamunda')
     song = songs.get_song('song')
     self.assertNotIn('mp3', song['files'])
Beispiel #18
0
 def test_download_song_with_unsupported_provider(self):
     with self.assertRaisesRegex(ValueError, 'Provider not found: zamunda'):
         songs.download_song({'name': 'song'}, provider='zamunda')
     song = songs.get_song('song')
     self.assertNotIn('mp3', song['files'])
Beispiel #19
0
 def test_has_song_format_if_song_is_being_downloaded(self):
     self.assertFalse(songs.has_song_format('some song', 'mp3'))
     song = songs.get_song('some song')
     song['files']['mp3'] = 'downloading'
     songs.cache.set('some song', song)
     self.assertFalse(songs.has_song_format('some song', 'mp3'))
Beispiel #20
0
 def test_add_song_file(self):
     song = songs.get_song('some song')
     self.assertNotIn('mp3', song['files'])
     songs.add_song_file('some song', 'some song.mp3', 'mp3')
     song = songs.get_song('some song')
     self.assertEqual(song['files']['mp3'], 'some song.mp3')
Beispiel #21
0
 def test_add_song_file(self):
     song = songs.get_song('some song')
     self.assertNotIn('mp3', song['files'])
     songs.add_song_file('some song', 'some song.mp3', 'mp3')
     song = songs.get_song('some song')
     self.assertEqual(song['files']['mp3'], 'some song.mp3')
Beispiel #22
0
 def test_download_song_with_youtube(self, download_song, *args):
     songs.download_song({'name': 'song'})
     download_song.assert_called_with('dQw4w9WgXcQ', 'mp3')
     song = songs.get_song('song')
     self.assertEqual(song['files']['mp3'], './songs/dQw4w9WgXcQ.mp3')
Beispiel #23
0
 def test_download_song_with_soundcloud(self, download_song, *args):
     songs.download_song({'name': 'song'}, provider='soundcloud')
     download_song.assert_called_with('song id', 'mp3')
     song = songs.get_song('song')
     self.assertEqual(song['files']['mp3'], './songs/1234.mp3')
Beispiel #24
0
 def test_get_song_if_song_is_in_cache(self):
     song = songs.get_song('some song')
     self.assertEqual(song['name'], 'some song')
     self.assertEqual(song['files'], {})
Beispiel #25
0
 def test_get_song_if_song_is_in_cache(self):
     song = songs.get_song('some song')
     self.assertEqual(song['name'], 'some song')
     self.assertEqual(song['files'], {})