Class: Cinch::Plugins::UrlScraper

Inherits:
Object
  • Object
show all
Includes:
Helpers, Cinch::Plugin
Defined in:
lib/cinch/plugins/urlscraper.rb

Instance Method Summary collapse

Instance Method Details

#execute(m, option) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cinch/plugins/urlscraper.rb', line 138

def execute(m, option)
         
         config[:enabled_channels] ||= [bot.channels.map(&:name)]
         puts bot.channels.map(&:name)

@url = option == "on"

case option
  when "on"
    config[:enabled_channels] << m.channel.name
  else
    config[:enabled_channels].delete(m.channel.name)
  end
  
  m.reply Format(:green, "URL Scraping for #{m.channel} is now #{@url ? 'enabled' : 'disabled'}!")
  
  @bot.debug("#{self.class.name}#{config[:enabled_channels].inspect}");
  
  config[:enabled_channels]=nil if config[:enabled_channels]==[]
  
rescue 
  m.reply Format(:red, "Error: #{$!}")
end

#listen(m) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cinch/plugins/urlscraper.rb', line 22

def listen(m)
return if m.message.include? "nospoil"
return if config[:enabled_channels] && ! config[:enabled_channels].include?(m.channel.name)
# Create mechanize agent
if @agent.nil?
  @agent = Mechanize.new
  @agent.user_agent_alias = "Linux Mozilla"
  @agent.max_history      = 0
end

URI.extract(m.message.gsub(/git@(gist.github.com):/,'git://\1/'), ["http", "https", "git"]).map do |link|
  link=~/^(.*?)[:;,\)]?$/
  $1
end.each do |link|
  # Fetch data
  begin
    if git = link =~ /^git:\/\/(gist.github.com\/.*)\.git$/
      link = "https://#{$1}"
    end
    uri  = URI.parse(link)
    page = @agent.get(link)
  rescue Mechanize::ResponseCodeError
    if "www.youtube.com" == uri.host
      m.reply "Thank you, GEMA!"
    else
      m.reply "Y U POST BROKEN LINKS?", true
    end

    next
  end

  # Replace strange characters
  title = page.title.gsub(/[\x00-\x1f]*/, "").gsub(/[ ]{2,}/, " ").strip rescue nil

  # Check host
  case uri.host
    when "www.imdb.com"
      # Get user rating
      rating = page.search("//strong/span[@itemprop='ratingValue']").text

      # Get votes
      votes = page.search("//a/span[@itemprop='ratingCount']").text

      m.reply "#{m.user.nick}'s IMDB Title: %s (Rating: %s/10 from %s users)" % [
        title, rating, votes
      ]
      
    when "www.youtube.com"
      # Reload with nofeather
      page = @agent.get(link + "&nofeather=True")

      # Get page hits
      hits = page.search("//span[@class='watch-view-count ']")
      hits = hits.text.gsub(/[.,]/, ",")

      # Get likes
      likes = page.search("//span[@class='likes-count']")
      likes = likes.text.gsub(/[.,]/, ",")
      
      # Get dislikes
      dislikes = page.search("//span[@class='dislikes-count']")
      dislikes = dislikes.text.gsub(/[.,]/, ",")

      m.reply "#{m.user.nick}'s YT Title: %s (Views: %s, Likes: %s || Dislikes: %s)" % [
        title, hits.strip, likes.strip, dislikes.strip
      ]
      

    when "gist.github.com"
      # Get owner
      owner = page.search("//div[@class='name']/a").inner_html

      # Get time
      age = page.search("//span[@class='date']/time")
      age = age.first[:datetime] rescue age.text if age
      age = Time.parse(age) rescue nil
      age = age.strftime("%Y-%m-%d %H:%M") if age

      if git
        m.reply "Title: %s (at %s, %s on %s), Url: %s" % [
          title, uri.host, owner, age, link
        ]
      else
        m.reply "Title: %s (at %s, %s on %s)" % [
          title, uri.host, owner, age
        ]
      end
    when "pastie.org"
      # Get time
      age = Time.parse(page.search("//span[@class='typo_date']").text)
      age = age.strftime("%Y-%m-%d %H:%M")

      m.reply "Title: %s (at %s, on %s)" % [
        title, uri.host, age
      ]
    when "subforge.org", "subtle.de"
      m.reply "Title: %s (at %s)" % [ title, uri.host ]
      
    when "twitter.com"
      if link =~ /\/status\/(\d+)$/
        json      = @agent.get("https://api.twitter.com/1/statuses/show/#{$1}.json?trim_user=1").body
        tweet     = JSON.parse(json)
        unescaped = CGI.unescapeHTML(tweet["text"])

        m.reply "@%s: %s" % [ tweet["user"]["screen_name"], unescaped ]
      else
        m.reply "Broken twitter link: %s (at %s)" % [ title, uri.host ] if title
      end
    else
      m.reply "Title: %s (at %s)" % [ title, uri.host ] if title
    end
  end
end