PageRenderTime 55ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/media_wassr.rb

https://bitbucket.org/pen/fakebot
Ruby | 82 lines | 61 code | 17 blank | 4 comment | 5 complexity | 433e4c5091b18fefcb71472d62355c71 MD5 | raw file
  1. require "mechanize"
  2. require "app_config"
  3. module Media
  4. class Wassr
  5. def post(text)
  6. agent.post(
  7. "http://api.wassr.jp/statuses/update.json",
  8. {
  9. "source" => "fakebot/1.0",
  10. "status" => text,
  11. }
  12. )
  13. end
  14. def read_new(user, last)
  15. last ||= "0000-00-00 00:00:00"
  16. current = "9999-99-99 99:99:99"
  17. page_no = 0
  18. loop do
  19. sleep 5
  20. page_no += 1
  21. page = agent.get("http://wassr.jp/user/#{user}", { "page" => page_no })
  22. nick_cache(user, page) if page_no == 1
  23. may_continue = false
  24. page.search("a.MsgDateTime").each do |elem|
  25. datetime = elem.content.sub(/\(.*?\)/, "")
  26. # ????????????????
  27. if datetime <= last
  28. may_continue = false
  29. break
  30. end
  31. # ??????????????????????????
  32. next if datetime >= current
  33. # ?????????????????1????????
  34. # ????
  35. text = elem["title"]
  36. yield(text, datetime)
  37. current = datetime
  38. may_continue = true
  39. end
  40. break unless may_continue
  41. end
  42. end
  43. def get_nick(user)
  44. nick_cache(user)
  45. end
  46. private
  47. def initialize
  48. @nick_cache = { }
  49. end
  50. def agent
  51. @agent ||= begin
  52. agent = Mechanize.new
  53. agent.auth(*App::Config.get["media"]["init"])
  54. agent.user_agent = "fakebot/1.0 (http://bitbucket.com/pen/fakebot/)"
  55. agent.max_history = 1
  56. agent
  57. end
  58. end
  59. def nick_cache(user, page = nil)
  60. @nick_cache[user] ||= (
  61. page || agent.get("http://wassr.jp/user/#{user}")
  62. ).title.sub(/ - Wassr .*/, "")
  63. end
  64. end
  65. end