Пример #1
0
    def get_comment_info(self, comment_id=None, parts=None, return_json=False):
        """
        Retrieve comment data by comment id.

        Args:
            comment_id (str, optional)
                Provide a comma-separated list of comment IDs or just a comment id
                for the resources that are being retrieved
            parts (str, optional)
                Comma-separated list of one or more comments resource properties.
                If not provided. will use default public properties.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.Comment.
        Returns:
            The list data for you given comment id.
        """
        comma_separated_validator(comment_id=comment_id, parts=parts)
        incompatible_validator(comment_id=comment_id)
        if parts is None:
            parts = constants.COMMENT_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('comments', parts=parts)

        args = {'part': parts, 'id': comment_id}

        resp = self._request(resource='comments', args=args)
        data = self._parse_response(resp, api=True)
        if return_json:
            return data
        else:
            return [Comment.new_from_json_dict(item) for item in data]
Пример #2
0
    def get_guide_categories(self,
                             category_id=None,
                             region_code=None,
                             parts=None,
                             hl='en_US',
                             return_json=False):
        """
        Retrieve a list of categories that can be associated with YouTube channels.

        Refer: https://developers.google.com/youtube/v3/docs/guideCategories

        Args:
            category_id (str, optional)
                Provide a comma-separated list of guide category IDs or just a guide category id
                for the resources that are being retrieved.
            region_code (str, optional)
                Provide country code for the list of guide categories available.
                The country code is an ISO 3166-1 alpha-2 country code.
            parts (str, optional)
                Comma-separated list of one or more guideCategories resource properties.
                If not provided. will use default public properties.
            hl (str, optional)
                Specifies the language that should be used for text values.
                Default is en_US.
            return_json (bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.GuideCategory.

        Returns:
            The list of categories.
        """

        comma_separated_validator(category_id=category_id, parts=parts)
        incompatible_validator(category_id=category_id,
                               region_code=region_code)
        if parts is None:
            parts = constants.GUIDE_CATEGORY_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('guideCategories', parts=parts)

        args = {
            'part': parts,
            'hl': hl,
        }

        if category_id is not None:
            args['id'] = category_id
        elif region_code is not None:
            args['regionCode'] = region_code

        resp = self._request(resource='guideCategories', args=args)

        data = self._parse_response(resp, api=True)
        if return_json:
            return data
        else:
            return [GuideCategory.new_from_json_dict(item) for item in data]
Пример #3
0
    def get_video_by_id(self,
                        video_id=None,
                        hl='en_US',
                        parts=None,
                        return_json=False):
        """
        Retrieve data from YouTube Data Api for video which id or id list you point .

        Args:
            video_id (str)
                The id or comma-separated id list of video which you want to get data.
            hl (str, optional)
                If provide this. Will return video snippet's language localized info.
                This value need https://developers.google.com/youtube/v3/docs/i18nLanguages.
            parts (str, optional)
                Comma-separated list of one or more videos resource properties.
                If not provided. will use default public properties.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.Video
        Returns:
            The data for you given video.
        """
        comma_separated_validator(video_id=video_id, parts=parts)
        incompatible_validator(video_id=video_id)

        if parts is None:
            parts = constants.VIDEO_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('videos', parts=parts)

        args = {
            'id': video_id,
            'hl': hl,
            'part': parts,
        }

        resp = self._request(resource='videos', method='GET', args=args)

        data = self._parse_response(resp, api=True)
        self.calc_quota(resource='videos', parts=args['part'])
        if return_json:
            return data
        else:
            return [Video.new_from_json_dict(item) for item in data]
Пример #4
0
    def get_comment_thread_info(self,
                                comment_thread_id=None,
                                parts=None,
                                return_json=False):
        """
        Retrieve the comment thread info by single id.

        Refer: https://developers.google.com/youtube/v3/docs/commentThreads/list

        Args:
            comment_thread_id (str)
                The id parameter specifies a comma-separated list of comment thread IDs
                for the resources that should be retrieved.
            parts (str, optional)
                Comma-separated list of one or more commentThreads resource properties.
                If not provided. will use default public properties.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.CommentThread.
        Returns:
            The list data for you given comment thread.
        """

        comma_separated_validator(comment_thread_id=comment_thread_id,
                                  parts=parts)
        incompatible_validator(comment_thread_id=comment_thread_id)
        if parts is None:
            parts = constants.COMMENT_THREAD_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('commentThreads', parts=parts)

        args = {'id': comment_thread_id, 'part': parts}

        resp = self._request(resource='commentThreads', args=args)

        data = self._parse_response(resp, api=True)
        if return_json:
            return data
        else:
            return [CommentThread.new_from_json_dict(item) for item in data]
