The template can clearly explain the alert data, making it easier for users to understand.
Because the data fields referenced in each indicator are different, there is no reusability. In order to improve the flexibility of the data and make the expression of the alert more understandable, we have separated the processing of the indicator data from the broadcast text, and introduced the template engine. We create an alert template corresponding to the language for each indicator, and the data that needs to be used in the template is replaced by variables. After the logic of the indicators is processed, the corresponding data variables are passed to the template, which can be easily compiled into broadcast text in different languages.
Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. More info: https://github.com/pallets/jinja
For Example
The corresponding EN template is:
According to KingData monitoring, {% for coin in info%}In the past 4 hours, {{coin.symbol}} Long/Short Ratio across network is {{coin.long_short_rate}}, with {{coin.long_rate}}% longs and {{coin.short_rate}}% shorts. {%if coin.long_short_rate>1%}Longs outweigh shorts{% else %}Shorts outweigh Longs{% endif %}.
Among leading exchanges:{% for exchange in coin.list %}
{{exchange.exchange_name}}: long {{exchange.long_rate}}%, short {{exchange.short_rate}}%{% endfor %}
{% endfor %}
The rendered EN text is:
According to KingData monitoring, In the past 4 hours, BTC Long/Short Ratio across network is 1.08, with 51.86% longs and 48.14% shorts. Longs outweigh shorts.
Among leading exchanges:
Binance: long 46.4%, short 53.6%
OKX: long 57.96%, short 42.04%
Bitget: long 50.53%, short 49.47%
The corresponding CN template is:
据 KingData 数据监控,{% for coin in info%}最近4小时,{{coin.symbol}}全网合约多空比为 {{coin.long_short_rate}},多单占比 {{coin.long_rate}}%,空单占比{{coin.short_rate}}%,{%if coin.long_short_rate>1%}看多人数大于看空人数{% else %}看空人数大于看多人数{% endif %}。
其中主流交易所:{% for exchange in coin.list %}
{{exchange.exchange_name}}:做多{{exchange.long_rate}}% 做空{{exchange.short_rate}}%{% endfor %}
{% endfor %}
from crawlers.utils import SpiderBasefrom jinja2 import Templatefrom crawlers.utils.group_alarm import catch_exceptdefbuild_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]]}classContractPositionRatio(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_exceptdefparse(self,response,**kwargs): data = response.json()['data'][0] params ={'info': [build_coin_info(data)]}print(Template(self.alert_en_template()).render(params))print(Template(self.alert_cn_template()).render(params))# must be declaredefalert_en_template(self):return"""According to KingData monitoring, {% for coin in info%}In the past 4 hours, {{coin.symbol}} Long/Short Ratio across network is {{coin.long_short_rate}}, with {{coin.long_rate}}% longs and {{coin.short_rate}}% shorts. {%if coin.long_short_rate>1%}Longs outweigh shorts{% else %}Shorts outweigh Longs{% endif %}.Among leading exchanges:{% for exchange in coin.list %}{{exchange.exchange_name}}: long {{exchange.long_rate}}%, short {{exchange.short_rate}}%{% endfor %}{% endfor %}"""# must be declaredefalert_cn_template(self):return"""据 KingData 数据监控,{% for coin in info%}最近4小时,{{coin.symbol}}全网合约多空比为 {{coin.long_short_rate}},多单占比 {{coin.long_rate}}%,空单占比{{coin.short_rate}}%,{%if coin.long_short_rate>1%}看多人数大于看空人数{% else %}看空人数大于看多人数{% endif %}。其中主流交易所:{% for exchange in coin.list %}{{exchange.exchange_name}}:做多{{exchange.long_rate}}% 做空{{exchange.short_rate}}%{% endfor %}{% endfor %}"""