-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytautoexpand.rb
151 lines (124 loc) · 3.32 KB
/
ytautoexpand.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'net/http'
require 'cgi'
$SIGNATURE = [
'yt-autoexpand',
'Grzegorz Antoniak',
'0.1',
'BSD',
'Autoexpand YouTube links',
'weechat_unload',
'UTF-8'
]
class TooManyRedirectsError < Exception
def to_s
"Too many redirects"
end
end
class RequestError < Exception
def to_s
"HTTP request error"
end
end
class YoutubeExpander
def extract_link_from(message)
if message =~ /.*((http|https):\/\/www\.youtube\.com\/.*) .*/i
return $1.strip
end
if message =~ /.*((http|https):\/\/www\.youtube\.com\/.*)$/i
return $1.strip
end
if message =~ /.*((http|https):\/\/youtu\.be\/.*?) .*/i
return $1.strip
end
if message =~ /.*((http|https):\/\/youtu\.be\/.*?)$/i
return $1.strip
end
return nil
end
def fetch(url, limit)
raise TooManyRedirectsError if limit == 0
uri = URI(url)
req = Net::HTTP::Get.new "#{uri.path}?#{uri.query}", {
'User-Agent' => 'WeeChat autoexpander/1.0'
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
resp = http.request req
case resp
when Net::HTTPSuccess then
resp
when Net::HTTPRedirection then
return fetch(resp['location'], limit - 1)
else
raise RequestError
end
title = ""
description = ""
keywords = ""
res = resp.read_body
if res =~ /.*<title>(.*?)<\/title>.*/i
title = $1.gsub "- YouTube", ""
title.strip!
end
if res =~ /.*<meta name="description" content="(.*?)".*/i
description = $1.strip
end
if res =~ /.*,"keywords":"(.*?)".*/i
keywords = $1.strip.split(",").join ", "
end
{:title => title, :description => description, :keywords => keywords }
end
def expand(url)
fetch(url, 2)
rescue TooManyRedirectsError => e
{:error => "Too many redirects"}
rescue RequestError => e
{:error => "HTTP error"}
rescue URI::InvalidURIError => e
{:error => "URI error"}
end
end
def out(buf, msg)
Weechat.print(buf, msg)
end
def fix(str)
CGI.unescapeHTML(str)
end
def hook_print_cb(data, buffer, date, tags, displayed, highlight, prefix, message)
yt = YoutubeExpander.new
url = yt.extract_link_from message
if url == nil
return Weechat::WEECHAT_RC_OK
end
info = yt.expand(url)
if info == nil
out(buffer, "Error while autoexpanding this YouTube URL: #{url}")
return Weechat::WEECHAT_RC_OK
end
title = fix(info[:title])
description = fix(info[:description])
keywords = fix(info[:keywords])
c1 = Weechat.color("red,white")
c2 = Weechat.color("white,red")
cd = Weechat.color("reset")
ctext = Weechat.color("darkgray")
banner = "[#{c1}You#{c2}Tube#{cd}"
if title != ""
out(buffer, "#{banner} #{ctext}#{title}#{cd}]")
out(buffer, "#{banner} desc: #{ctext}#{description}#{cd}]") unless description == ""
out(buffer, "#{banner} tags: #{ctext}#{keywords}#{cd}]") unless keywords == ""
else
out(buffer, "YouTube auto-expansion failed for this URL!")
end
Weechat::WEECHAT_RC_OK
end
def weechat_init
Weechat.register(*$SIGNATURE)
Weechat.hook_print("", "notify_message", "://", 1, "hook_print_cb", "")
Weechat.hook_print("", "notify_private", "://", 1, "hook_print_cb", "")
Weechat.hook_print("", "notify_none", "://", 1, "hook_print_cb", "")
Weechat::WEECHAT_RC_OK
end
def weechat_unload
Weechat::WEECHAT_RC_OK
end