This returns the minute for which the results matching your query are greatest


In [ ]:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import base64
import json
import xml
import sys
import pprint
pp = pprint.PrettyPrinter(indent=4)

In [ ]:
class RequestWithMethod(urllib2.Request):
    def __init__(self, url, method, headers={}):
        self._method = method
        urllib2.Request.__init__(self, url, headers)

    def get_method(self):
        if self._method:
            return self._method
        else:
            return urllib2.Request.get_method(self) 

if __name__ == "__main__":

	url = 'https://search.gnip.com/accounts/Periscopic/search/prod/counts.json'
    #username is typically an email address
    UN = 'email@email.com'
	PWD = 'password'
	query = "venezuela&fromDate=201403030100&toDate=201403030300&bucket=minute"

	queryString = url + '?publisher=twitter&query=' + query

	base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '')

	req = RequestWithMethod(queryString, 'GET')
	req.add_header("Authorization", "Basic %s" % base64string)

	try:
		response = urllib2.urlopen(req)
	except urllib2.HTTPError as e:
            print e.read()

	result = response.read()
	json_result = json.loads(result)

	twit_dicts = json_result['results']
	max_value = {'count':0}
	for item in twit_dicts:
		if item['count'] > max_value['count']:
			max_value = item
	print result
	# print max_value