Пример #5
0
    def get_comments_by_parent(self,
                               parent_id=None,
                               parts=None,
                               limit=20,
                               count=20,
                               return_json=False):
        """
        Retrieve data from YouTube Data Api for top level comment which you point.

        Refer: https://developers.google.com/youtube/v3/docs/comments/list

        Args:
            parent_id (str, optional)
                Provide the ID of the comment for which replies should be retrieved.
                Now YouTube currently supports replies only for top-level comments
            parts (str, optional)
                Comma-separated list of one or more comments resource properties.
                If not provided. will use default public properties.
            limit (int, optional)
                Each request retrieve comments from data api.
                For comments, this should not be more than 100.
                Default is 20.
            count (int, optional)
                The count will retrieve comments data.
                Default is 20.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.Comment.
        Returns:
            The list data for you given comment.
        """

        comma_separated_validator(parts=parts)
        incompatible_validator(parent_id=parent_id)
        if parts is None:
            parts = constants.COMMENT_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('comments', parts=parts)

        args = {'part': parts, 'maxResults': limit, 'parentId': parent_id}

        comments = []
        next_page_token = None
        while True:
            _, next_page_token, data = self.paged_by_page_token(
                resource='comments',
                args=args,
                page_token=next_page_token,
            )
            items = self._parse_data(data)
            if return_json:
                comments += items
            else:
                comments += [
                    Comment.new_from_json_dict(item) for item in items
                ]
            if len(comments) >= count:
                break
            if next_page_token is None:
                break
        return comments[:count]
Пример #6
0
    def get_comment_threads(self,
                            all_to_channel_id=None,
                            channel_id=None,
                            video_id=None,
                            parts=None,
                            order='time',
                            search_term=None,
                            limit=20,
                            count=20,
                            return_json=False):
        """
        Retrieve the comment thread info by single id.

        Refer: https://developers.google.com/youtube/v3/docs/commentThreads/list

        Args:
            all_to_channel_id (str, optional)
                If you provide channel id by this parameter.
                Will return all comment threads associated with the specified channel.
                The response can include comments about the channel or about the channel's videos.
            channel_id (str, optional)
                If you provide channel id by this parameter.
                Will return comment threads containing comments about the specified channel.
                But not include comments about the channel's videos.
            video_id (str, optional)
                If you provide video id by this parameter.
                Will return comment threads containing comments about the specified video.
            parts (str, optional)
                Comma-separated list of one or more commentThreads resource properties.
                If not provided. will use default public properties.
            order (str, optional)
                Provide the response order type. Valid value are: time, relevance.
                Default is time. order by the commented time.
            search_term (str, optional)
                If you provide this. Only return the comments that contain the search terms.
            limit (int, optional)
                Each request retrieve comment threads from data api.
                For comment threads, this should not be more than 100.
                Default is 20.
            count (int, optional)
                The count will retrieve comment threads data.
                Default is 20.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.CommentThread.
        Returns:
            The list data for you given comment thread.
        """

        comma_separated_validator(parts=parts)
        incompatible_validator(all_to_channel_id=all_to_channel_id,
                               channel_id=channel_id,
                               video_id=video_id)
        if parts is None:
            parts = constants.COMMENT_THREAD_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('commentThreads', parts=parts)

        args = {'part': parts, 'maxResults': limit}
        if all_to_channel_id is not None:
            args['allThreadsRelatedToChannelId'] = all_to_channel_id
        elif channel_id is not None:
            args['channelId'] = channel_id
        elif video_id is not None:
            args['videoId'] = video_id

        if order not in ['time', 'relevance']:
            raise PyYouTubeException(
                ErrorMessage(status_code=ErrorCode.INVALID_PARAMS,
                             message='Order type must be time or relevance.'))

        if search_term is not None:
            args['searchTerms'] = search_term

        comment_threads = []
        next_page_token = None
        while True:
            _, next_page_token, data = self.paged_by_page_token(
                resource='commentThreads',
                args=args,
                page_token=next_page_token,
            )
            items = self._parse_data(data)
            if return_json:
                comment_threads += items
            else:
                comment_threads += [
                    CommentThread.new_from_json_dict(item) for item in items
                ]
            if next_page_token is None:
                break
            if len(comment_threads) >= count:
                break
        return comment_threads[:count]
