Skip to content

Instantly share code, notes, and snippets.

@LindseyB
Last active August 12, 2021 21:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LindseyB/180de9e29003ec71a5c8dfaa7200932c to your computer and use it in GitHub Desktop.
Save LindseyB/180de9e29003ec71a5c8dfaa7200932c to your computer and use it in GitHub Desktop.
takes posts from instagram and posts them over to mastodon
source "https://rubygems.org"
gem 'mastodon-api', require: 'mastodon', github: 'tootsuite/mastodon-api'
gem 'instagram', require: 'instagram'
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
@mitchellurgero
Copy link

Good work! Looks pretty clean!

@LindseyB
Copy link
Author

@LindseyB
Copy link
Author

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.

Copy link

ghost commented Feb 2, 2018

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