Optimized for API

This commit is contained in:
2023-01-17 14:11:23 +01:00
parent 9f0ed202da
commit aebe804b58
7 changed files with 156 additions and 36 deletions

View File

@@ -1,10 +1,12 @@
from os import listdir, sep
from os import listdir, remove, sep
from random import choice
from shutil import move
from shutil import copyfileobj, move
from requests import get
from traceback import format_exc
from pyrogram.client import Client
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from modules.logger import logWrite
from modules.api_client import authorize, move_pic, random_pic
from modules.utils import jsonLoad, jsonSave, configGet, locale
async def send_content(app: Client):
@@ -13,40 +15,66 @@ async def send_content(app: Client):
try:
index = jsonLoad(configGet("index", "locations"))
list_queue = listdir(configGet("queue", "locations"))
for file in list_queue:
if configGet("api_based", "mode"):
if not file in index["sent"]:
try:
pic = random_pic()
except:
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
await app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale")))
return
ext_match = False
token = authorize()
for ext in configGet("photo", "posting", "extensions"):
if file.endswith(ext):
ext_match = True
ext_type = "photo"
break
response = get(f'{configGet("address", "posting", "api")}/photos/{pic[0]}', headers={"Authorization": f"Bearer {token}"}, stream=True)
for ext in configGet("video", "posting", "extensions"):
if file.endswith(ext):
ext_match = True
ext_type = "video"
break
with open(configGet("tmp", "locations")+sep+pic[0]+".jpg", 'wb') as out_file:
copyfileobj(response.raw, out_file)
if not ext_match:
del response
candidate = configGet("tmp", "locations")+sep+pic[0]+".jpg"
candidate_file = pic[1]
ext_type = "photo"
if not configGet("api_based", "mode"):
list_queue = listdir(configGet("queue", "locations"))
for file in list_queue:
if not file in index["sent"]:
ext_match = False
for ext in configGet("photo", "posting", "extensions"):
if file.endswith(ext):
ext_match = True
ext_type = "photo"
break
for ext in configGet("video", "posting", "extensions"):
if file.endswith(ext):
ext_match = True
ext_type = "video"
break
if not ext_match:
list_queue.remove(file)
else:
list_queue.remove(file)
if len(list_queue) > 0:
candidate_file = choice(list_queue)
candidate = configGet("queue", "locations")+sep+candidate_file
else:
list_queue.remove(file)
if len(list_queue) > 0:
candidate_file = choice(list_queue)
candidate = configGet("queue", "locations")+sep+candidate_file
else:
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
await app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale")))
return
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
await app.send_message(configGet("admin"), locale("post_empty", "message", locale=configGet("locale")))
return
if candidate_file in index["captions"]:
caption = index["captions"][candidate_file]
@@ -84,6 +112,10 @@ async def send_content(app: Client):
else:
return
if configGet("api_based", "mode"):
remove(configGet("tmp", "locations")+sep+pic[0]+".jpg")
move_pic(pic[0])
index["sent"].append(candidate_file)
index["last_id"] = sent.id