65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
|
from typing import Tuple
|
||
|
import os
|
||
|
|
||
|
from ig import contentPictuterProma, IG
|
||
|
from db import dbhelper
|
||
|
from utils import fileProcessor
|
||
|
from utils.const import DEBUG
|
||
|
from s3 import s3helper
|
||
|
|
||
|
# return (errmsg | code, errcode)
|
||
|
def upload(aid:int) -> Tuple[str, int]:
|
||
|
# 抓取文章本體
|
||
|
article, code = dbhelper.solo_article_fetcher(role="general", key=aid) # visible -> post
|
||
|
if code != 200:
|
||
|
return "Post not found", 1
|
||
|
|
||
|
# 抓取檔案
|
||
|
files = []
|
||
|
for k in article["files_hash"]:
|
||
|
f, code = s3helper.solo_file_fetcher(fnhash=k)
|
||
|
if code:
|
||
|
return "File not found", 1
|
||
|
else:
|
||
|
files.append(f)
|
||
|
|
||
|
# 轉出暫存檔案
|
||
|
tmp_path:list = []
|
||
|
for t in files:
|
||
|
filename, err = fileProcessor.file_saver(t.get("mime"), t.get("binary"))
|
||
|
if err: # 如果錯誤
|
||
|
return filename, 1
|
||
|
tmp_path.append(filename)
|
||
|
|
||
|
# 合成文字圖
|
||
|
proma_file = contentPictuterProma.new_proma(article["content"])
|
||
|
tmp_path = [proma_file] + tmp_path
|
||
|
|
||
|
# 送交 IG 上傳
|
||
|
if not DEBUG:
|
||
|
media = IG.upload_media(article["content"], 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(code:str) -> Tuple[str, int]:
|
||
|
# 抓取文章本體 - 叫你刪除的時候可能已經找不到本體了
|
||
|
# article, code = dbhelper.solo_article_fetcher(role="general", key=aid)
|
||
|
# if code != 200:
|
||
|
# return "Post not found", 1
|
||
|
|
||
|
err = IG.delete_media(code)
|
||
|
if err:
|
||
|
return "Remove failed", 1
|
||
|
|
||
|
return "OK", 0
|