Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Historical API examples follow PEP8 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions Historical PowerTrack/Python/AcceptRejectHistoricalJob.py
Original file line number Diff line number Diff line change
@@ -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()
85 changes: 51 additions & 34 deletions Historical PowerTrack/Python/CreateHistoricalJob2.0.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 16 additions & 21 deletions Historical PowerTrack/Python/MonitorJobStatus.py
Original file line number Diff line number Diff line change
@@ -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={}):
Expand All @@ -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)
14 changes: 8 additions & 6 deletions Historical PowerTrack/Python/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h1>Historical PowerTrack API Requests</h1>
<h2>Python Examples</h2>
<p>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
<p>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