Skip to content

Instantly share code, notes, and snippets.

@jrnk
Last active July 30, 2019 10:41
Show Gist options
  • Save jrnk/a946bfa8eb87ab4a77729fe1aa92d2ef to your computer and use it in GitHub Desktop.
Save jrnk/a946bfa8eb87ab4a77729fe1aa92d2ef to your computer and use it in GitHub Desktop.
Get latest instagram photos from profile
# 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
@jrnk
Copy link
Author

jrnk commented May 6, 2019

to use just add http.rb gem first: bundle add http :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment