Skip to content

Instantly share code, notes, and snippets.

@cobodo
Last active October 22, 2018 10:26
Show Gist options
  • Save cobodo/a462f32cf2b633a98ca19137589e7d6b to your computer and use it in GitHub Desktop.
Save cobodo/a462f32cf2b633a98ca19137589e7d6b to your computer and use it in GitHub Desktop.
Plugin.create(:photo_support) do
amazon_json_regex = %r!'colorImages': { 'initial': (\[.*MAIN.*\])!.freeze
defimageopener('Amazon', %r<\Ahttps?://www\.amazon\.(?:co\.jp|com)/(?:.*/)?(?:dp/|gp/product/)[0-9A-Za-z_]+>) do |url|
html = HTTPClient.new.get_content(url, [], [['User-Agent', 'mikutter']])
m = amazon_json_regex.match(html)
next nil unless m
arr = JSON.parse(m[1], symbolize_names: true)
main = arr.find {|v| v[:variant] == "MAIN" }
open(main[:large]) # :hiRes, :thumb, :large, :lowRes (all variations might be nullable). :main is an array containing other variations of [{url => [width, height]}]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment