-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (53 loc) · 1.62 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
from log1 import setup
import time
import sqlite3
import requests
logger = setup.setup_logging()
def run():
try:
endpoint = " https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search"
body = {
"page": 1,
"rows": 10,
"payTypes": [],
"asset": "USDT",
"tradeType": "BUY",
"fiat": "VND",
"publisherType": "merchant"
}
# Dinh dang data
headers = {
"Content-Type": "application/json"
}
except Exception as e:
return f'{logger.error("Error")}'
try:
reponse = requests.post(url=endpoint, headers=headers, json=body)
content = reponse.json()['data']
with sqlite3.connect('price.db') as conn:
curs = conn.cursor()
curs.execute("CREATE TABLE IF NOT EXISTS price (Price REAL)")
logger.info("CREATE TABLE")
for each in content:
price = float(each['adv']['price'])
curs.execute("INSERT INTO price VALUES (?)", (price,))
logger.info("INSERT DATA TO DATABASE")
print(price)
except Exception as e:
logger.exception("Exception:")
logger.error(e)
def insertDB():
try:
conn = sqlite3.connect("price.db")
cur = conn.cursor()
cur.execute("SELECT AVG(price) FROM price")
rows = cur.fetchall()
for row in rows:
logger.warning(row)
except Exception as e:
return logger.error(e)
if __name__ == '__main__':
print("Running!")
logger.info("----------")
run()
insertDB()