-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.py
38 lines (25 loc) · 999 Bytes
/
query.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
import base64
import ConfigParser
import hmac
import json
import requests
from hashlib import sha256
from datetime import datetime
config = ConfigParser.ConfigParser()
config.read('config.ini')
channel_id = config.get('config', 'channel_id')
api_key_id = config.get('config', 'api_key_id')
api_key_secret = config.get('config', 'api_key_secret')
url = 'https://news-api.apple.com/channels/%s' % channel_id
path = raw_input('Enter path: ')
url += path
date = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
canonical_request = 'GET' + url + str(date)
key = base64.b64decode(api_key_secret)
hashed = hmac.new(key, canonical_request, sha256)
signature = hashed.digest().encode("base64").rstrip('\n')
authorization = 'HHMAC; key=%s; signature=%s; date=%s' % (api_key_id, str(signature), date)
headers = {'Authorization': authorization}
response = requests.get(url, headers=headers)
data = json.loads(response.text)
print(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))