Ejemplo n.º 1
0
def get_videos_from_playlist(user_profile, playlist_id):
    whole_playlist = []
    try:
        next_page = ''
        while True:
            # iterate playlist videos to see if any pending videos is already in it and purge pending dict
            list_uploaded_videos = user_profile.youtube.playlistItems().list(
                playlistId=playlist_id,
                part='snippet',
                maxResults=50,
                pageToken=next_page).execute()
            next_page = list_uploaded_videos.get('nextPageToken')
            for lista_yt in list_uploaded_videos['items']:
                whole_playlist.append({
                    'videoId':
                    lista_yt['snippet']['resourceId']['videoId'],
                    'etag':
                    lista_yt['etag']
                })
            if not next_page:
                break
    except HttpError as err:
        process_exception(user_profile,
                          err,
                          custom_str='Error reading playlist ' + playlist_id)
    return whole_playlist
Ejemplo n.º 2
0
def search_playlist_by_name(user_profile, name=''):
    try:
        next_page = ''
        while True:
            mis_listas = user_profile.youtube.playlists().list(
                part='id,snippet,status', pageToken=next_page,
                mine=True).execute(num_retries=3)
            next_page = mis_listas.get('nextPageToken')
            for lista_yt in mis_listas['items']:
                if lista_yt['snippet'].get('title') == name:
                    return lista_yt['id']
            if not next_page:
                break
    except HttpError as err:
        process_exception(user_profile,
                          err,
                          custom_str='Error searching playlist ' + name)
    return ''
Ejemplo n.º 3
0
 def insert_videos(self, video_list, playlist_id):
     if not video_list:
         threads_log.debug(
             'Video list is empty - No videos will be inserted')
     inserted_videos = []
     video_id = ''
     for video_to_insert in video_list:
         try:
             video_id = extract_thread_id_from_url(video_to_insert['url'])
             threads_log.debug("Inserting video " + video_id +
                               ' into playlist ' + playlist_id)
             insert_result = playlist_insert(playlist_id, video_id,
                                             self.__user_profile.youtube)
             inserted_videos.append(insert_result)
             threads_log.debug("Video inserted: " + str(insert_result))
         except HttpError as err:
             process_exception(self.__user_profile,
                               err,
                               custom_str='Error adding video: ' +
                               video_id + '\n' + str(err.content))
             error_names_list = get_error_names(err)
             threads_log.error('Error adding video: ' + video_id + '\n' +
                               str(err.content))
             threads_log.error('Error list: ' + str(error_names_list))
             # remove video for some errors
             # there is no duplicate control!!!!!
             # https://stackoverflow.com/questions/39687442/how-to-disable-inserting-the-same-video-in-playlist-using-youtube-api-v3
             # apparently the limit of a playlist is 5.000 videos
             # https://webapps.stackexchange.com/questions/77790/what-is-the-maximum-youtube-playlist-length
             # threads in vBulletin go up to 2.000 posts so this limit won't be reached
             if ('videoAlreadyInPlaylist'
                     in error_names_list) or ('videoNotFound'
                                              in error_names_list):
                 inserted_videos.append({'videoId': video_id, 'etag': ''})
                 threads_log.debug(
                     'Error adding video: ' + video_id +
                     '. Video can be removed from pending list')
             if not self.__user_profile.has_quota:
                 break
     return inserted_videos
Ejemplo n.º 4
0
def create_playlist(user_profile,
                    name='',
                    description='',
                    privacy='unlisted',
                    url=''):
    if not user_profile.has_quota:
        return ''
    if not name:
        name = description
    name = name.strip()
    body = dict(snippet=dict(title=name,
                             description=video_description(description, url)),
                status=dict(privacyStatus=privacy))
    try:
        playlists_insert_response = user_profile.youtube.playlists().insert(
            part='snippet,status', body=body).execute()
        playlist_id = playlists_insert_response['id']
        return playlist_id
    except HttpError as err:
        process_exception(user_profile,
                          err,
                          custom_str='Error creating playlist ' + name)
    return ''
Ejemplo n.º 5
0
def iterate_video_dict_and_insert(user_profile, playlist_id, vid_dict):
    added_videos = []
    video_ids = list(vid_dict.keys())
    # print('Insertando ' + str(len(video_ids)) + ' videos en ' + playlist_id)
    for video_id in video_ids:
        try:
            insert_result = playlist_insert(playlist_id, video_id, user_profile.youtube)
            added_videos.append(insert_result)
            vid_dict.pop(video_id)
        except HttpError as err:
            process_exception(user_profile, err, custom_str='Error adding video: ' + video_id + '\n' + str(err.content))
            error_names_list = get_error_names(err)
            # remove video for some errors
            if ('duplicate' in error_names_list) or ('playlistItemsNotAccessible' in error_names_list) or \
                    ('videoNotFound' in error_names_list):
                vid_dict.pop(video_id)
        except Exception as err:
            # another kind of error I can't solve
            process_exception(user_profile, err, custom_str='Error adding video: ' + video_id + '\n' + str(err.content))
            break
        if not user_profile.has_quota:
            break
    return added_videos
Ejemplo n.º 6
0
def read_etags_from_playlists(user_profile, playlist_id=None):
    if not user_profile.has_quota:
        return ''
    etag_list = []
    if not playlist_id:
        playlist_ids = []
        for thread in user_profile.mongo_thread_list():
            id = thread.get('playlist_id', None)
            if id:
                playlist_ids.append(id)
        ids_as_str = ','.join(playlist_ids)
    else:
        ids_as_str = playlist_id
    try:
        mis_listas = user_profile.youtube.playlists().list(
            part='id,snippet,status', id=ids_as_str).execute(num_retries=3)
        for item in mis_listas['items']:
            etag_list.append({
                'playlist_id': item['id'],
                'playlist_etag': item['etag']
            })
    except Exception as err:
        process_exception(user_profile, err, custom_str='')
    return etag_list