Пример #7
0
    def get_video_by_filter(self,
                            chart=None,
                            my_rating=None,
                            region_code=None,
                            category_id=None,
                            summary=True,
                            count=5,
                            limit=5,
                            hl='en_US',
                            parts=None,
                            return_json=False):
        """
        Retrieve data from YouTube Data Api for video which you point.

        Args:
            chart (str, optional)
                Now only mostPopular parameter valid.
                Will return most popular videos for point region or category.
                If use this must provide either region code or category id.
            my_rating(str, optional)
                Now dislike and like parameter can be pointed.
                - dislike will return set disliked by you.
                - like will return set liked by you.
                Must need you give authorization.
            region_code (str, optional)
                Provide region code for filter for the chart parameter.
            category_id (str, optional)
                Provide video category id for filter for the chart parameter.
            summary (bool, optional)
                 If True will return results videos summary of metadata.
                 Notice this depend on your query.
            count (int, optional)
                The count will retrieve videos data.
                Default is 5.
            limit (int, optional)
                The maximum number of items each request retrieve.
                For videos, this should not be more than 50.
                Default is 5
            hl (str, optional)
                If provide this. Will return video snippet's language localized info.
                This value need https://developers.google.com/youtube/v3/docs/i18nLanguages.
            parts (str, optional)
                Comma-separated list of one or more playlist items resource properties.
                If not provided. will use default public properties.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.Video
        Returns:
            The data for videos by your filter.
        """

        comma_separated_validator(parts=parts)
        incompatible_validator(chart=chart, my_rating=my_rating)

        if parts is None:
            parts = constants.VIDEO_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('videos', parts=parts)

        args = {'part': parts, 'hl': hl, 'maxResults': limit}

        if chart is not None:
            args['chart'] = chart
            if region_code is not None:
                args['regionCode'] = region_code
            elif category_id is not None:
                args['videoCategoryId'] = category_id
        elif my_rating is not None:
            args['myRating'] = my_rating

        videos = []
        videos_summary = None
        next_page_token = None
        while True:
            prev_page_token, next_page_token, data = self.paged_by_page_token(
                resource='videos',
                args=args,
                page_token=next_page_token,
            )
            items = self._parse_data(data)
            if return_json:
                videos += items
            else:
                videos += [Video.new_from_json_dict(item) for item in items]
            if summary:
                videos_summary = data.get('pageInfo', {})
            if next_page_token is None:
                break
            if len(videos) >= count:
                break
        return videos[:count], videos_summary
Пример #8
0
    def get_playlist_item(self,
                          playlist_id=None,
                          playlist_item_id=None,
                          video_id=None,
                          parts=None,
                          summary=True,
                          count=5,
                          limit=5,
                          return_json=False):
        """
        Retrieve channel's playlist Items info.

        Args:
            playlist_id (str, optional)
                If provide channel id, this will return pointed playlist's item info.
            playlist_item_id (str optional)
                If provide this. will return those playlistItem's info.
            video_id (str, optional)
                If provide this, will return playlist items which contain the specify video.
            parts (str, optional)
                Comma-separated list of one or more playlist items resource properties.
                If not provided. will use default public properties.
            summary (bool, optional)
                 If True will return playlist item summary of metadata.
                 Notice this depend on your query.
            count (int, optional)
                The count will retrieve playlist items data.
                Default is 5.
            limit (int, optional)
                The maximum number of items each request retrieve.
                For playlistItem, this should not be more than 50.
                Default is 5
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.PlayListItem
        Returns:
            return tuple.
            (playlistItem data, playlistItem summary)
        """
        comma_separated_validator(playlist_item_id=playlist_item_id,
                                  parts=parts)
        incompatible_validator(playlist_id=playlist_id,
                               playlist_item_id=playlist_item_id)

        if parts is None:
            parts = constants.PLAYLIST_ITEM_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('playlistItems', parts=parts)

        args = {
            'part': parts,
            'maxResults': limit,
        }

        if playlist_id is not None:
            args['playlistId'] = playlist_id
        elif playlist_item_id is not None:
            args['id'] = playlist_item_id

        if video_id is not None:
            args['videoId'] = video_id

        playlist_items = []
        playlist_items_summary = None
        next_page_token = None
        while True:
            prev_page_token, next_page_token, data = self.paged_by_page_token(
                resource='playlistItems',
                args=args,
                page_token=next_page_token,
            )
            items = self._parse_data(data)
            if return_json:
                playlist_items += items
            else:
                playlist_items += [
                    PlaylistItem.new_from_json_dict(item) for item in items
                ]
            if summary:
                playlist_items_summary = data.get('pageInfo', {})
            if next_page_token is None:
                break
            if len(playlist_items) >= count:
                break
        return playlist_items[:count], playlist_items_summary
