Instagram图表api媒体发布日期之间的帖子

时间:2018-05-21 11:19:57

标签: python python-3.x facebook-graph-api instagram instagram-api

我尝试使用'since''until'从我管理的Instagram商家资料中检索上个月的媒体帖子,但它似乎并不是当API返回超出我选择的时间范围的帖子时,工作正常。

我使用以下字符串来调用API:

business_profile_id/media?fields=timestamp&since=2018-04-01&until=2018-04-30

虽然Python片段是这样的(使用来自facebook-python-sdk的相同init脚本)

import facebook

graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)    
posts = graph.get_connections(profile['id'], 'media?fields=caption,permalink,timestamp&since=2018-04-01&until=2018-04-30')

其中get.connections是

def get_connections(self, id, connection_name, **args):
    """Fetches the connections for given object."""
    return self.request(
        "{0}/{1}/{2}".format(self.version, id, connection_name), args)

和请求

def request(
        self, path, args=None, post_args=None, files=None, method=None):
    """Fetches the given path in the Graph API.
    We translate args to a valid query string. If post_args is
    given, we send a POST request to the given path with the given
    arguments.
    """
    if args is None:
        args = dict()
    if post_args is not None:
        method = "POST"

    # Add `access_token` to post_args or args if it has not already been
    # included.
    if self.access_token:
        # If post_args exists, we assume that args either does not exists
        # or it does not need `access_token`.
        if post_args and "access_token" not in post_args:
            post_args["access_token"] = self.access_token
        elif "access_token" not in args:
            args["access_token"] = self.access_token

    try:
        response = self.session.request(
            method or "GET",
            FACEBOOK_GRAPH_URL + path,
            timeout=self.timeout,
            params=args,
            data=post_args,
            proxies=self.proxies,
            files=files)
    except requests.HTTPError as e:
        response = json.loads(e.read())
        raise GraphAPIError(response)

    headers = response.headers
    if 'json' in headers['content-type']:
        result = response.json()
    elif 'image/' in headers['content-type']:
        mimetype = headers['content-type']
        result = {"data": response.content,
                  "mime-type": mimetype,
                  "url": response.url}
    elif "access_token" in parse_qs(response.text):
        query_str = parse_qs(response.text)
        if "access_token" in query_str:
            result = {"access_token": query_str["access_token"][0]}
            if "expires" in query_str:
                result["expires"] = query_str["expires"][0]
        else:
            raise GraphAPIError(response.json())
    else:
        raise GraphAPIError('Maintype was not text, image, or querystring')

    if result and isinstance(result, dict) and result.get("error"):
        raise GraphAPIError(result)
    return result

基本上我想在一段时间内获得帖子,然后获取每个帖子的见解。

之前有没有人遇到过这个问题?

2 个答案:

答案 0 :(得分:2)

不幸的是,此端点不支持sinceuntil参数,并且该端点仅支持基于游标的分页。要做我想做的唯一方法是使用API​​响应中提供的beforeafter游标分别加载结果的每一页。

答案 1 :(得分:0)

对于您的任务,我建议您不要使用InstagramAPI库。我将使用instabot库为您展示一个简单的解决方案。对于该库的pip安装,请使用以下命令:

pip install instabot

使用以下python代码在指定的日期范围内获取媒体。

import datetime
from instabot import Bot
bot = Bot()
bot.login(username="YOUR USERNAME", password="YOUR PASSWORD")

def get_media_posts(start_date, end_date):
   all_posts = bot.get_your_medias()
   filtered_posts = []

    for post in all_posts:
        post_info = bot.get_media_info(post) #the media info for the post
        post_timestamp = post_info[0].get('taken_at') #get the timestamp of the post
        post_date = datetime.datetime.fromtimestamp(post_timestamp).date() #convert timestamp to date

        if post_date >= start_date and post_date <= end_date:
            filtered_posts.append(post) #or you can also use: filtered_posts.append(post_info)

    return filtered_posts

这将返回指定日期内所有帖子的列表,您可以使用bot.get_media_info(post)查看每个帖子中的内容。

注意:根据此代码,开始日期和结束日期应为date()(而不是日期时间)格式,但您可以将其与所需的任何日期时间函数进行比较:)