From 095fe7e5fdc4c0307a2452c9e8a5ae1a1136258f Mon Sep 17 00:00:00 2001 From: horhik Date: Tue, 31 Aug 2021 17:03:06 +0300 Subject: [PATCH] init commit --- Dockerfile | 9 +++++++ __init__.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ __init__.py~ | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 Dockerfile create mode 100644 __init__.py create mode 100644 __init__.py~ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..de84e79 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python + +RUN pip install instabot +RUN pip3 install Mastodon.py + +WORKDIR /app +COPY . /app + +ENTRYPOINT [ "python", "/app/__init__.py", "innubis"] diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..3d7db0e --- /dev/null +++ b/__init__.py @@ -0,0 +1,73 @@ +import os +import sys +import requests +from instabot import Bot +from mastodon import Mastodon + +id_filename = "already_posted.txt" +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] + +bot = Bot() +bot.login(username = username, password = passwd) + +mastodon = Mastodon( + access_token = mastodon_token, + api_base_url = 'https://mastodon.ml' +) + +def get_post(media_id, filename): + media = bot.get_media_info(media_id)[0] + id = media["id"] + post_text = media["caption"]["text"] + link = bot.get_media_id_from_link(id) + img = [] + if ("image_versions2" in media.keys()): + url = media["image_versions2"]["candidates"][0]["url"] + response = requests.get(url) + response.raw.decode_content = True + img.append(response.content) + 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 + img.append(response.content) + return { + "id" : id, + "text": post_text, + "link": link, + "img" : img + } + +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() + + + + +twony_last_medias = bot.get_user_medias(fetched_user, filtration = None) +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"])): + media = mastodon.media_post(media_file = post["img"], mime_type = "image/jpeg", description = post["text"]) # sending image to mastodon + post_text = str(post["text"]) + "\n" + "crosposted from " + str(post["link"]) # creating post text + mastodon.status_post(post_text, media_ids = [media["id"]]) # attaching image to post and creating a toot + add_id(post["id"]) # pushing id to "already_posted" file + diff --git a/__init__.py~ b/__init__.py~ new file mode 100644 index 0000000..f366021 --- /dev/null +++ b/__init__.py~ @@ -0,0 +1,74 @@ +import os +import sys +import requests +from instabot import Bot +from mastodon import Mastodon + +id_filename = "already_posted.txt" +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] + +bot = Bot() +bot.login(username = username, password = passwd) + +mastodon = Mastodon( + access_token = mastodon_token, + api_base_url = 'https://mastodon.ml' +) +mastodon.status_post("test", media_ids = ['106850837805381319']) + +def get_post(media_id, filename): + media = bot.get_media_info(media_id)[0] + id = media["id"] + post_text = media["caption"]["text"] + link = bot.get_media_id_from_link(id) + img = None + if ("image_versions2" in media.keys()): + url = media["image_versions2"]["candidates"][0]["url"] + response = requests.get(url) + response.raw.decode_content = True + img = response.content + 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 + img = response.content + return { + "id" : id, + "text": post_text, + "link": link, + "img" : img + } + +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() + + + + +twony_last_medias = bot.get_user_medias(fetched_user, filtration = None) +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"])): + media = mastodon.media_post(media_file = post["img"], mime_type = "image/jpeg", description = post["text"]) # sending image to mastodon + post_text = post["text"] + "\n" + "crosposted from " + post["link"] # creating post text + mastodon.status_post(post_text, media_ids = [media["id"]]) # attaching image to post and creating a toot + add_id(post["id"]) # pushing id to "already_posted" file +