from typing import Tuple import os from ig import ctxPictuterProma, IG from db import DBHelper from utils import fileProcessor from utils.const import DEBUG # returns (errmsg | code, errcode) def upload(aid:int) -> Tuple[str, int]: # 抓取文章本體 article = DBHelper.solo_article_fetcher(key = aid) if article is None: return "Post not found", 1 # 抓取檔案 files = [ DBHelper.solo_file_fetcher(id = k) for k in article["files"] ] if None in files: return "File not found", 1 # 轉出暫存檔案 tmp_path:list = [] for t in files: filename, err = fileProcessor.file_saver(t.get("type"), t.get("binary")) if err: # 如果錯誤 return filename, 1 tmp_path.append(filename) # 合成文字圖 proma_file = ctxPictuterProma.new_proma(article["ctx"]) tmp_path = [proma_file] + tmp_path # 送交 IG 上傳 if not DEBUG: media = IG.upload_media(article["ctx"], tmp_path) if media is None: return "Upload failed", 1 else: media = {"code":"fake_data"} # 刪除檔案 for t in tmp_path: os.remove(t) return media["code"], 0 # return (errmsg, code) def remove(aid:int) -> Tuple[str, int]: # 抓取文章本體 article = DBHelper.solo_article_fetcher(key = aid) if article is None: return "Post not found", 1 err = IG.delete_media(article["igid"]) if err: return "Remove failed", 1 return "OK", 0