def _http_request(self, http_endpoint, timeout_in_n_seconds=False): ''' A wrapper function for making an http request to the YouTube Data API. Will print the `http_endpoint` if the YouTubeDataAPI class is instantiated with verbose = True. Attempts to load the response of the http request, and returns json response. ''' if self.verbose: # Print the Http req and replace the API key with a placeholder print(http_endpoint.replace(self.key, '{API_KEY_PLACEHOLDER}')) response = self.session.get(http_endpoint, timeout=self._timeout) response_json = _load_response(response) return response_json
def get_featured_channels_gen(self, channel_id, parser=P.parse_featured_channels, part=["id", "brandingSettings"], **kwargs): ''' Given a `channel_id` returns a dictionary {channel_id : [list, of, channel_ids]} of featured channels. Optionally, can take a list of channel IDS, and returns a list of dictionaries. Read the docs: https://developers.google.com/youtube/v3/docs/channels/list :param channel_id: channel_ids IE: ['UCn8zNIfYAQNdrFRrr8oibKw'] :type channel_id: str of list of str :param parser: the function to parse the json document :type parser: :mod:`youtube_api.parsers module` :param part: The part parameter specifies a comma-separated list of one or more resource properties that the API response will include. Different parameters cost different quota costs from the API. :type part: list :returns: yields metadata for featured channels :rtype: dict ''' parser = parser if parser else P.raw_json part = ','.join(part) if isinstance(channel_id, list): for chunk in _chunker(channel_id, 50): id_input = ','.join(chunk) http_endpoint = ( "https://www.googleapis.com/youtube/v{}/channels" "?part={}&id={}&key={}".format(self.api_version, part, id_input, self.key)) for k, v in kwargs.items(): http_endpoint += '&{}={}'.format(k, v) response_json = self._http_request(http_endpoint) if response_json.get('items'): for item in response_json['items']: yield parser(item) else: yield parser(None) else: http_endpoint = ("https://www.googleapis.com/youtube/v{}/channels" "?part={}&id={}&key={}".format( self.api_version, part, channel_id, self.key)) for k, v in kwargs.items(): http_endpoint += '&{}={}'.format(k, v) response = self.session.get(http_endpoint) response_json = _load_response(response) for item in response_json['items']: yield parser(item)
def get_video_comments(self, video_id, get_replies=True, max_results=None, next_page_token=False, parser=P.parse_comment_metadata, part = ['snippet'], **kwargs): """ Returns comments and replies to comments for a given video. Read the docs: https://developers.google.com/youtube/v3/docs/commentThreads/list :param video_id: a video_id IE: "eqwPlwHSL_M" :type video_id: str :param get_replies: whether or not to get replies to comments :type get_replies: bool :param parser: the function to parse the json document :type parser: :mod:`youtube_api.parsers module` :param part: The part parameter specifies a comma-separated list of one or more resource properties that the API response will include. Different parameters cost different quota costs from the API. :type part: list :returns: comments and responses to comments of the given ``video_id``. :rtype: list of dict """ parser=parser if parser else P.raw_json part = ','.join(part) comments = [] run = True while run: http_endpoint = ("https://www.googleapis.com/youtube/v{}/commentThreads?" "part={}&textFormat=plainText&maxResults=100&" "videoId={}&key={}".format( self.api_version, part, video_id, self.key)) for k,v in kwargs.items(): http_endpoint += '&{}={}'.format(k, v) if next_page_token: http_endpoint += "&pageToken={}".format(next_page_token) response = self.session.get(http_endpoint) response_json = _load_response(response) if response_json.get('items'): items = response_json.get('items') for item in items: if max_results: if len(comments) >= max_results: return comments comments.append(parser(item)) if response_json.get('nextPageToken'): next_page_token = response_json['nextPageToken'] else: run=False break if get_replies: for comment in comments: if comment.get('reply_count') and comment.get('reply_count') > 0: comment_id = comment.get('comment_id') http_endpoint = ("https://www.googleapis.com/youtube/v{}/comments?" "part={}&textFormat=plainText&maxResults=100&" "parentId={}&key={}".format( self.api_version, part, comment_id, self.key)) for k,v in kwargs.items(): http_endpoint += '&{}={}'.format(k, v) response_json = self._http_request(http_endpoint) if response_json.get('items'): for item in response_json.get('items'): if max_results: if len(comments) >= max_results: return comments comments.append(parser(item)) return comments