def get_channel_info(username): api = Api(api_key='redacted') channel_by_name = api.get_channel_info(channel_name=username) try: response = channel_by_name.items[0].to_dict() except: return 'User doesnt exist' channel_name = response["snippet"]["localized"]["title"] created_at = response["snippet"]["publishedAt"] pfp_url = response["snippet"]["thumbnails"]["default"]["url"] view_count = response["statistics"]["viewCount"] subscribers = response["statistics"]["subscriberCount"] video_count = response["statistics"]["videoCount"] userid = response["id"] description = response["brandingSettings"]["channel"]["description"] match = re.findall(r'[\w\.-]+@[\w\.-]+', str(description)) if len(match) > 0: emails = ','.join(match) else: emails = "None found" data = { "name": channel_name, "created_at": parser.parse(created_at).timestamp(), "pfp_url": pfp_url, "total_views": view_count, "subsribers": subscribers, "video_count": video_count, "userid": userid, "emails": emails, "url": f'https://www.youtube.com/channel/{userid}' } return data
def get_all_comments(YOUTUBE_API_KEY, query, count_video=10, limit=30, maxResults=10, nextPageToken=''): """ Выгрузка maxResults комментариев """ api = Api(api_key=YOUTUBE_API_KEY) video_by_keywords = api.search_by_keywords(q=query, search_type=["video"], count=count_video, limit=limit) videoId = [x.id.videoId for x in video_by_keywords.items] comments_all = [] for id_video in videoId: try: data = get_data(YOUTUBE_API_KEY, id_video, maxResults=maxResults, nextPageToken=nextPageToken) comment = list(get_text_of_comment(data)) comments_all.append(comment) except: continue comments = sum(comments_all, []) return comments
def process_play_list_step(message): try: chat_id = message.chat.id text = message.text if ":" not in text: bot.reply_to(message, 'Wrong message format') return data = str(text).split(':') plaid = data[0] title = data[1] if len(plaid) <= 5 or len(title) <= 5: bot.reply_to(message, 'Wrong message format') return api = Api(api_key=youtube_api_key) playlist_item_by_playlist = api.get_playlist_items(playlist_id=plaid, count=1000) videos = playlist_item_by_playlist.items if len(videos) <= 0: bot.send_message(message.chat.id, "No Vidoes found for playlist = " + str(title)) return bot.send_message(message.chat.id, "Found " + str(len(videos)) + " videos from youtube") real_video_count = len(videos) count = 0 lessons = [] for video in videos: video_by_id = api.get_video_by_id( video_id=video.snippet.resourceId.videoId, parts=('snippet', 'contentDetails', 'statistics')) if len(video_by_id.items) <= 0: real_video_count = real_video_count - 1 continue count = count + 1 item = video_by_id.items[0] title = title + " " + formatLessonName( count) + " " + item.snippet.title time_val = isodate.parse_duration(item.contentDetails.duration) code = getYoutubeEmbedCode(video.snippet.resourceId.videoId) lesson = Lesson(title, code, time_val) lessons.append(lesson) bot.send_message( message.chat.id, "Total avalibale vidoe count = " + str(real_video_count)) if len(lessons) <= 0: bot.send_message(message.chat.id, "No lesson found!") loginAndUpdate(lessons, message, plaid) except Exception as e: bot.reply_to(message, str(e))
def fetchYoutubeData(): processedChannelDataID = None try: successMessage('- Gathering youtube channel & video data...') api = Api(api_key=os.getenv('YOUTUBE_DATA_API_KEY')) channelById = api.get_channel_info( channel_id=os.getenv('YOUTUBE_CHANNEL_ID')) successMessage('- Fetched youtube channel & video data...') uploadsPlaylistId = channelById.items[ 0].contentDetails.relatedPlaylists.uploads allChannelVideos = api.get_playlist_items( playlist_id=uploadsPlaylistId, count=30, limit=30) successMessage('- Constructing youtube channel & video data...') processedData = [] for video in allChannelVideos.items: processedData.append({ "videoUrl": video.contentDetails.videoId, "videoTitle": video.snippet.channelTitle, "videoDescription": video.snippet.description, }) successMessage('- Storing youtube video & channel data...') processedChannelDataID = saveDataToMongoDB( { "thumbnail": channelById.items[0].snippet.thumbnails.high.url, "channelName": channelById.items[0].snippet.title, "channelDescription": channelById.items[0].snippet.description, "keywords": channelById.items[0].brandingSettings.channel.keywords.split(), "resetAt": round(time.time()) }, "youtubeChannelData") saveDataToMongoDB( { "_id": processedChannelDataID, "channelName": channelById.items[0].snippet.title, "videos": processedData, "resetAt": round(time.time()), "hasBeenProcessed": False }, "youtubeVideoData") successMessage('- Completed storing youtube video & channel data...') except: errorMessage('- An exception occurred') else: successMessage('- Completed youtube data step... ') return processedChannelDataID
def random_video(mood): api = Api(api_key=apikey) videos = [] playlist_item_by_playlist = api.get_playlist_items( playlist_id=playlists_by_mood[mood], count=None).items for item in iter(playlist_item_by_playlist): resource = item.snippet.resourceId if resource.kind == 'youtube#video': videos.append(resource.videoId) video = videos[random.randint(0, len(videos) - 1)] return 'https://www.youtube.com/watch?v=' + video
def get_music_titles(playlistID): #Returns the music titles on the youtube's playlist api = Api(api_key=YOUTUBE_KEY) titles = api.get_playlist_items(playlist_id=playlistID, count=None) list_music = [] for titulo in titles.items: musica = titulo.snippet.title.upper() list_music.append(musica) print(list_music) return list_music
async def find_first_youtube_match(keyword: str): youtube = Api(api_key=os.environ['YOUTUBE_TOKEN']) results = youtube.search_by_keywords( q=keyword, search_type=('video', ), count=1, ).items if len(results): msg = f'https://www.youtube.com/watch?v={results[0].id.videoId}' else: msg = 'I have found nothing' return msg
def get_audio(url=None, file_name=None, index_file=None, since=None, limit=None, prefix_name=None, prefix_num=None): api = Api(api_key=conf['api_key']) if index_file and os.path.exists(index_file): return get_audios_from_indexed_list(api, store_dir, index_file, since, limit, prefix_name, prefix_num) vid = get_video_id(url) if not vid: return 'Please provide video ID or full URL' return get_audio_by_id(api, store_dir, vid, file_name)
############################################################################################### #################### Extract information of a Youtube Playlist ################################ ############################################################################################### from pyyoutube import Api api = Api(api_key='AIzaSyCKfE-e5tPy6XcD_5ArjrxTV8jrMfuGTKs') def get_playlists_by_channel(channel_id="", count=None): """ Given a channel ID, get all playlists under the channel :param channel_id: string type, channel ID :param count: number of playlists returned, if count is None, then get ALL playlists :return: playlists, including id, channelId, channelTitle, title, and description """ playlists = api.get_playlists(channel_id=channel_id, count=count) playlists = playlists.items.to_dict() results = [] for pl in playlists: results.append(parse_playlist_response(pl)) return results def get_playlist_by_id(playlist_id=''): """ Given a playlist_id, get metadata of the playlist :param playlist_id: :return: """
def get_playlist(url=None, filename=None): pid = get_playlist_id(url) if not pid: return 'Please provide playlist ID or full URL' api = Api(api_key=conf['api_key']) return get_playlist_index(api, store_dir, pid, filename)
import json import os from youtube_transcript_api import YouTubeTranscriptApi from pyyoutube import Api from pytube import Playlist # create api api = Api(api_key='AIzaSyAUdxEy_nslLj9mTdOVKj5diUvs9OKtQsU') def get_video_ids(playlist_id): """Retrieves individual video id from a playlist params: playlist_id: youtube playlist id """ video_ids = [] playlist_items = api.get_playlist_items(playlist_id=playlist_id, count=None) for item in playlist_items.items: video_ids.append(item.snippet.resourceId.videoId) return video_ids def download_videos_of_playlist(playlist_id, videos_dir): """Downloads entire videos in youtube video playlists params: playlist_id: id of youtube playlist to download videos_dir: path to download videos to """