Last active
August 12, 2021 21:50
-
-
Save LindseyB/180de9e29003ec71a5c8dfaa7200932c to your computer and use it in GitHub Desktop.
takes posts from instagram and posts them over to mastodon
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
source "https://rubygems.org" | |
gem 'mastodon-api', require: 'mastodon', github: 'tootsuite/mastodon-api' | |
gem 'instagram', require: '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 "bundler" | |
require "open-uri" | |
Bundler.require | |
# quick and dirty instagram client | |
client = Instagram.client(:access_token => [INSTAGRAM_ACCESS_TOKEN]) | |
m_client = Mastodon::REST::Client.new(base_url: 'https://mastodon.social', bearer_token: [MASTODON_ACCESS_TOKEN]) | |
# get the last media item id | |
last_media_id = client.user_recent_media(count: 1).first.id | |
# forever | |
while true do | |
# if there are new items | |
if items = client.user_recent_media(count: 1) | |
if items.first.id != last_media_id # This gem doesn't support the min_id argument so :/ | |
last_media_id = items.first.id | |
items.each do |item| | |
# create a toot for each media item | |
file = open(item.images.standard_resolution.url) | |
media_file = HTTP::FormData::File.new(file) | |
media = m_client.upload_media(media_file) | |
caption = item.caption ? item.caption.text : '' | |
m_client.create_status("#{caption} #{media.text_url}", in_reply_to_id = nil, media_ids = [media.id]) | |
puts "I TOOTED" | |
end | |
end | |
end | |
sleep 60 | |
end |
- You can get an access token for instagram using: http://instagram.pixelunion.net/ or do the handshake by hand using curl or postman
- For mastodon the following can help you quickly get an access token: https://tinysubversions.com/notes/mastodon-bot/index.html
Additional notes: Yes it's pulling the mastodon api directly from github this is bad but I wanted to really quickly get latest as it's in flux and latest is good for me right now.
Hi there LindseyB, I've just purchased / set up an instance via masto.host and I'd like to create a user and then feed an instagram feed / account via that user to the instance. Is this possible using the above?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work! Looks pretty clean!