예제 #1
0
    def get_media_info(self, media_id=None, return_json=False):
        """
        Obtain given media's info. Must the media is the token's owner's.
        Others can not get data by this method.

        Args:
            media_id (str)
                The id for you want to retrieve media.
            return_json (bool, optional):
                If True origin data by facebook will be returned, or will return pyfacebook.InstagramMedia.
        Returns:
            media basic data.
        """
        if media_id is None:
            raise PyFacebookError({'message': "Must specify the media id"})
        args = {
            'fields':
            ','.join(constant.INSTAGRAM_MEDIA_PUBLIC_FIELD +
                     constant.INSTAGRAM_MEDIA_OWNER_FIELD)
        }

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

        data = self._parse_response(resp.content.decode('utf-8'))
        if return_json:
            return data
        else:
            return InstagramMedia.new_from_json_dict(data)
예제 #2
0
    def get_medias(self,
                   username=None,
                   since_time=None,
                   until_time=None,
                   count=10,
                   limit=5,
                   return_json=False):
        """
        Obtain given user's media.
        If username not provide, will return the instagram business account's media.

        Args:
            username (str)
                the user you want to retrieve data. If not provide. use the business account.
            since_time (str, optional)
                The medias retrieve begin time.
            until_time ()
                The media retrieve until time.
                If neither since_time or until_time, it will by now time.
            count (int, optional)
                The count is you want to retrieve medias.
            limit (int, optional)
                The count each request get the result count. default is 5.
            return_json (bool, optional):
                If True origin data by facebook will be returned, or will return pyfacebook.InstagramMedia list

        Returns:
            media data list.
        """
        if username is None:
            owner = True
            args = {
                'fields':
                ','.join(constant.INSTAGRAM_MEDIA_OWNER_FIELD +
                         constant.INSTAGRAM_MEDIA_PUBLIC_FIELD),
                'limit':
                limit,
            }
        else:
            # notice:
            # this args is to provide origin data to paged methods.
            owner = False
            args = {
                'limit':
                limit,
                'username':
                username,
                'fields':
                'business_discovery.username({username}){{media.limit({limit}){{{fields}}}}}'
                .format(username=username,
                        limit=limit,
                        fields=','.join(constant.INSTAGRAM_MEDIA_PUBLIC_FIELD))
            }

        result = []
        next_cursor = None

        while True:
            next_cursor, previous_cursor, medias = self.get_media_paged(
                args=args,
                since_time=since_time,
                until_time=until_time,
                next_cursor=next_cursor,
                owner=owner)
            if return_json:
                result += medias
            else:
                result += [
                    InstagramMedia.new_from_json_dict(item) for item in medias
                ]
            if next_cursor is None:
                break
            if len(result) >= count:
                result = result[:count]
                break
        return result