switch_discounts.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. require 'net/http'
  2. require 'uri'
  3. require 'json'
  4. require 'rufus-scheduler'
  5. def post_msg(bot_token, chat_id, txt)
  6. uri = URI.parse("https://api.telegram.org/bot"+bot_token+"/sendMessage")
  7. request = Net::HTTP::Post.new(uri)
  8. request.set_form_data(
  9. "chat_id" => chat_id,
  10. "text" => txt,
  11. )
  12. req_options = {
  13. use_ssl: uri.scheme == "https",
  14. }
  15. response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  16. http.request(request)
  17. end
  18. end
  19. bot_token = ENV["BOT_TOKEN"]
  20. chat_id = ENV["CHAT_ID"]
  21. scheduler = Rufus::Scheduler.new
  22. scheduler.every '1m' do
  23. arr = File.readlines("name")
  24. list=arr.inspect
  25. uri = URI.parse("https://ec.nintendo.com/api/US/en/search/sales?count=30&offset=0")
  26. response = Net::HTTP.get_response(uri)
  27. # response.code
  28. #puts(response.body)
  29. parse=JSON.parse(response.body)
  30. file=File.open("name","w")
  31. $i=0
  32. while $i < 30 do
  33. if list.include?(parse['contents'][$i]['formal_name'])==false then
  34. post_msg(bot_token, chat_id, parse['contents'][$i]['formal_name'])
  35. end
  36. file.write parse['contents'][$i]['formal_name'] + "\n"
  37. $i=$i+1
  38. end
  39. file.close
  40. end
  41. scheduler.join