-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransactions.py
82 lines (76 loc) · 3.74 KB
/
transactions.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import re
from datetime import datetime
from requests_retry import requests_retry_session
def process_transactions(mydb, cursor, logger, symbol, transaction_list):
new_transactions = []
transactions_url = "{}api/sedi/filings?symbol={}".format(os.environ['BASE_URL'], symbol)
try:
transactions_response = requests_retry_session().get(transactions_url, timeout=10)
except Exception as x:
logger.error("{} : {}".format(repr(x), transactions_url))
return
if transactions_response.status_code != 200:
logger.error("{} : {}".format(transactions_response.status_code, transactions_url))
return
transactions_response = transactions_response.json()[:10]
for transaction in transactions_response:
transaction_b = transaction["datab"]
if str(transaction_b["Transaction ID"]) not in transaction_list:
units = transaction_b["Number or value acquired or disposed of"].replace(',', '')
unit_price = transaction_b["Unit price or exercise price"]
try:
total = '${0:+,.2f}'.format(float(units or 0) * float(unit_price or 0))
total = re.sub(r"\.00$", "", total)
except Exception as x:
logger.debug("{} : {}".format(repr(x), transactions_url))
continue
price_statement = []
if units and unit_price:
price_statement.append({
"name": "Total",
"value": total,
"inline": True})
if units:
price_statement.append({
"name": "Volume",
"value": units,
"inline": True})
if unit_price:
price_statement.append({
"name": "Unit Price",
"value": unit_price,
"inline": True})
created_at = datetime.utcfromtimestamp(transaction["timestamp"]/1000).\
strftime("%Y-%m-%dT%H:%M:%SZ")
embed = {
"author": {
"name": transaction_b["Issuer Name"]
},
"title": transaction_b["Nature of transaction"],
"description": "{}\n{}\nDate of Transcation: {}\n[See more transactions]({})".format(
transaction_b["Insider Name"],
transaction_b["Insider's Relationship to Issuer"],
transaction_b["Date of transaction"],
os.environ['BASE_URL'] + "api/sedi?symbol={}".format(symbol)),
"fields": price_statement,
"timestamp": created_at
}
new_transactions.append(embed)
try:
statement = "INSERT INTO transactions(`transaction_id`, `symbol`, `transaction_date`, `insider_name`, `issuer_name`, `relationship_to_issuer`, `nature_of_transaction`, `price`, `units`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (transaction_b["Transaction ID"],
symbol,
transaction_b["Date of transaction"],
transaction_b["Insider Name"],
transaction_b["Issuer Name"],
transaction_b["Insider's Relationship to Issuer"],
transaction_b["Nature of transaction"],
transaction_b["Unit price or exercise price"],
transaction_b["Number or value acquired or disposed of"])
cursor.execute(statement, values)
mydb.commit()
except Exception as x:
logger.error("{} : {}".format(repr(x), transactions_url))
continue
return new_transactions