Last active
July 30, 2019 10:41
-
-
Save jrnk/a946bfa8eb87ab4a77729fe1aa92d2ef to your computer and use it in GitHub Desktop.
Get latest instagram photos from profile
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
# usage: | |
# ig = Instagram.new(profile: "shrt").images | |
class Instagram | |
def initialize(args) | |
@profile = args[:profile] | |
@output = args[:output] ? args[:output] : :full | |
begin | |
response = HTTP.get("https://www.instagram.com/#{@profile}/") | |
match_regex = /<script type="text\/javascript">window\._sharedData = (.*);<\/script>/ | |
@parsed = JSON.parse(response.body.to_s.match(match_regex)[1]) | |
rescue | |
raise "Failed to parse instagram page" | |
end | |
end | |
def images | |
media = @parsed["entry_data"]["ProfilePage"][0]["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"] | |
images = [] | |
media.each do |res| | |
node = res["node"] | |
item = { | |
code: node["shortcode"], | |
thumb: node["thumbnail_src"], | |
} | |
item[:caption] = node.dig("edge_media_to_caption", "edges", 0, "node", "text") | |
item[:likes] = node.dig("edge_media_preview_like", "count") | |
images << item | |
end | |
images | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to use just add
http.rb
gem first:bundle add http
:p