diff --git a/Historical PowerTrack/Python/AcceptRejectHistoricalJob.py b/Historical PowerTrack/Python/AcceptRejectHistoricalJob.py index 89c682f..93985c3 100755 --- a/Historical PowerTrack/Python/AcceptRejectHistoricalJob.py +++ b/Historical PowerTrack/Python/AcceptRejectHistoricalJob.py @@ -1,33 +1,34 @@ -#!/usr/bin/env python +# !/usr/bin/env python import urllib2 import base64 import json -import sys + def post(): - url = 'ENTER_HISTORICAL_JOB_URL' - UN = 'ENTER_USERNAME' - PWD = 'ENTER_PASSWORD' + url = 'ENTER_HISTORICAL_JOB_URL' + username = 'ENTER_USERNAME' + password = 'ENTER_PASSWORD' - choice = 'accept' # Switch to 'reject' to reject the job. + # Switch to 'reject' to reject the job. + choice = 'accept' - payload = '{"status":"' + choice + '"}' + payload = { + "status": choice + } + base64string = base64.encodestring("{0}:{1}".format(username, password)) + req = urllib2.Request(url=url, data=json.dumps(payload)) + req.add_header('Content-type', 'application/json') + req.add_header("Authorization", "Basic {}".format(base64string.strip())) + req.get_method = lambda: 'PUT' - base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '') - req = urllib2.Request(url=url, data=payload) - req.add_header('Content-type', 'application/json') - req.add_header("Authorization", "Basic %s" % base64string) - req.get_method = lambda: 'PUT' - - try: - response = urllib2.urlopen(req) - except urllib2.HTTPError as e: - print e.read() - the_page = response.read() - print the_page + try: + response = urllib2.urlopen(req) + print(response.read()) + except urllib2.HTTPError as e: + print(e) -if __name__ == "__main__": + if __name__ == "__main__": post() diff --git a/Historical PowerTrack/Python/CreateHistoricalJob2.0.py b/Historical PowerTrack/Python/CreateHistoricalJob2.0.py old mode 100644 new mode 100755 index 34b9d52..84d1ab9 --- a/Historical PowerTrack/Python/CreateHistoricalJob2.0.py +++ b/Historical PowerTrack/Python/CreateHistoricalJob2.0.py @@ -1,40 +1,57 @@ #!/usr/bin/env python - import urllib2 import base64 import json -import sys - -def post(): - - UN = 'ENTER_USERNAME' - PWD = 'ENTER_PASSWORD' - account = 'ENTER_ACCOUNT_NAME' - - url = 'https://gnip-api.gnip.com/historical/powertrack/accounts/' + account + '/publishers/twitter/jobs.json' - publisher = "Twitter" - streamType = "track_v2" - dataFormat = "activity-streams" - fromDate = "201301010000" # This time is inclusive -- meaning the minute specified will be included in the data returned - toDate = "201301010601" # This time is exclusive -- meaning the data returned will not contain the minute specified, but will contain the minute immediately preceding it - jobTitle = "test-job-python1" - rules = [{"value":"rule 1","tag":"ruleTag"},{"value":"rule 2","tag":"ruleTag"}] - - job = {"publisher":publisher,"streamType":streamType,"dataFormat":dataFormat,"fromDate":fromDate,"toDate":toDate,"title":jobTitle,"rules":rules} - jobString = json.dumps(job) - print(jobString) - - base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '') - req = urllib2.Request(url=url, data=jobString) - req.add_header('Content-type', 'application/json') - req.add_header("Authorization", "Basic %s" % base64string) - - try: - response = urllib2.urlopen(req) - the_page = response.read() - print the_page - except urllib2.HTTPError as e: - print str(e) + + +def create_job(): + + username = 'ENTER_USERNAME' + password = 'ENTER_PASSWORD' + account = 'ENTER_ACCOUNT_NAME' + + url = 'https://gnip-api.gnip.com/historical/powertrack/accounts/{}/publishers/twitter/jobs.json'.format(account) + publisher = "Twitter" + stream_type = "track_v2" + data_format = "activity-streams" + job_title = "test-job-python1" + + # This time is inclusive + # meaning the minute specified will be included in the data returned + from_date = "201301010000" + + # This time is exclusive + # meaning the data returned will not contain the minute specified, + # but will contain the minute immediately preceding it + to_date = "201301010601" + rules = [ + {"value": "rule 1", + "tag": "ruleTag "}, + {"value": "rule 2", + "tag": "ruleTag"} + ] + + job = { + "publisher": publisher, + "streamType": stream_type, + "dataFormat": data_format, + "fromDate": from_date, + "toDate": to_date, + "title": job_title, + "rules": rules + } + + base64string = base64.encodestring("{0}:{1}".format(username, password)) + + req = urllib2.Request(url=url, data=json.dumps(job)) + req.add_header("Content-type", "application/json") + req.add_header("Authorization", "Basic {}".format(base64string.strip())) + + try: + response = urllib2.urlopen(req) + print(response.read()) + except urllib2.HTTPError as e: + print(str(e)) if __name__ == "__main__": - post() + create_job() diff --git a/Historical PowerTrack/Python/MonitorJobStatus.py b/Historical PowerTrack/Python/MonitorJobStatus.py index e539a94..b5f4fe3 100755 --- a/Historical PowerTrack/Python/MonitorJobStatus.py +++ b/Historical PowerTrack/Python/MonitorJobStatus.py @@ -1,9 +1,7 @@ #!/usr/bin/env python - import urllib2 import base64 -import json -import sys + class RequestWithMethod(urllib2.Request): def __init__(self, url, method, headers={}): @@ -14,24 +12,21 @@ def get_method(self): if self._method: return self._method else: - return urllib2.Request.get_method(self) + return urllib2.Request.get_method(self) if __name__ == "__main__": - - url = 'ENTER_HISTORICAL_JOB_URL' - UN = 'ENTER_USERNAME' - PWD = 'ENTER_PASSWORD' + url = 'ENTER_HISTORICAL_JOB_URL' + username = 'ENTER_USERNAME' + password = 'ENTER_PASSWORD' + + base64string = base64.encodestring("{0}:{1}".format(username, password)) + + req = RequestWithMethod(url, 'GET') + req.add_header('Content-type', 'application/json') + req.add_header("Authorization", "Basic {}".format(base64string.strip())) - base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '') - - req = RequestWithMethod(url, 'GET') - req.add_header('Content-type', 'application/json') - req.add_header("Authorization", "Basic %s" % base64string) - - try: - response = urllib2.urlopen(req) - except urllib2.HTTPError as e: - print e.read() - - the_page = response.read() - print the_page + try: + response = urllib2.urlopen(req) + print(response.read()) + except urllib2.HTTPError as e: + print(e) diff --git a/Historical PowerTrack/Python/README.md b/Historical PowerTrack/Python/README.md index 6ff5bd2..2614f97 100644 --- a/Historical PowerTrack/Python/README.md +++ b/Historical PowerTrack/Python/README.md @@ -1,7 +1,9 @@ -
The following Python snippets demonstrate how to create and monitor jobs on the the Gnip Historical PowerTrack API. HPT 2.0 has a separate file for creating a job, however the accept/reject and monitoring samples will work for both HPT 1.0 and HPT 2.0 -
CreateHistoricalJob: To run, replace credentials and replace {"value":"rule 1","tag":"ruleTag"},{"value":"rule 2","tag":"ruleTag"} with each rule and rule tag for testing. +# Historical PowerTrack API Requests +## Python Examples -python CreateHistoricalJob.py -python CreateHistoricalJob2.0.py +The following Python snippets demonstrate how to create and monitor jobs on the the Gnip Historical PowerTrack API. HPT 2.0 has a separate file for creating a job, however the accept/reject and monitoring samples will work for both HPT 1.0 and HPT 2.0 + +CreateHistoricalJob: To run, replace credentials and replace {"value":"rule 1","tag":"ruleTag"},{"value":"rule 2","tag":"ruleTag"} with each rule and rule tag for testing. + + python CreateHistoricalJob.py + python CreateHistoricalJob2.0.py