This commit is contained in:
flaskfarm
2022-10-07 01:48:42 +09:00
parent 4b72b7dc65
commit cde69d4d8a
55 changed files with 523 additions and 7703 deletions

View File

@@ -1,13 +1,12 @@
from support import logger
"""
from support import d, get_logger, logger
from support import d, logger
from .aes import SupportAES
from .discord import SupportDiscord
from .ffmpeg import SupportFfmpeg
from .file import SupportFile
from .image import SupportImage
from .process import SupportProcess
from .string import SupportString
from .util import SupportUtil, pt, default_headers, SingletonClass, AlchemyEncoder
from .aes import SupportAES
from .util import (AlchemyEncoder, SingletonClass, SupportUtil,
default_headers, pt)
from .yaml import SupportYaml
"""

View File

@@ -1,12 +1,18 @@
import os, traceback, re, json, codecs
import codecs
import json
import os
import re
import traceback
from . import logger
class SupportFile(object):
@classmethod
def read_file(cls, filename):
def read_file(cls, filename, mode='r'):
try:
ifp = codecs.open(filename, 'r', encoding='utf8')
ifp = codecs.open(filename, mode, encoding='utf8')
data = ifp.read()
ifp.close()
return data
@@ -15,10 +21,10 @@ class SupportFile(object):
logger.error(traceback.format_exc())
@classmethod
def write_file(cls, filename, data):
def write_file(cls, filename, data, mode='w'):
try:
import codecs
ofp = codecs.open(filename, 'w', encoding='utf8')
ofp = codecs.open(filename, mode, encoding='utf8')
ofp.write(data)
ofp.close()
except Exception as exception:
@@ -26,25 +32,8 @@ class SupportFile(object):
logger.error(traceback.format_exc())
@classmethod
def download(cls, url, filepath):
def download_file(cls, url, filepath):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
@@ -54,11 +43,8 @@ class SupportFile(object):
}
import requests
response = requests.get(url, headers=headers) # get request
if len(response.content) == 0:
return False
with open(filepath, "wb") as file_is: # open in binary mode
response = requests.get(url, headers=headers) # get request
file_is.write(response.content) # write to file
return True
except Exception as exception:
@@ -67,6 +53,47 @@ class SupportFile(object):
return False
@classmethod
def text_for_filename(cls, text):
#text = text.replace('/', '')
# 2021-07-31 X:X
#text = text.replace(':', ' ')
text = re.sub('[\\/:*?\"<>|]', ' ', text).strip()
text = re.sub("\s{2,}", ' ', text)
return text
@classmethod
def write(cls, data, filepath, mode='w'):
try:
@@ -83,14 +110,7 @@ class SupportFile(object):
return False
@classmethod
def text_for_filename(cls, text):
#text = text.replace('/', '')
# 2021-07-31 X:X
#text = text.replace(':', ' ')
text = re.sub('[\\/:*?\"<>|]', ' ', text).strip()
text = re.sub("\s{2,}", ' ', text)
return text
@classmethod
@@ -106,7 +126,8 @@ class SupportFile(object):
@classmethod
def file_move(cls, source_path, target_dir, target_filename):
try:
import time, shutil
import shutil
import time
if os.path.exists(target_dir) == False:
os.makedirs(target_dir)
target_path = os.path.join(target_dir, target_filename)
@@ -217,7 +238,8 @@ class SupportFile(object):
@classmethod
def makezip(cls, zip_path, zip_extension='zip', remove_zip_path=True):
import zipfile, shutil
import shutil
import zipfile
try:
if os.path.exists(zip_path) == False:
return False
@@ -248,7 +270,8 @@ class SupportFile(object):
@classmethod
def makezip_all(cls, zip_path, zip_filepath=None, zip_extension='zip', remove_zip_path=True):
import zipfile, shutil
import shutil
import zipfile
from pathlib import Path
try:
if os.path.exists(zip_path) == False:

View File

@@ -1,9 +1,7 @@
import os, traceback, io, re, json, codecs
from . import logger
class SupportString(object):
@classmethod
def get_cate_char_by_first(cls, title): # get_first
value = ord(title[0].upper())

View File

@@ -0,0 +1,76 @@
import json
import os
import platform
import subprocess
import traceback
from . import logger
class SupportSubprocess(object):
# 2021-10-25
# timeout 적용
@classmethod
def execute_command_return(cls, command, format=None, force_log=False, shell=False, env=None, timeout=None, uid=0, gid=0):
def demote(user_uid, user_gid):
def result():
os.setgid(user_gid)
os.setuid(user_uid)
return result
try:
if platform.system() == 'Windows':
tmp = []
if type(command) == type([]):
for x in command:
if x.find(' ') == -1:
tmp.append(x)
else:
tmp.append(f'"{x}"')
command = ' '.join(tmp)
iter_arg = ''
if platform.system() == 'Windows':
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
else:
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, preexec_fn=demote(uid, gid), encoding='utf8')
new_ret = {'status':'finish', 'log':None}
try:
process_ret = process.wait(timeout=timeout) # wait for the subprocess to exit
except:
import psutil
process = psutil.Process(process.pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
new_ret['status'] = "timeout"
ret = []
with process.stdout:
for line in iter(process.stdout.readline, iter_arg):
ret.append(line.strip())
if force_log:
logger.debug(ret[-1])
if format is None:
ret2 = '\n'.join(ret)
elif format == 'json':
try:
index = 0
for idx, tmp in enumerate(ret):
#logger.debug(tmp)
if tmp.startswith('{') or tmp.startswith('['):
index = idx
break
ret2 = json.loads(''.join(ret[index:]))
except:
ret2 = None
new_ret['log'] = ret2
return new_ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('command : %s', command)

View File

@@ -1,3 +1,7 @@
import traceback
from telepot_mod import Bot
from . import logger
@@ -5,24 +9,11 @@ class SupportTelegram:
@classmethod
def send_telegram_message(cls, text, bot_token=None, chat_id=None, image_url=None, disable_notification=None):
from system.model import ModelSetting as SystemModelSetting
try:
if bot_token is None:
bot_token = SystemModelSetting.get('notify_telegram_token')
if chat_id is None:
chat_id = SystemModelSetting.get('notify_telegram_chat_id')
if disable_notification is None:
disable_notification = SystemModelSetting.get_bool('notify_telegram_disable_notification')
bot = Bot(bot_token)
if image_url is not None:
#bot.sendPhoto(chat_id, text, caption=caption, disable_notification=disable_notification)
bot.sendPhoto(chat_id, image_url, disable_notification=disable_notification)
bot.sendMessage(chat_id, text, disable_web_page_preview=True, disable_notification=disable_notification)
#elif mime == 'video':
# bot.sendVideo(chat_id, text, disable_notification=disable_notification)
return True
except Exception as exception:
logger.error('Exception:%s', exception)