From 74fc36fefd178f70d33162361fde4fefd41da416 Mon Sep 17 00:00:00 2001 From: p23 Date: Thu, 21 Nov 2024 18:18:06 +0000 Subject: [PATCH] some change --- grpc/grpcServer.py | 67 +++---------------------------------------- grpc/postProcessor.py | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 63 deletions(-) create mode 100644 grpc/postProcessor.py diff --git a/grpc/grpcServer.py b/grpc/grpcServer.py index 4375962..74323b3 100644 --- a/grpc/grpcServer.py +++ b/grpc/grpcServer.py @@ -1,70 +1,10 @@ -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 +from grpc.postProcessor import upload, remove def serve(): pass +""" # for testing def _serve(): print(IG.account_info()) @@ -85,4 +25,5 @@ def _serve(): msg, err = remove(aid) if err: print(msg) - return \ No newline at end of file + return +""" \ No newline at end of file diff --git a/grpc/postProcessor.py b/grpc/postProcessor.py new file mode 100644 index 0000000..e7fa168 --- /dev/null +++ b/grpc/postProcessor.py @@ -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 \ No newline at end of file