Пример #9
0
    def get_playlist(self,
                     channel_id=None,
                     playlist_id=None,
                     parts=None,
                     summary=True,
                     count=5,
                     limit=5,
                     hl='en_US',
                     mine=None,
                     return_json=False):
        """
        Retrieve channel playlists info.
        Provide two methods: by channel ID, or by playlist id (ids)

        Args:
            channel_id (str, optional)
                If provide channel id, this will return pointed channel's playlist info.
            playlist_id (str optional)
                If provide this. will return those playlist's info.
            mine (bool, optional)
                If you have give the authorization. Will return your playlists.
                Must provide the access token.
            parts (str, optional)
                Comma-separated list of one or more playlist resource properties.
                If not provided. will use default public properties.
            summary (bool, optional)
                 If True will return channel playlist summary of metadata.
                 Notice this depend on your query.
            count (int, optional)
                The count will retrieve playlist data.
                Default is 5.
            limit (int, optional)
                The maximum number of items each request to retrieve.
                For playlist, this should not be more than 50.
                Default is 5
            hl (str, optional)
                If provide this. Will return playlist's language localized info.
                This value need https://developers.google.com/youtube/v3/docs/i18nLanguages.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.PlayList
        Returns:
            return tuple.
            (playlist data, playlist summary)
        """

        comma_separated_validator(playlist_id=playlist_id)
        incompatible_validator(channel_id=channel_id,
                               playlist_id=playlist_id,
                               mine=mine)

        if parts is None:
            parts = constants.PLAYLIST_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('playlists', parts=parts)

        args = {'part': parts, 'hl': hl, 'maxResults': limit}

        if channel_id is not None:
            args['channelId'] = channel_id
        elif playlist_id is not None:
            args['id'] = playlist_id
        elif mine is not None:
            args['mine'] = mine

        playlists = []
        playlists_summary = None
        next_page_token = None
        while True:
            prev_page_token, next_page_token, data = self.paged_by_page_token(
                resource='playlists',
                args=args,
                page_token=next_page_token,
            )
            items = self._parse_data(data)
            if return_json:
                playlists += items
            else:
                playlists += [
                    PlayList.new_from_json_dict(item) for item in items
                ]
            if summary:
                playlists_summary = data.get('pageInfo', {})
            if next_page_token is None:
                break
            if len(playlists) >= count:
                break
        return playlists[:count], playlists_summary
Пример #10
0
    def get_channel_info(self,
                         category_id=None,
                         channel_id=None,
                         channel_name=None,
                         mine=None,
                         parts=None,
                         hl='en_US',
                         return_json=False):
        """
        Retrieve channel data from YouTube Data API for channel which you given.

        Args:
            category_id (str, optional)
                The guide category id for channels associated which that category
            channel_id (str, optional)
                The id or comma-separated id list for youtube channel which you want to get.
            channel_name (str, optional)
                The name for youtube channel which you want to get.
            mine (bool, optional)
                If you have give the authorization. Will return your channels.
                Must provide the access token.
            parts (str, optional)
                Comma-separated list of one or more channel resource properties.
                If not provided. will use default public properties.
            hl (str, optional)
                If provide this. Will return channel's language localized info.
                This value need https://developers.google.com/youtube/v3/docs/i18nLanguages.
            return_json(bool, optional)
                The return data type. If you set True JSON data will be returned.
                False will return pyyoutube.Channel
        Returns:
            The data for you given channel.
        """
        comma_separated_validator(channel_id=channel_id, parts=parts)
        incompatible_validator(category_id=category_id,
                               channel_id=channel_id,
                               channel_name=channel_name,
                               mine=mine)

        if parts is None:
            parts = constants.CHANNEL_RESOURCE_PROPERTIES
            parts = ','.join(parts)
        else:
            parts_validator('channels', parts=parts)

        args = {'hl': hl, 'part': parts}
        if channel_name is not None:
            args['forUsername'] = channel_name
        elif channel_id is not None:
            args['id'] = channel_id
        elif category_id is not None:
            args['categoryId'] = channel_id
        elif mine is not None:
            args['mine'] = mine

        resp = self._request(resource='channels', method='GET', args=args)

        data = self._parse_response(resp, api=True)
        self.calc_quota(resource='channels', parts=args['part'])
        if return_json:
            return data
        else:
            return [Channel.new_from_json_dict(item) for item in data]
Пример #11
0
 def testCommaValidator(self):
     with self.assertRaises(pyyoutube.PyYouTubeException):
         validator.comma_separated_validator(kw1=1, kw2=2)
     validator.comma_separated_validator(kw1='1,2')