Advertisement
sheredega

Untitled

Oct 31st, 2023
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.60 KB | None | 0 0
  1. require './config/environment'
  2.  
  3.  
  4. class Parser
  5.   USER_LOGIN = ENV['LOGIN']
  6.   USER_PASSWORD = ENV['PASSWORD']
  7.   URL_LOGIN = ENV['URL_LOGIN']
  8.   URL_SUPPORT_LIST = ENV['URL_SUPPORT_LIST']
  9.   URL_POST_REQUEST = ENV['URL_POST_REQUEST']
  10.  
  11.   def auth(agent)
  12.     page = agent.get(URL_LOGIN)
  13.     login_form = page.form(action: "/login/login")
  14.     login_form.field(name: "login").value = USER_LOGIN
  15.     login_form.field(name: "password").value = USER_PASSWORD
  16.     agent.submit(login_form)
  17.   end
  18.  
  19.   def parse(page)
  20.     support_list = Array.new
  21.     text = page.at('/html/body/div[2]/div/div[3]/div/div[2]/div/div/div[2]/div[1]/div[3]/div/article/div/div[2]/div/div[1]/div').text
  22.     server = nil
  23.  
  24.     text.delete("\t").split(/\n/, -1).each do |str|
  25.       if str.include? "Revolution"
  26.         server = 101
  27.       elsif str.include? "Legacy"
  28.         server = 103
  29.       elsif str.include? "Underground"
  30.         server = 104
  31.       end
  32.  
  33.       if str.match(/(\w+_\w+)[ ]*\//)
  34.         nick = str.match(/(\w+_\w+)[ ]*\//)[1]
  35.         support_list << {name: nick, server: server}
  36.       end
  37.     end
  38.  
  39.     support_list
  40.   end
  41.  
  42.   def send_post_request(support_list)
  43.     if support_list.length > 5
  44.       json_list = support_list.to_json
  45.       uri = URI(URL_POST_REQUEST)
  46.       print(json_list)
  47.       headers = { 'Content-Type': 'application/json' }
  48.       response = Net::HTTP.post(uri, json_list, headers)
  49.     end
  50.   end
  51.  
  52.   def execute
  53.     agent = Mechanize.new
  54.     auth(agent)
  55.     page = agent.get(URL_SUPPORT_LIST)
  56.     support_list = parse(page)
  57.     send_post_request(support_list)
  58.   end
  59. end
  60.  
  61. Parser.new.execute
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement