コード例 #1
0
ファイル: api.py プロジェクト: shizacat/python-facebook
    def get_post_info(self, post_id=None, return_json=False):
        """
        Obtain give page's basic info.

        Args:
            post_id (str)
                The id for you want to retrieve post.
            return_json (bool, optional):
                If True JSON data will be returned, instead of pyfacebook.Post, or return origin data by facebook.

        Returns:
            post info.
        """
        if post_id is None:
            raise PyFacebookError({'message': "Must specify the post id"})

        args = {
            'fields':
            ','.join(
                set(constant.POST_BASIC_FIELDS +
                    constant.POST_REACTIONS_FIELD))
        }
        resp = self._request(method='GET',
                             path='{0}/{1}'.format(self.version, post_id),
                             args=args)

        data = self._parse_response(resp.content.decode('utf-8'))
        if return_json:
            return data
        else:
            return Post.new_from_json_dict(data)
コード例 #2
0
    def get_post_info(
        self,
        post_id,  # type: str
        fields=None,  # type: Optional[Union[str, List, Tuple, Set]]
        return_json=False  # type: bool
    ):
        # type: (...) -> Optional[Post, dict]
        """
        Obtain give post's basic info.
        :param post_id: The id for post 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_POST_BASIC_FIELDS.union(
                constant.FB_POST_REACTIONS_FIELD)

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

        data = self._parse_response(resp)
        if return_json:
            return data
        else:
            return Post.new_from_json_dict(data)
コード例 #3
0
    def get_posts(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 posts 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 Page instances.
        Or return json data. Default is false.
        """
        if fields is None:
            fields = constant.FB_POST_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)
        if return_json:
            return data
        else:
            return {_id: Post.new_from_json_dict(p_data) for _id, p_data in iteritems(data)}
コード例 #4
0
ファイル: api.py プロジェクト: shizacat/python-facebook
    def get_feeds(self,
                  resource=None,
                  page_id=None,
                  username=None,
                  since_time=None,
                  until_time=None,
                  count=10,
                  limit=10,
                  access_token=None,
                  return_json=False):
        """
        Obtain give page's posts info.
        Refer: https://developers.facebook.com/docs/graph-api/reference/v4.0/page/feed
        Args:
            resource (str, optional)
                The connection resource for you want to do.
                Now have four: feed, posts, tagged, published_posts. if you not pointed. use feed.
                Notice: the tagged and published_posts resource need page access_token.
            page_id (int, optional)
                The id for you want to retrieve data
            username (str, optional)
                The username (page username) for you want to retrieve data
                Either page_id or username is required. if all given. use username.
            since_time (str, optional)
                The posts retrieve begin time.
            until_time ()
                The posts retrieve until time.
                If neither since_time or until_time, it will by now time.
            count (int, optional)
                The count will retrieve posts.
            limit (int, optional)
                Each request retrieve posts count from api.
                For posts it should no more than 100.
            access_token (str, optional):
                If you want use other token to get data. you can point this.
            return_json (bool, optional):
                If True JSON data will be returned, instead of pyfacebook.Post, or return origin data by facebook.
        Returns:
            posts info list.
        :return:
        """
        if resource is None:
            resource = 'feed'
        if page_id:
            target = page_id
        elif username:
            target = username
        else:
            raise PyFacebookError(
                {'message': "Specify at least one of page_id or username"})

        args = {
            'fields':
            ','.join(
                set(constant.POST_BASIC_FIELDS +
                    constant.POST_REACTIONS_FIELD)),
            '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=target,
                args=args,
                next_cursor=next_cursor,
            )
            if return_json:
                posts += data.get('data', [])
            else:
                posts += [
                    Post.new_from_json_dict(item) for item in data['data']
                ]
            if next_cursor is None:
                break
            if len(posts) >= count:
                break
        return posts[:count]
コード例 #5
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,
            )
            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
コード例 #6
0
ファイル: api.py プロジェクト: ffmpegd/python-facebook
    def get_posts(self,
                  page_id=None,
                  username=None,
                  since_time=None,
                  until_time=None,
                  count=10,
                  limit=10,
                  return_json=False):
        """
        Obtain give page's posts info.

        Args:
            page_id (int, optional)
                The id for you want to retrieve data
            username (str, optional)
                The username (page username) for you want to retrieve data
                Either page_id or username is required. if all given. use username.
            since_time (str, optional)
                The posts retrieve begin time.
            until_time ()
                The posts retrieve until time.
                If neither since_time or until_time, it will by now time.
            count (int, optional)
                The count will retrieve posts.
            limit (int, optional)
                Each request retrieve posts count from api.
                For posts it should no more than 100.
            return_json (bool, optional):
                If True JSON data will be returned, instead of pyfacebook.Post, or return origin data by facebook.
        Returns:
            posts info list.
        """
        if page_id:
            target = page_id
        elif username:
            target = username
        else:
            raise PyFacebookError(
                {'message': "Specify at least one of page_id or username"})

        args = {
            'fields':
            ','.join(
                set(constant.POST_BASIC_FIELDS +
                    constant.POST_REACTIONS_FIELD)),
            'since':
            since_time,
            'until':
            until_time,
            'limit':
            limit,
        }

        posts = []
        next_page = None

        while True:
            next_page, previous_page, data = self.paged_by_next(
                resource='posts',
                target=target,
                next_page=next_page,
                args=args)
            if return_json:
                posts += data.get('data', [])
            else:
                posts += [
                    Post.new_from_json_dict(item) for item in data['data']
                ]
            if next_page is None:
                break
            if len(posts) >= count:
                break
        return posts[:count]