Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class AuthenticatedScraper
  2. def initialize(args)
  3. if args[:session]
  4. @cookie_jar = args[:session]
  5. @agent = Mechanize.new
  6. @agent.cookie_jar = load_session(@cookie_jar)
  7. else
  8. @agent = Mechanize.new
  9. @agent.login(args[:username], args[ password])
  10. end
  11. @page = agent.get(initial_page)
  12. @page_source = @page.parser.xpath("//html").to_html.to_s
  13. @login_status = check_session_status
  14. end
  15.  
  16. def load_session(cookie_jar)
  17. @agent.cookie_jar = YAML::load(cookie_jar)
  18. end
  19.  
  20. def save_session
  21. @agent.cookie_jar.to_yaml
  22. end
  23. end
  24.  
  25. # Initial instantiation
  26. if user.settings(:browser).session.nil?
  27. obj = AuthenticatedScraper.new(username: "user", password: "pass")
  28. serialized_session = obj.save_session
  29. user.settings(:browser).session = serialized_session
  30. end
  31. obj.get(page)
  32.  
  33. # Subsequent instantiations
  34. serialized_sesion = user.settings(:browser).session
  35. obj = AuthenticatedScraper.new(sesion: serialized_sesion)
  36. obj.get(page)
  37. user.settings(:browser).session = obj.save_session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement