def __init__(self, api_key, channels): Session = sessionmaker(bind=engine) self.logger = getLogger('youtuber.grabber') self.api = API(api_key=api_key, client_id='', client_secret='') self.channels = channels self.session = Session()
def init(): YouTubeController.refresh_token = SecureSettings.get_string( "youtube_refresh_token") YouTubeController.api = API( client_id=SecureSettings.get_string("youtube_client_id"), client_secret=SecureSettings.get_string("youtube_client_secret"), api_key=SecureSettings.get_string("youtube_api_key"))
def main(): parser = argparse.ArgumentParser() parser.add_argument( '-o', '--output', help='Absolute path to output file. Output file can exist.', required=True) parser.add_argument('-k', '--api-key', help='Youtube API key.', required=True) parser.add_argument('-v', '--visible', help='Append new videos as visible.', default=False) parser.add_argument('-u', '--update', help='Update video in output file if it exists.', default=False) parser.add_argument( '--update-attributes', '--update-attributes', help= 'Comma separated list of attributes allowed to update. Works only when --update flag is true', default='description,title,preview') args = parser.parse_args() global api api = API(api_key=args.api_key, client_secret='', client_id='') original_videos = get_original_videos(args.output) youtube_videos = fetch_videos() merged_videos = merge_videos(original_videos, youtube_videos, args.visible, args.update, args.update_attributes) save_videos(merged_videos, args.output)
class YoutubeAPI(object): api_key = None yt = None def __init__(self, api_key, *args, **kwargs): self.api_key = api_key self.set_up_youtube() def set_up_youtube(self): self.yt = API(client_id='', client_secret='', api_key=self.api_key) def get_youtube_video(self, search_string): video = self.yt.get('search', q=search_string, maxResults=1, type='video', order='relevance') return ("https://www.youtube.com/watch?v=" + video["items"][0]["id"]["videoId"])
class Grabber: def __init__(self, api_key, channels): Session = sessionmaker(bind=engine) self.logger = getLogger('youtuber.grabber') self.api = API(api_key=api_key, client_id='', client_secret='') self.channels = channels self.session = Session() def _get(self, resource, part, **kwargs): try: result = self.api.get(resource, part=part, **kwargs) if result['pageInfo']['totalResults'] == 0: self.logger.error('Resource "{}" is empty for "{}"'.format( resource, kwargs)) exit(1) else: return result['items'], result.get('nextPageToken', None) except YouTubeException as e: self.logger.error(e.error_type) return [], None def run(self): for name in self.channels: channels, _ = self._get('channels', 'id', forUsername=name) channel_id = channels[0]['id'] playlists, _ = self._get('playlists', part='id', channelId=channel_id, maxResults=50) playlist_ids = [item['id'] for item in playlists] for pl_id in playlist_ids: page_token = None while True: pl_items, page_token = self._get( 'playlistItems', part='snippet,contentDetails', playlistId=pl_id, maxResults=50, pageToken=page_token) for item in pl_items: video_id = item['contentDetails']['videoId'] video_published_at = item['contentDetails'].get( 'videoPublishedAt', None) if not video_published_at: continue # deleted video published_at = datetime.strptime( video_published_at, '%Y-%m-%dT%H:%M:%S.%fZ') title = item['snippet']['title'] video = self.session.query(Video).get(video_id) if not video: self.session.add( Video(video_id=video_id, channel=name, title=title, published_at=published_at)) self.session.commit() stats, _ = self._get('videos', part='statistics', id=video_id) try: video_stat = stats[0] except IndexError: continue count = int(video_stat['statistics']['viewCount']) likes = int(video_stat['statistics']['likeCount']) dislikes = int( video_stat['statistics']['dislikeCount']) views = Views(video_id, count, likes, dislikes) self.session.add(views) self.session.commit() if not page_token: break
def add_song_info_from_spotify_url(url: str, username): details = dict(sp.track(url)) name = details['name'] duration = round(int(details['duration_ms']) / 60000, 2) cover_url = details['album']['images'][0]['url'] add_song_if_not_exists(name, duration, username, cover_url) # Youtube ## login g_client_id = "724786455286-h797c1jpr7nq4ivsqca73gplkbol5u1c.apps.googleusercontent.com" g_client_secret = "AxRk4DHa9IfLHd4kDMUXuilQ" g_key = "AIzaSyDvyloHqIVcizjDdwu0CvTwVvZTGDmrrbE" g = API(client_id=g_client_id, client_secret=g_client_secret, api_key=g_key) ## Functions def get_id_from_url(url: str): if "youtu.be" in url: result = urlparse(url) v_id = result.path.replace('/', '') return v_id else: result = urlparse(url) v_id = result.query.replace('v=', '') return v_id
def set_up_youtube(self): self.yt = API(client_id='', client_secret='', api_key=self.api_key)
import json from yaml import load, FullLoader from youtube import API from youtube_transcript_api import YouTubeTranscriptApi with open("keys.yaml", "r") as keys: config = load(keys, Loader=FullLoader) api = API(client_id=config['client_id'], client_secret=config["client_secret"], api_key=config["api_key"]) terms = [ "linear algebra", "calculus", "derivative", "shakespear", "python coding tutorial" ] def get_transcript(video_id): return YouTubeTranscriptApi.get_transcript(video_id, languages=["en"]) def write_transcript_to_file(name, transcript): with open("transcripts/" + name + '.json', 'w') as writefile: json.dump(transcript, writefile) def get_search_ids(term, max_ids=10000): ids = list() token = None while len(ids) < max_ids: response = api.get( 'search', q=term,