2021-08-31 14:03:06 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import requests
|
2021-08-31 20:44:43 +00:00
|
|
|
import time
|
2021-09-01 13:31:33 +00:00
|
|
|
import hashlib
|
2021-08-31 14:03:06 +00:00
|
|
|
from mastodon import Mastodon
|
2021-08-31 15:47:21 +00:00
|
|
|
from colorama import Fore, Back, Style
|
2021-08-31 20:35:05 +00:00
|
|
|
from instaloader import Profile, Instaloader
|
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")
|
2021-08-31 18:57:12 +00:00
|
|
|
f.write("\n")
|
2021-08-31 14:03:06 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
fetched_user = sys.argv[1]
|
2021-08-31 20:57:31 +00:00
|
|
|
mastodon_instance = sys.argv[2]
|
|
|
|
mastodon_token = sys.argv[3]
|
2021-08-31 14:03:06 +00:00
|
|
|
|
2021-09-01 13:31:33 +00:00
|
|
|
post_limit = 100
|
2021-09-01 04:14:05 +00:00
|
|
|
time_interval_sec = 1000
|
|
|
|
|
2021-08-31 20:35:05 +00:00
|
|
|
print(Fore.GREEN + '🚀 > Connecting to Instagram...')
|
2021-08-31 15:47:21 +00:00
|
|
|
print(Style.RESET_ALL)
|
2021-08-31 20:35:05 +00:00
|
|
|
|
|
|
|
L = Instaloader()
|
|
|
|
profile = Profile.from_username(L.context, fetched_user)
|
2021-08-31 14:03:06 +00:00
|
|
|
|
2021-08-31 18:57:12 +00:00
|
|
|
print(Fore.GREEN + '🚀 > Connecting to Mastodon/Pixelfed...')
|
2021-08-31 15:47:21 +00:00
|
|
|
print(Style.RESET_ALL)
|
2021-08-31 14:03:06 +00:00
|
|
|
mastodon = Mastodon(
|
|
|
|
access_token = mastodon_token,
|
2021-08-31 20:57:31 +00:00
|
|
|
api_base_url = mastodon_instance
|
2021-08-31 18:57:12 +00:00
|
|
|
# api_base_url = 'https://pixelfed.tokyo/'
|
2021-08-31 14:03:06 +00:00
|
|
|
)
|
|
|
|
|
2021-08-31 20:35:05 +00:00
|
|
|
def get_image(url):
|
2021-09-01 04:14:05 +00:00
|
|
|
try:
|
2021-08-31 20:35:05 +00:00
|
|
|
print(Fore.YELLOW + "🚀 > Downloading Image...", url)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
2021-08-31 14:03:06 +00:00
|
|
|
response = requests.get(url)
|
|
|
|
response.raw.decode_content = True
|
2021-08-31 20:35:05 +00:00
|
|
|
|
|
|
|
print(Fore.GREEN + "✨ > Downloaded!")
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
return response.content
|
|
|
|
except:
|
|
|
|
|
|
|
|
print(Fore.RED + "💥 > Failed to download image.")
|
|
|
|
print(Style.RESET_ALL)
|
2021-08-31 14:03:06 +00:00
|
|
|
|
2021-09-01 13:31:33 +00:00
|
|
|
|
2021-08-31 14:03:06 +00:00
|
|
|
def already_posted(id):
|
2021-08-31 20:35:05 +00:00
|
|
|
file = open(id_filename);
|
2021-09-01 13:31:33 +00:00
|
|
|
content = file.read().split("\n")
|
|
|
|
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
|
|
|
|
if sha1 in content:
|
|
|
|
file.close()
|
|
|
|
return True
|
|
|
|
file.close()
|
|
|
|
return False
|
2021-08-31 14:03:06 +00:00
|
|
|
|
2021-08-31 20:35:05 +00:00
|
|
|
def mark_as_posted(id):
|
2021-08-31 14:03:06 +00:00
|
|
|
file = open(id_filename, 'a');
|
2021-09-01 13:31:33 +00:00
|
|
|
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
|
|
|
|
file.write(sha1+'\n')
|
2021-08-31 14:03:06 +00:00
|
|
|
file.close()
|
|
|
|
|
2021-08-31 20:35:05 +00:00
|
|
|
def upload_image_to_mastodon(url):
|
|
|
|
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)
|
|
|
|
except:
|
|
|
|
print(Fore.RED + "💥 > failed to upload image to mastodon")
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
return media["id"]
|
|
|
|
|
|
|
|
def toot(url, title ):
|
|
|
|
try:
|
|
|
|
print(Fore.YELLOW + "🐘 > Creating Toot...", title)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
id = upload_image_to_mastodon(url)
|
2021-08-31 20:50:06 +00:00
|
|
|
post_text = str(title) + "\n" + "crosposted from https://instagram.com/"+fetched_user # creating post text
|
2021-08-31 20:35:05 +00:00
|
|
|
print(id)
|
|
|
|
mastodon.status_post(post_text, media_ids = [id])
|
2021-08-31 14:03:06 +00:00
|
|
|
|
2021-08-31 20:35:05 +00:00
|
|
|
except:
|
|
|
|
print(Fore.RED + "😿 > Failed to create toot")
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
|
|
|
|
def none_convert(title):
|
|
|
|
if title == None:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return str(title)
|
|
|
|
|
|
|
|
def generate_title(post):
|
|
|
|
text = ""
|
|
|
|
try:
|
|
|
|
print(post.title)
|
|
|
|
text += none_convert(post.title) + "\n"
|
|
|
|
except:
|
|
|
|
print("no title")
|
|
|
|
try:
|
|
|
|
print(post.accessibility_caption)
|
|
|
|
text += none_convert(post.accessibility_caption) + "\n"
|
|
|
|
except:
|
|
|
|
print("no accessibilitycaption")
|
|
|
|
try:
|
|
|
|
print(post.edge_media_to_caption['edges'][0]['node']['text'])
|
|
|
|
text += none_convert(post.edge_media_to_caption['edges'][0]['node']['text'])
|
|
|
|
except:
|
|
|
|
print("no edge_media_to_caption")
|
|
|
|
return text
|
|
|
|
# 'edge_media_to_caption': {'edges': [{'node': {'text': 'Good morning!\n#komikaki #всемкартинки'}}]}
|
|
|
|
|
|
|
|
posts = profile.get_posts()
|
2021-08-31 20:44:43 +00:00
|
|
|
def get_new_posts():
|
2021-08-31 20:50:06 +00:00
|
|
|
stupidcounter = 0
|
2021-08-31 20:44:43 +00:00
|
|
|
for post in posts:
|
2021-09-01 04:14:05 +00:00
|
|
|
stupidcounter += 1
|
2021-09-01 13:31:33 +00:00
|
|
|
if stupidcounter <= post_limit:
|
2021-08-31 20:44:43 +00:00
|
|
|
if already_posted(str(post.url)):
|
2021-09-01 13:31:33 +00:00
|
|
|
print(Fore.YELLOW + "🐘 > Already Posted ", post.url)
|
2021-08-31 20:44:43 +00:00
|
|
|
print(Style.RESET_ALL)
|
|
|
|
continue
|
2021-09-01 13:31:33 +00:00
|
|
|
print("Posting... ", post.url)
|
|
|
|
# toot(post.url, post.caption)
|
2021-08-31 20:44:43 +00:00
|
|
|
mark_as_posted(str(post.url))
|
|
|
|
else:
|
2021-09-01 04:14:05 +00:00
|
|
|
return
|
2021-08-31 20:44:43 +00:00
|
|
|
|
|
|
|
|
2021-08-31 20:50:06 +00:00
|
|
|
while True:
|
2021-08-31 20:44:43 +00:00
|
|
|
get_new_posts()
|
2021-09-01 04:14:05 +00:00
|
|
|
time.sleep(time_interval_sec)
|