anupam
anupam

Reputation: 91

instagram media search timestamp issues

I am using instagram's media search api

https://api.instagram.com/v1/media/search?lat=8.51&lng=115.239394&MAX_TIMESTAMP=xyz&MIN_TIMESTAMP=abc&client_id=my_client_id

Now I am making calls to this api under a for-loop and changing the value of min and max timestamps in each iteration. So that I get images for that timeframe.

The issue I am facing is I get same result (or set of images) in each iteration. So changing timestamps has no effect on the result.

Is it because of caching? or Instagram does not allow requests in iteration like this? Or there is some other way e.g. pagination or something to make such requests?

Here is the code (in python):

import urllib2
import datetime
import time
import json

clientId = 'ef63de4634b344e3856b4f4138a8db56'
city = "Bali"
baliLat = '8.51'
baliLong = '115.239394'
distance = 1000

def callApi(startStamp, endStamp):
    searchStr = 'https://api.instagram.com/v1/media/search?lat=' + baliLat + '&lng=' + baliLong + '&client_id=' + clientId + '&MAX_TIMESTAMP=' + str(endStamp) + '&MIN_TIMESTAMP=' + str(startStamp) + '&distance='  + str(distance) + '&count=10'
    print searchStr
    response = urllib2.urlopen(searchStr)
    res = json.loads(response.read())
    return res

if __name__ == "__main__":
    # call instagram api for 1 year for each month for 1st week.
    # compare the dates of creation for each month.
    start = '1/05/2014'
    startDate = datetime.datetime.strptime(start, '%d/%m/%Y')
    count = 0
    result = {}
    while count < 12:
        endDate = startDate + datetime.timedelta(days=7)
        minStamp = time.mktime(startDate.timetuple())
        maxStamp = time.mktime(endDate.timetuple())
        res = callApi(minStamp, maxStamp)
        result[str(startDate)] = res
        startDate = startDate + datetime.timedelta(days=30)
        count += 1
    f = open('out-y.json', 'w')
    f.write(json.dumps(result))
    f.close()

Upvotes: 1

Views: 2454

Answers (3)

Yuval Adam
Yuval Adam

Reputation: 165212

Instagram's API uses timestamps in seconds, not milliseconds. Divide all your timestamps by 1000.

Upvotes: 0

Cody Kestigian
Cody Kestigian

Reputation: 11

I was struggling with the same issue over the last week and I think my problems stemmed from time.mktime() - this assumes that dates are set to the local timezone, rather than the UTC/GMT times that the Instagram data is based on. Try replacing all your time.mktime() calls with calendar.timegm() and see if that does the trick - seemed to fix the issue for me.

Upvotes: 0

krisrak
krisrak

Reputation: 12952

MAX_TIMESTAMP and MIN_TIMESTAMP should be lower case: max_timestamp and min_timestamp

the api documentation on Instagram shows params in upper case, its misleading, it should be all lower case.

Upvotes: 2

Related Questions