From 0334b00e02116d0b2efb8d5e7b9daaab3f3f5a0e Mon Sep 17 00:00:00 2001 From: Pan Xiong Date: Thu, 19 Oct 2023 22:37:01 +1100 Subject: [PATCH] Add notify new symbol job --- app/jobs/notify_new_symbol_job.rb | 45 ++++++++++++++++++++++++ app/services/binance_futures_service.rb | 5 +-- app/services/fetch_new_symbol_service.rb | 20 +++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 app/jobs/notify_new_symbol_job.rb create mode 100644 app/services/fetch_new_symbol_service.rb diff --git a/app/jobs/notify_new_symbol_job.rb b/app/jobs/notify_new_symbol_job.rb new file mode 100644 index 0000000..be06742 --- /dev/null +++ b/app/jobs/notify_new_symbol_job.rb @@ -0,0 +1,45 @@ +class NotifyNewSymbolJob < ApplicationJob + queue_as :daily_job + + def perform + msgs = FetchNewSymbolService.execute + msgs.each{|msg| format_notice_blocks(msg)} + + check_symbol_from_api + end + + def check_symbol_from_api + symbols = BinanceFuturesService.new.get_ticker_price.map{|x| x['symbol']} rescue [] + return if symbols.empty? + + redis_key = 'binance_futures_symbols' + prev_symbols = JSON.parse($redis.get(redis_key)) rescue [] + diff_symbols = symbols - prev_symbols + $redis.set(redis_key, symbols) + SlackService.send_notification(nil, format_blocks(diff_symbols)) if diff_symbols.any? + end + + def format_notice_blocks(msg) + [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": msg + } + } + ] + end + + def format_api_blocks(symbols) + [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*币安合约API新增币种: #{symbols.join(", ")}, 共#{symbols.length}个*" + } + } + ] + end +end \ No newline at end of file diff --git a/app/services/binance_futures_service.rb b/app/services/binance_futures_service.rb index effdfb5..edb6970 100644 --- a/app/services/binance_futures_service.rb +++ b/app/services/binance_futures_service.rb @@ -73,8 +73,9 @@ def get_24hr_tickers end.map{|k,v| v[0]} end - def get_ticker_price(symbol) - url = BASE_URL + "/fapi/v1/ticker/price?symbol=#{symbol}" + def get_ticker_price(symbol=nil) + url = BASE_URL + "/fapi/v1/ticker/price" + url += "?symbol=#{symbol}" if symbol response = RestClient.get(url) JSON.parse(response) end diff --git a/app/services/fetch_new_symbol_service.rb b/app/services/fetch_new_symbol_service.rb new file mode 100644 index 0000000..e435486 --- /dev/null +++ b/app/services/fetch_new_symbol_service.rb @@ -0,0 +1,20 @@ +require 'openssl' +require 'rest-client' + +class FetchNewSymbolService + BASE_URL = ENV['BINANCE_ANNOUNCEMENT_URL'] + + class << self + def execute + redis_key = 'binance_announcement_fetch_time' + last_fetch_time = $redis.get(redis_key).to_i + page = Nokogiri::HTML(URI.open(BASE_URL)) + data = JSON.parse(page.css("script").children[10]) + catalog = data['appState']['loader']['dataByRouteId']['2a3f']['catalogs'].select{|c| c['catalogName'] == '数字货币及交易对上新'}.first rescue nil + return if catalog.nil? + + $redis.set(redis_key, Time.current.to_i * 1000) + catalog['articles'].select{|a| a['releaseDate'] >= last_fetch_time && a['title'].match(/将上线.*永续合约/)}.map{|a| a['title']} + end + end +end \ No newline at end of file