-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateAssets.py
104 lines (76 loc) · 2.38 KB
/
updateAssets.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
import json
import csv
from api_keys import CREDS_DICT, KEY
#############################################################
CST = None
SECURITY_TOKEN = None
def getCreds():
key = KEY
identifierDict = {
"identifier": CREDS_DICT["identifier"],
"password": CREDS_DICT["password"],
"encryptedPassword": "false",
}
headers = {"x-cap-api-key": key}
if not CST is None:
headers["cst"] = CST
if not SECURITY_TOKEN is None:
headers["x-security-token"] = SECURITY_TOKEN
return identifierDict, headers
def prepareRequest(payload):
requestData = {}
requestHeaders = {}
secHeaders, secDict = getCreds()
requestData.update(secHeaders)
requestData.update(payload)
requestHeaders.update(secDict)
return requestData, requestHeaders
def intialize():
resp = sendPost("/api/v1/session")
cst = resp.headers["cst"]
global CST
global SECURITY_TOKEN
security_token = resp.headers["x-security-token"]
CST = cst
SECURITY_TOKEN = security_token
def prepareUrl(api):
return "https://api-capital.backend-capital.com" + api
def sendPost(api, payload={}):
data, headers = prepareRequest(payload)
apiUrl = prepareUrl(api)
responce = requests.post(apiUrl, headers=headers, json=data)
print(">>> ", apiUrl)
print("<<< ", responce.status_code)
return responce
def sendGet(api, payload={}):
data, headers = prepareRequest(payload)
apiUrl = prepareUrl(api)
responce = requests.get(apiUrl, headers=headers, json=data)
print(">>> ", apiUrl)
print("<<< ", responce.status_code)
return responce
def getWLIds(WLJson):
for wl in WLJson["watchlists"]:
print(wl)
return [_["id"] for _ in WLJson["watchlists"]]
def createWLIdsUrls(WLIds):
return ["/api/v1/watchlists/" + _ for _ in WLIds]
def dumpSelected(epics):
with open("capital_asset_urls.csv", "w") as epicslist:
writer = csv.writer(epicslist)
for asset in epics:
writer.writerow([asset])
intialize()
# "/api/v1/marketnavigation"
wlItems = getWLIds(sendGet("/api/v1/watchlists").json())
print(wlItems)
wlUrls = createWLIdsUrls(wlItems)
assets = []
for wl in wlUrls:
wlData = sendGet(wl)
for asset in wlData.json()["markets"]:
print("*** ", asset["epic"])
assets.append(asset["epic"])
dumpSelected(assets)
# print(wlData.json())