Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.46 KB | None | 0 0
  1. require 'rubygems'
  2. require 'nokogiri'
  3. require 'rest-client'
  4. require 'mail'
  5. require 'uri'
  6.  
  7. options = { address:               "smtp.gmail.com",
  8.             port:                   587,
  9.             domain:                'localhost',
  10.             user_name:             'pavel.shebalkou@gmail.com',
  11.             password:              '*********',
  12.             authentication:        'plain',
  13.             enable_starttls_auto:   true }
  14.  
  15. Mail.defaults do
  16.   delivery_method :smtp, options
  17. end
  18.  
  19. def execution_loop(period)
  20.   loop do
  21.     before = Time.now
  22.     yield
  23.     interval = period - (Time.now - before)
  24.     sleep(interval) if interval > 0
  25.   end
  26. end
  27.  
  28. def get_url
  29.   url = RestClient.get('https://vk.com/psychohell')
  30. end
  31.  
  32. def read_html(link)
  33.   page = Nokogiri::HTML(link)
  34. end
  35.  
  36. def get_status(page)
  37.   status = page.css('.profile_time_lv').text
  38. end
  39.  
  40. def send_email(status)
  41.   if status.include?("заходил") || status.include?("сети")
  42.     puts "Вадим #{status}"
  43.  
  44.   else
  45.     Mail.deliver do
  46.       to 'elizeoban@gmail.com'
  47.       from 'pavel.shebalkou@gmail.com'
  48.       subject 'vk_online_checker_notification'
  49.       body 'Nell is now online'
  50.     end
  51.     puts 'Вадим сейчас Online'
  52.   end
  53. end
  54.  
  55. begin
  56.   execution_loop(60) do
  57.     url = get_url
  58.     html_page = read_html(url)
  59.     status = get_status(html_page)
  60.     send_email(status)
  61.   end
  62.  
  63. rescue
  64.   LoadError
  65.  
  66. ensure
  67.   puts "Попробуйте еще раз"
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement