add adbility to post carousel
This commit is contained in:
parent
2f8b99c90d
commit
6b3e254f44
|
@ -3,7 +3,7 @@ FROM python
|
||||||
RUN pip install instabot
|
RUN pip install instabot
|
||||||
RUN pip3 install Mastodon.py
|
RUN pip3 install Mastodon.py
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
COPY . /app
|
COPY . /app
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
ENTRYPOINT [ "python", "/app/__init__.py", "innubis"]
|
ENTRYPOINT [ "python", "/app/__init__.py", "innubis"]
|
||||||
|
|
23
__init__.py
23
__init__.py
|
@ -4,7 +4,7 @@ import requests
|
||||||
from instabot import Bot
|
from instabot import Bot
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
|
|
||||||
id_filename = "already_posted.txt"
|
id_filename = "/app/already_posted.txt"
|
||||||
f = open(id_filename, "a")
|
f = open(id_filename, "a")
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
@ -26,23 +26,23 @@ def get_post(media_id, filename):
|
||||||
id = media["id"]
|
id = media["id"]
|
||||||
post_text = media["caption"]["text"]
|
post_text = media["caption"]["text"]
|
||||||
link = bot.get_media_id_from_link(id)
|
link = bot.get_media_id_from_link(id)
|
||||||
img = []
|
images = []
|
||||||
if ("image_versions2" in media.keys()):
|
if ("image_versions2" in media.keys()):
|
||||||
url = media["image_versions2"]["candidates"][0]["url"]
|
url = media["image_versions2"]["candidates"][0]["url"]
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
response.raw.decode_content = True
|
response.raw.decode_content = True
|
||||||
img.append(response.content)
|
images.append(response.content)
|
||||||
elif("carousel_media" in media.keys()):
|
elif("carousel_media" in media.keys()):
|
||||||
for e, element in enumerate(media["carousel_media"]):
|
for e, element in enumerate(media["carousel_media"]):
|
||||||
url = element['image_versions2']["candidates"][0]["url"]
|
url = element['image_versions2']["candidates"][0]["url"]
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
response.raw.decode_content = True
|
response.raw.decode_content = True
|
||||||
img.append(response.content)
|
images.append(response.content)
|
||||||
return {
|
return {
|
||||||
"id" : id,
|
"id" : id,
|
||||||
"text": post_text,
|
"text": post_text,
|
||||||
"link": link,
|
"link": link,
|
||||||
"img" : img
|
"images" : images
|
||||||
}
|
}
|
||||||
|
|
||||||
def already_posted(id):
|
def already_posted(id):
|
||||||
|
@ -59,15 +59,20 @@ def add_id(id):
|
||||||
file.write(id + "\n")
|
file.write(id + "\n")
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
def upload_images_to_mastodon(images_array):
|
||||||
|
ids = []
|
||||||
|
for i in images_array:
|
||||||
|
media = mastodon.media_post(media_file = i, mime_type = "image/jpeg") # sending image to mastodon
|
||||||
|
ids.append(media["id"])
|
||||||
|
return ids
|
||||||
|
|
||||||
twony_last_medias = bot.get_user_medias(fetched_user, filtration = None)
|
twony_last_medias = bot.get_user_medias(fetched_user, filtration = None)
|
||||||
|
|
||||||
for e,media_id in enumerate(twony_last_medias):
|
for e,media_id in enumerate(twony_last_medias):
|
||||||
post = get_post(media_id, "img_"+str(e)) # getting post info
|
post = get_post(media_id, "img_"+str(e)) # getting post info
|
||||||
if(not already_posted(post["id"])):
|
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
|
image_ids = upload_images_to_mastodon(post["images"])
|
||||||
post_text = str(post["text"]) + "\n" + "crosposted from " + str(post["link"]) # creating post text
|
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
|
mastodon.status_post(post_text, media_ids = image_ids) # attaching image to post and creating a toot
|
||||||
add_id(post["id"]) # pushing id to "already_posted" file
|
add_id(post["id"]) # pushing id to "already_posted" file
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue