This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodaqAPI.py
150 lines (112 loc) · 4.62 KB
/
TodaqAPI.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from collections import Counter
import requests
from Item import draw_a_random_item
myApiKey = "3e24c1f2-4db1-4820-a868-e8e763d3988d"
origin = "https://api.todaqfinance.net"
headers = {"Content-Type": "application/json",
"x-api-key": myApiKey}
# Get a list of all the accounts
def get_accounts() -> list:
url = origin + "/accounts"
# page starts at 1
page = 1
params = {"filter[active]": "true",
"page": page,
"limit": 10000}
total = []
while True:
r = requests.get(url, params=params, headers=headers).json()
for user in r["data"]:
if user["attributes"]["admin-email"] == "[email protected]":
total.append(user["id"])
if "next" not in r["links"]:
return total
else:
page += 1
params["page"] = page
# Return the dictionary of items to quantity that owned by the account
def get_files_from_account(account: str) -> dict:
url = origin + "/accounts/" + account + \
"/files?page=1&limit=10"
response = requests.get(url, headers=headers).json()
items = {}
if "errors" in response:
raise Exception("%s: %s" % (response["errors"][0]["status"], response["errors"][0]["detail"]))
for file in response["data"]:
if "id" not in file["attributes"]["payload"] or \
file["attributes"]["payload"]["id"] is None or type(
file["attributes"]["payload"]["id"]) == int or len(
file["attributes"]["payload"]["id"]) == 36:
# Ignore the accounts that are not associated to our marketplace
break
elif file["attributes"]["payload"]["id"] in items:
items[file["attributes"]["payload"]["id"]] += 1
else:
items[file["attributes"]["payload"]["id"]] = 1
return items
# TODO
def get_transactions_from_account(account: str) -> dict:
url = origin + "/accounts/" + account + "/transactions?page=1&limit=10000"
response = requests.get(url, headers=headers).json()
items = {}
for file in response["data"]:
if "id" not in file["attributes"]["payload"] or \
file["attributes"]["payload"]["id"] is None or type(
file["attributes"]["payload"]["id"]) == int or len(
file["attributes"]["payload"]["id"]) == 36:
# Ignore the accounts that are not associated to our marketplace
break
elif file["attributes"]["payload"]["id"] in items:
items[file["attributes"]["payload"]["id"]] += 1
else:
items[file["attributes"]["payload"]["id"]] = 1
return items
# !!Inefficient
def get_all_files() -> dict:
total = Counter({})
accounts = get_accounts()
for account in accounts:
a = Counter(get_files_from_account(account))
total = total + a
return total
# Create an account and return its account id
def create_account() -> str:
url = origin + "/accounts"
data = {
"type": "account",
"data": {
"attributes": {
"account-type": "individual",
"admin-email": "[email protected]",
"contact": {
"last-name": "anonymous",
"first-name": "anonymous"
}
}
}
}
r = requests.post(url, json=data, headers=headers).json()
account_id = r["data"][0]["id"]
print(account_id)
return account_id
# We initialize a new account by randomly adding 5 items
def account_initialization() -> str:
account_id = create_account()
for i in range(5):
create_item(draw_a_random_item(), account_id)
def create_item(item: str, owner: str) -> None:
url = origin + "/files"
payload = "{ \"data\": { \"type\":\"file\", " \
"\"attributes\":{ \"payload\":{ \"id\": \"" + item + "\", " \
"\"type\": \"loyalty-token\", \"member-type\": \"gold\" }, " \
"\"metadata\": {}}, \"relationships\":{ \"initial-account\":{ \"data\":{ \"type\":\"account\", \"id\":\"" + owner + "\" } } } }} "
response = requests.post(url, data=payload.encode('utf-8'), headers=headers)
print(response.json())
if __name__ == "__main__":
recipient = "3c8263ec-fc26-4186-85c2-2f91c7d1762f"
# get_accounts()
# account_initialization()
# create_item("Sticker | OpTic Gaming (Holo) | Cologne 2016", recipient)
# get_files_from_account(recipient)
# get_all_files()
# get_files_from_account("AFDFSD")