Created
July 6, 2012 15:40
-
-
Save ebibibi/3060973 to your computer and use it in GitHub Desktop.
get instagram picture url from page of instagram.
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
| require 'rubygems' | |
| require 'net/http' | |
| require 'uri' | |
| require 'json' | |
| class ExpandUrl | |
| def self.expand_url(url) | |
| uri = url.kind_of?(URI) ? url : URI.parse(url) | |
| Net::HTTP.start(uri.host, uri.port) { |io| | |
| r = io.head(uri.path) | |
| r['Location'] || uri.to_s | |
| } | |
| end | |
| end | |
| class Instagram | |
| def self.isInstagram?(url) | |
| if url =~ /http:\/\/instagr\.am\/.*/ | |
| return true | |
| end | |
| begin | |
| url = ExpandUrl.expand_url(url) | |
| if url =~ /http:\/\/instagr\.am\/.*/ | |
| return true | |
| end | |
| rescue | |
| return false | |
| end | |
| false | |
| end | |
| def self.imgsrc(url) | |
| if (!(url =~ /http:\/\/instagr\.am\/.*/)) | |
| url = ExpandUrl.expand_url(url) | |
| end | |
| host = host = "api.instagram.com" | |
| page = "/oembed?url=" + url | |
| insta = JSON.parse(Net::HTTP.get(host, page)) | |
| insta['url'] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment