2021-11-14 19:47:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-09-06 08:39:24 +00:00
|
|
|
from colorama import Fore, Back, Style
|
|
|
|
import requests
|
2021-09-07 08:44:47 +00:00
|
|
|
import time
|
|
|
|
from already_posted import already_posted, mark_as_posted
|
2021-09-06 08:39:24 +00:00
|
|
|
from converters import split_array, try_to_get_carousel
|
2021-09-07 08:44:47 +00:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def get_image(url):
|
|
|
|
try:
|
|
|
|
print(Fore.YELLOW + "🚀 > Downloading Image...", url)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
response = requests.get(url)
|
|
|
|
response.raw.decode_content = True
|
|
|
|
|
|
|
|
print(Fore.GREEN + "✨ > Downloaded!")
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
return response.content
|
2021-09-07 09:34:29 +00:00
|
|
|
except Exception as e:
|
2021-09-07 08:44:47 +00:00
|
|
|
|
2021-09-07 09:34:29 +00:00
|
|
|
print(Fore.RED + "💥 > Failed to download image. \n", e)
|
2021-09-07 08:44:47 +00:00
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
2021-09-06 08:39:24 +00:00
|
|
|
|
|
|
|
def upload_image_to_mastodon(url, mastodon):
|
|
|
|
try:
|
|
|
|
print(Fore.YELLOW + "🐘 > Uploading Image...")
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
media = mastodon.media_post(media_file = get_image(url), mime_type = "image/jpeg") # sending image to mastodon
|
|
|
|
print(Fore.GREEN + "✨ > Uploaded!")
|
|
|
|
print(Style.RESET_ALL)
|
2021-09-07 08:44:47 +00:00
|
|
|
return media["id"]
|
2021-09-07 09:34:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(Fore.RED + "💥 > failed to upload image to mastodon. \n", e)
|
2021-09-06 08:39:24 +00:00
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
2021-09-07 08:44:47 +00:00
|
|
|
def toot(urls, title, mastodon, fetched_user ):
|
2021-09-07 09:34:29 +00:00
|
|
|
try:
|
|
|
|
print(Fore.YELLOW + "🐘 > Creating Toot...", title)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
ids = []
|
|
|
|
for url in urls:
|
|
|
|
ids.append(upload_image_to_mastodon(url, mastodon))
|
2021-09-08 19:01:49 +00:00
|
|
|
post_text = str(title) + "\n" + "crosposted from https://instagram.com/"+fetched_user # creating post text
|
|
|
|
post_text = post_text[0:1000]
|
2021-11-11 11:51:48 +00:00
|
|
|
if(ids):
|
|
|
|
print(ids)
|
|
|
|
mastodon.status_post(post_text, media_ids = ids)
|
2021-09-07 09:34:29 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(Fore.RED + "😿 > Failed to create toot \n", e)
|
|
|
|
print(Style.RESET_ALL)
|
2021-09-06 08:39:24 +00:00
|
|
|
|
2021-09-07 08:44:47 +00:00
|
|
|
def get_new_posts(mastodon, profile, mastodon_carousel_size, post_limit, already_posted_path, using_mastodon, carousel_size, post_interval, fetched_user):
|
2021-09-06 08:39:24 +00:00
|
|
|
posts = profile.get_posts()
|
|
|
|
stupidcounter = 0
|
|
|
|
for post in posts:
|
|
|
|
stupidcounter += 1
|
2021-09-07 08:44:47 +00:00
|
|
|
url_arr = try_to_get_carousel([post.url], post)
|
2021-09-06 08:39:24 +00:00
|
|
|
if stupidcounter <= post_limit:
|
2021-09-07 08:44:47 +00:00
|
|
|
if already_posted(str(post.mediaid), already_posted_path):
|
2021-09-06 08:39:24 +00:00
|
|
|
print(Fore.YELLOW + "🐘 > Already Posted ", post.url)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
continue
|
|
|
|
print("Posting... ", post.url)
|
|
|
|
if using_mastodon:
|
2021-09-07 08:44:47 +00:00
|
|
|
urls_arr = split_array(url_arr, carousel_size)
|
2021-09-06 08:39:24 +00:00
|
|
|
for urls in urls_arr:
|
2021-09-07 08:44:47 +00:00
|
|
|
toot(urls, post.caption, mastodon, fetched_user)
|
2021-09-06 08:39:24 +00:00
|
|
|
else:
|
2021-09-07 08:44:47 +00:00
|
|
|
toot(url_arr, post.caption, mastodon, fetched_user)
|
|
|
|
mark_as_posted(str(post.mediaid), already_posted_path)
|
2021-09-06 08:39:24 +00:00
|
|
|
time.sleep(post_interval)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
|