Beispiel #1
0
def build():
    app.logger.debug('Creating token')
    token = spotify.get_token(secrets.SPOTIFY_CLIENT_ID, secrets.SPOTIFY_CLIENT_SECRET)

    app.logger.debug('Loading playlist')
    playlist = spotify.get_playlist(token, flask.request.args.get('spotify_uri'))

    app.logger.debug('Getting Youtube Videos')
    suggestions = youtube.find_videos(get_client(), playlist.tracks)
    return flask.render_template('build.html', name='build', playlist=playlist, suggestions=suggestions)
Beispiel #2
0
def get_input(bot, update):
    user = update.message.from_user
    if update.message.photo:
        update.message.reply_text("Thinking hard...")
        logger.info("Photo received from %s" % user.first_name)
        photo_id = update.message.photo[-1].file_id
        json_url = ('https://api.telegram.org/bot' +
                    KEYS['488464613:AAHMHWkjBlmWnP_1dn3hGYJBamHlbDKBhBE'] +
                    '/getFile?file_id=' + photo_id)
        logger.info(update.message.photo[-1].file_size)

        logger.info(requests.get(json_url).json())

        file_path = (requests.get(json_url).json())['result']['file_path']
        photo_url = 'https://api.telegram.org/file/bot' + KEYS[
            '488464613:AAHMHWkjBlmWnP_1dn3hGYJBamHlbDKBhBE'] + "/" + file_path
        logger.info(photo_url)

        headers = dict()
        headers['Ocp-Apim-Subscription-Key'] = KEYS['EmotionAPI']
        headers['Content-Type'] = 'application/json'

        json = {"url": photo_url}
        data = None
        params = None

        result = emotion_api.processRequest(json, data, headers, params)

        if result is not None and result != []:
            # Load the original image, fetched from the URL
            logger.info('Result found: ' + str(result))
            scores = result[0]['scores']
            new_scores = {
                key: val
                for key, val in scores.items()
                if key in ['happiness', 'anger', 'sadness', 'neutral', 'fear']
            }
            logger.info('Filtered emotions: ' + str(new_scores))
            new_scores = {k: float(v) for k, v in new_scores.items()}
            sorted_scores = [
                key for (key, value) in sorted(
                    new_scores.items(), key=lambda em: em[1], reverse=True)
            ]
            highest_score = sorted_scores[0]

            logger.info(sorted_scores)
            update.message.reply_text('Enjoy your playlist!\n' +
                                      spotify.get_playlist(highest_score))
        else:
            update.message.reply_text(
                "Face not found.\nPlease send a photo with a face")

    elif not is_image(update.message.text):
        update.message.reply_text("Please send a photo with a face")
Beispiel #3
0
def search():
    tweets = ""
    wiki = ""
    playlist = ""
    recs = ""
    result = ""
    if 'q' in request.args:
        wiki = wiki1.search(request.args['q'])
        playlist = "<a href=" + spotify.get_playlist(
            request.args['q']) + ">here</a>"
        tweets = twitter.search(request.args['q'])
        recs = spotify.get_recommendations(request.args['q'])
        result = "<h2/>Summary:<br/></h2/>" + wiki + "<br/><h2/>For Playlist Click " + playlist + "</h2/><h2/>TWEETS:<br/></h2/>" + tweets + "<br/><h2/>RECOMENDED FOR YOU</h2/>" + "<h2/>" + recs + "</h2/>"
        return result
    return "WRONG"
