Skip to content

Commit

Permalink
Add notify new symbol job
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Oct 19, 2023
1 parent c0b5c4b commit 0334b00
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
45 changes: 45 additions & 0 deletions app/jobs/notify_new_symbol_job.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions app/services/binance_futures_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions app/services/fetch_new_symbol_service.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0334b00

Please sign in to comment.