pythongoogle-api-python-clientgoogle-latitude

Google Latitude API returns 0 results for location history


I'm trying to retrieve my location history using v1 (via OAuth 2.0) of the Google Latitude API, specifically the list method.

When attempting to retrieve records between two timestamps I do not get any locations back. However, I can retrieve my most recent locations (~860, only in the last 24 hours). Trying to retrieve locations within the same time periods as those returned in the "recent" set also returns zero results.

Does anyone know if I'm doing something wrong here, or if the issue lies in Latitude? Can I retrieve my location history in this way?

Resolved: specified start and end timestamps need to be in milliseconds

Here are some functions I used to help translate datetime objects to milliseconds:

def dt_from_epoch(epoch):
    epoch = int(epoch)
    secs = epoch/1000
    mils = epoch - (secs*1000)

    dt = datetime.datetime.fromtimestamp(secs)
    dt.replace(microsecond=mils*1000)
    return dt

def dt_to_mils(dt):
    return int((time.mktime(dt.timetuple())*1000) + (dt.microsecond/1000))

Original examples

Test case script:

# Service obtained in the usual OAuth 2.0 way using oauth2client and
# apiclient.discovery.build.
# Scope used: https://www.googleapis.com/auth/latitude.all.best

print "## Retrieve most recent history"
locs = service.location().list(granularity='best', max_results=1000).execute()
first = locs['items'][len(locs['items'])-1]
last= locs['items'][0]

first = datetime.datetime.fromtimestamp(int(first['timestampMs'][:-3]))
last = datetime.datetime.fromtimestamp(int(last['timestampMs'][:-3]))

print "%s - %s: %d" % (first.strftime('%Y-%m-%d %H:%M:%S'),
    last.strftime('%Y-%m-%d %H:%M:%S'), len(locs['items']))


print "## Retrieve history between %s and %s" % (
    first.strftime('%Y-%m-%d %H:%M:%S'), last.strftime('%Y-%m-%d %H:%M:%S'))

locs = service.location().list(
    granularity='best',
    min_time=int(time.mktime(first.timetuple())),
    max_time=int(time.mktime(last.timetuple())),
    max_results=1000
).execute()

results = len(locs['items']) if 'items' in locs else 0
print "%s - %s: %d" % (first.strftime('%Y-%m-%d %H:%M:%S'),
    last.strftime('%Y-%m-%d %H:%M:%S'), results)

Script output:

## Retrieve most recent history
2012-08-25 23:02:12 - 2012-08-26 15:37:09: 846
## Retrieve history between 2012-08-25 23:02:12 and 2012-08-26 15:37:09
2012-08-25 23:02:12 - 2012-08-26 15:37:09: 0

JSON output of "most recent history":

{
  "data": {
    "kind": "latitude#locationFeed",
    "items": [
      {
        "kind": "latitude#location",
        "timestampMs": "1345995443316",
        "latitude": (latitude),
        "longitude": (longitude)
      },
    (continues...)
    ]
  }
}

JSON output of "history between X and Y":

{
 "data": {
    "kind": "latitude#locationFeed"
  }
}

Solution

  • Looks like you're passing in seconds instead of milliseconds (since epoch). Specifically, note how you're dividing by 1000 with your [:-3]...