Last active
March 1, 2017 10:51
-
-
Save pboling/593637174f949a69feb290c5aa1f0a49 to your computer and use it in GitHub Desktop.
Extract image URL from Flickr Embedded Image Code for use in Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example: | |
# | |
# embedded = '<a data-flickr-embed="true" href="https://www.flickr.com/photos/galtzo/32796076230/in/album-72157679012034441/" title="IMG_2054"><img src="https://c1.staticflickr.com/3/2939/32796076230_773e54a3b0_h.jpg" width="1200" height="1600" alt="IMG_2054"></a><script async src="//embedr.flickr.com/assets/client-code.js" charset="utf-8"></script>' | |
# extract_embedded_flickr_image_url_for_google_sheets(embedded) | |
# | |
def extract_embedded_flickr_image_url_for_google_sheets(embedded, mode: 4, aspect: "4:3", height: nil, width: nil) | |
options = [] | |
url = embedded[(start = embedded.index("img src=\"https")+9)..(start+embedded[start..(-1)].index("g\" "))] | |
options << "\"#{url}\"" | |
options << mode.to_s | |
if mode == 4 | |
if height.nil? && width.nil? | |
options.concat( | |
case aspect | |
when "4:3" then | |
%w(100 75) | |
when "3:4" then | |
%w(75 100) | |
else | |
%w(100 100) | |
end | |
) | |
else | |
options << height.to_s | |
options << width.to_s | |
end | |
end | |
result = "=IMAGE(#{options.join(", ")})" | |
`printf '#{result}' | pbcopy` | |
puts "for Google Sheets:\n#{result}" | |
puts "copied to clipboard" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment