some change

This commit is contained in:
p23 2024-11-21 18:18:06 +00:00
parent 0c7fc73c40
commit 74fc36fefd
2 changed files with 66 additions and 63 deletions

View File

@ -1,70 +1,10 @@
from typing import Tuple from grpc.postProcessor import upload, remove
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
def serve(): def serve():
pass pass
""" # for testing
def _serve(): def _serve():
print(IG.account_info()) print(IG.account_info())
@ -85,4 +25,5 @@ def _serve():
msg, err = remove(aid) msg, err = remove(aid)
if err: if err:
print(msg) print(msg)
return return
"""

62
grpc/postProcessor.py Normal file
View File

@ -0,0 +1,62 @@
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