Created
July 15, 2024 16:36
-
-
Save joewils/ad9de5032068a8f28720f9f3482e6490 to your computer and use it in GitHub Desktop.
Ruby script to archive a copy of your Instagram images and movies using the official basic display api
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
| # Ruby script to archive a copy of your Instagram images and movies | |
| # USAGE: ruby ig.rb | |
| # https://developers.facebook.com/docs/instagram-basic-display-api/ | |
| # Setup Instagram App | |
| # 1. login to your facebook developer account | |
| # 2. create an app | |
| # 3. setup instagram basic display api | |
| # 4. invite yourself as an instagram tester | |
| # Accept Instagram Tester invite | |
| # 1. login to your instagram account | |
| # 2. settings --> apps and websites --> tester invites --> accept invitation | |
| # Generate and Retrieve Access Token | |
| # 1. login to facebook developer account | |
| # 2. goto your app | |
| # 3. basic display --> user token generator --> generate token | |
| # 4. save token as an environment variable | |
| access_token = ENV['JEKYLL_INSTAGRAM_ACCESS_TOKEN'] | |
| require 'date' | |
| require 'json' | |
| require 'httparty' | |
| def archive_media(id, url, type) | |
| puts url | |
| # Video or Image | |
| if type == 'VIDEO' | |
| file_extension = '.mp4' | |
| else | |
| file_extension = '.jpg' | |
| end | |
| # Archive Image | |
| filename = 'instagram/'+id+file_extension | |
| if !File.exist? filename | |
| File.open(filename, "w") do |file| | |
| file.binmode | |
| HTTParty.get(url, stream_body: true) do |fragment| | |
| file.write(fragment) | |
| end | |
| end | |
| end | |
| return filename | |
| end | |
| # Media Request | |
| media_request = HTTParty.get('https://graph.instagram.com/me/media?fields=id,permalink,caption,media_type,media_url,timestamp,children&access_token='+access_token) | |
| media = JSON.parse(media_request.to_s) | |
| # Process Data | |
| if media and media.has_key? 'data' | |
| media['data'].each do |asset| | |
| asset_date = Date.parse(asset['timestamp']) | |
| # Archive media | |
| archive_media(asset['id'], asset['media_url'], media['media_type']) | |
| # Children? | |
| if asset['children'] and asset['children']['data'].length > 0 | |
| asset['children']['data'].each_with_index do |child_asset,idx| | |
| # skip the first asset | |
| next if idx == 0 | |
| # look up media asset | |
| media_request = HTTParty.get('https://graph.instagram.com/'+child_asset['id'].to_s+'?fields=id,media_type,media_url,timestamp&access_token='+access_token) | |
| if media_request | |
| media = JSON.parse(media_request.to_s) | |
| # Archive media | |
| archive_media(media['id'], media['media_url'], media['media_type']) | |
| end | |
| end | |
| end # children? | |
| end # data | |
| end # media? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment