mirror of
https://github.com/Horhik/Instagram2Fedi.git
synced 2025-01-15 13:26:40 +00:00
add carousels and split code by files
This commit is contained in:
parent
34f1b3f5bd
commit
2710d025db
BIN
src/__pycache__/already_posted.cpython-39.pyc
Normal file
BIN
src/__pycache__/already_posted.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/converters.cpython-39.pyc
Normal file
BIN
src/__pycache__/converters.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/network.cpython-39.pyc
Normal file
BIN
src/__pycache__/network.cpython-39.pyc
Normal file
Binary file not shown.
|
@ -1,14 +1,14 @@
|
|||
|
||||
def already_posted(id, id_filename):
|
||||
with open(id_filename) as file:
|
||||
import hashlib
|
||||
def already_posted(id, path):
|
||||
with open(path) as file:
|
||||
content = file.read().split("\n")
|
||||
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
|
||||
if sha1 in content:
|
||||
return True
|
||||
return False
|
||||
|
||||
def mark_as_posted(id, id_filename):
|
||||
with open(id_filename, 'a') as file:
|
||||
def mark_as_posted(id, path):
|
||||
with open(path, 'a') as file:
|
||||
sha1 = hashlib.sha1(bytes(id, "utf-8")).hexdigest()
|
||||
file.write(sha1+'\n')
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from colorama import Fore, Back, Style
|
||||
|
||||
def split_array(arr, size):
|
||||
count = len(arr) // size + 1
|
||||
new_arr = []
|
||||
|
@ -6,12 +8,15 @@ def split_array(arr, size):
|
|||
return new_arr
|
||||
|
||||
|
||||
def try_to_get_carousel(arr, post):
|
||||
def try_to_get_carousel(array, post):
|
||||
try:
|
||||
urls = list(map(lambda arr: arr['node']['display_url'], vars(post)['_node']['edge_sidecar_to_children']['edges']))
|
||||
return urls
|
||||
print("Found carousel")
|
||||
print(Fore.GREEN + "🎠 > Found carousel!")
|
||||
print(Style.RESET_ALL)
|
||||
except:
|
||||
print("No carousel")
|
||||
return arr
|
||||
print(Fore.RED + "🎠💥 > No carousel :( ")
|
||||
print(Style.RESET_ALL)
|
||||
return array
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import os
|
||||
import sys
|
||||
import time
|
||||
import hashlib
|
||||
import json
|
||||
from mastodon import Mastodon
|
||||
from colorama import Fore, Back, Style
|
||||
from instaloader import Profile, Instaloader, LatestStamps
|
||||
|
||||
from network import get_new_posts
|
||||
|
||||
id_filename = "/app/already_posted.txt"
|
||||
with open(id_filename, "a") as f:
|
||||
f.write("\n")
|
||||
|
@ -36,5 +37,5 @@ mastodon = Mastodon(
|
|||
# api_base_url = 'https://pixelfed.tokyo/'
|
||||
)
|
||||
while True:
|
||||
get_new_posts(mastodon, profile)
|
||||
get_new_posts(mastodon, profile, mastodon_carousel_size, post_limit, id_filename, using_mastodon, mastodon_carousel_size, post_interval, fetched_user)
|
||||
time.sleep(time_interval_sec)
|
||||
|
|
114
src/network.py
114
src/network.py
|
@ -1,61 +1,9 @@
|
|||
from colorama import Fore, Back, Style
|
||||
import requests
|
||||
|
||||
import time
|
||||
from already_posted import already_posted, mark_as_posted
|
||||
from converters import split_array, try_to_get_carousel
|
||||
|
||||
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)
|
||||
except:
|
||||
print(Fore.RED + "💥 > failed to upload image to mastodon")
|
||||
print(Style.RESET_ALL)
|
||||
return media["id"]
|
||||
|
||||
def toot(urls, title ):
|
||||
try:
|
||||
print(Fore.YELLOW + "🐘 > Creating Toot...", title)
|
||||
print(Style.RESET_ALL)
|
||||
ids = []
|
||||
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
|
||||
print(ids)
|
||||
mastodon.status_post(post_text, media_ids = ids)
|
||||
|
||||
except:
|
||||
print(Fore.RED + "😿 > Failed to create toot")
|
||||
print(Style.RESET_ALL)
|
||||
|
||||
def get_new_posts(mastodon, profile, mastodon_carousel_size, ):
|
||||
posts = profile.get_posts()
|
||||
stupidcounter = 0
|
||||
for post in posts:
|
||||
stupidcounter += 1
|
||||
url_arr = try_to_get_carousel([post.url], post), mastodon_carousel_size
|
||||
|
||||
if stupidcounter <= post_limit:
|
||||
if already_posted(str(post.mediaid), id_filename):
|
||||
print(Fore.YELLOW + "🐘 > Already Posted ", post.url)
|
||||
print(Style.RESET_ALL)
|
||||
continue
|
||||
print("Posting... ", post.url)
|
||||
if using_mastodon:
|
||||
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), id_filename)
|
||||
time.sleep(post_interval)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
import hashlib
|
||||
|
||||
def get_image(url):
|
||||
try:
|
||||
|
@ -74,3 +22,59 @@ def get_image(url):
|
|||
print(Fore.RED + "💥 > Failed to download image.")
|
||||
print(Style.RESET_ALL)
|
||||
|
||||
|
||||
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)
|
||||
return media["id"]
|
||||
except:
|
||||
print(Fore.RED + "💥 > failed to upload image to mastodon")
|
||||
print(Style.RESET_ALL)
|
||||
|
||||
def toot(urls, title, mastodon, fetched_user ):
|
||||
#try:
|
||||
print(Fore.YELLOW + "🐘 > Creating Toot...", title)
|
||||
print(Style.RESET_ALL)
|
||||
ids = []
|
||||
for url in urls:
|
||||
ids.append(upload_image_to_mastodon(url, mastodon))
|
||||
print(url)
|
||||
post_text = str(title) + "\n" + "crosposted from https://instagram.com/"+fetched_user # creating post text
|
||||
post_text = post_text[0:1000]
|
||||
print(ids)
|
||||
mastodon.status_post(post_text, media_ids = ids)
|
||||
|
||||
#except:
|
||||
# print(urls)
|
||||
# print(Fore.RED + "😿 > Failed to create toot")
|
||||
# print(Style.RESET_ALL)
|
||||
|
||||
def get_new_posts(mastodon, profile, mastodon_carousel_size, post_limit, already_posted_path, using_mastodon, carousel_size, post_interval, fetched_user):
|
||||
posts = profile.get_posts()
|
||||
stupidcounter = 0
|
||||
for post in posts:
|
||||
stupidcounter += 1
|
||||
url_arr = try_to_get_carousel([post.url], post)
|
||||
if stupidcounter <= post_limit:
|
||||
if already_posted(str(post.mediaid), already_posted_path):
|
||||
print(Fore.YELLOW + "🐘 > Already Posted ", post.url)
|
||||
print(Style.RESET_ALL)
|
||||
continue
|
||||
print("Posting... ", post.url)
|
||||
if using_mastodon:
|
||||
urls_arr = split_array(url_arr, carousel_size)
|
||||
for urls in urls_arr:
|
||||
toot(urls, post.caption, mastodon, fetched_user)
|
||||
else:
|
||||
toot(url_arr, post.caption, mastodon, fetched_user)
|
||||
|
||||
mark_as_posted(str(post.mediaid), already_posted_path)
|
||||
time.sleep(post_interval)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue