| 1 | # $Id$
|
|---|
| 2 | include Misuzilla::Applications::TwitterIrcGateway
|
|---|
| 3 |
|
|---|
| 4 | class Scraping
|
|---|
| 5 | @@interval = 30
|
|---|
| 6 | @@user_cache = {}
|
|---|
| 7 | @@thread = nil
|
|---|
| 8 |
|
|---|
| 9 | def self.interval=(val); @@interval = val; end
|
|---|
| 10 | def self.interval; @@interval; end
|
|---|
| 11 | def self.user_cache; @@user_cache; end
|
|---|
| 12 | def self.thread=(val); @@thread = val; end
|
|---|
| 13 | def self.thread; @@thread; end
|
|---|
| 14 |
|
|---|
| 15 | def self.prepare_user_cache
|
|---|
| 16 | # begin
|
|---|
| 17 | Session.TwitterService.GetFriends(20).each do |u|
|
|---|
| 18 | @@user_cache[u.ScreenName.to_s] = u
|
|---|
| 19 | end
|
|---|
| 20 | # rescue
|
|---|
| 21 | # end
|
|---|
| 22 | end
|
|---|
| 23 |
|
|---|
| 24 | def self.fetch_home
|
|---|
| 25 | home = Session.TwitterService.GETWithCookie("/home").to_s
|
|---|
| 26 | if statuses = home.scan(%r{(<li class="hentry status.*?</li>)})
|
|---|
| 27 | statuses.reverse.each do |status1|
|
|---|
| 28 | status = status1[0]
|
|---|
| 29 | s = Status.new
|
|---|
| 30 |
|
|---|
| 31 | # User
|
|---|
| 32 | m = status.match(%r{class="screen-name" title="([^"]+)">(.*?)</a>})
|
|---|
| 33 | if @@user_cache[m[2]]
|
|---|
| 34 | s.User = @@user_cache[m[2]]
|
|---|
| 35 | else
|
|---|
| 36 | #begin
|
|---|
| 37 | # s.User = Session.TwitterService.GetUser(m[2])
|
|---|
| 38 | # @@user_cache[m[2]] = s.User
|
|---|
| 39 | #rescue
|
|---|
| 40 | s.User = User.new
|
|---|
| 41 | s.User.Id = 0
|
|---|
| 42 | s.User.Name = m[1]
|
|---|
| 43 | s.User.ScreenName = m[2]
|
|---|
| 44 | #end
|
|---|
| 45 | end
|
|---|
| 46 |
|
|---|
| 47 | # Status
|
|---|
| 48 | s.Source = status.match(%r{<span>from (.*?)</span>})[1].to_s
|
|---|
| 49 | s.Text = Utility::UnescapeCharReference(status.match(%r{class="entry-content">(.*?)</span>})[1].to_s.gsub(%r{<a href="(http://[^"]*)"[^>]*>.*?</a>}, '\1').gsub(/<[^>]*>/, ''))
|
|---|
| 50 | s.Id = status.match(%r{id="status_(\d+)"})[1].to_i
|
|---|
| 51 | s.CreatedAt = Time.now
|
|---|
| 52 |
|
|---|
| 53 | # 送信
|
|---|
| 54 | #System::Diagnostics::Trace::WriteLine(s)
|
|---|
| 55 | Session.TwitterService.ProcessStatus(s, System::Action[Status].new{|s1| Session.ProcessTimelineStatus(s, false, false) })
|
|---|
| 56 | end
|
|---|
| 57 | end
|
|---|
| 58 | end
|
|---|
| 59 | end
|
|---|
| 60 |
|
|---|
| 61 | Scraping.thread = Thread.new do
|
|---|
| 62 | # ちゃんとThreadを終わらせないと大変残念なことになる
|
|---|
| 63 | dlr_addin = Session.AddInManager.GetAddIn(Misuzilla::Applications::TwitterIrcGateway::AddIns::DLRIntegration::DLRIntegrationAddIn.to_clr_type)
|
|---|
| 64 | dlr_addin.BeforeUnload do |sender, e|
|
|---|
| 65 | Scraping.interval = 0
|
|---|
| 66 | Scraping.thread.join
|
|---|
| 67 | end
|
|---|
| 68 |
|
|---|
| 69 | # ユーザ情報をキャッシュする
|
|---|
| 70 | Session.pre_send_message_timeline_status do |sender, e|
|
|---|
| 71 | if e.status.user.Id != 0
|
|---|
| 72 | Scraping.user_cache[e.status.user.ScreenName.to_s] = e.status.user
|
|---|
| 73 | end
|
|---|
| 74 | end
|
|---|
| 75 |
|
|---|
| 76 | # ログインする
|
|---|
| 77 | Session.TwitterService.CookieLogin
|
|---|
| 78 |
|
|---|
| 79 | while Scraping.interval > 0 do
|
|---|
| 80 | begin
|
|---|
| 81 | Scraping.fetch_home
|
|---|
| 82 | rescue => e
|
|---|
| 83 | # なんもしないけど
|
|---|
| 84 | System::Diagnostics::Trace::WriteLine(e.to_s)
|
|---|
| 85 | end
|
|---|
| 86 | sleep(Scraping.interval)
|
|---|
| 87 | end
|
|---|
| 88 | end
|
|---|
| 89 |
|
|---|
| 90 | Scraping.thread.run
|
|---|