| 1 |
|
| 2 |
|
| 3 |
require 'nokogiri' |
| 4 |
require 'httpclient' |
| 5 |
require 'json' |
| 6 |
|
| 7 |
module Plugin::PhotoSupport |
| 8 |
SUPPORTED_IMAGE_FORMATS = %w[jpeg jpg png gif].freeze |
| 9 |
deprecate_constant :SUPPORTED_IMAGE_FORMATS |
| 10 |
|
| 11 |
INSTAGRAM_PATTERN = %r{\Ahttps?://(?:instagr\.am|(?:www\.)?instagram\.com)/p/([a-zA-Z0-9_\-]+)/} |
| 12 |
GITHUB_IMAGE_PATTERN = %r<\Ahttps://github\.com/(\w+/\w+)/blob/(.*\.(?:jpe?g|png|gif))\z> |
| 13 |
YOUTUBE_PATTERN = %r<\Ahttps?://(?:www\.youtube\.com/watch\?v=|youtu\.be/)(\w+)> |
| 14 |
NICOVIDEO_PATTERN = %r<\Ahttps?://(?:(?:www|sp)\.nicovideo\.jp/watch|nico\.ms)/([sn][mo][1-9]\d*)> |
| 15 |
|
| 16 |
class << self |
| 17 |
extend Memoist |
| 18 |
|
| 19 |
def via_xpath(display_url, xpath) |
| 20 |
connection = HTTPClient.new |
| 21 |
connection.ssl_config.set_default_paths |
| 22 |
page = connection.get_content(display_url) |
| 23 |
unless page.empty? |
| 24 |
doc = Nokogiri::HTML(page) |
| 25 |
doc.xpath(xpath).first |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
URLに置き換える。 |
| 30 |
ように、このメソッドを通すことでメモ化している。 |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
jpg) |
| 35 |
def d250g2(display_url) |
| 36 |
r = via_xpath(display_url, "//meta[@name='twitter:image']/@content") |
| 37 |
return r if r |
| 38 |
via_xpath(display_url, "//meta[@name='twitter:image:src']/@content") |
| 39 |
end |
| 40 |
memoize :d250g2 |
| 41 |
|
| 42 |
像のURLに置き換える。 |
| 43 |
ように、このメソッドを通すことでメモ化している。 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
jpg) |
| 48 |
def インスタ映え(display_url) |
| 49 |
via_xpath(display_url, "//meta[@property='og:image']/@content") end |
| 50 |
memoize :"インスタ映え" |
| 51 |
end |
| 52 |
end |
| 53 |
|
| 54 |
Plugin.create :photo_support do |
| 55 |
|
| 56 |
defimageopener('twitpic', %r<^https?://twitpic\.com/[a-zA-Z0-9]+>) do |display_url| |
| 57 |
connection = HTTPClient.new |
| 58 |
connection.ssl_config.set_default_paths |
| 59 |
connection.transparent_gzip_decompression = true |
| 60 |
page = connection.get_content(display_url) |
| 61 |
next nil if page.empty? |
| 62 |
doc = Nokogiri::HTML(page) |
| 63 |
result = doc.css('img').lazy.find_all{ |dom| |
| 64 |
%r<https?://.*?\.cloudfront\.net/photos/(?:large|full)/.*> =~ dom.attribute('src') |
| 65 |
}.first |
| 66 |
URI.open(result.attribute('src')) |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
defimageopener('gyazo', %r<\Ahttps?://gyazo.com/[a-zA-Z0-9]+>) do |display_url| |
| 71 |
begin |
| 72 |
connection = HTTPClient.new |
| 73 |
connection.ssl_config.set_default_paths |
| 74 |
json = connection.get_content("https://api.gyazo.com/api/oembed", url: display_url) |
| 75 |
hash = JSON.parse(json, symbolize_names: true) |
| 76 |
URI.open(hash[:url]) if hash[:url] |
| 77 |
rescue => e |
| 78 |
error e.to_s |
| 79 |
nil |
| 80 |
end |
| 81 |
end |
| 82 |
|
| 83 |
|
| 84 |
defimageopener('piapro', %r<^http://piapro.jp/t/[a-zA-Z0-9]+>) do |display_url| |
| 85 |
connection = HTTPClient.new |
| 86 |
connection.ssl_config.set_default_paths |
| 87 |
page = connection.get_content(display_url) |
| 88 |
next nil if page.empty? |
| 89 |
doc = Nokogiri::HTML(page) |
| 90 |
dom = doc.css('.illust-whole img').first |
| 91 |
url = dom && dom.attribute('src') |
| 92 |
if url |
| 93 |
URI.open(url) end |
| 94 |
end |
| 95 |
|
| 96 |
フォトライフ |
| 97 |
defimageopener('はてなフォトライフ', %r<^http://f\.hatena\.ne\.jp/[-\w]+/\d{9,}>) do |display_url| |
| 98 |
connection = HTTPClient.new |
| 99 |
connection.ssl_config.set_default_paths |
| 100 |
page = connection.get_content(display_url) |
| 101 |
next nil if page.empty? |
| 102 |
doc = Nokogiri::HTML(page) |
| 103 |
result = doc.css('img.foto').first |
| 104 |
URI.open(result.attribute('src')) |
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
defimageopener('imgur', %r<\Ahttps?://imgur\.com(?:/gallery)?/[a-zA-Z0-9]+>) do |display_url| |
| 109 |
connection = HTTPClient.new |
| 110 |
connection.ssl_config.set_default_paths |
| 111 |
page = connection.get_content(display_url) |
| 112 |
next nil if page.empty? |
| 113 |
doc = Nokogiri::HTML(page) |
| 114 |
result = doc.css('link[rel="image_src"]').first |
| 115 |
URI.open(result.attribute('href').to_s) |
| 116 |
end |
| 117 |
|
| 118 |
ォト蔵 |
| 119 |
defimageopener('フォト蔵', %r<^http://photozou\.jp/photo/show/\d+/\d+>) do |display_url| |
| 120 |
connection = HTTPClient.new |
| 121 |
connection.ssl_config.set_default_paths |
| 122 |
page = connection.get_content(display_url) |
| 123 |
next nil if page.empty? |
| 124 |
doc = Nokogiri::HTML(page) |
| 125 |
URI.open(doc.css('img[itemprop="image"]').first.attribute('src')) |
| 126 |
end |
| 127 |
|
| 128 |
|
| 129 |
defimageopener('instagram', Plugin::PhotoSupport::INSTAGRAM_PATTERN) do |display_url| |
| 130 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 131 |
URI.open(img) if img |
| 132 |
end |
| 133 |
|
| 134 |
|
| 135 |
defimageopener('d250g2', %r#\Ahttps?://(?:[\w\-]+\.)?d250g2\.com/?\Z#) do |display_url| |
| 136 |
img = Plugin::PhotoSupport.d250g2(display_url) |
| 137 |
URI.open(img) if img |
| 138 |
end |
| 139 |
|
| 140 |
えたとき用) |
| 141 |
defimageopener('d250g2(Twitpicが消えたとき用)', %r#\Ahttp://twitpic\.com/d250g2\Z#) do |
| 142 |
URI.open('http://d250g2.com/d250g2.jpg') |
| 143 |
end |
| 144 |
|
| 145 |
|
| 146 |
defimageopener('600eur.gochiusa.net', %r#\Ahttp://600eur\.gochiusa\.net/?\Z#) do |display_url| |
| 147 |
img = Plugin::PhotoSupport.d250g2(display_url) |
| 148 |
URI.open(img) if img |
| 149 |
end |
| 150 |
|
| 151 |
defimageopener('いらすとや', %r<https?://(?:www.)?irasutoya\.com/\d{4}/\d{2}/.+\.html>) do |display_url| |
| 152 |
img = Plugin::PhotoSupport.d250g2(display_url) |
| 153 |
URI.open(img) if img |
| 154 |
end |
| 155 |
|
| 156 |
|
| 157 |
defimageopener('xkcd', %r<\Ahttps?://xkcd\.com/[0-9]+>) do |display_url| |
| 158 |
connection = HTTPClient.new |
| 159 |
connection.ssl_config.set_default_paths |
| 160 |
page = connection.get_content(display_url) |
| 161 |
next nil if page.empty? |
| 162 |
doc = Nokogiri::HTML(page) |
| 163 |
result = doc.css('#comic > img').first |
| 164 |
src = result.attribute('src').to_s |
| 165 |
if src.start_with?('//') |
| 166 |
src = Diva::URI.new(display_url).scheme + ':' + src |
| 167 |
end |
| 168 |
URI.open(src) |
| 169 |
end |
| 170 |
|
| 171 |
シュマロ |
| 172 |
defimageopener('marshmallow-qa', %r<\Ahttps?://marshmallow-qa\.com/messages/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>) do |display_url| |
| 173 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 174 |
URI.open(img) if img |
| 175 |
end |
| 176 |
|
| 177 |
|
| 178 |
defimageopener('peing', %r<\Ahttps?://peing\.net/\w+/(?:qs/\d+|q/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})>) do |display_url| |
| 179 |
img = Plugin::PhotoSupport.d250g2(display_url) |
| 180 |
URI.open(img) if img |
| 181 |
end |
| 182 |
|
| 183 |
ニコ静画 |
| 184 |
defimageopener('NicoSeiga', %r<\Ahttps?://seiga\.nicovideo\.jp/seiga/im\d+>) do |display_url| |
| 185 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 186 |
URI.open(img) if img |
| 187 |
end |
| 188 |
|
| 189 |
(nico.ms経由) |
| 190 |
defimageopener('NicoSeiga via nico.ms', %r<\Ahttps?://nico\.ms/im\d+>) do |display_url| |
| 191 |
connection = HTTPClient.new |
| 192 |
connection.ssl_config.set_default_paths |
| 193 |
location = connection.get(display_url).header['Location'].first |
| 194 |
next nil unless location |
| 195 |
img = Plugin::PhotoSupport.インスタ映え(location) |
| 196 |
URI.open(img) if img |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
defimageopener('github', Plugin::PhotoSupport::GITHUB_IMAGE_PATTERN) do |display_url| |
| 201 |
url = Plugin::PhotoSupport::GITHUB_IMAGE_PATTERN.match(display_url) do |m| |
| 202 |
"https://raw.githubusercontent.com/#{m[1]}/#{m[2]}" |
| 203 |
end |
| 204 |
URI.open(url) if url |
| 205 |
end |
| 206 |
|
| 207 |
|
| 208 |
amazon_json_regex1 = %r!'colorImages': { 'initial': (\[.*MAIN.*\])! |
| 209 |
amazon_json_regex2 = %r! data-a-dynamic-image="([^"]*)"! |
| 210 |
defimageopener('Amazon', %r<\Ahttps?://(?:www\.amazon\.(?:co\.jp|com)/(?:.*/)?(?:dp/|gp/product/)|amzn.asia/d/)[0-9A-Za-z_]+>) do |url| |
| 211 |
connection = HTTPClient.new |
| 212 |
connection.ssl_config.set_default_paths |
| 213 |
html = connection.get_content(url, [], [['User-Agent', 'mikutter']]) |
| 214 |
m = amazon_json_regex1.match(html) |
| 215 |
via_data_attr = false |
| 216 |
unless m |
| 217 |
m = amazon_json_regex2.match(html) |
| 218 |
via_data_attr = true |
| 219 |
end |
| 220 |
next nil unless m |
| 221 |
json = m[1] |
| 222 |
unless via_data_attr |
| 223 |
arr = JSON.parse(json, symbolize_names: true) |
| 224 |
main = arr.find {|v| v[:variant] == "MAIN" } |
| 225 |
image_url = main[:large] |
| 226 |
else |
| 227 |
json = CGI.unescapeHTML(json) |
| 228 |
hash = JSON.parse(json) |
| 229 |
max = hash.to_a.max {|v| v[1][0] } |
| 230 |
image_url = max[0] |
| 231 |
end |
| 232 |
URI.open(image_url) |
| 233 |
end |
| 234 |
|
| 235 |
|
| 236 |
defimageopener('reddit', %r|^https?://www\.reddit\.com/r/(?:[^/]*)/comments/(?:[^/]*)/(?:[^/]*)|) do |display_url| |
| 237 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 238 |
URI.open(img) if img |
| 239 |
end |
| 240 |
|
| 241 |
|
| 242 |
defimageopener('pixiv', %r<https?://(?:www\.)?pixiv\.net/member_illust\.php\?(?:.*)&?illust_id=\d+(?:&.*)?$>) do |display_url| |
| 243 |
img = Plugin::PhotoSupport.インスタ映え(display_url) << '&mode=sns-automator' |
| 244 |
URI.open(img) if img |
| 245 |
end |
| 246 |
|
| 247 |
|
| 248 |
defimageopener('pixiv', %r<https?://(?:www\.)?pixiv\.net/artworks/\d+$>) do |display_url| |
| 249 |
img = Plugin::PhotoSupport.インスタ映え(display_url) << '&mode=sns-automator' |
| 250 |
URI.open(img) if img |
| 251 |
end |
| 252 |
|
| 253 |
シドットコム |
| 254 |
defimageopener('ヨドバシドットコム', %r<\Ahttps://www\.yodobashi\.com/product/\d+>) do |display_url| |
| 255 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 256 |
URI.open(img) if img |
| 257 |
end |
| 258 |
|
| 259 |
|
| 260 |
defimageopener('YouTube', Plugin::PhotoSupport::YOUTUBE_PATTERN) do |display_url| |
| 261 |
url = Plugin::PhotoSupport::YOUTUBE_PATTERN.match(display_url) do |m| |
| 262 |
"https://img.youtube.com/vi/#{m[1]}/0.jpg" |
| 263 |
end |
| 264 |
URI.open(url) if url |
| 265 |
end |
| 266 |
|
| 267 |
ニコ動画 |
| 268 |
defimageopener('NicoVideo', Plugin::PhotoSupport::NICOVIDEO_PATTERN) do |display_url| |
| 269 |
img = Plugin::PhotoSupport.インスタ映え(display_url) |
| 270 |
URI.open(img) if img |
| 271 |
end |
| 272 |
end |