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)
if @agent.nil?
@agent = Mechanize.new
@agent.user_agent_alias = "Linux Mozilla"
@agent.max_history = 0
end
URI.(m.message.gsub(/git@(gist.github.com):/,'git://\1/'), ["http", "https", "git"]).map do |link|
link=~/^(.*?)[:;,\)]?$/
$1
end.each do |link|
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
title = page.title.gsub(/[\x00-\x1f]*/, "").gsub(/[ ]{2,}/, " ").strip rescue nil
case uri.host
when "www.imdb.com"
rating = page.search("//strong/span[@itemprop='ratingValue']").text
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"
page = @agent.get(link + "&nofeather=True")
hits = page.search("//span[@class='watch-view-count ']")
hits = hits.text.gsub(/[.,]/, ",")
likes = page.search("//span[@class='likes-count']")
likes = likes.text.gsub(/[.,]/, ",")
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"
owner = page.search("//div[@class='name']/a").inner_html
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"
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
= JSON.parse(json)
unescaped = CGI.unescapeHTML(["text"])
m.reply "@%s: %s" % [ ["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
|