forked from kennethreitz/coinbin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
44 lines (29 loc) · 944 Bytes
/
graph.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
import scraper
import graphene
def name_to_ticker(name):
for coin in scraper.get_coins().values():
if coin['name'].lower() == name.lower():
return coin['ticker']
class Coin(graphene.ObjectType):
ticker = graphene.String()
name = graphene.String()
rank = graphene.Int()
usd = graphene.Float()
@classmethod
def from_coin(klass, c):
klass.ticker = c.ticker
klass.name = c.name
klass.rank = c.rank
klass.usd = c.usd
return klass
class Query(graphene.ObjectType):
coin = graphene.Field(Coin, name=graphene.String())
recent_top_coins = graphene.List(Coin)
@graphene.resolve_only_args
def resolve_coin(self, name=None, ticker=None):
if name and not ticker:
ticker = name_to_ticker(name)
c = Coin.from_coin(scraper.Coin(ticker))
c.name = name
return c
schema = graphene.Schema(query=Query)