Exemplo n.º 1
0
    def get_caption_by_video_id(self,
                                video_id,  # type: str
                                fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                                return_json=False  # type: bool
                                ):
        # type: (...) -> Union[List[VideoCaption], dict]
        """
        Retrieve caption for video.
        :param video_id: The id for video you want to retrieve data.
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param return_json: Set to false will return a list of Post instances.
        Or return json data. Default is false.
        :return: VideoCaption instance or dict
        """
        if fields is None:
            fields = constant.FB_VIDEO_CAPTION_BASIC_FIELDS

        args = {
            "fields": enf_comma_separated("fields", fields)
        }

        resp = self._request(
            method='GET',
            path='{0}/{1}/captions'.format(self.version, video_id),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data
        else:
            return [VideoCaption.new_from_json_dict(item) for item in data.get('data', [])]
Exemplo n.º 2
0
    def get_album_info(self,
                       album_id,  # type: str
                       fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                       return_json=False  # type: bool
                       ):
        # type: (...) -> Union[Album, dict]
        """
        Retrieve album info by id.
        :param album_id: The id for Album you want to retrieve data.
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param return_json: Set to false will return a list of Album instances.
        Or return json data. Default is false.
        :return: Photo instance or dict
        """
        if fields is None:
            fields = constant.FB_ALBUM_BASIC_FIELDS

        args = {
            "fields": enf_comma_separated("fields", fields)
        }

        resp = self._request(
            method='GET',
            path='{0}/{1}'.format(self.version, album_id),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data
        else:
            return Album.new_from_json_dict(data)
Exemplo n.º 3
0
    def get_comments(self,
                     ids,  # type: Optional[Union[str, List, Tuple, Set]]
                     fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                     return_json=False  # type: bool
                     ):
        # type: (...) -> dict
        """
        Retrieve multi comments info by one request.
        :param ids: Comma-separated id(username) string for page which you want to get.
        You can also pass this with an id list, tuple, set.
        :param fields:Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param return_json: Set to false will return a dict of Comment instances.
        Or return json data. Default is false.
        """
        if fields is None:
            fields = constant.FB_COMMENT_BASIC_FIELDS.union(constant.FB_POST_REACTIONS_FIELD)

        args = {
            "ids": enf_comma_separated("ids", ids),
            "fields": enf_comma_separated("fields", fields)
        }
        resp = self._request(
            method='GET',
            path='{0}/'.format(self.version),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data
        else:
            return {_id: Comment.new_from_json_dict(p_data) for _id, p_data in iteritems(data)}
Exemplo n.º 4
0
    def get_comment_info(self,
                         comment_id,  # type: str
                         fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                         return_json=False  # type: bool
                         ):
        # type: (...) -> Union[Comment, dict]
        """
        Retrieve given comment's basic info.
        :param comment_id: The id for comment you want to retrieve data.
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param return_json: Set to false will return a list of Post instances.
        Or return json data. Default is false.
        """
        if fields is None:
            fields = constant.FB_COMMENT_BASIC_FIELDS

        args = {
            'fields': enf_comma_separated("fields", fields)
        }

        resp = self._request(
            method='GET',
            path='{0}/{1}'.format(self.version, comment_id),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data
        else:
            return Comment.new_from_json_dict(data)
Exemplo n.º 5
0
    def get_videos_by_object(self,
                             object_id,
                             fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                             filter_type="uploaded",  # type: Optional[str]
                             count=10,  # type: Optional[int]
                             limit=25,  # type: int
                             return_json=False  # type: bool
                             ):
        # type: (...) -> List[Union[Video, dict]]
        """
        Retrieve videos from object(such as page,user,group...)
        :param object_id: The id for object(post, photo..)
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param filter_type: The video type to query.
                valid parameters are:
                - uploaded
                - tagged
        :param count: The count will retrieve videos. If you want to get all data. Set it to None.
        :param limit: Each request retrieve posts count from api. For videos it should no more than 100.
        :param return_json: Set to false will return a list of Comment instances.
        Or return json data. Default is false.
        :return: Videos list.
        """
        if fields is None:
            fields = constant.FB_VIDEO_BASIC_FIELDS

        if count is not None:
            limit = min(count, limit)

        args = {
            'fields': enf_comma_separated("fields", fields),
            'type': filter_type,
            'limit': limit,
        }

        videos = []
        next_cursor = None

        while True:
            next_cursor, previous_cursor, data = self.paged_by_cursor(
                resource='videos',
                target=object_id,
                args=args,
                next_cursor=next_cursor
            )
            data = replace_from_keyword_in_json(data=data)
            if return_json:
                videos += data.get('data', [])
            else:
                videos += [Video.new_from_json_dict(item) for item in data.get('data', [])]
            if count is not None:
                if len(videos) >= count:
                    videos = videos[:count]
                    break
            if next_cursor is None:
                break
        return videos
Exemplo n.º 6
0
    def get_albums_by_object(self,
                             object_id,
                             fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                             count=10,  # type: Optional[int]
                             limit=25,  # type: int
                             return_json=False  # type: bool
                             ):
        # type: (...) -> List[Union[Album, dict]]
        """
        Retrieve a list of Albums on one object.
        :param object_id: The id for object(page..)
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param count: The count will retrieve videos. If you want to get all data. Set it to None.
        :param limit: Each request retrieve posts count from api. It should no more than 100.
        :param return_json: Set to false will return a list of Album instances.
        Or return json data. Default is false.
        :return: Albums list.
        """

        if fields is None:
            fields = constant.FB_ALBUM_BASIC_FIELDS

        if count is not None:
            limit = min(count, limit)

        args = {
            'fields': enf_comma_separated("fields", fields),
            'limit': limit,
        }

        albums = []
        next_cursor = None

        while True:
            next_cursor, previous_cursor, data = self.paged_by_cursor(
                resource='albums',
                target=object_id,
                args=args,
                next_cursor=next_cursor
            )
            data = replace_from_keyword_in_json(data=data)
            if return_json:
                albums += data.get('data', [])
            else:
                albums += [Album.new_from_json_dict(item) for item in data.get('data', [])]
            if count is not None:
                if len(albums) >= count:
                    albums = albums[:count]
                    break
            if next_cursor is None:
                break
        return albums
Exemplo n.º 7
0
    def get_pictures(self,
                     ids,  # type: Optional[Union[str, List, Tuple, Set]]
                     pic_type=None,  # type: Optional[str]
                     return_json=False  # type: bool
                     ):
        # type: (...) -> dict
        """
        :param ids: Comma-separated id(username) string for page which you want to get.
        You can also pass this with an id list, tuple, set.
        :param pic_type: The picture type.
        :param return_json: Set to false will return a dict of Comment instances.
        Or return json data. Default is false.
        """
        if pic_type is not None and pic_type not in constant.FB_PAGE_PICTURE_TYPE:
            raise PyFacebookException(ErrorMessage(
                code=ErrorCode.INVALID_PARAMS,
                message="For field picture: pic_type must be one of the following values: {}".format(
                    ', '.join(constant.FB_PAGE_PICTURE_TYPE)
                )))

        args = {
            "ids": enf_comma_separated("ids", ids),
            'redirect': 0,  # if set 0 the api will return json response.
            'type': 'normal' if pic_type is None else pic_type,
        }
        resp = self._request(
            method='GET',
            path='{0}/picture'.format(self.version),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        res = {}
        for _id, p_data in iteritems(data):
            picture_data = p_data["data"]
            if return_json:
                res[_id] = picture_data
            else:
                res[_id] = ProfilePictureSource.new_from_json_dict(picture_data)
        return res
Exemplo n.º 8
0
    def get_picture(self,
                    page_id,  # type: str
                    pic_type=None,  # type: Optional[str]
                    return_json=False  # type: bool
                    ):
        # type: (...) -> Union[ProfilePictureSource, dict]
        """
        Retrieve the page's picture.

        :param page_id: The id for picture you want to retrieve data.
        :param pic_type: The picture type.
        :param return_json: Set to false will return a dict of Comment instances.
        Or return json data. Default is false.
        """

        if pic_type is not None and pic_type not in constant.FB_PAGE_PICTURE_TYPE:
            raise PyFacebookException(ErrorMessage(
                code=ErrorCode.INVALID_PARAMS,
                message="For field picture: pic_type must be one of the following values: {}".format(
                    ', '.join(constant.FB_PAGE_PICTURE_TYPE)
                )))

        args = {
            'redirect': 0,  # if set 0 the api will return json response.
            'type': 'normal' if pic_type is None else pic_type,
        }

        resp = self._request(
            method='GET',
            path='{0}/{1}/picture'.format(self.version, page_id),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data['data']
        else:
            return ProfilePictureSource.new_from_json_dict(data['data'])
Exemplo n.º 9
0
    def get_caption_by_video_ids(self,
                                 ids,  # type: Optional[Union[str, List, Tuple, Set]]
                                 fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                                 return_json=False  # type: bool
                                 ):
        # type: (...) -> dict
        """
        Retrieves captions for multi videos.
        :param ids: Comma-separated id(username) string for page which you want to get.
        You can also pass this with an id list, tuple, set.
        Notice not more than 50.
        :param fields:Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param return_json: Set to false will return a dict of Comment instances.
        Or return json data. Default is false.
        :return: VideoCaptions dict.
        """
        if fields is None:
            fields = constant.FB_VIDEO_CAPTION_BASIC_FIELDS

        args = {
            "ids": enf_comma_separated("ids", ids),
            "fields": enf_comma_separated("fields", fields)
        }
        resp = self._request(
            method='GET',
            path='{0}/captions'.format(self.version),
            args=args
        )

        data = self._parse_response(resp)
        data = replace_from_keyword_in_json(data=data)

        if return_json:
            return data
        else:
            return {_id: [Video.new_from_json_dict(item) for item in p_data["data"]] for _id, p_data in iteritems(data)}
Exemplo n.º 10
0
    def get_comments_by_object_since_date(self,
                               object_id,  # type: str
                               summary=True,  # type: bool
                               fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                               # count=10,  # type: Optional[int]
                               limit=50,  # type: int
                               since_date_str=None,  # type:
                               ):
        # type: (...) -> (List[Union[Comment, dict]], Union[CommentSummary, dict])
        """
        Retrieve object's comments.

        Refer: https://developers.facebook.com/docs/graph-api/reference/v4.0/object/comments.

        :param object_id: The id for object(post, photo..)
        :param summary: The summary for comments
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param filter_type: Valid params are toplevel/stream,
                If you chose toplevel only return top level comment.
                stream will return parent and child comment.
                default is toplevel
        :param order_type: Valid params are chronological/reverse_chronological,
                If chronological, will return comments sorted by the oldest comments first.
                If reverse_chronological, will return comments sorted by the newest comments first.
        :param count: The count will retrieve posts. If you want to get all data. Set it to None.
        :param limit: Each request retrieve posts count from api. For posts it should no more than 100.
        :param return_json: Set to false will return a list of Comment instances.
        Or return json data. Default is false.
        """
        if fields is None:
            fields = constant.FB_COMMENT_BASIC_FIELDS

        if since_date_str is not None:
            since_date_arrow = arrow.get(since_date_str).to('utc')
        else:
            since_date_arrow = None

        args = {
            'fields': enf_comma_separated("fields", fields),
            'summary': summary,
            'filter': 'stream',
            'order': 'reverse_chronological',
            'limit': limit,
        }

        comments = []
        next_cursor = None

        while True:
            next_cursor, previous_cursor, data = self.paged_by_cursor(
                resource='comments',
                target=object_id,
                args=args,
                next_cursor=next_cursor
            )
            reached_until = False

            data = replace_from_keyword_in_json(data=data)
            this_iter_comments = [Comment.new_from_json_dict(item) for item in data.get('data', [])]

            for comment in this_iter_comments:
                created_time_arrow = arrow.get(comment.created_time).to('utc')
                if created_time_arrow < since_date_arrow:
                    reached_until = True
                    break
                comments.append(comment)

            comment_summary = CommentSummary.new_from_json_dict(data.get('summary', {}))

            if next_cursor is None:
                break
            if reached_until:
                break
        return comments, comment_summary
Exemplo n.º 11
0
    def get_page_feeds(self,
                       page_id,  # type: str
                       fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
                       resource=None,  # type: Optional[str]
                       since_time=None,  # type: str
                       until_time=None,  # type: str
                       count=10,  # type: Optional[int]
                       limit=10,  # type: int
                       access_token=None,  # type: str
                       return_json=False  # type: bool
                       ):
        # type: (...) -> List[Union[Post, dict]]
        """
        Retrieve data for give page's posts info.

        Refer: https://developers.facebook.com/docs/graph-api/reference/v4.0/page/feed

        :param page_id: The id(username) for page you want to retrieve data.
        :param fields: Comma-separated id string for data fields which you want.
        You can also pass this with an id list, tuple, set.
        :param resource: The data endpoint type, may have posts,feed,tagged,published_posts.
        :param since_time: A Unix timestamp that points to the start of the range of time-based data.
        :param until_time: A Unix timestamp that points to the end of the range of time-based data.
        :param count: The count will retrieve posts. If you want to get all data. Set it to None.
        :param limit: Each request retrieve posts count from api. For posts it should no more than 100.
        :param access_token: If you want to pass with own access token, can with this parameter.
        :param return_json: Set to false will return a list of Post instances.
        Or return json data. Default is false.
        :return:
        """
        if resource is None:
            resource = 'feed'

        if fields is None:
            fields = constant.FB_POST_BASIC_FIELDS.union(constant.FB_POST_REACTIONS_FIELD)

        args = {
            'fields': enf_comma_separated("fields", fields),
            'since': since_time,
            'until': until_time,
            'limit': limit,
        }

        if access_token is not None:
            args['access_token'] = access_token

        posts = []
        next_cursor = None

        while True:
            next_cursor, previous_cursor, data = self.paged_by_cursor(
                resource=resource,
                target=page_id,
                args=args,
                next_cursor=next_cursor,
            )

            data = replace_from_keyword_in_json(data=data)

            if return_json:
                posts += data.get('data', [])
            else:
                posts += [Post.new_from_json_dict(item) for item in data['data']]
            if count is not None:
                if len(posts) >= count:
                    posts = posts[:count]
                    break
            if next_cursor is None:
                break
        return posts