-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
74 lines (61 loc) · 1.7 KB
/
main.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
import os
from dotenv import load_dotenv
from binance.client import Client
from binance import BinanceSocketManager
import asyncio
import csv
from csv import writer
from python_settings import settings
process_list = []
async def main():
"""
Main
"""
# Loading environment variables
load_dotenv('.')
api_key = os.environ.get("api_key")
api_secret = os.environ.get("api_secret")
# Loading settings
os.environ["SETTINGS_MODULE"] = 'settings'
# Creating the client
client = Client(api_key, api_secret)
# Defining sockets based on settings
symbols = settings.SYMBOLS
# Multiprocessing the api requests
tasks = []
for s in symbols:
task = asyncio.create_task(scrape_process(s, client))
tasks.append(task)
# Waiting for all the processes to end
for task in tasks:
await task
async def scrape_process(s, client):
"""
Gathering data into csv
"""
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket(s)
writeRow(await getData(socket))
async def getData(socket):
"""
Returns the data received by a socket
"""
await socket.__aenter__()
res = await socket.recv()
print("######### " + str(res["s"]) + " #############")
print(res)
return(res)
def writeRow(row):
"""
Appends a dict's values as a row to csv file
"""
s = []
for _ , v in row.items():
s.append(str(v))
with open("data/" + row["s"] + ".csv", 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(s)
# Running main() through asyncio for async/await
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())