from crawlers.utils import SpiderBase
from jinja2 import Template
from crawlers.utils.group_alarm import catch_except
def build_coin_info(data):
return {
'symbol': data['symbol'],
'long_short_rate': round(data['longRate'] / data['shortRate'], 2),
'long_rate': data['longRate'],
'short_rate': data['shortRate'],
'list': [{
'exchange_name': exchange_info['exchangeName'],
'long_rate': exchange_info['longRate'],
'short_rate': exchange_info['shortRate']
} for exchange_info in data['list'][:3]]
}
class ContractPositionRatio(SpiderBase):
name = 'idx-contract-position-ratio'
start_urls = [
'https://fapi.coinglass.com/api/futures/longShortRate?symbol=BTC&timeType=3',
'https://fapi.coinglass.com/api/futures/longShortRate?symbol=ETH&timeType=3'
]
@catch_except
def parse(self, response, **kwargs):
data = response.json()['data'][0]
params = {
'info': [build_coin_info(data)]
}
Next, we need to define the template of the broadcast, and the template is used to render the variables in params