Ejemplo n.º 1
0
def run():
    os.environ['SPOTIFY_AUTH_TOKEN'] = 'put spotify token here '

    # Get a list of our playlists from youtube
    youtube_client = YoutubeClient('./credentials/client_secret.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlists()

    # Ask whcih playlists we want to get the video from
    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")

    choice = int(input("Enter your choice: "))
    chosen_playlist = playlists[choice]
    print(f"You selected: {chosen_playlist.title}")

    # For each video in the playlist, get the song information form youtube
    songs = youtube_client.get_videos_from_playlist(chosen_playlist.id)
    print(f"Attempting to add {len(songs)} ")

    # Search for the song on spotify
    for song in songs:
        spotify_song_id = spotify_client.search_song(song.artist, song.track)
        if spotify_song_id:
            added_song = spotify_client.add_song_to_spotify(spotify_song_id)
            if added_song:
                print(f" Added {song.artist} ")

    # If we find the song add it to our Spotify liked songs

    pass
Ejemplo n.º 2
0
def run():

    youtube_client = YoutubeClient('./creds/client_secret.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlists()

    # ask which playlist we want to get music vids from
    for index, playlist in enumerate(playlists):
        print(f'{index} : {playlist.title}')

    choice = int(
        input('Please select the playlist  want to get music vids from'))
    chosen_playlist = playlists[choice]
    print("You chose : ", chosen_playlist.title)

    # for each playlist get the songs info from youtube
    songs = youtube_client.get_vids_from_playlists(chosen_playlist.id)
    print(f'trying to add {len(songs)}')

    # search for a song in spotify if found add it to the playlist
    for song in songs:
        spotify_song_id = spotify_client.search_for_a_song(
            song.artist, song.track)
        if spotify_song_id:
            added_song = spotify_client.add_song_to_playlist(spotify_song_id)
            if added_song:
                print(f'{song.track} added successfully !! ')

            else:
                print("sorry could not add song : ( ")

        else:
            print("song not found on Spotify :( ")
Ejemplo n.º 3
0
def run():
    youtube_client = YoutubeClient('client_secret.json')
    spotify_client = SpotifyClient(
        'BQCZQpa6N1lnjZtdNdkSZpG-Vwxk4zAwXG_DLGN2rOa_D6Ddq7_S6jdUOwalI2J_rU4IUaZwgZ-dVrGLfEKT1_L2WsyVR6PieIxcgOBkD1OFAE6J7OeMk-mXGKdH5Njww__7eA8lkfKDZAAV7OSRjg'
    )
    playlists = youtube_client.get_playlists()

    for index, playlist in enumerate(playlists):
        print(f'{index}: {playlist.title}')

    choice = int(input('Enter a corresponding number: '))
    chosen = playlists[choice]
    print(f'You selected {chosen.title}')

    songs = youtube_client.get_videos(chosen.id)
    print(f'Attempting to read {len(songs)}')

    for song in songs:
        sid = spotify_client.search_song(song.artist, song.track)

        if sid:
            added_song = spotify_client.add_song(sid)

            if added_song():
                print('Successful')
def run():
    """
    main driver code
    """
    youtube_client = YoutubeClient('./creds/client_secret.json')
    spotify_client = SpotifyClient(SPOTIFY_API_KEY)
    playlists = youtube_client.get_playlists()

    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")

    choice = int(input("Enter your choice: "))
    chosen_playlist = playlists[choice]
    print(f"You selected {chosen_playlist.title}")

    collected_songs = youtube_client.get_videos_from_playlist(
        chosen_playlist.id)
    print(f"Attempting to add {len(collected_songs)} to the spotify library")

    for song in collected_songs:
        spotify_song_id = spotify_client.search_song(song.artist, song.track)

        if spotify_song_id:
            added_flag = spotify_client.add_song_to_spotify(spotify_song_id)
            if added_flag:
                print(f"Successfully added {song.artist} - {song.track}")
Ejemplo n.º 5
0
def main():
    # get playlists from our youtube
    youtube_client = YoutubeClient('./creds/client_secret.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlist()

    # ask which playlist we want the music video from, make it more interactive
    for index, playlist in enumerate(playlists):
        print(f"{index}:{playlist.title}")

    choice = int(input("Enter your choice: "))
    chosen_playlist = playlists[choice]
    print(f"You selected: {chosen_playlist.title}")

    # for each video in the playlist get the song information from youtube
    songs = youtube_client.get_videos_from_playlist(chosen_playlist.id)
    print(f"Attempting to add {len(songs)}")

    # search for song on spotify
    for song in songs:
        spotify_song_id = spotify_client.search_song(song.artist, song.track)

        # if we find the song, add it to our liked song
        if spotify_song_id:
            added_song = spotify_client.add_song_to_spotify_liked(
                spotify_song_id)

            # if the song is added then let the user know!
            if added_song:
                print(f"Added {song.artist}")
Ejemplo n.º 6
0
    def test_get_link_metadata(self):
        metadata = YoutubeClient.get_link_metadata(LINKS[0])
        self.assertEqual(metadata['playlist_id'], EXPECTED_LIST)
        self.assertEqual(metadata['video_id'], EXPECTED_ID)

        metadata = YoutubeClient.get_link_metadata(LINKS[1])
        self.assertNotIn('playlist_id', metadata)
        self.assertEqual(metadata['video_id'], EXPECTED_ID)
Ejemplo n.º 7
0
def run():
    yt = YoutubeClient()

    while True:
        video_id = "W9Zh0nhqR3Y"
        comments = yt.video_comments(video_id)
        final_img = thumb.create_thumbnail(comments)
        yt.set_thumbnail(video_id, final_img)
        time.sleep(15)
Ejemplo n.º 8
0
def run():
    #get a list of playlist from youtube 
    youtube_client = YoutubeClient('./credits/client_id.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlists()

# ask which playlist we want to get the music video from  
    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")
    choice = int(input("Enter your choice: "))
    chosen_playlist = playlists[choice] 
    print(f"You selected: {chosen_playlist.title} ")
Ejemplo n.º 9
0
def run():
    youtube_client = YoutubeClient('./creds/client_secret.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlists()

    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")
    choice = input("Enter your choice: ")
    chosen_playlist = playlists[choice]
    print(f"You selected : {chosen_playlist.title}")

    songs = youtube_client.get_videos_from_playlist(chosen_playlist.id)
    print(f"Attempting to add {len(songs)} songs")

    for song in songs:
        spotify_song_id = spotify_client.search_song(song.artist, song.track)
        if spotify_song_id:
            added_song = spotify_client.add_song_to_spotify(spotify_song_id)
            if added_song:
                print(f"Successfully added {song.artist}, {song.track}")
Ejemplo n.º 10
0
def run():
    youtube_client = YoutubeClient()
    spotify_client = SpotifyClient('/secrets/secrets.spotify_token')
    playlists = youtube_client.get_playlists()

    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")
    choice = int(input("Enter your Playlist choice: "))
    chosen_playlist = playlists[choice]
    print(f"You selected: {chosen_playlist.title}")

    songs = youtube_client.get_videos_from_playlist(chosen_playlist.id)
    print(f"Attempting to add {len(songs)}")

    for song in songs:
        spotify_song_id = spotify_client.search_song(song.track)
        if spotify_song_id:
            added_song = spotify_client.add_song_to_spotify(spotify_song_id)
            if added_song:
                print(f"Added {song.track}")
Ejemplo n.º 11
0
def run():
    # Get youtube playlists
    youtube_client = YoutubeClient('./creds/client_secret.json')
    spotify_client = SpotifyClient(os.getenv('SPOTIFY_AUTH_TOKEN'))
    playlists = youtube_client.get_playlists()

    # Add 20 random tracks to your Liked Songs playlist
    # random_tracks = spotify_client.get_random_tracks()
    # track_ids = [track['id'] for track in random_tracks]

    # was_added_to_library = spotify_client.add_tracks_to_library(track_ids)
    # if was_added_to_library:
    #   for track in random_tracks:
    #     print(f"Added {track['name']} to your library")

    # Choose video's playlist
    # iterate through playlist and print names and position for choice
    for index, playlist in enumerate(playlists):
        print(f"{index}: {playlist.title}")
    choice = int(input("Enter your choice: "))
    chosen_playlist = playlists[choice]
    print(f"Selection: {chosen_playlist.title}")

    # Get song videos from playlist user selected
    songs = youtube_client.get_videos_from_playlist(chosen_playlist.id)
    print(f"Adding {len(songs)} songs")

    # Search Songs in Spotify
    for song in songs:
        spotify_song_id = spotify_client.search_song(song.artist, song.track)
        if spotify_song_id:
            added_song = spotify_client.add_song_to_spotify(spotify_song_id)
            if added_song:
                print(
                    f"Added {song.artist} - {song.track} to your Like Songs Playlist"
                )
Ejemplo n.º 12
0
def main():
    env = os.environ.get('ENV')

    if env not in ['development', 'production']:
        print("[ERROR] Invalid environment.")
        return

    print("[NOTICE] Init clients.")
    youtube_client = YoutubeClient()
    spotify_client = SpotifyClient()

    # Refresh tokens for both clients
    print("[NOTICE] Refreshing clients.")
    youtube_client.refresh()
    spotify_client.refresh()

    # Get Spotify playlist id
    print("[NOTICE] Get playlist id.")
    playlist_id = spotify_client.get_playlist()

    # Get uris of tracks already in the Spotify playlist
    print("[NOTICE] Get existing track uris.")
    existing_track_uris = spotify_client.get_existing_tracks(playlist_id)

    # Get recent video id
    print("[NOTICE] Get most recent video id.")
    recent_video_id = youtube_client.get_recent_video_id()
    recent_video_id_updated = False

    # Get first page of Youtube liked videos
    res = youtube_client.get_liked_videos()

    while res:
        # Get Youtube Valid Songs
        print("[NOTICE] Getting valid Youtube songs.")
        songs, already_processed = youtube_client.get_valid_songs(
            res, recent_video_id)

        # Add songs to playlist
        try:
            print("[NOTICE] Adding songs to Spotify playlist.")
            spotify_client.add_songs_to_playlist(songs, playlist_id,
                                                 existing_track_uris)
        except spotipy.exceptions.SpotifyException as err:
            # Catch Spotify client error
            if err.http_status == 401 and 'access token expired' in err.msg:
                # If the access token has expired, refresh it and continue
                spotify_client.refresh()
                continue
            else:
                print(
                    "[ERROR] There was an unexpected error with the Spotify client."
                )
                print(err)
                break
        except:
            break

        if not recent_video_id_updated:
            youtube_client.store_recent_video_id(res["items"][0]["id"])
            recent_video_id_updated = True

        if already_processed or 'nextPageToken' not in res:
            break

        # Get next page of liked Youtube videos
        # Youtube client will refresh itself automatically (if refresh token present in credentials)
        try:
            res = youtube_client.get_liked_videos(res['nextPageToken'])
        except Exception as err:
            print(
                "[ERROR] There was an unexpected error with the Youtube client."
            )
            print(err)
            break

    return
Ejemplo n.º 13
0
from youtube_client import YoutubeClient

client = YoutubeClient()
url = 'http://gdata.youtube.com/feeds/api/users/pr3dat0r002/uploads'
client.get_items(client.yt_service.GetYouTubeVideoFeed(url))

Ejemplo n.º 14
0
 def test_get_youtube_id(self):
     for link in LINKS:
         self.assertEqual(EXPECTED_ID, YoutubeClient.get_youtube_id(link))
Ejemplo n.º 15
0
 def test_create_video_link(self):
     self.assertEqual(
         YoutubeClient.create_video_link(EXPECTED_ID, EXPECTED_LIST),
         LINKS[0])
     self.assertEqual(YoutubeClient.create_video_link(EXPECTED_ID),
                      LINKS[1])
Ejemplo n.º 16
0
EXPECTED_ID = 'M4hXAZiiIZw'
EXPECTED_LIST = 'PLbh6KXqwIdGAsHxGlkb6sEVv1kXtBHuc2'
EXPECTED_LIST_IDS = ['M4hXAZiiIZw', 'HIgDFXeGOIg']
CAPTIONED_VIDEO = "ur_Uk5MwYJQ"
CAPTION_ID = 'gT93Vm4dZW3z_pi8djEZCRRxGJuGUZ9SDJ2L-JSYoeo='


class MockExecute:
    def __init__(self, items):
        self.items = items

    def execute(self):
        return self.items


youtube_client = YoutubeClient(None)
youtube_client.youtube = MagicMock()
youtube_client.youtube.playlistItems.return_value.list.return_value.execute.return_value = {
    'items': [{
        'contentDetails': {
            'videoId': EXPECTED_LIST_IDS[0]
        }
    }, {
        'contentDetails': {
            'videoId': EXPECTED_LIST_IDS[1]
        }
    }]
}


def caption_side_effect(part, videoId):
Ejemplo n.º 17
0
import json

from spotify_client import SpotifyClient
from youtube_client import YoutubeClient


class YoutubeToSpotify:
    @staticmethod
    def get_secrets():
        with open("secrets.json") as f:
            return json.load(f)


if __name__ == "__main__":
    secrets = YoutubeToSpotify.get_secrets()
    yt_client = YoutubeClient()
    spot_client = SpotifyClient()

    spot_client.authenticate_all()
    yt_playlist_id = input("Enter youtube playlist id:")

    song_arr = yt_client.playlist_items(yt_playlist_id)

    spot_playlist_id = spot_client.add_playlist()

    for i in range(len(song_arr)):
        if yt_client.get_artist_track(song_arr[i]["title"]) is None:
            song_arr[i] = song_arr[i]["title"]
        else:
            song_arr[i] = yt_client.get_artist_track(song_arr[i]["title"])