forked from ALADEGAZALIY/python-enterprise-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_accept_historical_powertrack_sample.py
49 lines (40 loc) · 1.56 KB
/
python_accept_historical_powertrack_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Sample Python Script for Creating a New Job with the Historical PowerTrack Search API (with Python 3)
import base64
import json
import urllib.request
import urllib.parse
# Insert Gnip Console Username and Password Below
UN = ''
PWD = ''
# Insert Your Account Name and Stream Label Into the URL
base_url = 'https://gnip-api.gnip.com/historical/powertrack/accounts/<INSERT_ACCOUNT_NAME_HERE>/publishers/twitter/jobs/<JOB_UUID>.json'
headers = {"status":"accept"}
# Create URL Structure
class RequestWithMethod(urllib.request.Request):
def __init__(self, base_url, method, headers={}):
self._method = method
urllib.request.Request.__init__(self, base_url, headers)
def get_method(self):
if self._method:
return self._method
else:
return urllib.request.Request.get_method(self)
# Create Endpoint & Add Credentials
def accept_job_endpoint():
base64string = ('%s:%s' % (UN, PWD)).replace('\n', '')
base = base64.b64encode(base64string.encode('ascii'))
final_url = urllib.request.Request(url=base_url)
final_url.add_header('Authorization', 'Basic %s' % base.decode('ascii'))
return final_url
# Create the Endpoint
job_endpoint = accept_job_endpoint()
# Take in the Endpoint and Make the Request
def make_request(job_endpoint):
try:
response = urllib.request.urlopen(job_endpoint)
response_data = response.read()
print("RESPONSE: %s:" % response_data)
except urllib.request.HTTPError as error:
print("ERROR: %s" % error)
# Make the Request
make_request(job_endpoint)