-
Notifications
You must be signed in to change notification settings - Fork 0
/
Appfunctions.py
64 lines (49 loc) · 1.74 KB
/
Appfunctions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import configuration
import generalfunctions
from datetime import date
import hashlib
import json
import requests
import time
# uncomment these to make debugging easier.
# print ('api key: ' + api_key);
# print ('api secret: ' + api_secret);
# print ('query: ' + query);
# print ('url: ' + url);
# the api follows the Amazon style authentication
# see https://docs.aws.amazon.com/AmazonS3/latest/dev/S3_Authentication2.html
def request_header():
# setup some basic vars for the search api.
# for more information, see https://api.podcastindex.org/developer_docs
global config
api_key = configuration.config["podcastindex"]["key"]
api_secret = configuration.config["podcastindex"]["secret"]
# we'll need the unix time
epoch_time = int(time.time())
# our hash here is the api key + secret + time
data_to_hash = api_key + api_secret + str(epoch_time)
# which is then sha-1'd
sha_1 = hashlib.sha1(data_to_hash.encode()).hexdigest()
# now we build our request headers
headers = {
'X-Auth-Date': str(epoch_time),
'X-Auth-Key': api_key,
'Authorization': sha_1,
'User-Agent': 'postcasting-index-python-cli'
}
return headers
def request(url, log_path):
try:
result = None
# perform the actual post request
#r = requests.post(url, headers=request_header())
r = requests.get(url)
# dump the contents (in a prettified json-format)
result = json.loads(r.text)
except Exception as e:
message = 'App API call: ' + str(e)
generalfunctions.log(log_path, message, True, False)
message = 'URL: \'' + str(url) + '\''
generalfunctions.log(log_path, message, True, False)
return result