コード例 #1
0
    def get_channel_metadata_gen(self, channel_id, parser=P.parse_channel_metadata,
                                 part=["id", "snippet", "contentDetails", "statistics",
                                       "topicDetails", "brandingSettings"],
                                **kwargs):
        '''
        Gets a dictionary of channel metadata given a channel_id, or a list of channel_ids.

        :param channel_id:  channel id(s)
        :type channel_id: str or list
        :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 the YouTube channel metadata
        :rtype: dict
        '''
        parser=parser if parser else P.raw_json
        part = ','.join(part)
        if isinstance(channel_id, list) or isinstance(channel_id, pd.Series):
            for chunk in _chunker(channel_id, 50):
                id_input = ','.join(chunk)
                http_endpoint = ("https://www.googleapis.com/youtube/{}/channels?"
                                "part={}&id={}&key={}&maxResults=50".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)
コード例 #2
0
    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)
コード例 #3
0
    def get_video_metadata_gen(
            self,
            video_id,
            parser=P.parse_video_metadata,
            part=['statistics', 'snippet', 'contentDetails'],
            **kwargs):
        '''
        Given a `video_id` returns metrics (views, likes, comments, duration) and metadata (description, category) as a dictionary.

        Read the docs: https://developers.google.com/youtube/v3/docs/videos/list

        :param video_id: The ID of a video IE: "kNbhUWLH_yY", this can be found at the end of YouTube urls and by parsing links using :meth:`youtube_api.youtube_api_utils.strip_youtube_id`.
        :type video_id: str or 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: returns metadata from the inputted ``video_id``s.
        :rtype: dict
        '''
        part = ','.join(part)
        parser = parser if parser else P.raw_json
        if isinstance(video_id, list) or isinstance(video_id, pd.Series):
            for chunk in _chunker(video_id, 50):
                id_input = ','.join(chunk)
                http_endpoint = (
                    "https://www.googleapis.com/youtube/v{}/videos"
                    "?part={}"
                    "&id={}&key={}&maxResults=50".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:
            raise Exception('This function only takes iterables!')