Dan
Dan

Reputation: 451

Instagram graph api media posts between dates

I'm trying to retrieve last month's media posts from an Instagram Business profile I manage, by using 'since' and 'until', but it doesn't seem to work properly as the API returns posts which are out of the time range I selected.

I'm using the following string to call the API:

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

while the Python snippet would be this (using the same init script from the facebook-python-sdk)

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')

where get.connections is

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)

and request is

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

Basically I'd like to get posts for a certain period and then get insights for each one.

Has anyone encountered this problem before?

Upvotes: 9

Views: 5342

Answers (2)

za_ali33
za_ali33

Reputation: 436

For your task, I would recommend you to not use InstagramAPI library. I will show you a simple solution for this using instabot library. For pip installation of this library, use this command:

pip install instabot

Use the following python code to get the media within the specified date range.

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

This will return you a list of all the posts within the specified date and you can use the bot.get_media_info(post) to see what is inside every post.

NOTE: start_date and end_date should be in date() (and not in datetime) format according to this code but you can compare with whatever datetime function you want :)

Upvotes: 0

Dan
Dan

Reputation: 451

Unfortunately the since and until parameters are not supported on this endpoint and this endpoint has only support cursor based pagination. The only way to do what I wish to do is to load each page of results individually using the before and after cursors provided in the API response.

Upvotes: 5

Related Questions