Instagram2Fedi/__init__.py

96 lines
2.9 KiB
Python
Raw Normal View History

2021-08-31 14:03:06 +00:00
import os
import sys
import requests
from instabot import Bot
from mastodon import Mastodon
2021-08-31 15:47:21 +00:00
from colorama import Fore, Back, Style
2021-08-31 14:03:06 +00:00
2021-08-31 14:45:42 +00:00
id_filename = "/app/already_posted.txt"
2021-08-31 14:03:06 +00:00
f = open(id_filename, "a")
f.close()
fetched_user = sys.argv[1]
username = sys.argv[2]
passwd = sys.argv[3]
mastodon_token = sys.argv[4]
2021-08-31 15:47:21 +00:00
print(Fore.GREEN + '🚀> Loginning into Instagram...')
print(Style.RESET_ALL)
2021-08-31 14:03:06 +00:00
bot = Bot()
bot.login(username = username, password = passwd)
2021-08-31 15:47:21 +00:00
print(Fore.GREEN + '🚀> Connecting to Mastodon/Pixelfed...')
print(Style.RESET_ALL)
2021-08-31 14:03:06 +00:00
mastodon = Mastodon(
access_token = mastodon_token,
2021-08-31 15:47:21 +00:00
# api_base_url = 'https://mastodon.ml'
api_base_url = 'https://pixelfed.tokyo/'
2021-08-31 14:03:06 +00:00
)
def get_post(media_id, filename):
2021-08-31 15:47:21 +00:00
print(Fore.YELLOW + '🔃> getting post: ' + media_id)
print(Style.RESET_ALL)
2021-08-31 14:03:06 +00:00
media = bot.get_media_info(media_id)[0]
id = media["id"]
post_text = media["caption"]["text"]
link = bot.get_media_id_from_link(id)
2021-08-31 14:45:42 +00:00
images = []
2021-08-31 14:03:06 +00:00
if ("image_versions2" in media.keys()):
url = media["image_versions2"]["candidates"][0]["url"]
response = requests.get(url)
response.raw.decode_content = True
2021-08-31 14:45:42 +00:00
images.append(response.content)
2021-08-31 14:03:06 +00:00
elif("carousel_media" in media.keys()):
for e, element in enumerate(media["carousel_media"]):
url = element['image_versions2']["candidates"][0]["url"]
response = requests.get(url)
response.raw.decode_content = True
2021-08-31 14:45:42 +00:00
images.append(response.content)
2021-08-31 14:03:06 +00:00
return {
"id" : id,
"text": post_text,
"link": link,
2021-08-31 14:45:42 +00:00
"images" : images
2021-08-31 14:03:06 +00:00
}
def already_posted(id):
file = open(id_filename, 'r');
if id in file:
file.close()
return True
else:
file.close()
return False
def add_id(id):
file = open(id_filename, 'a');
file.write(id + "\n")
file.close()
2021-08-31 14:45:42 +00:00
def upload_images_to_mastodon(images_array):
ids = []
for i in images_array:
2021-08-31 15:47:21 +00:00
try:
media = mastodon.media_post(media_file = i, mime_type = "image/jpeg") # sending image to mastodon
ids.append(media["id"])
except:
print(Fore.RED + "💥> failed to send photo")
print(Style.RESET_ALL)
2021-08-31 14:45:42 +00:00
return ids
2021-08-31 14:03:06 +00:00
twony_last_medias = bot.get_user_medias(fetched_user, filtration = None)
2021-08-31 14:45:42 +00:00
2021-08-31 14:03:06 +00:00
for e,media_id in enumerate(twony_last_medias):
post = get_post(media_id, "img_"+str(e)) # getting post info
if(not already_posted(post["id"])):
2021-08-31 15:47:21 +00:00
try:
image_ids = upload_images_to_mastodon(post["images"])
post_text = str(post["text"]) + "\n" + "crosposted from " + str(post["link"]) # creating post text
mastodon.status_post(post_text, media_ids = image_ids) # attaching image to post and creating a toot
add_id(post["id"]) # pushing id to "already_posted" file
except:
print(Fore.RED + "😿> failed to create toot")
print(Style.RESET_ALL)
2021-08-31 14:03:06 +00:00