Beispiel #4
0
def _load_database_from_playlist():
    app = Flask(__name__)
    app.config.from_pyfile('../config.py')
    init_app(app)
    with app.app_context():
        pl_obj = get_playlist("Spencer")
        itterations = 0
        # print(dumps(pl_obj['tracks']['items'], indent=4, sort_keys=True))
        print(len(pl_obj['tracks']['items']))
        for item in pl_obj['tracks']['items']:
            itterations += 1
            print(itterations)
            # print(dumps(item['track'], indent=4, sort_keys=True))
            # Add all artists if they don't exist
            artists = item['track']['album']['artists']
            print("Number of artist: " + str(len(artists)))
            for artist in artists:
                artist_id = artist['id']
                cur_artist = Artist.query.filter_by(id=artist_id).first()
                if not cur_artist:
                    obj = dict()
                    obj['id'] = artist_id
                    print("artist_ID: " + artist_id)
                    artist_res = get_artist(artist_id)
                    print(dumps(artist_res, indent=4, sort_keys=True))
                    obj['name'] = artist_res['name']
                    if len(artist_res['images']) > 0:
                        obj['image_url'] = artist_res['images'][0]['url']
                    else:
                        obj['image_url'] = None
                    obj['followers'] = artist_res['followers']['total']
                    obj['popularity'] = artist_res['popularity']
                    cur_artist = Artist(**obj)

                # Create an Album Object
                album_id = item['track']['album']['id']
                cur_album = Album.query.filter_by(id=album_id).first()
                if not cur_album:
                    obj = dict()
                    obj['id'] = album_id
                    album_res = get_album(album_id)
                    obj['name'] = album_res['name']
                    if len(album_res['images']) > 0:
                        obj['image_url'] = album_res['images'][0]['url']
                    else:
                        obj['image_url'] = None
                    print(album_res['release_date'])
                    obj['release_date'] = album_res['release_date']
                    obj['number_of_tracks'] = album_res['tracks']['total']
                    obj['popularity'] = album_res['popularity']
                    cur_album = Album(**obj)

                # Create a Track Object
                track_id = item['track']['id']
                cur_track = Track.query.filter_by(id=track_id).first()
                if not cur_track:
                    obj = dict()
                    obj['id'] = track_id
                    track_res = get_track(track_id)
                    print
                    obj['name'] = track_res['name']
                    obj['id'] = track_res['id']
                    obj['explicit'] = track_res['explicit']
                    obj['runtime'] = track_res['duration_ms']
                    obj['popularity'] = track_res['popularity']
                    obj['preview_url'] = track_res['preview_url']
                    cur_track = Track(**obj)

                cur_album.artist.append(cur_artist)
                cur_artist.albums.append(cur_album)
                cur_track.artist.append(cur_artist)
                cur_track.albums.append(cur_album)
                cur_album.tracks.append(cur_track)
                cur_artist.tracks.append(cur_track)

                # Finally add the artist object containing the albums and track
                print(cur_artist)
                db.session.add(cur_artist)
                db.session.commit()
Beispiel #5
0
def add_tracks_from_playlist():
    with session_scope() as session:
        pl_obj = get_playlist("Spencer")
        itterations = 0
        # print(dumps(pl_obj['tracks']['items'], indent=4, sort_keys=True))
        print(len(pl_obj['tracks']['items']))
        for item in pl_obj['tracks']['items']:
            itterations += 1
            print(itterations)
            # print(dumps(item['track'], indent=4, sort_keys=True))
            # Add all artists if they don't exist
            artists = item['track']['album']['artists']
            print(len(artists))
            for artist in artists:
                print(artist)
                artist_spotify_id = artist['id']
                print(artist_spotify_id)
                cur_artist = Artist.query.filter_by(
                    spotify_id=artist_spotify_id).first()
                print("after query")
                if not cur_artist:
                    obj = dict()
                    obj['spotify_id'] = artist_spotify_id
                    artist_res = get_artist(artist_spotify_id)
                    obj['name'] = artist_res['name']
                    obj['image_url'] = artist_res['images'][0][
                        'image_url'] if len(artist_res['images']) > 0 else None
                    obj['followers'] = artist_res['followers']['total']
                    obj['popularity'] = artist_res['popularity']
                    cur_artist = Artist(obj)

                # Create an Album Object
                album_spotify_id = item['track']['album']['id']
                cur_album = Album.query.filter_by(
                    spotify_id=album_spotify_id).first()
                if not cur_album:
                    obj = dict()
                    obj['spotify_id'] = album_spotify_id
                    album_res = get_album(album_spotify_id)
                    obj['name'] = album_res['name']
                    obj['image_url'] = artist_res['images'][0][
                        'images_url'] if len(album_res['images']) > 0 else None
                    obj['release_date'] = album_res['release_date']
                    obj['number_of_tracks'] = album_res['tracks']['total']
                    obj['popularity'] = album_res['popularity']
                    cur_album = Album(obj)

                # Create a Track Object
                track_spotify_id = item['track']['id']
                cur_track = Track.query.filter_by(
                    spotify_id=track_spotify_id).first()
                if not cur_track:
                    obj = dict()
                    obj['spotify_id'] = track_spotify_id
                    track_res = get_track(track_spotify_id)
                    obj['name'] = track_res['name']
                    obj['spotify_id'] = track_res['spotify_id']
                    obj['explicit'] = track_res['explicit']
                    obj['runtime'] = track_res['duration_ms']
                    obj['popularity'] = track_res['popularity']
                    obj['preview_url'] = track_res['preview_url']

                cur_album.artist.append(cur_artist)
                cur_artist.albums.append(cur_album)
                cur_track.artist.append(cur_artist)
                cur_track.albums.append(cur_album)
                cur_album.tracks.append(cur_track)
                cur_artist.tracks.append(cur_track)

                # Finally add the artist object containing the albums and track
                print(cur_artist)
                session.add(cur_artist)
                session.commit()