make several carousels if defautl carousel size is too small

This commit is contained in:
horhik 2021-09-06 10:02:50 +03:00
parent 005d6a7290
commit 6bf8f301fd
1 changed files with 36 additions and 42 deletions

View File

@ -9,9 +9,8 @@ from colorama import Fore, Back, Style
from instaloader import Profile, Instaloader, LatestStamps from instaloader import Profile, Instaloader, LatestStamps
id_filename = "/app/already_posted.txt" id_filename = "/app/already_posted.txt"
f = open(id_filename, "a") with open(id_filename, "a") as f:
f.write("\n") f.write("\n")
f.close()
fetched_user = sys.argv[1] fetched_user = sys.argv[1]
mastodon_instance = sys.argv[2] mastodon_instance = sys.argv[2]
@ -20,6 +19,10 @@ mastodon_token = sys.argv[3]
post_limit = 1 post_limit = 1
time_interval_sec = 86400 time_interval_sec = 86400
post_interval = 10 post_interval = 10
using_mastodon = True;
mastodon_carousel_size = 4
print(Fore.GREEN + '🚀 > Connecting to Instagram...') print(Fore.GREEN + '🚀 > Connecting to Instagram...')
print(Style.RESET_ALL) print(Style.RESET_ALL)
@ -53,20 +56,17 @@ def get_image(url):
def already_posted(id): def already_posted(id):
file = open(id_filename); with open(id_filename) as file:
content = file.read().split("\n") content = file.read().split("\n")
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest() sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
if sha1 in content: if sha1 in content:
file.close()
return True return True
file.close() return False
return False
def mark_as_posted(id): def mark_as_posted(id):
file = open(id_filename, 'a'); with open(id_filename, 'a') as file:
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest() sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
file.write(sha1+'\n') file.write(sha1+'\n')
file.close()
def upload_image_to_mastodon(url): def upload_image_to_mastodon(url):
try: try:
@ -80,15 +80,16 @@ def upload_image_to_mastodon(url):
print(Style.RESET_ALL) print(Style.RESET_ALL)
return media["id"] return media["id"]
def toot(url, title ): def toot(urls, title ):
try: try:
print(Fore.YELLOW + "🐘 > Creating Toot...", title) print(Fore.YELLOW + "🐘 > Creating Toot...", title)
print(Style.RESET_ALL) print(Style.RESET_ALL)
ids = []
id = upload_image_to_mastodon(url) for url in urls:
ids.append(upload_image_to_mastodon(url))
post_text = str(title) + "\n" + "crosposted from https://instagram.com/"+fetched_user # creating post text post_text = str(title) + "\n" + "crosposted from https://instagram.com/"+fetched_user # creating post text
print(id) print(ids)
mastodon.status_post(post_text, media_ids = [id]) mastodon.status_post(post_text, media_ids = ids)
except: except:
print(Fore.RED + "😿 > Failed to create toot") print(Fore.RED + "😿 > Failed to create toot")
@ -100,26 +101,6 @@ def none_convert(title):
else: else:
return str(title) 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 #всемкартинки'}}]}
def try_to_get_carousel(arr, post): def try_to_get_carousel(arr, post):
try: try:
urls = list(map(lambda arr: arr['node']['display_url'], vars(post)['_node']['edge_sidecar_to_children']['edges'])) urls = list(map(lambda arr: arr['node']['display_url'], vars(post)['_node']['edge_sidecar_to_children']['edges']))
@ -129,20 +110,33 @@ def try_to_get_carousel(arr, post):
print("No carousel") print("No carousel")
return arr return arr
def split_array(arr, size):
count = len(arr) // size + 1
new_arr = []
for i in range(count):
new_arr.append(arr[i*size:(i+1)*mastodon_carousel_size])
return new_arr
posts = profile.get_posts() posts = profile.get_posts()
def get_new_posts(): def get_new_posts():
stupidcounter = 0 stupidcounter = 0
for post in posts: for post in posts:
stupidcounter += 1 stupidcounter += 1
urls = try_to_get_carousel([post.url], post) url_arr = try_to_get_carousel([post.url], post), mastodon_carousel_size
if stupidcounter <= post_limit: if stupidcounter <= post_limit:
if already_posted(str(post.mediaid)): if already_posted(str(post.mediaid)):
print(Fore.YELLOW + "🐘 > Already Posted ", post.url) print(Fore.YELLOW + "🐘 > Already Posted ", post.url)
print(Style.RESET_ALL) print(Style.RESET_ALL)
continue continue
print("Posting... ", post.url) print("Posting... ", post.url)
for url in urls: if using_mastodon:
toot(url, post.caption) urls_arr = split_array(url_arr)
for urls in urls_arr:
toot(urls, post.caption)
else:
toot(url_arr, post.caption)
mark_as_posted(str(post.mediaid)) mark_as_posted(str(post.mediaid))
time.sleep(post_interval) time.sleep(post_interval)
else: else: