test
This commit is contained in:
13
lib/support/base/__init__.py
Normal file
13
lib/support/base/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from support import logger
|
||||
"""
|
||||
from support import d, get_logger, logger
|
||||
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 .yaml import SupportYaml
|
||||
"""
|
||||
43
lib/support/base/aes.py
Normal file
43
lib/support/base/aes.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os, base64, traceback
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto import Random
|
||||
from . import logger
|
||||
BS = 16
|
||||
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
|
||||
unpad = lambda s : s[0:-s[-1]]
|
||||
key = b'140b41b22a29beb4061bda66b6747e14'
|
||||
|
||||
class SupportAES(object):
|
||||
@classmethod
|
||||
def encrypt(cls, raw, mykey=None):
|
||||
try:
|
||||
Random.atfork()
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
raw = pad(raw)
|
||||
if type(raw) == type(''):
|
||||
raw = raw.encode()
|
||||
if mykey is not None and type(mykey) == type(''):
|
||||
mykey = mykey.encode()
|
||||
iv = Random.new().read( AES.block_size )
|
||||
cipher = AES.new(key if mykey is None else mykey, AES.MODE_CBC, iv )
|
||||
try:
|
||||
tmp = cipher.encrypt( raw )
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
tmp = cipher.encrypt( raw.encode() )
|
||||
ret = base64.b64encode( iv + tmp )
|
||||
ret = ret.decode()
|
||||
return ret
|
||||
|
||||
@classmethod
|
||||
def decrypt(cls, enc, mykey=None):
|
||||
enc = base64.b64decode(enc)
|
||||
iv = enc[:16]
|
||||
if len(iv) != 16:
|
||||
iv = os.urandom(16)
|
||||
cipher = AES.new(key if mykey is None else mykey, AES.MODE_CBC, iv )
|
||||
return unpad(cipher.decrypt( enc[16:] )).decode()
|
||||
211
lib/support/base/discord.py
Normal file
211
lib/support/base/discord.py
Normal file
@@ -0,0 +1,211 @@
|
||||
import os, io, traceback, time, random, requests
|
||||
try:
|
||||
from discord_webhook import DiscordWebhook, DiscordEmbed
|
||||
except:
|
||||
os.system('pip3 install discord-webhook')
|
||||
|
||||
from discord_webhook import DiscordWebhook, DiscordEmbed
|
||||
from . import logger
|
||||
|
||||
webhook_list = [
|
||||
#'https://discord.com/api/webhooks/933908493612744705/DGPWBQN8LiMnt2cnCSNVy6rCc5Gi_vj98QpJ3ZEeihohzsfOsCWvcixJU1A2fQuepGFq', # 1
|
||||
#'https://discord.com/api/webhooks/932754078839234731/R2iFzQ7P8IKV-MGWp820ToWX07s5q8X-st-QsUJs7j3JInUj6ZlI4uDYKeR_cwIi98mf', # 2
|
||||
#'https://discord.com/api/webhooks/932754171835351131/50RLrYa_B69ybk4BWoLruNqU7YlZ3pl3gpPr9bwuankWyTIGtRGbgf0CJ9ExJWJmvXwo', # 3
|
||||
'https://discord.com/api/webhooks/794661043863027752/A9O-vZSHIgfQ3KX7wO5_e2xisqpLw5TJxg2Qs1stBHxyd5PK-Zx0IJbAQXmyDN1ixZ-n', # 4
|
||||
'https://discord.com/api/webhooks/810373348776476683/h_uJLBBlHzD0w_CG0nUajFO-XEh3fvy-vQofQt1_8TMD7zHiR7a28t3jF-xBCP6EVlow', # 5
|
||||
'https://discord.com/api/webhooks/810373405508501534/wovhf-1pqcxW5h9xy7iwkYaf8KMDjHU49cMWuLKtBWjAnj-tzS1_j8RJ7tsMyViDbZCE', # 6
|
||||
'https://discord.com/api/webhooks/796558388326039552/k2VV356S1gKQa9ht-JuAs5Dqw5eVkxgZsLUzFoxmFG5lW6jqKl7zCBbbKVhs3pcLOetm', # 7
|
||||
'https://discord.com/api/webhooks/810373566452858920/Qf2V8BoLOy2kQzlZGHy5HZ1nTj7lK72ol_UFrR3_eHKEOK5fyR_fQ8Yw8YzVh9EQG54o', # 8
|
||||
'https://discord.com/api/webhooks/810373654411739157/SGgdO49OCkTNIlc_BSMSy7IXQwwXVonG3DsVfvBVE6luTCwvgCqEBpEk30WBeMMieCyI', # 9
|
||||
'https://discord.com/api/webhooks/810373722341900288/FwcRJ4YxYjpyHpnRwF5f2an0ltEm8JPqcWeZqQi3Qz4QnhEY-kR2sjF9fo_n6stMGnf_', # 10
|
||||
'https://discord.com/api/webhooks/931779811691626536/vvwCm1YQvE5tW4QJ4SNKRmXhQQrmOQxbjsgRjbTMMXOSiclB66qipiZaax5giAqqu2IB', # 11
|
||||
'https://discord.com/api/webhooks/931779905631420416/VKlDwfxWQPJfIaj94-ww_hM1MNEayRKoMq0adMffCC4WQS60yoAub_nqPbpnfFRR3VU5', # 12
|
||||
'https://discord.com/api/webhooks/931779947914231840/22amQuHSOI7wPijSt3U01mXwd5hTo_WHfVkeaowDQMawCo5tXVfeEMd6wAWf1n7CseiG', # 13
|
||||
'https://discord.com/api/webhooks/810374294416654346/T3-TEdKIg7rwMZeDzNr46KPDvO7ZF8pRdJ3lfl39lJw2XEZamAG8uACIXagbNMX_B0YN', # 14
|
||||
'https://discord.com/api/webhooks/810374337403289641/_esFkQXwlPlhxJWtlqDAdLg2Nujo-LjGPEG3mUmjiRZto69NQpkBJ0F2xtSNrCH4VAgb', # 15
|
||||
'https://discord.com/api/webhooks/810374384736534568/mH5-OkBVpi7XqJioaQ8Ma-NiL-bOx7B5nYJpL1gZ03JaJaUaIW4bCHeCt5O_VGLJwAtj', # 16
|
||||
'https://discord.com/api/webhooks/810374428604104724/Z1Tdxz3mb0ytWq5LHWi4rG5CeJnr9KWXy5aO_waeD0NcImQnhRXe7h7ra7UrIDRQ2jOg', # 17
|
||||
'https://discord.com/api/webhooks/810374475773509643/QCPPN4djNzhuOmbS3DlrGBunK0SVR5Py9vMyCiPL-0T2VPgitFZS4YM6GCLfM2fkrn4-', # 18
|
||||
'https://discord.com/api/webhooks/810374527652855819/5ypaKI_r-hYzwmdDlVmgAU6xNgU833L9tFlPnf3nw4ZDaPMSppjt77aYOiFks4KLGQk8', # 19
|
||||
'https://discord.com/api/webhooks/810374587917402162/lHrG7CEysGUM_41DMnrxL2Q8eh1-xPjJXstYE68WWfLQbuUAV3rOfsNB9adncJzinYKi', # 20
|
||||
]
|
||||
|
||||
|
||||
class SupportDiscord(object):
|
||||
|
||||
@classmethod
|
||||
def send_discord_message(cls, text, image_url=None, webhook_url=None):
|
||||
try:
|
||||
webhook = DiscordWebhook(url=webhook_url, content=text)
|
||||
if image_url is not None:
|
||||
embed = DiscordEmbed()
|
||||
embed.set_timestamp()
|
||||
embed.set_image(url=image_url)
|
||||
webhook.add_embed(embed)
|
||||
response = webhook.execute()
|
||||
return True
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
return False
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def discord_proxy_image(cls, image_url, webhook_url=None, retry=True):
|
||||
#2020-12-23
|
||||
#image_url = None
|
||||
if image_url == '' or image_url is None:
|
||||
return
|
||||
data = None
|
||||
|
||||
if webhook_url is None or webhook_url == '':
|
||||
webhook_url = webhook_list[random.randint(0,len(webhook_list)-1)]
|
||||
|
||||
try:
|
||||
webhook = DiscordWebhook(url=webhook_url, content='')
|
||||
embed = DiscordEmbed()
|
||||
embed.set_timestamp()
|
||||
embed.set_image(url=image_url)
|
||||
webhook.add_embed(embed)
|
||||
import io
|
||||
byteio = io.BytesIO()
|
||||
webhook.add_file(file=byteio.getvalue(), filename='dummy')
|
||||
response = webhook.execute()
|
||||
data = None
|
||||
if type(response) == type([]):
|
||||
if len(response) > 0:
|
||||
data = response[0].json()
|
||||
else:
|
||||
data = response.json()
|
||||
|
||||
if data is not None and 'embeds' in data:
|
||||
target = data['embeds'][0]['image']['proxy_url']
|
||||
if requests.get(target).status_code == 200:
|
||||
return target
|
||||
else:
|
||||
return image_url
|
||||
else:
|
||||
raise Exception(str(data))
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image(image_url, webhook_url=None, retry=False)
|
||||
else:
|
||||
return image_url
|
||||
|
||||
|
||||
@classmethod
|
||||
def discord_proxy_image_localfile(cls, filepath, retry=True):
|
||||
data = None
|
||||
webhook_url = webhook_list[random.randint(0,len(webhook_list)-1)]
|
||||
|
||||
try:
|
||||
webhook = DiscordWebhook(url=webhook_url, content='')
|
||||
import io
|
||||
with open(filepath, 'rb') as fh:
|
||||
byteio = io.BytesIO(fh.read())
|
||||
webhook.add_file(file=byteio.getvalue(), filename='image.jpg')
|
||||
embed = DiscordEmbed()
|
||||
embed.set_image(url="attachment://image.jpg")
|
||||
response = webhook.execute()
|
||||
data = None
|
||||
if type(response) == type([]):
|
||||
if len(response) > 0:
|
||||
data = response[0].json()
|
||||
else:
|
||||
data = response.json()
|
||||
|
||||
if data is not None and 'attachments' in data:
|
||||
target = data['attachments'][0]['url']
|
||||
if requests.get(target).status_code == 200:
|
||||
return target
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_localfile(filepath, retry=False)
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_localfile(filepath, retry=False)
|
||||
|
||||
|
||||
@classmethod
|
||||
def discord_proxy_image_bytes(cls, bytes, retry=True):
|
||||
data = None
|
||||
idx = random.randint(0,len(webhook_list)-1)
|
||||
webhook_url = webhook_list[idx]
|
||||
try:
|
||||
webhook = DiscordWebhook(url=webhook_url, content='')
|
||||
webhook.add_file(file=bytes, filename='image.jpg')
|
||||
embed = DiscordEmbed()
|
||||
embed.set_image(url="attachment://image.jpg")
|
||||
response = webhook.execute()
|
||||
data = None
|
||||
if type(response) == type([]):
|
||||
if len(response) > 0:
|
||||
data = response[0].json()
|
||||
else:
|
||||
data = response.json()
|
||||
if data is not None and 'attachments' in data:
|
||||
target = data['attachments'][0]['url']
|
||||
if requests.get(target).status_code == 200:
|
||||
return target
|
||||
logger.error(f"discord webhook error : {webhook_url}")
|
||||
logger.error(f"discord webhook error : {idx}")
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_bytes(bytes, retry=False)
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_bytes(bytes, retry=False)
|
||||
|
||||
|
||||
|
||||
|
||||
# RSS에서 자막 올린거
|
||||
@classmethod
|
||||
def discord_cdn(cls, byteio=None, filepath=None, filename=None, webhook_url=None, content='', retry=True):
|
||||
data = None
|
||||
if webhook_url is None:
|
||||
webhook_url = webhook_list[random.randint(0,9)] # sjva 채널
|
||||
|
||||
try:
|
||||
webhook = DiscordWebhook(url=webhook_url, content=content)
|
||||
if byteio is None and filepath is not None:
|
||||
import io
|
||||
with open(filepath, 'rb') as fh:
|
||||
byteio = io.BytesIO(fh.read())
|
||||
|
||||
webhook.add_file(file=byteio.getvalue(), filename=filename)
|
||||
embed = DiscordEmbed()
|
||||
response = webhook.execute()
|
||||
data = None
|
||||
if type(response) == type([]):
|
||||
if len(response) > 0:
|
||||
data = response[0].json()
|
||||
else:
|
||||
data = response.json()
|
||||
|
||||
if data is not None and 'attachments' in data:
|
||||
target = data['attachments'][0]['url']
|
||||
if requests.get(target).status_code == 200:
|
||||
return target
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_localfile(filepath, retry=False)
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
if retry:
|
||||
time.sleep(1)
|
||||
return cls.discord_proxy_image_localfile(filepath, retry=False)
|
||||
|
||||
47
lib/support/base/ffmpeg.py
Normal file
47
lib/support/base/ffmpeg.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import os, sys, traceback, subprocess, json, platform, time
|
||||
import shutil
|
||||
from . import logger
|
||||
|
||||
|
||||
class SupportFfmpeg(object):
|
||||
|
||||
@classmethod
|
||||
def download_m3u8(cls, config):
|
||||
try:
|
||||
|
||||
if config.get('proxy') == None:
|
||||
if config.get('headers') == None:
|
||||
command = [config.get('ffmpeg_path'), '-y', '-correct_ts_overflow', '0', '-i', config['url'], '-c', 'copy', '-bsf:a', 'aac_adtstoasc']
|
||||
else:
|
||||
headers_command = []
|
||||
for key, value in config.get('headers').items():
|
||||
if key.lower() == 'user-agent':
|
||||
headers_command.append('-user_agent')
|
||||
headers_command.append(value)
|
||||
pass
|
||||
else:
|
||||
headers_command.append('-headers')
|
||||
headers_command.append('\'%s:%s\''%(key,value))
|
||||
command = [config.get('ffmpeg_path'), '-y', '-correct_ts_overflow', '0'] + headers_command + ['-i', config['url'], '-c', 'copy', '-bsf:a', 'aac_adtstoasc']
|
||||
else:
|
||||
command = [config.get('ffmpeg_path'), '-y', '-correct_ts_overflow', '0', '-http_proxy', config.get('proxy'), '-i', config['url'], '-c', 'copy', '-bsf:a', 'aac_adtstoasc']
|
||||
|
||||
filename = str(int(time.time())) + '.mp4'
|
||||
tmp = config.get('tmp_dir')
|
||||
if tmp == None:
|
||||
tmp = os.getcwd()
|
||||
tmp_filepath = os.path.join(tmp, filename)
|
||||
command.append(tmp_filepath)
|
||||
logger.debug(' '.join(command))
|
||||
from . import SupportSubprocess
|
||||
ret = SupportSubprocess.execute(command, timeout=10)
|
||||
logger.error(ret)
|
||||
if os.path.exists(tmp_filepath):
|
||||
shutil.move(tmp_filepath, config.get('output_filepath'))
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}', )
|
||||
logger.error(traceback.format_exc())
|
||||
logger.error('command : %s', command)
|
||||
|
||||
293
lib/support/base/file.py
Normal file
293
lib/support/base/file.py
Normal file
@@ -0,0 +1,293 @@
|
||||
import os, traceback, re, json, codecs
|
||||
from . import logger
|
||||
|
||||
class SupportFile(object):
|
||||
|
||||
@classmethod
|
||||
def read_file(cls, filename):
|
||||
try:
|
||||
ifp = codecs.open(filename, 'r', encoding='utf8')
|
||||
data = ifp.read()
|
||||
ifp.close()
|
||||
return data
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
@classmethod
|
||||
def write_file(cls, filename, data):
|
||||
try:
|
||||
import codecs
|
||||
ofp = codecs.open(filename, 'w', encoding='utf8')
|
||||
ofp.write(data)
|
||||
ofp.close()
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def download(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',
|
||||
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
||||
'Accept-Language' : 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
|
||||
'Connection': 'Keep-Alive',
|
||||
}
|
||||
|
||||
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
|
||||
file_is.write(response.content) # write to file
|
||||
return True
|
||||
except Exception as exception:
|
||||
logger.debug('Exception:%s', exception)
|
||||
logger.debug(traceback.format_exc())
|
||||
return False
|
||||
|
||||
|
||||
@classmethod
|
||||
def write(cls, data, filepath, mode='w'):
|
||||
try:
|
||||
import codecs
|
||||
ofp = codecs.open(filepath, mode, encoding='utf8')
|
||||
if isinstance(data, bytes) and mode == 'w':
|
||||
data = data.decode('utf-8')
|
||||
ofp.write(data)
|
||||
ofp.close()
|
||||
return True
|
||||
except Exception as exception:
|
||||
logger.debug('Exception:%s', exception)
|
||||
logger.debug(traceback.format_exc())
|
||||
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 size(cls, start_path = '.'):
|
||||
total_size = 0
|
||||
for dirpath, dirnames, filenames in os.walk(start_path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dirpath, f)
|
||||
if not os.path.islink(fp):
|
||||
total_size += os.path.getsize(fp)
|
||||
return total_size
|
||||
|
||||
@classmethod
|
||||
def file_move(cls, source_path, target_dir, target_filename):
|
||||
try:
|
||||
import time, shutil
|
||||
if os.path.exists(target_dir) == False:
|
||||
os.makedirs(target_dir)
|
||||
target_path = os.path.join(target_dir, target_filename)
|
||||
if source_path != target_path:
|
||||
if os.path.exists(target_path):
|
||||
tmp = os.path.splitext(target_filename)
|
||||
new_target_filename = f"{tmp[0]} {str(time.time()).split('.')[0]}{tmp[1]}"
|
||||
target_path = os.path.join(target_dir, new_target_filename)
|
||||
shutil.move(source_path, target_path)
|
||||
except Exception as exception:
|
||||
logger.debug('Exception:%s', exception)
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
"""
|
||||
@classmethod
|
||||
def makezip(cls, zip_path, zip_folder=None, zip_extension='zip', remove_folder=False):
|
||||
import zipfile
|
||||
try:
|
||||
zip_path = zip_path.rstrip('/')
|
||||
if zip_folder is None:
|
||||
zip_folder = os.path.dirname(zip_path)
|
||||
elif zip_folder == 'tmp':
|
||||
from framework import path_data
|
||||
zip_folder = os.path.join(path_data, 'tmp')
|
||||
if os.path.isdir(zip_path):
|
||||
zipfilepath = os.path.join(zip_folder, f"{os.path.basename(zip_path)}.{zip_extension}")
|
||||
fantasy_zip = zipfile.ZipFile(zipfilepath, 'w')
|
||||
for f in os.listdir(zip_path):
|
||||
src = os.path.join(zip_path, f)
|
||||
fantasy_zip.write(src, os.path.basename(src), compress_type = zipfile.ZIP_DEFLATED)
|
||||
fantasy_zip.close()
|
||||
if remove_folder:
|
||||
import shutil
|
||||
shutil.rmtree(zip_path)
|
||||
return zipfilepath
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
return
|
||||
"""
|
||||
|
||||
|
||||
@classmethod
|
||||
def rmtree(cls, folderpath):
|
||||
import shutil
|
||||
try:
|
||||
shutil.rmtree(folderpath)
|
||||
return True
|
||||
except:
|
||||
try:
|
||||
os.system("rm -rf '{folderpath}'")
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
@classmethod
|
||||
def rmtree2(cls, folderpath):
|
||||
import shutil
|
||||
try:
|
||||
for root, dirs, files in os.walk(folderpath):
|
||||
for name in files:
|
||||
os.remove(os.path.join(root, name))
|
||||
for name in dirs:
|
||||
shutil.rmtree(os.path.join(root, name))
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
@classmethod
|
||||
def write_json(cls, filepath, data):
|
||||
try:
|
||||
if os.path.dirname(filepath) != '':
|
||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||
with open(filepath, "w", encoding='utf8') as json_file:
|
||||
json.dump(data, json_file, indent=4, ensure_ascii=False)
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
@classmethod
|
||||
def read_json(cls, filepath):
|
||||
try:
|
||||
with open(filepath, "r", encoding='utf8') as json_file:
|
||||
data = json.load(json_file)
|
||||
return data
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def write_binary(cls, filename, data):
|
||||
try:
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(data)
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def makezip(cls, zip_path, zip_extension='zip', remove_zip_path=True):
|
||||
import zipfile, shutil
|
||||
try:
|
||||
if os.path.exists(zip_path) == False:
|
||||
return False
|
||||
zipfilepath = os.path.join(os.path.dirname(zip_path), f"{os.path.basename(zip_path)}.{zip_extension}")
|
||||
if os.path.exists(zipfilepath):
|
||||
return True
|
||||
zip = zipfile.ZipFile(zipfilepath, 'w')
|
||||
for f in os.listdir(zip_path):
|
||||
src = os.path.join(zip_path, f)
|
||||
zip.write(src, f, compress_type = zipfile.ZIP_DEFLATED)
|
||||
zip.close()
|
||||
if remove_zip_path:
|
||||
shutil.rmtree(zip_path)
|
||||
return zipfilepath
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}')
|
||||
logger.error(traceback.format_exc())
|
||||
return None
|
||||
|
||||
|
||||
@classmethod
|
||||
def write_yaml(cls, filepath, data):
|
||||
import yaml
|
||||
with open(filepath, 'w', encoding='utf8') as f:
|
||||
yaml.dump(data, f, default_flow_style=False, allow_unicode=True)
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def makezip_all(cls, zip_path, zip_filepath=None, zip_extension='zip', remove_zip_path=True):
|
||||
import zipfile, shutil
|
||||
from pathlib import Path
|
||||
try:
|
||||
if os.path.exists(zip_path) == False:
|
||||
return False
|
||||
if zip_filepath == None:
|
||||
zipfilepath = os.path.join(os.path.dirname(zip_path), f"{os.path.basename(zip_path)}.{zip_extension}")
|
||||
if os.path.exists(zipfilepath):
|
||||
os.remove(zipfilepath)
|
||||
zip = zipfile.ZipFile(zipfilepath, 'w')
|
||||
for file_path in Path(zip_path).rglob("*"):
|
||||
zip.write(file_path, file_path.name)
|
||||
|
||||
|
||||
for (path, dir, files) in os.walk(zip_path):
|
||||
for file in files:
|
||||
zip.write(os.path.join(path.replace(zip_path+'/', '').replace(zip_path+'\\', ''), file), compress_type=zipfile.ZIP_DEFLATED)
|
||||
zip.close()
|
||||
if remove_zip_path:
|
||||
shutil.rmtree(zip_path)
|
||||
return zipfilepath
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}')
|
||||
logger.error(traceback.format_exc())
|
||||
return None
|
||||
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def read(cls, filepath, mode='r'):
|
||||
try:
|
||||
import codecs
|
||||
ifp = codecs.open(filepath, mode, encoding='utf8')
|
||||
data = ifp.read()
|
||||
ifp.close()
|
||||
if isinstance(data, bytes):
|
||||
data = data.decode('utf-8')
|
||||
return data
|
||||
except Exception as exception:
|
||||
logger.error('Exception:%s', exception)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
"""
|
||||
25
lib/support/base/image.py
Normal file
25
lib/support/base/image.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os, sys, traceback, requests
|
||||
from io import BytesIO
|
||||
from . import logger
|
||||
|
||||
class SupportImage(object):
|
||||
|
||||
@classmethod
|
||||
def horizontal_to_vertical(cls, url):
|
||||
try:
|
||||
from PIL import Image
|
||||
im = Image.open(requests.get(url, stream=True).raw)
|
||||
width,height = im.size
|
||||
new_height = int(width * 1.5)
|
||||
new_im = Image.new('RGB', (width, new_height))
|
||||
new_im.paste(im, (0, int((new_height-height)/2)))
|
||||
|
||||
img_byte_arr = BytesIO()
|
||||
new_im.save(img_byte_arr, format='PNG')
|
||||
img_byte_arr = img_byte_arr.getvalue()
|
||||
from . import SupportDiscord
|
||||
return SupportDiscord.discord_proxy_image_bytes(img_byte_arr)
|
||||
except Exception as e:
|
||||
logger.error('Exception:%s', e)
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
48
lib/support/base/process.py
Normal file
48
lib/support/base/process.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import os, sys, traceback, subprocess, json, platform
|
||||
from . import logger
|
||||
|
||||
|
||||
class SupportProcess(object):
|
||||
|
||||
@classmethod
|
||||
def execute(cls, command, format=None, shell=False, env=None, timeout=1000):
|
||||
logger.debug(command)
|
||||
try:
|
||||
if platform.system() == 'Windows':
|
||||
command = ' '.join(command)
|
||||
|
||||
iter_arg = ''
|
||||
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
|
||||
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()
|
||||
return "timeout"
|
||||
ret = []
|
||||
with process.stdout:
|
||||
for line in iter(process.stdout.readline, iter_arg):
|
||||
ret.append(line.strip())
|
||||
|
||||
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
|
||||
return ret2
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}', )
|
||||
logger.error(traceback.format_exc())
|
||||
logger.error('command : %s', command)
|
||||
|
||||
28
lib/support/base/string.py
Normal file
28
lib/support/base/string.py
Normal file
@@ -0,0 +1,28 @@
|
||||
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())
|
||||
if value >= ord('0') and value <= ord('9'): return '0Z'
|
||||
elif value >= ord('A') and value <= ord('Z'): return '0Z'
|
||||
elif value >= ord('가') and value < ord('나'): return '가'
|
||||
elif value < ord('다'): return '나'
|
||||
elif value < ord('라'): return '다'
|
||||
elif value < ord('마'): return '라'
|
||||
elif value < ord('바'): return '마'
|
||||
elif value < ord('사'): return '바'
|
||||
elif value < ord('아'): return '사'
|
||||
elif value < ord('자'): return '아'
|
||||
elif value < ord('차'): return '자'
|
||||
elif value < ord('카'): return '차'
|
||||
elif value < ord('타'): return '카'
|
||||
elif value < ord('파'): return '타'
|
||||
elif value < ord('하'): return '파'
|
||||
elif value <= ord('힣'): return '하'
|
||||
else: return '0Z'
|
||||
|
||||
|
||||
103
lib/support/base/util.py
Normal file
103
lib/support/base/util.py
Normal file
@@ -0,0 +1,103 @@
|
||||
import os, traceback, io, re, json, codecs
|
||||
from . import logger
|
||||
|
||||
from functools import wraps
|
||||
import time
|
||||
def pt(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwds):
|
||||
start = time.time()
|
||||
#logger.debug(f"FUNC START [{f.__name__}]")
|
||||
result = f(*args, **kwds)
|
||||
elapsed = time.time() - start
|
||||
logger.info(f"FUNC END [{f.__name__}] {elapsed}")
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
default_headers = {
|
||||
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
||||
'accept-language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
|
||||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
|
||||
}
|
||||
|
||||
|
||||
class SupportUtil(object):
|
||||
@classmethod
|
||||
def sizeof_fmt(cls, num, suffix='Bytes'):
|
||||
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
|
||||
if abs(num) < 1024.0:
|
||||
return "%3.1f%s%s" % (num, unit, suffix)
|
||||
num /= 1024.0
|
||||
return "%.1f%s%s" % (num, 'Y', suffix)
|
||||
|
||||
@classmethod
|
||||
def is_arm(cls):
|
||||
try:
|
||||
ret = False
|
||||
import platform
|
||||
if platform.system() == 'Linux':
|
||||
if platform.platform().find('86') == -1 and platform.platform().find('64') == -1:
|
||||
ret = True
|
||||
if platform.platform().find('arch') != -1:
|
||||
ret = True
|
||||
if platform.platform().find('arm') != -1:
|
||||
ret = True
|
||||
return ret
|
||||
except Exception as e:
|
||||
logger.error(f"Exception:{str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
def dummy_func():
|
||||
pass
|
||||
|
||||
class celery(object):
|
||||
|
||||
class task(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if len(args) > 0:
|
||||
self.f = args[0]
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
if len(args) > 0 and type(args[0]) == type(dummy_func):
|
||||
return args[0]
|
||||
self.f(*args, **kwargs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class SingletonClass(object):
|
||||
__instance = None
|
||||
|
||||
@classmethod
|
||||
def __getInstance(cls):
|
||||
return cls.__instance
|
||||
|
||||
@classmethod
|
||||
def instance(cls, *args, **kargs):
|
||||
cls.__instance = cls(*args, **kargs)
|
||||
cls.instance = cls.__getInstance
|
||||
return cls.__instance
|
||||
|
||||
|
||||
|
||||
class AlchemyEncoder(json.JSONEncoder):
|
||||
|
||||
def default(self, obj):
|
||||
from sqlalchemy.ext.declarative import DeclarativeMeta
|
||||
if isinstance(obj.__class__, DeclarativeMeta):
|
||||
# an SQLAlchemy class
|
||||
fields = {}
|
||||
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
|
||||
data = obj.__getattribute__(field)
|
||||
try:
|
||||
json.dumps(data) # this will fail on non-encodable values, like other classes
|
||||
fields[field] = data
|
||||
except TypeError:
|
||||
fields[field] = None
|
||||
# a json-encodable dict
|
||||
return fields
|
||||
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
13
lib/support/base/yaml.py
Normal file
13
lib/support/base/yaml.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import yaml
|
||||
|
||||
class SupportYaml(object):
|
||||
@classmethod
|
||||
def write_yaml(cls, filepath, data):
|
||||
with open(filepath, 'w', encoding='utf8') as f:
|
||||
yaml.dump(data, f, default_flow_style=False, allow_unicode=True)
|
||||
|
||||
@classmethod
|
||||
def read_yaml(self, filepath):
|
||||
with open(filepath, encoding='utf8') as file:
|
||||
data = yaml.load(file, Loader=yaml.FullLoader)
|
||||
return data
|
||||
Reference in New Issue
Block a user