-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (56 loc) · 2.5 KB
/
Copy pathmain.py
File metadata and controls
86 lines (56 loc) · 2.5 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import csv
import datetime
import hmac, hashlib
import requests, yaml
from flask import Flask, request
app = Flask(__name__)
# Load Settings
with open('./settings.yml', encoding="utf8") as file:
settings = yaml.load(file, Loader=yaml.FullLoader)
def get_resource_price(resource_id):
r = requests.get(f'https://api.polymart.org/v1/getResourceInfo?api_key={settings["polymart_api_key"]}&resource_id={resource_id}')
data = r.json()
if r.status_code != 200:
return None
return data['response']['resource']['price']
def verify_request(request_data, polymart_signature):
for webhook_secret in settings['webhook_secrets']:
# Form the request signature
hmac_object = hmac.new(bytes(webhook_secret, 'utf-8'), request_data, hashlib.sha256)
request_signature = hmac_object.hexdigest()
if polymart_signature == request_signature:
return True
return False
@app.route('/webhook', methods=['POST'])
def webhook():
if not verify_request(request_data=request.data, polymart_signature=request.headers["X-Polymart-Signature"]):
return 'Unauthorised', 403
data = request.get_json()
if data['event'] == 'ping':
print(f'Received a test ping!')
return '', 200
# Return if the event isn't a user purchase.
if data['event'] != 'product.user.purchase':
return 'Invalid Event', 400
product_id = data['payload']['product']['id']
product_title = data['payload']['product']['title']
user_id = data['payload']['user']['id']
# Get the resource price
product_price = get_resource_price(product_id)
# Get date and time
current_datetime = datetime.datetime.now()
formatted_date = current_datetime.strftime("%d/%m/%Y")
formatted_time = current_datetime.strftime("%H:%M")
new_data = [formatted_date, formatted_time, product_title, product_id, product_price, user_id]
with open(settings['result_file_name'], mode='a', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow(new_data)
print('A new purchase has come through and added to the CSV file.')
return '', 200
if __name__ == '__main__':
print('Starting Web Server...')
if settings['debug_mode']:
app.run(host='0.0.0.0', port=int(settings['web_port']), debug=settings['debug_mode'])
else:
from waitress import serve
serve(app, port=int(settings['web_port']), host='0.0.0.0')