-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.py
74 lines (62 loc) · 1.78 KB
/
application.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 json
import requests
import pymongo
import pandas as pd
from flask import Flask
from flask import request
from bs4 import BeautifulSoup
from pymongo import MongoClient
app = Flask(__name__)
def get_db():
client = MongoClient('localhost:27017')
db = client.cmcdata
return db
@app.route('/')
def status():
return json.dumps({"status":"Success"})
@app.route('/get_data')
def get_data():
coin = request.args.get('coin_name')
auth_key = request.args.get('auth_key')
db = get_db()
if auth_key != "fdsrtw435s6af8dsd9sa":
return "{ACCESS DENIED:Authentication Failed}"
if coin:
coin = coin.split(",")
data = db.cryptodata.find({'name':coin[0]})
else:
data = db.cryptodata.find({})
ret_data = []
for hist_data in data:
for dts in hist_data['data']:
ret_data.append(dts)
return json.dumps(ret_data)
@app.route('/get_coin_list')
def get_coin_list():
db = get_db()
data = db.cryptodata.find({})
coins = {}
coin_data = []
for each in data:
for cn in each['data']:
try:
coin_data.append({cn['org_name']: cn['ticker']})
except Exception as e:
pass
coins["data"] = coin_data
return json.dumps(coins)
@app.route('/get_coins')
def get_coins():
url = "https://coinmarketcap.com/all/views/all/"
html = requests.get(url).text
soup = BeautifulSoup(html, 'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
k = (df[0].to_json(orient='records'))
data = json.loads(k)
coin_list = []
for rec in data:
coin_list.append({"coin":rec['Name'], "ticker":rec['Symbol']})
return json.dumps({"coins":coin_list})
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)