This commit is contained in:
soju6jan
2022-10-02 20:18:05 +09:00
parent b9c3aac91f
commit 29930fdef7
150 changed files with 53982 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
# 데이터 폴더 루트 경로
# 윈도우의 경우 폴더 구분 기호 \ 를 두개 사용
# 예) data_folder: "C:\\work\\data"
# 현재 폴더인 경우 .
path_data: "C:\\work\\FlaskFarm\\working"
#path_data: "/mnt/c/work/FlaskFarm/working"
# gevent 사용여부
# 거의 항상 true로 사용.
# 플러그인 개발이나 termux 환경에서의 실행 같이 특수한 경우에만 false로 사용.
# 실행환경에 gevent 관련 패키지가 설치되어 있지 않는다면 값과 상관 없이 false로 동작.
# false인 경우
use_gevent: true
# celery 사용 여부
use_celery: true
# redis port
# celery를 사용하는 경우 사용하는 redis 포트
redis_port: 6379
# 포트
# 생략시 DB 값을 사용.
port: 9999
# 소스 수정시 재로딩
# 두번 로딩되는 것을 감안하여 코딩해야 함. 기본실행, subporcess 실행
# 기본적으로 main.py 하위 파일의 변경만 감시
debug: true
use_reloader: true
# 플러그인 업데이트 여부
# - true인 경우 로딩시 플러그인을 업데이트 함.
# 데이터폴더/plugins 폴더 안에 플러그인 만을 대상으로 함.
# - debug 값이 true인 경우에는 항상 false
plugin_update: false
# url subpath
url_prefix: "/sf"
# running_type
# termux, entware 인 경우 입력 함. (이외 사용하는 값 native, docker)
running_type: "native"
# 개발용 폴더만 로딩할 경우 사용
#plugin_loading_only_devpath: true
# 로딩할 플러그인 package 명
# 타 플러그인과 연동되는 플러그인 개발시 사용.
# import 로 런타임에 로딩할 수 있지만 타 패키지 메뉴 등은 표시되지 않음.
#plugin_loading_list: ['command', 'flaskcode']
# 로딩 제외할 플러그인 package 명
plugin_except_list: ['terminal', 'membership']
+55
View File
@@ -0,0 +1,55 @@
# 카테고리
# uri 가 plugin인 경우 name 값은 대체
- name: "토렌트"
list:
- uri: "rss2"
- name: "즐겨찾기"
list:
- uri: "number_baseball"
# - uri: "command"
# - uri: "flaskfilemanager"
# - uri: "flaskcode"
- name: "기본 기능"
list:
- uri: "terminal"
- uri: "command"
- uri: "flaskfilemanager"
- uri: "flaskcode"
- uri: "number_baseball"
- name: "링크"
list:
- uri: "https://app.plex.tv"
name: "Plex"
target: "_self"
- uri: "https://app.plex.tv"
name: "Netflix"
- name: "Plex"
uri: "https://app.plex.tv"
- name: "시스템"
list:
- uri: "system"
name: "설정"
- uri: "setting"
name: "확장 설정"
- uri: "system/plugin"
name: "플러그인 관리"
- uri: "-"
- uri: "system/logout"
name: "로그아웃"
- uri: "system/restart"
name: "재시작(업데이트)"
- uri: "system/shutdown"
#uri: "javascript:shutdown_confirm();"
name: "종료"
+21
View File
@@ -0,0 +1,21 @@
#flask
Flask==1.1.1
Flask-SQLAlchemy
Flask-Login==0.4.1
Flask-Cors==3.0.8
Flask-Markdown
Flask-SocketIO==4.3.1
python-engineio==3.13.2
python-socketio==4.6.0
#Werkzeug==2.0.1
Werkzeug==0.16.1
Jinja2==2.10.1
markupsafe==2.0.1
itsdangerous==2.0.1
# common util
apscheduler
pytz
requests==2.26.0
discord-webhook
pyyaml
+8
View File
@@ -0,0 +1,8 @@
psutil
pycryptodome
gevent
gevent-websocket
celery==4.3.0
redis
BIN
View File
Binary file not shown.
+23
View File
@@ -0,0 +1,23 @@
VERSION="4.0.0"
from support import d
from .init_main import Framework
frame = Framework.get_instance()
F = frame
logger = frame.logger
app = frame.app
celery = frame.celery
db = frame.db
scheduler = frame.scheduler
socketio = frame.socketio
path_app_root = frame.path_app_root
path_data = frame.path_data
get_logger = frame.get_logger
from .init_declare import check_api, User
from flask_login import login_required
from .scheduler import Job
frame.initialize_system()
from system.setup import SystemModelSetting
frame.initialize_plugin()
+102
View File
@@ -0,0 +1,102 @@
from flask import request, abort
from functools import wraps
from flask import request
def check_api(original_function):
@wraps(original_function)
def wrapper_function(*args, **kwargs): #1
from framework import F
#logger.debug('CHECK API... {} '.format(original_function.__module__))
#logger.warning(request.url)
#logger.warning(request.form)
try:
if F.SystemModelSetting.get_bool('auth_use_apikey'):
if request.method == 'POST':
apikey = request.form['apikey']
else:
apikey = request.args.get('apikey')
#apikey = request.args.get('apikey')
if apikey is None or apikey != F.SystemModelSetting.get('auth_apikey'):
F.logger.warning('CHECK API : ABORT no match ({})'.format(apikey))
F.logger.warning(request.environ.get('HTTP_X_REAL_IP', request.remote_addr))
abort(403)
return
except Exception as e:
F.logger.warning('CHECK API : ABORT exception')
abort(403)
return
return original_function(*args, **kwargs) #2
return wrapper_function
# Suuport를 logger 생성전에 쓰지 않기 위해 중복 선언
import logging
class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""
grey = "\x1b[38;21m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
green = "\x1B[32m"
# pathname filename
#format = "[%(asctime)s|%(name)s|%(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
format = '[{yellow}%(asctime)s{reset}|{color}%(levelname)s{reset}|{green}%(name)s{reset}|%(pathname)s:%(lineno)s] {color}%(message)s{reset}'
FORMATS = {
logging.DEBUG: format.format(color=grey, reset=reset, yellow=yellow, green=green),
logging.INFO: format.format(color=green, reset=reset, yellow=yellow, green=green),
logging.WARNING: format.format(color=yellow, reset=reset, yellow=yellow, green=green),
logging.ERROR: format.format(color=red, reset=reset, yellow=yellow, green=green),
logging.CRITICAL: format.format(color=bold_red, reset=reset, yellow=yellow, green=green)
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
# Suuport를 logger 생성전에 쓰지 않기 위해 중복 선언
def read_yaml(filepath):
import yaml
with open(filepath, encoding='utf8') as file:
data = yaml.load(file, Loader=yaml.FullLoader)
return data
class User:
def __init__(self, user_id, email=None, passwd_hash=None, authenticated=False):
self.user_id = user_id
self.email = email
self.passwd_hash = passwd_hash
self.authenticated = authenticated
def __repr__(self):
r = {
'user_id': self.user_id,
'email': self.email,
'passwd_hash': self.passwd_hash,
'authenticated': self.authenticated,
}
return str(r)
def can_login(self, passwd_hash):
from support.base.aes import SupportAES
tmp = SupportAES.decrypt(self.passwd_hash)
return passwd_hash == tmp
def is_active(self):
return True
def get_id(self):
return self.user_id
def is_authenticated(self):
return self.authenticated
def is_anonymous(self):
return False
+442
View File
@@ -0,0 +1,442 @@
import os, sys, traceback, time, logging, logging.handlers, shutil, platform
from datetime import datetime
from pytz import timezone, utc
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO
from flask_cors import CORS
from flaskext.markdown import Markdown
from flask_login import LoginManager, login_required
from .init_declare import check_api, CustomFormatter
class Framework:
__instance = None
@classmethod
def get_instance(cls):
if cls.__instance == None:
cls.__instance = Framework()
return cls.__instance
def __init__(self):
self.logger = None
self.app = None
self.celery = None
self.db = None
self.scheduler = None
self.socketio = None
self.path_app_root = None
self.path_data = None
self.users = {}
self.__level_unset_logger_list = []
self.__logger_list = []
self.__exit_code = -1
self.login_manager = None
#self.plugin_instance_list = {}
#self.plugin_menus = {}
# 그냥 F. 로 접근 하게....
self.SystemModelSetting = None
self.Job = None
self.login_required = login_required
self.check_api = check_api
self.__initialize()
def __initialize(self):
self.__config_initialize("first")
self.__make_default_dir()
self.logger = self.get_logger(__package__)
from support import set_logger
set_logger(self.logger)
self.__prepare_starting()
self.app = Flask(__name__)
self.__config_initialize('flask')
self.db = SQLAlchemy(self.app, session_options={"autoflush": False})
if True or self.config['run_flask']:
from .scheduler import Scheduler, Job
self.scheduler = Scheduler(self)
self.Job = Job
if self.config['use_gevent']:
self.socketio = SocketIO(self.app, cors_allowed_origins="*")
else:
self.socketio = SocketIO(self.app, cors_allowed_origins="*", async_mode='threading')
CORS(self.app)
Markdown(self.app)
self.login_manager = LoginManager()
self.login_manager.init_app(self.app)
self.login_manager.login_view = "/system/login"
self.celery = self.__init_celery()
def __init_celery(self):
try:
from celery import Celery
#if frame.config['use_celery'] == False or platform.system() == 'Windows':
if self.config['use_celery'] == False:
raise Exception('no celery')
try:
redis_port = os.environ['REDIS_PORT']
except:
redis_port = '6379'
self.app.config['CELERY_BROKER_URL'] = 'redis://localhost:%s/0' % redis_port
self.app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:%s/0' % redis_port
celery = Celery(self.app.name, broker=self.app.config['CELERY_BROKER_URL'], backend=self.app.config['CELERY_RESULT_BACKEND'])
celery.conf['CELERY_ENABLE_UTC'] = False
celery.conf.update(
task_serializer='pickle',
result_serializer='pickle',
accept_content=['pickle'],
timezone='Asia/Seoul'
)
from celery import bootsteps
from celery.bin import Option
celery.user_options['worker'].add(
Option('--config_filepath', action='store', dest='config_filepath', default='.', help='')
)
class CustomArgs(bootsteps.Step):
def __init__(self, worker, config_filepath=None, **options):
from . import F
F.logger.info("celery config filepath: {config_filepath}")
celery.steps['worker'].add(CustomArgs)
except Exception as e:
self.logger.error('CELERY!!!')
self.logger.error(f'Exception:{str(e)}')
self.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)
return celery
def initialize_system(self):
from system.setup import P
SystemInstance = P
try:
self.db.create_all()
except Exception as e:
self.logger.error('CRITICAL db.create_all()!!!')
self.logger.error(f'Exception:{str(e)}')
self.logger.error(traceback.format_exc())
SystemInstance.plugin_load()
self.app.register_blueprint(SystemInstance.blueprint)
self.config['flag_system_loading'] = True
self.__config_initialize('member')
self.__config_initialize('system_loading_after')
self.SystemModelSetting = SystemInstance.ModelSetting
def initialize_plugin(self):
from system.setup import P as SP
from .init_web import jinja_initialize
jinja_initialize(self.app)
#system.LogicPlugin.custom_plugin_update()
from .init_plugin import PluginManager
self.PluginManager = PluginManager
PluginManager.plugin_init()
PluginManager.plugin_menus['system'] = {'menu':SP.menu, 'match':False}
#from .init_menu import init_menu, get_menu_map
from .init_menu import MenuManager
MenuManager.init_menu()
#init_menu(self.plugin_menu)
#system.SystemLogic.apply_menu_link()
if self.config['run_flask']:
if self.config.get('port') == None:
self.config['port'] = SP.SystemModelSetting.get_int('port')
from . import log_viewer
from . import init_route
self.__make_default_logger()
self.logger.info('### LAST')
self.logger.info(f"### PORT: {self.config['port']}")
self.logger.info('### Now you can access App by webbrowser!!')
def __prepare_starting(self):
# 여기서 monkey.patch시 너무 늦다고 문제 발생
if self.config['run_flask'] and self.config.get('use_celery') == True:
try:
from gevent import monkey
#from gevent import monkey;monkey.patch_all()
#print('[MAIN] gevent mokey patch!!')
#sys.getfilesystemencoding = lambda: 'UTF-8'
except:
self.config['use_celery'] = False
print('[MAIN] gevent not installed!!')
###################################################
# 환경
###################################################
def __config_initialize(self, mode):
if mode == "first":
self.config = {}
self.config['os'] = platform.system()
self.config['flag_system_loading'] = False
self.config['run_flask'] = True if sys.argv[0].endswith('main.py') else False
self.config['run_celery'] = True if sys.argv[0].find('celery') != -1 else False
self.config['path_app'] = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if self.config['os'] == 'Windows' and self.config['path_app'][0] != '/':
self.config['path_app'] = self.config['path_app'][0].upper() + self.config['path_app'][1:]
self.path_app_root = self.config['path_app']
self.config['path_working'] = os.getcwd()
self.__process_args()
self.__load_config()
self.__init_define()
elif mode == "flask":
self.app.secret_key = os.urandom(24)
#self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/db/system.db?check_same_thread=False'
self.app.config['SQLALCHEMY_BINDS'] = {}
self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app.config['TEMPLATES_AUTO_RELOAD'] = True
self.app.config['JSON_AS_ASCII'] = False
elif mode == 'system_loading_after':
pass
#from system import SystemModelSetting
"""
app.config['config']['running_type'] = 'native'
if 'SJVA_RUNNING_TYPE' in os.environ:
app.config['config']['running_type'] = os.environ['SJVA_RUNNING_TYPE']
else:
import platform
if platform.system() == 'Windows':
app.config['config']['running_type'] = 'windows'
"""
def __init_define(self):
self.config['DEFINE'] = {}
# 이건 필요 없음
self.config['DEFINE']['MAIN_SERVER_URL'] = 'https://server.sjva.me'
def __process_args(self):
# celery 에서 args 처리시 문제 발생.
if self.config['run_flask']:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--config', default='.', help='config filepath. Default: {current folder}/config.yaml')
parser.add_argument('--repeat', default=0, type=int, help=u'Do not set. This value is set by automatic')
args = parser.parse_args()
self.config['arg_repeat'] = args.repeat
self.config['arg_config'] = args.config
else:
# 아주 안좋은 구조..
# celery user_options으로 configfilepath를 받은 후 처리해야하나, 로그파일 경로 등에서 데이터 폴더 위치를 미리 사용하는 경우가 많다.
# sys.argv에서 데이터 경로를 바로 가져와서 사용.
self.config['arg_repeat'] = 0
self.config['arg_config'] = sys.argv[-1].split('=')[-1]
#self.config['arg_config'] =
def __load_config(self):
from .init_declare import read_yaml
#if self.config['run_flask']:
if self.config['arg_config'] == '.':
#self.config['config_filepath'] = os.path.join(self.path_app_root, 'config.yaml')
self.config['config_filepath'] = os.path.join(self.config['path_working'], 'config.yaml')
if not os.path.exists(self.config['config_filepath']):
shutil.copy(
os.path.join(self.path_app_root, 'files', 'config.yaml.template'),
self.config['config_filepath']
)
else:
self.config['config_filepath'] = self.config['arg_config']
#os.environ['FLASK_FARM_CONFIG_FILEPATH'] = self.config['config_filepath']
#else:
# self.config['config_filepath'] = os.environ['FLASK_FARM_CONFIG_FILEPATH']
# self.logger.info(f"CELERY config : {self.config['config_filepath']}")
data = read_yaml(self.config['config_filepath'])
for key, value in data.items():
self.config[key] = value
if self.config['path_data'] == '.':
self.config['path_data'] = self.config['path_working']
# 예외적으로 현재폴더가 app일 경우 지저분해지는 것을 방지하기 위해 data 로 지정
if self.config['path_data'] == self.config['path_working']:
self.config['path_data'] = os.path.join(self.config['path_working'], 'data')
self.path_data = self.config['path_data']
def __make_default_dir(self):
os.makedirs(self.config['path_data'], exist_ok=True)
tmp = os.path.join(self.config['path_data'], 'tmp')
try:
import shutil
if os.path.exists(tmp):
shutil.rmtree(tmp)
except:
pass
sub = ['db', 'log', 'tmp']
for item in sub:
tmp = os.path.join(self.config['path_data'], item)
os.makedirs(tmp, exist_ok=True)
###################################################
###################################################
# 로그
###################################################
def get_logger(self, name):
logger = logging.getLogger(name)
if not logger.handlers:
level = logging.DEBUG
try:
if self.config['flag_system_loading']:
try:
from system import SystemModelSetting
level = SystemModelSetting.get_int('log_level')
except:
level = logging.DEBUG
if self.__level_unset_logger_list is not None:
for item in self.__level_unset_logger_list:
item.setLevel(level)
self.__level_unset_logger_list = None
else:
self.__level_unset_logger_list.append(logger)
if name.startswith('apscheduler'):
level = logging.CRITICAL
else:
self.__logger_list.append(logger)
except:
pass
logger.setLevel(level)
file_formatter = logging.Formatter(u'[%(asctime)s|%(levelname)s|%(filename)s:%(lineno)s] %(message)s')
def customTime(*args):
utc_dt = utc.localize(datetime.utcnow())
my_tz = timezone("Asia/Seoul")
converted = utc_dt.astimezone(my_tz)
return converted.timetuple()
file_formatter.converter = customTime
file_max_bytes = 1 * 1024 * 1024
fileHandler = logging.handlers.RotatingFileHandler(filename=os.path.join(self.path_data, 'log', f'{name}.log'), maxBytes=file_max_bytes, backupCount=5, encoding='utf8', delay=True)
streamHandler = logging.StreamHandler()
# handler에 fommater 세팅
fileHandler.setFormatter(file_formatter)
streamHandler.setFormatter(CustomFormatter())
# Handler를 logging에 추가
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
return logger
def __make_default_logger(self):
self.get_logger('apscheduler.scheduler')
self.get_logger('apscheduler.executors.default')
try: logging.getLogger('socketio').setLevel(logging.ERROR)
except: pass
try: logging.getLogger('engineio').setLevel(logging.ERROR)
except: pass
try: logging.getLogger('apscheduler.scheduler').setLevel(logging.ERROR)
except: pass
try: logging.getLogger('apscheduler.executors.default').setLevel(logging.ERROR)
except: pass
try: logging.getLogger('werkzeug').setLevel(logging.ERROR)
except: pass
def set_level(self, level):
try:
for l in self.__logger_list:
l.setLevel(level)
self.__make_default_logger()
except:
pass
###################################################
def start(self):
host = '0.0.0.0'
for i in range(10):
try:
#self.logger.debug(d(self.config))
self.socketio.run(self.app, host=host, port=self.config['port'], debug=self.config['debug'], use_reloader=self.config['use_reloader'])
self.logger.warning(f"EXIT CODE : {self.__exit_code}")
# 2021-05-18
if self.config['running_type'] in ['termux', 'entware']:
os._exit(self.__exit_code)
else:
if self.__exit_code != -1:
sys.exit(self.__exit_code)
else:
self.logger.warning(f"framework.exit_code is -1")
break
except Exception as exception:
self.logger.error(f"Start ERROR : {str(exception)}")
host = '127.0.0.1'
time.sleep(10*i)
continue
except KeyboardInterrupt:
self.logger.error('KeyboardInterrupt !!')
#except SystemExit:
# return
#sys.exit(self.__exit_code)
# system 플러그인에서 콜
def restart(self):
self.__exit_code = 1
self.__app_close()
def shutdown(self):
self.__exit_code = 0
self.__app_close()
def __app_close(self):
try:
from .init_plugin import PluginManager
PluginManager.plugin_unload()
self.socketio.stop()
except Exception as exception:
self.logger.error('Exception:%s', exception)
self.logger.error(traceback.format_exc())
def get_recent_version(self):
try:
import requests
url = f"{self.config['DEFINE']['MAIN_SERVER_URL']}/version"
self.config['recent_version'] = requests.get(url).text
return True
except Exception as e:
self.logger.error(f'Exception:{str(e)}')
self.logger.error(traceback.format_exc())
self.config['recent_version'] = '확인 실패'
return False
+104
View File
@@ -0,0 +1,104 @@
import os, copy, shutil
from framework import F, d
from support.base.yaml import SupportYaml
class MenuManager:
menu_map = None
@classmethod
def __load_menu_yaml(cls):
menu_yaml_filepath = os.path.join(F.config['path_data'], 'db', 'menu.yaml')
if os.path.exists(menu_yaml_filepath) == False:
shutil.copy(
os.path.join(F.config['path_app'], 'files', 'menu.yaml.template'),
menu_yaml_filepath
)
cls.menu_map = SupportYaml.read_yaml(menu_yaml_filepath)
"""
for cate in cls.menu_map:
cate['count'] = 0
cls.menu_map.insert(len(cls.menu_map)-1, {
'name':'미분류', 'count':0, 'list':[]
})
cls.menu_map[-1]['count'] = 1
F.logger.debug(cls.menu_map)
"""
@classmethod
def init_menu(cls):
#F.logger.debug(d(plugin_menus))
#print(plugin_menus)
cls.__load_menu_yaml()
from .init_plugin import PluginManager
plugin_menus = PluginManager.plugin_menus
copy_map = []
for category in cls.menu_map:
if 'uri' in category:
copy_map.append(category)
continue
cate_count = 0
tmp_cate_list = []
for item in category['list']:
if item['uri'] in plugin_menus:
plugin_menus[item['uri']]['match'] = True
tmp_cate_list.append(plugin_menus[item['uri']]['menu'])
cate_count += 1
elif item['uri'].startswith('http'):
tmp_cate_list.append({
'uri': item['uri'],
'name': item['name'],
'target': item.get('target', '_blank')
})
cate_count += 1
elif (len(item['uri'].split('/')) > 1 and item['uri'].split('/')[0] in plugin_menus) or item['uri'].startswith('javascript') or item['uri'] in ['-']:
tmp_cate_list.append({
'uri': item['uri'],
'name': item.get('name', ''),
})
cate_count += 1
elif item['uri'] == 'setting':
if len(PluginManager.setting_menus) > 0:
tmp_cate_list.append({
'uri': item['uri'],
'name': item.get('name', ''),
'list': PluginManager.setting_menus
})
if cate_count > 0:
copy_map.append({
'name': category['name'],
'list': tmp_cate_list,
'count': cate_count
})
cls.menu_map = copy_map
make_dummy_cate = False
for name, plugin_menu in plugin_menus.items():
#F.logger.info(d(plugin_menu))
#if 'uri' not in plugin_menu['menu']:
# continue
if plugin_menu['match'] == False:
if make_dummy_cate == False:
make_dummy_cate = True
cls.menu_map.insert(len(cls.menu_map)-1, {
'name':'미분류', 'count':0, 'list':[]
})
c = cls.menu_map[-2]
c['count'] += 1
c['list'].append(plugin_menu['menu'])
#F.logger.warning(d(cls.menu_map))
@classmethod
def get_menu_map(cls):
return cls.menu_map
+293
View File
@@ -0,0 +1,293 @@
import os, sys, traceback, threading, platform
from framework import F, d
class PluginManager:
plugin_list = {}
plugin_menus = {}
setting_menus = []
@classmethod
def get_plugin_name_list(cls):
#if not app.config['config']['auth_status']:
# return
"""
plugin_path = os.path.join(frame.config['path_app'], 'plugins')
sys.path.insert(0, plugin_path)
from system import SystemModelSetting
plugins = os.listdir(plugin_path)
"""
plugins = []
pass_include = []
except_plugin_list = []
#2019-07-17
if F.config.get('plugin_loading_only_devpath', None) != True:
try:
plugin_path = os.path.join(F.config['path_data'], 'plugins')
if os.path.exists(plugin_path) == True and os.path.isdir(plugin_path) == True:
sys.path.insert(1, plugin_path)
tmps = os.listdir(plugin_path)
add_plugin_list = []
for t in tmps:
if not t.startswith('_') and os.path.isdir(os.path.join(plugin_path, t)):
add_plugin_list.append(t)
plugins = plugins + add_plugin_list
pass_include = pass_include + add_plugin_list
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
# 2018-09-04
try:
plugin_path = F.SystemModelSetting.get('plugin_dev_path')
if plugin_path != '':
if os.path.exists(plugin_path):
sys.path.insert(0, plugin_path)
tmps = os.listdir(plugin_path)
add_plugin_list = []
for t in tmps:
if not t.startswith('_') and os.path.isdir(os.path.join(plugin_path, t)):
add_plugin_list.append(t)
plugins = plugins + add_plugin_list
pass_include = pass_include + add_plugin_list
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
# plugin_loading_list
try:
plugin_loading_list = F.config.get('plugin_loading_list', None)
if plugin_loading_list != None and type(plugin_loading_list) == type([]):
new_plugins = []
for _ in plugins:
if _ in plugin_loading_list:
new_plugins.append(_)
plugins = new_plugins
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
# plugin_except_list
try:
plugin_except_list = F.config.get('plugin_except_list', None)
if plugin_except_list != None and type(plugin_except_list) == type([]):
new_plugins = []
for _ in plugins:
if _ not in plugin_except_list:
new_plugins.append(_)
plugins = new_plugins
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
return plugins
# menu, blueprint, plugin_info, plugin_load, plugin_unload
@classmethod
def plugin_init(cls):
try:
plugins = cls.get_plugin_name_list()
plugins = sorted(plugins)
F.logger.debug(plugins)
for plugin_name in plugins:
#logger.debug(len(system.LogicPlugin.current_loading_plugin_list))
#if plugin_name.startswith('_'):
# continue
#if plugin_name == 'terminal' and platform.system() == 'Windows':
# continue
#if plugin_name in except_plugin_list:
# F.logger.debug('Except plugin : %s' % frame.plugin_menu)
# continue
F.logger.debug(f'[+] PLUGIN LOADING Start.. [{plugin_name}]')
entity = {'version':'3'}
try:
mod = __import__('%s' % (plugin_name), fromlist=[])
mod_plugin_info = None
# 2021-12-31
#import system
#if plugin_name not in system.LogicPlugin.current_loading_plugin_list:
# system.LogicPlugin.current_loading_plugin_list[plugin_name] = {'status':'loading'}
try:
mod_plugin_info = getattr(mod, 'plugin_info')
entity['module'] = mod
"""
if 'category' not in mod_plugin_info and 'category_name' in mod_plugin_info:
mod_plugin_info['category'] = mod_plugin_info['category_name']
if 'policy_point' in mod_plugin_info:
if mod_plugin_info['policy_point'] > app.config['config']['point']:
system.LogicPlugin.current_loading_plugin_list[plugin_name]['status'] = 'violation_policy_point'
continue
if 'policy_level' in mod_plugin_info:
if mod_plugin_info['policy_level'] > app.config['config']['level']:
system.LogicPlugin.current_loading_plugin_list[plugin_name]['status'] = 'violation_policy_level'
continue
if 'category' in mod_plugin_info and mod_plugin_info['category'] == 'beta':
if SystemModelSetting.get_bool('use_beta') == False:
system.LogicPlugin.current_loading_plugin_list[plugin_name]['status'] = 'violation_beta'
continue
"""
except Exception as exception:
#logger.error('Exception:%s', exception)
#logger.error(traceback.format_exc())
#mod_plugin_info = getattr(mod, 'setup')
F.logger.warning(f'[!] PLUGIN_INFO not exist : [{plugin_name}]')
if mod_plugin_info == None:
try:
mod = __import__(f'{plugin_name}.setup', fromlist=['setup'])
entity['version'] = '4'
except Exception as e:
F.logger.error(f'Exception:{str(e)}')
F.logger.error(traceback.format_exc())
F.logger.warning(f'[!] NOT normal plugin : [{plugin_name}]')
#entity['version'] = 'not_plugin'
try:
if entity['version'] != '4':
mod_blue_print = getattr(mod, 'blueprint')
else:
entity['setup_mod'] = mod
entity['P'] = getattr(mod, 'P')
mod_blue_print = getattr(entity['P'], 'blueprint')
if mod_blue_print:
#if plugin_name in pass_include or is_include_menu(plugin_name):
F.app.register_blueprint(mod_blue_print)
except Exception as exception:
#logger.error('Exception:%s', exception)
#logger.error(traceback.format_exc())
F.logger.warning(f'[!] BLUEPRINT not exist : [{plugin_name}]')
cls.plugin_list[plugin_name] = entity
#system.LogicPlugin.current_loading_plugin_list[plugin_name]['status'] = 'success'
#system.LogicPlugin.current_loading_plugin_list[plugin_name]['info'] = mod_plugin_info
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
F.logger.debug('no blueprint')
#from tool_base import d
#logger.error(d(system.LogicPlugin.current_loading_plugin_list))
# 2021-07-01 모듈에 있는 DB 테이블 생성이 안되는 문제
# 기존 구조 : db.create_all() => 모듈 plugin_load => celery task 등록 후 리턴
# 변경 구조 : 모듈 plugin_load => db.create_all() => celery인 경우 리턴
# plugin_load 를 해야 하위 로직에 있는 DB가 로딩된다.
# plugin_load 에 db는 사용하는 코드가 있으면 안된다. (테이블도 없을 때 에러발생)
try:
#logger.warning('module plugin_load in celery ')
cls.plugin_list['mod']['module'].plugin_load()
except Exception as exception:
F.logger.debug(f'mod plugin_load error!!')
#logger.error('Exception:%s', exception)
#logger.error(traceback.format_exc())
# import가 끝나면 DB를 만든다.
# 플러그인 로드시 DB 초기화를 할 수 있다.
if not F.config['run_celery']:
try:
F.db.create_all()
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
F.logger.debug('db.create_all error')
if not F.config['run_flask']:
# 2021-06-03
# 모듈의 로직에 있는 celery 함수는 등록해주어야한다.
#try:
# logger.warning('module plugin_load in celery ')
# plugin_instance_list['mod'].plugin_load()
#except Exception as exception:
# logger.error('module plugin_load error')
# logger.error('Exception:%s', exception)
# logger.error(traceback.format_exc())
# 2021-07-01
# db때문에 위에서 로딩함.
return
for key, entity in cls.plugin_list.items():
try:
mod_plugin_load = None
if entity['version'] == '3':
mod_plugin_load = getattr(entity['module'], 'plugin_load')
elif entity['version'] == '4':
mod_plugin_load = getattr(entity['P'], 'plugin_load')
#if mod_plugin_load and (key in pass_include or is_include_menu(key)):
if mod_plugin_load:
def func(mod_plugin_load, key):
try:
F.logger.debug(f'[!] plugin_load threading start : [{key}]')
#mod.plugin_load()
mod_plugin_load()
F.logger.debug(f'[!] plugin_load threading end : [{key}]')
except Exception as exception:
F.logger.error('### plugin_load exception : %s', key)
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
# mod는 위에서 로딩
if key != 'mod':
t = threading.Thread(target=func, args=(mod_plugin_load, key))
t.setDaemon(True)
t.start()
#if key == 'mod':
# t.join()
except Exception as exception:
F.logger.debug(f'[!] PLUGIN_LOAD function not exist : [{key}]')
#logger.error('Exception:%s', exception)
#logger.error(traceback.format_exc())
#logger.debug('no init_scheduler')
try:
mod_menu = None
if entity['version'] == '3':
mod_menu = getattr(entity['module'], 'menu')
elif entity['version'] == '4':
mod_menu = getattr(entity['P'], 'menu')
if mod_menu:# and (key in pass_include or is_include_menu(key)):
cls.plugin_menus[key]= {'menu':mod_menu, 'match':False}
if entity['version'] == '4':
setting_menu = getattr(entity['P'], 'setting_menu')
if setting_menu != None:
cls.setting_menus.append(setting_menu)
except Exception as exception:
F.logger.debug('no menu')
F.logger.debug('### plugin_load threading all start.. : %s ', len(cls.plugin_list))
# 모든 모듈을 로드한 이후에 app 등록, table 생성, start
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
@classmethod
def plugin_unload(cls):
for key, entity in cls.plugin_list.items():
try:
if entity['version'] == '3':
mod_plugin_unload = getattr(entity['module'], 'plugin_unload')
elif entity['version'] == '4':
mod_plugin_unload = getattr(entity['P'], 'plugin_unload')
#if plugin_name == 'rss':
# continue
#mod_plugin_unload = getattr(mod, 'plugin_unload')
if mod_plugin_unload:
mod_plugin_unload()
#mod.plugin_unload()
except Exception as e:
F.logger.error('module:%s', key)
F.logger.error(f'Exception:{str(e)}')
F.logger.error(traceback.format_exc())
try:
from system.setup import P
P.plugin_unload()
except Exception as e:
F.logger.error(f'Exception:{str(e)}')
F.logger.error(traceback.format_exc())
+121
View File
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import sys
from datetime import datetime, timedelta
import json
import traceback
# third-party
from flask import redirect, render_template, Response, request, jsonify, send_from_directory
from flask_login import login_user, logout_user, current_user, login_required
# sjva 공용
from framework import F, check_api, app, db, VERSION, logger, path_data
import system
@F.app.route('/global/ajax/<sub>', methods=['GET', 'POST'])
@login_required
def global_ajax(sub):
#logger.debug('/global/ajax/%s', sub)
if sub == 'listdir':
if 'path' in request.form:
#if os.path.isfile(request.form['path']):
# return jsonify('')
path = request.form['path']
if os.path.isfile(path):
path = os.path.dirname(path)
result_list = os.listdir(path)
if 'only_dir' in request.form and request.form['only_dir'].lower() == 'true':
result_list = [name for name in result_list if os.path.isdir(os.path.join(path, name))]
result_list.sort()
result_list = [f"{x}|{os.path.join(path,x)}" for x in result_list]
tmp = os.path.dirname(path)
if path != tmp:
result_list = [f'..|{tmp}'] + result_list
return jsonify(result_list)
else:
return jsonify(None)
elif sub == 'is_available_edit':
# globalEditBtn
try:
import flaskcode
return jsonify(True)
except:
return jsonify(True)
@app.route('/robots.txt')
def robot_to_root():
return send_from_directory('', 'static/file/robots.txt')
@app.route("/")
@app.route("/None")
@app.route("/home")
def home():
return redirect('/system/home')
@app.route("/version")
def get_version():
return VERSION
@app.route("/open/<path:path>")
@login_required
def open_file(path):
return send_from_directory('/', path)
@app.route("/file/<path:path>")
@check_api
def file2(path):
logger.debug('file2 :%s', path)
return send_from_directory('/', path)
@app.route("/up", methods=['GET', 'POST'])
def upload():
# curl -F file=@downloader_video.tar https://dev.soju6jan.com/up
#
try:
if request.method == 'POST':
f = request.files['file']
from werkzeug import secure_filename
tmp = secure_filename(f.filename)
logger.debug('upload : %s', tmp)
f.save(os.path.join(path_data, 'upload', tmp))
return jsonify('success')
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return jsonify('fail')
+58
View File
@@ -0,0 +1,58 @@
import re
from flask_login import current_user
from framework import F
def get_menu(full_query):
match = re.compile(r'\/(?P<menu>.*?)\/manual\/(?P<sub2>.*?)($|\?)').match(full_query)
if match:
return match.group('menu'), 'manual', match.group('sub2')
match = re.compile(r'\/(?P<menu>.*?)\/(?P<sub>.*?)\/(?P<sub2>.*?)($|\/|\?)').match(full_query)
if match:
return match.group('menu'), match.group('sub'), match.group('sub2')
match = re.compile(r'\/(?P<menu>.*?)\/(?P<sub>.*?)($|\/|\?)').match(full_query)
if match:
return match.group('menu'), match.group('sub'), None
match = re.compile(r'\/(?P<menu>.*?)($|\/|\?)').match(full_query)
if match:
return match.group('menu'), None , None
return 'home', None, None
def get_theme():
return F.SystemModelSetting.get('theme')
#def get_login_status():
# if current_user is None:
# return False
# return current_user.is_authenticated
def get_web_title():
try:
return F.SystemModelSetting.get('web_title')
except:
return 'Home'
def is_https():
return (F.SystemModelSetting.get('ddns').find('https://') != -1)
def jinja_initialize(app):
#from .init_menu import get_menu_map, get_plugin_menu
from .init_menu import MenuManager
app.jinja_env.globals.update(get_menu=get_menu)
app.jinja_env.globals.update(get_theme=get_theme)
app.jinja_env.globals.update(get_menu_map=MenuManager.get_menu_map)
app.jinja_env.globals.update(get_web_title=get_web_title)
app.jinja_env.filters['get_menu'] = get_menu
app.jinja_env.filters['get_theme'] = get_theme
app.jinja_env.filters['get_menu_map'] = MenuManager.get_menu_map
app.jinja_env.filters['get_web_title'] = get_web_title
app.jinja_env.auto_reload = True
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
+132
View File
@@ -0,0 +1,132 @@
import os, traceback, time, threading
from flask import request
from framework import F
from support.base.util import SingletonClass
namespace = 'log'
@F.socketio.on('connect', namespace='/%s' % namespace)
def socket_connect():
F.logger.debug('log connect')
@F.socketio.on('start', namespace='/%s' % namespace)
def socket_file(data):
try:
package = filename = None
if 'package' in data:
package = data['package']
else:
filename = data['filename']
LogViewer.instance().start(package, filename, request.sid)
F.logger.debug('start package:%s filename:%s sid:%s', package, filename, request.sid)
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
@F.socketio.on('disconnect', namespace='/%s' % namespace)
def disconnect():
try:
LogViewer.instance().disconnect(request.sid)
F.logger.debug('disconnect sid:%s', request.sid)
except Exception as exception:
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
class WatchThread(threading.Thread):
def __init__(self, package, filename):
super(WatchThread, self).__init__()
self.stop_flag = False
self.package = package
self.filename = filename
self.daemon = True
def stop(self):
self.stop_flag = True
def run(self):
F.logger.debug('WatchThread.. Start %s', self.package)
if self.package is not None:
logfile = os.path.join(F.config['path_data'], 'log', f'{self.package}.log')
key = 'package'
value = self.package
else:
logfile = os.path.join(F.config['path_data'], 'log', self.filename)
key = 'filename'
value = self.filename
if os.path.exists(logfile):
with open(logfile, 'r') as f:
f.seek(0, os.SEEK_END)
while not self.stop_flag:
line = f.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
F.socketio.emit("add", {key : value, 'data': line}, namespace='/log', broadcast=True)
F.logger.debug('WatchThread.. End %s', value)
else:
F.socketio.emit("add", {key : value, 'data': 'not exist logfile'}, namespace='/log', broadcast=True)
class LogViewer(SingletonClass):
watch_list = {}
@classmethod
def start(cls, package, filename, sid):
# 2019-04-02 간만에 봤더니 헷깔려서 적는다
# 이 쓰레드는 오픈시 이전 데이타만을 보내는 쓰레드다. 실시간보는거 아님.
def thread_function():
if package is not None:
logfile = os.path.join(F.config['path_data'], 'log', f'{package}.log')
else:
logfile = os.path.join(F.config['path_data'], 'log', filename)
if os.path.exists(logfile):
ins_file = open(logfile, 'r', encoding='utf8') ## 3)
line = ins_file.read()
F.socketio.emit("on_start", {'data': line}, namespace='/log')
F.logger.debug('on_start end')
else:
F.socketio.emit("on_start", {'data': 'not exist logfile'}, namespace='/log')
if package is not None:
key = package
else:
key = filename
thread = threading.Thread(target=thread_function, args=())
thread.daemon = True
thread.start()
if key not in cls.watch_list:
cls.watch_list[key] = {}
cls.watch_list[key]['sid'] = []
cls.watch_list[key]['thread'] = WatchThread(package, filename)
cls.watch_list[key]['thread'].start()
cls.watch_list[key]['sid'].append(sid)
@classmethod
def disconnect(cls, sid):
find = False
find_key = None
for key, value in cls.watch_list.items():
F.logger.debug('key:%s value:%s', key, value)
for s in value['sid']:
if sid == s:
find = True
find_key = key
value['sid'].remove(s)
break
if find:
break
if not find:
return
if not cls.watch_list[find_key]['sid']:
F.logger.debug('thread kill')
cls.watch_list[find_key]['thread'].stop()
del cls.watch_list[find_key]
+253
View File
@@ -0,0 +1,253 @@
import traceback
from pytz import timezone
from datetime import datetime, timedelta
from random import randint
from apscheduler.jobstores.base import JobLookupError
from apscheduler.triggers.cron import CronTrigger
import traceback, threading
from datetime import datetime
from pytz import timezone
from random import randint
from support.base.util import pt
class Scheduler(object):
job_list = []
first_run_check_thread = None
def __init__(self, frame):
self.frame = frame
self.logger = frame.logger
try:
if frame.config['use_gevent']:
from apscheduler.schedulers.gevent import GeventScheduler
self.sched = GeventScheduler(timezone='Asia/Seoul')
else:
raise Exception('')
except:
from apscheduler.schedulers.background import BackgroundScheduler
self.sched = BackgroundScheduler(timezone='Asia/Seoul')
self.sched.start()
self.logger.info('SCHEDULER start..')
@pt
def first_run_check_thread_function(self):
try:
#time.sleep(60)
#for i in range(5):
flag_exit = True
for job_instance in self.job_list:
if not job_instance.run:
continue
if job_instance.count == 0 and not job_instance.is_running and job_instance.is_interval:
#if job_instance.count == 0 and not job_instance.is_running:
job = self.sched.get_job(job_instance.job_id)
if job is not None:
self.logger.warning('job_instance : %s', job_instance.plugin)
self.logger.warning('XX job re-sched:%s', job)
flag_exit = False
tmp = randint(1, 20)
job.modify(next_run_time=datetime.now(timezone('Asia/Seoul')) + timedelta(seconds=tmp))
#break
else:
pass
if flag_exit:
self.remove_job("scheduler_check")
#time.sleep(30)
except Exception as exception:
self.logger.error('Exception:%s', exception)
self.logger.error(traceback.format_exc())
def shutdown(self):
self.sched.shutdown()
def kill_scheduler(self, job_id):
try:
self.sched.remove_job(job_id)
except JobLookupError as err:
self.logger.debug("fail to stop Scheduler: {err}".format(err=err))
self.logger.debug(traceback.format_exc())
def add_job_instance(self, job_instance, run=True):
if self.frame.config['run_flask']:
if not self.is_include(job_instance.job_id):
job_instance.run = run
Scheduler.job_list.append(job_instance)
if job_instance.is_interval:
self.sched.add_job(job_instance.job_function, 'interval', minutes=job_instance.interval, seconds=job_instance.interval_seconds, id=job_instance.job_id, args=(None))
elif job_instance.is_cron:
self.sched.add_job(job_instance.job_function, CronTrigger.from_crontab(job_instance.interval), id=job_instance.job_id, args=(None))
job = self.sched.get_job(job_instance.job_id)
if run and job_instance.is_interval:
tmp = randint(5, 20)
job.modify(next_run_time=datetime.now(timezone('Asia/Seoul')) + timedelta(seconds=tmp))
def execute_job(self, job_id):
self.logger.debug('execute_job:%s', job_id)
job = self.sched.get_job(job_id)
tmp = randint(5, 20)
job.modify(next_run_time=datetime.now(timezone('Asia/Seoul')) + timedelta(seconds=tmp))
def is_include(self, job_id):
job = self.sched.get_job(job_id)
return (job is not None)
def remove_job(self, job_id):
try:
if self.is_include(job_id):
self.sched.remove_job(job_id)
job = self.get_job_instance(job_id)
if not job.is_running:
self.remove_job_instance(job_id)
self.logger.debug('remove job_id:%s', job_id)
return True
except JobLookupError as err:
self.logger.debug("fail to remove Scheduler: {err}".format(err=err))
self.logger.debug(traceback.format_exc())
return False
def get_job_instance(self, job_id):
for job in Scheduler.job_list:
if job.job_id == job_id:
return job
def is_running(self, job_id):
job = self.get_job_instance(job_id)
if job is None:
return False
else:
return job.is_running
# job에서만 호출한다..
def remove_job_instance(self, job_id):
# function이 실행중일때 제거하면..
# 실행중이나 목록에서 빠져버린다..
for job in Scheduler.job_list:
if job.job_id == job_id:
Scheduler.job_list.remove(job)
self.logger.debug('remove_job_instance : %s', job_id)
break
def get_job_list_info(self):
ret = []
idx = 0
job_list = self.sched.get_jobs()
#logger.debug('len jobs %s %s', len(jobs), len(Scheduler.job_list))
for j in job_list:
idx += 1
entity = {}
entity['no'] = idx
entity['id'] = j.id
entity['next_run_time'] = j.next_run_time.strftime('%m-%d %H:%M:%S')
remain = (j.next_run_time - datetime.now(timezone('Asia/Seoul')))
tmp = ''
if remain.days > 0:
tmp += '%s' % (remain.days)
remain = remain.seconds
if remain//3600 > 0:
tmp += '%s시간 ' % (remain//3600)
remain = remain % 3600
if remain // 60 > 0:
tmp += '%s' % (remain//60)
tmp += '%s' % (remain%60)
#entity['remain_time'] = (j.next_run_time - datetime.now(timezone('Asia/Seoul'))).seconds
entity['remain_time'] = tmp
job = self.get_job_instance(j.id)
if job is not None:
entity['count'] = job.count
entity['plugin'] = job.plugin
if job.is_cron:
entity['interval'] = job.interval
elif job.interval == 9999:
entity['interval'] = '항상 실행'
entity['remain_time'] = ''
else:
entity['interval'] = '%s%s' % (job.interval, job.interval_seconds)
entity['is_running'] = job.is_running
entity['description'] = job.description
entity['running_timedelta'] = job.running_timedelta.seconds if job.running_timedelta is not None else '-'
entity['make_time'] = job.make_time.strftime('%m-%d %H:%M:%S')
entity['run'] = job.run
else:
entity['count'] = ''
entity['plugin'] = ''
entity['interval'] = ''
entity['is_running'] = ''
entity['description'] = ''
entity['running_timedelta'] = ''
entity['make_time'] = ''
entity['run'] = True
ret.append(entity)
return ret
class Job(object):
def __init__(self, plugin, job_id, interval, target_function, description, args=None):
self.plugin = plugin
self.job_id = job_id
self.interval = '%s' % interval
self.interval_seconds = randint(1, 59)
self.target_function = target_function
self.description = description
self.is_running = False
self.thread = None
self.start_time = None
self.end_time = None
self.running_timedelta = None
self.status = None
self.count = 0
self.make_time = datetime.now(timezone('Asia/Seoul'))
if len(self.interval.strip().split(' ')) == 5:
self.is_cron = True
self.is_interval = False
else:
self.is_cron = False
self.is_interval = True
if self.is_interval:
if isinstance(self.interval, str):
self.interval = int(self.interval)
self.args = args
# true이고 interval이면 바로 실행
# false이면 스케쥴링시간이 되면 실행
# add_job_instance에서 true이면 20초 이내에 실행하려고 함.
# false이면 넣을 때는 실행하지 않고 다음 주기때 실행
self.run = True
def job_function(self):
try:
from framework import F
self.is_running = True
self.start_time = datetime.now(timezone('Asia/Seoul'))
if self.args is None:
self.thread = threading.Thread(target=self.target_function, args=())
else:
self.thread = threading.Thread(target=self.target_function, args=(self.args,))
self.thread.daemon = True
self.thread.start()
self.thread.join()
self.end_time = datetime.now(timezone('Asia/Seoul'))
self.running_timedelta = self.end_time - self.start_time
self.status = 'success'
if not F.scheduler.is_include(self.job_id):
F.scheduler.remove_job_instance(self.job_id)
self.count += 1
except Exception as exception:
self.status = 'exception'
F.logger.error('Exception:%s', exception)
F.logger.error(traceback.format_exc())
finally:
self.is_running = False
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+104
View File
@@ -0,0 +1,104 @@
body {
font-size: .850rem;
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.alert-minimalist {
background-color: rgb(241, 242, 240);
border-color: rgba(149, 149, 149, 0.3);
border-radius: 3px;
color: rgb(149, 149, 149);
padding: 10px;
}
.alert-minimalist > [data-notify="icon"] {
height: 50px;
margin-right: 12px;
}
.alert-minimalist > [data-notify="title"] {
color: rgb(51, 51, 51);
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.alert-minimalist > [data-notify="message"] {
font-size: 80%;
}
textarea {
font-family: Courier
}
.loading { position: fixed; left: 45%; top: 50%; background: #00000000; }
.table > tbody > tr.collapse > td {
background-color: #009fff0d !important;
}
.tableRowHover tbody tr:not(.tableRowHoverOff):hover td {
background-color: #ffff0080 !important;
}
.table > tbody > tr > td { vertical-align: middle; }
.tab-pane {
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
border-bottom: 1px solid #ddd;
border-radius: 0px 0px 5px 5px;
padding: 10px;
}
.btn-toolbar { text-align: center; }
.nav-tabs { margin-bottom: 0; }
.navbar-nav li:hover>.dropdown-menu { display: block; }
.dropdown-toggle::after { display:none; }
.set-left { text-align: left; }
@media (min-width: 768px) {
.set-left { text-align: right; }
}
.badge-sm {
min-width: 1.8em;
padding: .25em !important;
margin-left: .1em;
margin-right: .1em;
color: white !important;
cursor: pointer;
}
@media all and (min-width: 992px) {
.dropdown-menu li{
position: relative;
}
.dropdown-menu .submenu{
display: none;
position: absolute;
left:100%; top:-7px;
}
.dropdown-menu .submenu-left{
right:100%; left:auto;
}
.dropdown-menu > li:hover{ background-color: #f1f1f1 }
.dropdown-menu > li:hover > .submenu{
display: block;
}
}
/* ============ desktop view .end// ============ */
/* ============ small devices ============ */
@media (max-width: 991px) {
.dropdown-menu .dropdown-menu{
margin-left:0.7rem; margin-right:0.7rem; margin-bottom: .5rem;
}
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2
View File
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
Binary file not shown.

After

Width:  |  Height:  |  Size: 318 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+296
View File
@@ -0,0 +1,296 @@
var tmp = window.location.pathname.split('/');
if (tmp.length == 2) {
var PACKAGE_NAME = tmp[1];
var MODULE_NAME = "";
var PAGE_NAME = "";
} else if (tmp.length == 3) {
var PACKAGE_NAME = tmp[1];
var MODULE_NAME = tmp[2];
var PAGE_NAME = "";
} else if (tmp.length == 4){
var PACKAGE_NAME = tmp[1];
var MODULE_NAME = tmp[2];
var PAGE_NAME = tmp[3];
}
var current_data = null;
console.log("NAME: [" + PACKAGE_NAME + '] [' + MODULE_NAME + '] [' + PAGE_NAME + ']');
$(window).on("load resize", function (event) {
var $navbar = $(".navbar");
var $body = $("body");
$body.css("padding-top", $navbar.outerHeight());
});
///////////////////////////////////////
// 사용 미확인
///////////////////////////////////////
// 알림
$.notify({
// options
icon: 'glyphicon glyphicon-ok',
title: 'SJVA',
message: '',
url: '',
target: '_blank'
},{
// settings
element: 'body',
position: null,
type: "info",
allow_dismiss: true,
newest_on_top: false,
showProgressbar: false,
placement: {
from: "top",
align: "right"
},
offset: 20,
spacing: 10,
z_index: 1031,
delay: 10000,
timer: 1000,
url_target: '_blank',
mouse_over: null,
animate: {
enter: 'animated fadeInDown',
exit: 'animated fadeOutUp'
},
onShow: null,
onShown: null,
onClose: null,
onClosed: null,
icon_type: 'class',
template: '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">' +
'<button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>' +
'<span data-notify="icon"></span> ' +
'<span data-notify="title" style="word-break:break-all;">{1}</span> ' +
'<span data-notify="message" style="word-break:break-all;">{2}</span>' +
'<div class="progress" data-notify="progressbar">' +
'<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>' +
'</div>' +
'<a href="{3}" target="{4}" data-notify="url"></a>' +
'</div>'
});
function notify(msg, type) {
$.notify('<strong>' + msg + '</strong>', {type: type});
}
// 메뉴 제거
function hideMenu() {
$("#menu_div").html('');
hideMenuModule();
hideMenuPage();
}
function hideMenuModule() {
$("#menu_module_div").html('');
}
function hideMenuPage() {
$("#menu_page_div").html('');
}
// 넓은 화면
function setWide() {
$('#main_container').attr('class', 'container-fluid');
}
function showModal(data='EMPTY', title='JSON', json=true) {
document.getElementById("modal_title").innerHTML = title;
if (json) {
data = JSON.stringify(data, null, 2);
}
document.getElementById("modal_body").innerHTML = "<pre>"+ data + "</pre>";;
$("#large_modal").modal();
}
///////////////////////////////////////
// camel
function get_formdata(form_id) {
// on, off 일수도 있으니 모두 True, False로 통일하고
// 밑에서는 False인 경우 값이 추가되지 않으니.. 수동으로 넣어줌
var checkboxs = $(form_id + ' input[type=checkbox]');
//for (var i in checkboxs) {
for (var i =0 ; i < checkboxs.length; i++) {
if ( $(checkboxs[i]).is(':checked') ) {
$(checkboxs[i]).val('True');
} else {
$(checkboxs[i]).val('False');
}
}
var formData = $(form_id).serialize();
$.each($(form_id + ' input[type=checkbox]')
.filter(function(idx) {
return $(this).prop('checked') === false
}),
function(idx, el) {
var emptyVal = "False";
formData += '&' + $(el).attr('name') + '=' + emptyVal;
}
);
formData = formData.replace("&global_scheduler=True", "")
formData = formData.replace("&global_scheduler=False", "")
formData = formData.replace("global_scheduler=True&", "")
formData = formData.replace("global_scheduler=False&", "")
return formData;
}
function use_collapse(div, reverse=false) {
var ret = $('#' + div).prop('checked');
if (reverse) {
if (ret) {
$('#' + div + '_div').collapse('hide')
} else {
$('#' + div + '_div').collapse('show')
}
} else {
if (ret) {
$('#' + div + '_div').collapse('show')
} else {
$('#' + div + '_div').collapse('hide')
}
}
}
///////////////////////////////////////
// 유틸리티 - 프로젝트 관련성 없음
///////////////////////////////////////
function humanFileSize(bytes) {
var thresh = 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = ['KB','MB','GB','TB','PB','EB','ZB','YB']
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
}
function FormatNumberLength(num, length) {
var r = "" + num;
while (r.length < length) {
r = "0" + r;
}
return r;
}
function msToHMS( ms ) {
// 1- Convert to seconds:
var seconds = ms / 1000;
// 2- Extract hours:
var hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
var minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = seconds % 60;
return (''+hours).padStart(2, "0")+":"+(''+minutes).padStart(2, "0")+":"+parseInt(seconds);
}
///////////////////////////////////////
// 사용 미확인
///////////////////////////////////////
function duration_str(duration) {
duration = duration / 100;
var minutes = parseInt(duration / 60);
var hour = parseInt(minutes / 60);
var min = parseInt(minutes % 60);
var sec = parseInt((duration/60 - parseInt(duration/60)) * 60);
return pad(hour, 2) + ':' + pad(min, 2) + ':' + pad(sec,2);
}
// 자리맞춤
function pad(n, width) {
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
}
// jquery extend function
// post로 요청하면서 리다이렉트
// 푹 자동에서 푹 기본 검색할때 사용
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
//console.log(key);
//console.log(value);
value = value.split('"').join('\"')
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
+245
View File
@@ -0,0 +1,245 @@
// global socketio
$(document).ready(function(){
});
$(document).ready(function(){
$('.loading').hide();
})
.ajaxStart(function(){
$('.loading').show();
})
.ajaxStop(function(){
$('.loading').hide();
});
var protocol = window.location.protocol;
var frameSocket = io.connect(protocol + "//" + document.domain + ":" + location.port + "/framework");
frameSocket.on('notify', function(data){
$.notify({
message : data['msg'],
url: data['url'],
target: '_self'
},{
type: data['type'],
});
});
frameSocket.on('modal', function(data){
m_modal(data.data, data.title, false);
});
frameSocket.on('command_modal_add_text', function(data){
document.getElementById("command_modal_textarea").innerHTML += data ;
document.getElementById("command_modal_textarea").scrollTop = document.getElementById("command_modal_textarea").scrollHeight;
});
frameSocket.on('command_modal_show', function(data){
command_modal_show(data)
});
frameSocket.on('command_modal_clear', function(data){
document.getElementById("command_modal_textarea").innerHTML = ""
});
frameSocket.on('loading_hide', function(data){
$('#loading').hide();
});
frameSocket.on('refresh', function(data){
console.log('data')
window.location.reload();
});
///////////////////////////////////////
// Global - 버튼
///////////////////////////////////////
$("body").on('click', '#globalOpenBtn', function(e) {
e.preventDefault();
url = $(this).data('url')
window.open(url, "_blank");
});
$("body").on('click', '#globalLinkBtn', function(e) {
e.preventDefault();
url = $(this).data('url')
window.location.href = url;
});
// global_link_btn 모두 찾아 변경
$("body").on('click', '#globalSettingSaveBtn', function(e){
e.preventDefault();
globalSettingSave();
});
function globalSettingSave() {
var formData = get_formdata('#setting');
$.ajax({
url: '/' + PACKAGE_NAME + '/ajax/setting_save',
type: "POST",
cache: false,
data: formData,
dataType: "json",
success: function (ret) {
if (ret) {
$.notify('<strong>설정을 저장하였습니다.</strong>', {
type: 'success'
});
} else {
$.notify('<strong>설정 저장에 실패하였습니다.</strong>', {
type: 'warning'
});
}
}
});
}
$("body").on('click', '#globalEditBtn', function(e) {
e.preventDefault();
file = $(this).data('file');
console.log(file);
$.ajax({
url: '/global/ajax/is_available_edit',
type: "POST",
cache: false,
data: {},
dataType: "json",
success: function (ret) {
if (ret) {
window.location.href = '/flaskcode?open=' + file;
} else {
notify('편집기 플러그인을 설치해야 합니다.', 'warning');
}
}
});
});
///////////////////////////////////////
// Global - 함수
///////////////////////////////////////
function globalSendCommand(command, arg1, arg2, arg3, modal_title, callback) {
console.log("globalSendCommand [" + command + '] [' + arg1 + '] [' + arg2 + '] [' + arg3 + '] [' + modal_title + '] [' + callback);
console.log('/' + PACKAGE_NAME + '/ajax/' + MODULE_NAME + '/command');
$.ajax({
url: '/' + PACKAGE_NAME + '/ajax/' + MODULE_NAME + '/command',
type: "POST",
cache: false,
data:{command:command, arg1:arg1, arg2:arg2, arg3},
dataType: "json",
success: function (ret) {
if (ret.msg != null) notify(ret.msg, ret.ret);
if (ret.modal != null) m_modal(ret.modal, modal_title, false);
if (ret.json != null) m_modal(ret.json, modal_title, true);
if (callback != null) callback(ret);
}
});
}
///////////////////////////////////////
// 파일 선택 모달
///////////////////////////////////////
var select_local_file_modal_callback = null;
function selectLocalFile(title, init_path, func) {
_selectLocalFileModal(title, init_path, false, func);
}
function selectLocalFolder(title, init_path, func) {
_selectLocalFileModal(title, init_path, true, func);
}
function _selectLocalFileModal(title, init_path, only_dir, func) {
if (init_path == '' || init_path == null)
init_path = '/';
document.getElementById("select_local_file_modal_title").innerHTML = title;
document.getElementById("select_local_file_modal_path").value = init_path;
document.getElementById("select_local_file_modal_only_dir").value = only_dir;
select_local_file_modal_callback = func;
$("#select_local_file_modal").modal();
listdir(init_path, only_dir);
}
$("body").on('click', '#global_select_local_file_load_btn', function(e) {
e.preventDefault();
let current_path = $('#select_local_file_modal_path').val().trim();
only_dir = $('#select_local_file_modal_only_dir').val().trim();
listdir(current_path, only_dir);
});
$("body").on('click', '#select_local_file_modal_confirm_btn', function(e) {
e.preventDefault();
if (select_local_file_modal_callback != null)
select_local_file_modal_callback($('#select_local_file_modal_path').val().trim());
$("#select_local_file_modal").modal('toggle');
});
let listdir = (path = '/', only_dir = true) => {
$.ajax({
url: `/global/ajax/listdir`,
type: 'POST',
cache: false,
data: {
path: path,
only_dir : only_dir
},
dataType: 'json'
}).done((datas) => {
console.log(datas)
if (datas.length == 0) {
return false;
}
let new_obj = ``;
const path_spliter = (path.indexOf('/')>=0)?'/':'\\';
$('#select_local_file_modal_list_group').empty();
for (let dt of datas) {
tmp = dt.split('|');
new_obj += `<a href='#' class="list-group-item list-group-item-action item_path" data-value="${tmp[1]}">${tmp[0]}</a>`;
}
$('#select_local_file_modal_list_group').append(new_obj);
$('.item_path').off('click').click((evt) => {
let new_path = '';
/*
if ($(evt.currentTarget).text() === '..'){
let split_path = '';
split_path = path.split(path_spliter);
split_path.pop();
new_path = split_path.join(path_spliter);
if (new_path.length === 0){
new_path = path_spliter
}
} else {
//new_path = (path !== path_spliter) ? path + path_spliter + $(evt.currentTarget).text() : path + $(evt.currentTarget).text();
new_path = $(evt.currentTarget).data('value');
console.log(new_path)
console.log(evt)
}
*/
new_path = $(evt.currentTarget).data('value');
$('#select_local_file_modal_path').val(new_path);
listdir(new_path, only_dir);
});
}).fail((datas) => {
$.notify('<strong>경로 읽기 실패</strong><br/>${add_path}', {type: 'danger'});
});
return false;
}
// 파일 선택 모달 End
///////////////////////////////////////
+265
View File
@@ -0,0 +1,265 @@
// javascript에서 화면 생성
function text_color(text, color='red') {
return '<span style="color:'+color+'; font-weight:bold">' + text + '</span>';
}
function m_table(id, heads) {
str += '<table id="result_table" class="table table-sm tableRowHover " ><thead class="thead-dark"><tr> \
<th style="width:10%;text-align:center;">NO</th> \
<th style="width:15%;text-align:center;">물어본 숫자</th> \
<th style="width:10%;text-align:center;">스트라이크</th> \
<th style="width:10%;text-align:center;"></th> \
<th style="width:15%;text-align:center;">가능한 숫자 </th> \
<th style="width:40%;text-align:center;">Action</th> \
</tr></thead><tbody id="list">';
}
function m_row_start(padding='10', align='center') {
var str = '<div class="row" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_start_hover(padding='10', align='center') {
var str = '<div class="row my_hover" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_start_top(padding='10') {
return m_row_start(padding, 'top');
}
function m_row_start_color(padding='10', align='center', color='') {
var str = '<div class="row" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+'; background-color:'+color+'">';
return str;
}
function m_row_start_color2(padding='10', align='center') {
var str = '<div class="row bg-dark text-white" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_end() {
var str = '</div>';
return str;
}
//border
function m_col(w, h, align='left') {
var str = '<div class="col-sm-' + w + ' " style="text-align: '+align+'; word-break:break-all;">';
str += h
str += '</div>';
return str
}
function m_col2(w, h, align='left') {
var str = '<div class="col-sm-' + w + ' " style="padding:5px; margin:0px; text-align: '+align+'; word-break:break-all;">';
str += h
str += '</div>';
return str
}
function m_button_group(h) {
var str = '<div class="btn-group btn-group-sm flex-wrap mr-2" role="group">';
str += h
str += '</div>';
return str;
}
function m_button(id, text, data) {
var str = '<button id="'+id+'" name="'+id+'" class="btn btn-sm btn-outline-success" '
for ( var i in data) {
str += ' data-' + data[i].key + '="' + data[i].value+ '" '
}
str += '>' + text + '</button>';
return str;
}
function m_button2(id, text, data, outline_color) {
var str = '<button id="'+id+'" name="'+id+'" class="btn btn-sm btn-outline-'+outline_color+'" '
for ( var i in data) {
str += ' data-' + data[i].key + '="' + data[i].value+ '" '
}
str += '>' + text + '</button>';
return str;
}
function m_hr(margin='5') {
var str = '<hr style="width: 100%; margin:'+margin+'px;" />';
return str;
}
function m_hr_black() {
var str = '<hr style="width: 100%; color: black; height: 2px; background-color:black;" />';
return str;
}
// 체크박스는 자바로 하면 on/off 스크립트가 안먹힘.
function m_tab_head(name, active) {
if (active) {
var str = '<a class="nav-item nav-link active" id="id_'+name+'" data-toggle="tab" href="#'+name+'" role="tab">'+name+'</a>';
} else {
var str = '<a class="nav-item nav-link" id="id_'+name+'" data-toggle="tab" href="#'+name+'" role="tab">'+name+'</a>';
}
return str;
}
function m_tab_content(name, content, active) {
if (active) {
var str = '<div class="tab-pane fade show active" id="'+name+'" role="tabpanel" >';
} else {
var str = '<div class="tab-pane fade show" id="'+name+'" role="tabpanel" >';
}
str += content;
str += '</div>'
return str;
}
function m_progress(id, width, label) {
var str = '';
str += '<div class="progress" style="height: 25px;">'
str += '<div id="'+id+'" class="progress-bar" style="background-color:yellow;width:'+width+'%"></div>';
str += '<div id="'+id+'_label" class="justify-content-center d-flex w-100 position-absolute" style="margin-top:2px">'+label+'</div>';
str += '</div>'
return str;
}
function m_progress2(id, width, label) {
var str = '';
str += '<div class="progress" style="height: 25px;">'
str += '<div id="'+id+'" class="progress-bar" style="background-color:yellow;width:'+width+'%"></div>';
str += '<div id="'+id+'_label" class="justify-content-center d-flex w-100 position-absolute" style="margin:0px; margin-top:2px">'+label+'</div>';
str += '</div>'
return str;
}
function make_page_html(data) {
str = ' \
<div class="d-inline-block"></div> \
<div class="row mb-3"> \
<div class="col-sm-12"> \
<div class="btn-toolbar" style="justify-content: center;" role="toolbar" aria-label="Toolbar with button groups" > \
<div class="btn-group btn-group-sm mr-2" role="group" aria-label="First group">'
if (data.prev_page) {
str += '<button id="page" data-page="' + (data.start_page-1) + '" type="button" class="btn btn-secondary">&laquo;</button>'
}
for (var i = data.start_page ; i <= data.last_page ; i++) {
str += '<button id="page" data-page="' + i +'" type="button" class="btn btn-secondary" ';
if (i == data.current_page) {
str += 'disabled';
}
str += '>'+i+'</button>';
}
if (data.next_page) {
str += '<button id="page" data-page="' + (data.last_page+1) + '" type="button" class="btn btn-secondary">&raquo;</button>'
}
str += '</div> \
</div> \
</div> \
</div> \
'
document.getElementById("page1").innerHTML = str;
document.getElementById("page2").innerHTML = str;
}
function make_log(key, value, left=2, right=10) {
row = m_col(left, key, aligh='right');
row += m_col(right, value, aligh='left');
return row;
}
///////////////////////////////////////
// UI - 확장설정 - dropdown
///////////////////////////////////////
document.addEventListener("DOMContentLoaded", function(){
/////// Prevent closing from click inside dropdown
document.querySelectorAll('.dropdown-menu').forEach(function(element){
element.addEventListener('click', function (e) {
e.stopPropagation();
});
})
// make it as accordion for smaller screens
if (window.innerWidth < 992) {
// close all inner dropdowns when parent is closed
document.querySelectorAll('.navbar .dropdown').forEach(function(everydropdown){
everydropdown.addEventListener('hidden.bs.dropdown', function () {
// after dropdown is hidden, then find all submenus
this.querySelectorAll('.submenu').forEach(function(everysubmenu){
// hide every submenu as well
everysubmenu.style.display = 'none';
});
})
});
document.querySelectorAll('.dropdown-menu a').forEach(function(element){
element.addEventListener('click', function (e) {
let nextEl = this.nextElementSibling;
if(nextEl && nextEl.classList.contains('submenu')) {
// prevent opening link if link needs to open dropdown
e.preventDefault();
console.log(nextEl);
if(nextEl.style.display == 'block'){
nextEl.style.display = 'none';
} else {
nextEl.style.display = 'block';
}
}
});
})
}
// end if innerWidth
});
// DOMContentLoaded end
///////////////////////////////////////
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+675
View File
@@ -0,0 +1,675 @@
///////////////////////////////////////////////////////////////////////////////
// 공용 버튼
///////////////////////////////////////////////////////////////////////////////
// global_cache_confirm_btn => global_offcloud_cache_confirm_btn
$("body").on('click', '#global_offcloud_cache_confirm_btn', function(e){
e.preventDefault();
hash = $(this).data('hash');
$.ajax({
url: '/offcloud2/ajax/hash',
type: "POST",
cache: false,
data:{hash:hash},
dataType: "json",
success: function (data) {
if (data == 'true') {
$.notify('<strong>캐쉬 되어 있습니다.</strong>', {
type: 'success'
});
} else if (data == 'false') {
$.notify('<strong>캐쉬 되어 있지 않습니다.</strong>', {
type: 'warning'
});
} else if (data == 'fail') {
$.notify('<strong>캐쉬 확인 실패</strong>', {
type: 'warning'
});
}
}
});
//$(location).attr('href', '/offcloud/cache?magnet=' + hash)
});
//global_add_remote_btn -> global_offcloud_add_btn
$("body").on('click', '#global_offcloud_add_btn', function(e) {
e.preventDefault();
hash = $(this).data('hash');
$.ajax({
url: '/offcloud2/ajax/add_remote',
type: "POST",
cache: false,
data: {hash:hash},
dataType: "json",
success: function (data) {
m_modal(data)
}
});
});
$("body").on('click', '#global_downloader_add_btn', function(e){
e.preventDefault();
download_url = $(this).data('hash');
$.ajax({
url: '/downloader/ajax/add_download',
type: "POST",
cache: false,
data: {download_url:download_url},
dataType: "json",
success: function (data) {
show_result_add_download(data);
}
});
});
function command_modal_show(title) {
ClientHeight = window.innerHeight
document.getElementById("command_modal_title").innerHTML = title
$("#command_modal").height(ClientHeight-100);
$("#command_modal_textarea").height(ClientHeight-380);
$("#command_modal").modal();
}
// 토렌트 프로그램에 다운로드 추가할 결과를 보여주는
function show_result_add_download(data) {
try {
sub = ''
program = '토렌트'
if (data.default_torrent_program == '0') {
program = '트랜스미션에 토렌트'
sub = 'transmission'
} else if (data.default_torrent_program == '1') {
program = '다운로드스테이션에 토렌트'
sub = 'downloadstation'
} else if (data.default_torrent_program == '2') {
program = '큐빗토렌트다에 토렌트'
sub = 'qbittorrent'
} else if (data.default_torrent_program == '3') {
program = 'aria2에 토렌트'
sub = 'aria2'
} else if (data.default_torrent_program == '4') {
program = 'PikPak에 토렌트'
sub = 'pikpak'
}
}
catch {
}
if (data.ret == 'success') {
$.notify({message:'<strong>'+ program +'를 추가하였습니다.</strong><br>클릭시 다운로드 상태창으로 이동', url:'/downloader/'+sub+'/status',
target: '_self'}, {
type: 'success',
});
} else if (data.ret == 'success2') {
$.notify('<strong>일반 파일 다운로드를 시작하였습니다.</strong>', {
type: 'success'
});
} else if (data.ret == 'fail') {
$.notify('<strong>'+ program +' 추가에 실패하였습니다.</strong>', {
type: 'warning'
});
} else {
$.notify('<strong>'+ program +' 추가 에러<br>'+data.error+'</strong>', {
type: 'warning'
});
}
}
$("body").on('click', '#global_torrent_info_btn', function(e) {
e.preventDefault();
hash = $(this).data('hash');
$.ajax({
url: '/torrent_info/ajax/torrent_info',
type: "POST",
cache: false,
data: {hash:hash},
dataType: "json",
success: function (data) {
m_modal(data, "토렌트 정보")
}
});
});
function get_torrent_program_name(p) {
if (p == '0') return '트랜스미션'
else if (p == '1') return '다운로드스테이션'
else if (p == '2') return '큐빗토렌트'
else if (p == '3') return 'aria2'
else if (p == '4') return 'PikPak'
}
function global_relay_test(remote) {
$.ajax({
url: '/' + 'gd_share_client' + '/ajax/'+'base'+'/relay_test',
type: "POST",
cache: false,
data: {remote:remote},
dataType: "json",
success: function (data) {
if (data.ret == 'success') {
$.notify('<strong>릴레이 공유가 가능합니다.<strong>', {type: 'success'});
}else {
$.notify('<strong>설정이 잘못 되어 있습니다.</strong>', {type: 'warning'});
}
}
});
}
function shutdown_confirm() {
document.getElementById("confirm_title").innerHTML = "종료 확인";
document.getElementById("confirm_body").innerHTML = "종료 하시겠습니까?";
$('#confirm_button').attr('onclick', 'window.location.href = "/system/shutdown";');
$("#confirm_modal").modal();
}
$("#video_modal").on('hidden.bs.modal', function () {
document.getElementById("video_modal_video").pause();
//streaming_kill();
});
$("#video_modal").on('click', '#trailer_close_btn', function(e){
e.preventDefault();
document.getElementById("video_modal_video").pause();
//streaming_kill();
});
function streaming_kill(command, data={}) {
$.ajax({
url: '/' + 'ffmpeg' + '/ajax/streaming_kill',
type: "POST",
cache: false,
data:{},
dataType: "json",
success: function (data) {
}
});
}
///////////////////////////////////////////////////////////////////////////////
// Global.. JS 파일로 뺄것
///////////////////////////////////////////////////////////////////////////////
// 사용 on / off
$('#global_scheduler').change(function() {
var ret = $(this).prop('checked');
$.ajax({
url: '/'+package_name+'/ajax/scheduler',
type: "POST",
cache: false,
data: {scheduler : ret},
dataType: "json",
success: function (list) {
}
});
});
$('#global_scheduler_sub').change(function() {
var ret = $(this).prop('checked');
$.ajax({
url: '/'+package_name+'/ajax/scheduler',
type: "POST",
cache: false,
data: {scheduler : ret, sub:sub},
dataType: "json",
success: function (list) {
}
});
});
function global_setting_save_function() {
var formData = get_formdata('#setting');
$.ajax({
url: '/'+package_name+'/ajax/setting_save',
type: "POST",
cache: false,
data: formData,
dataType: "json",
success: function (ret) {
if (ret) {
$.notify('<strong>설정을 저장하였습니다.</strong>', {
type: 'success'
});
} else {
$.notify('<strong>설정 저장에 실패하였습니다.</strong>', {
type: 'warning'
});
}
}
});
}
$("#global_one_execute_btn").click(function(e) {
//$("body").on('click', '#one_execute_btn', function(e){
e.preventDefault();
$.ajax({
url: '/' + package_name + '/ajax/one_execute',
type: "POST",
cache: false,
data: {},
dataType: "json",
success: function (ret) {
if (ret=='scheduler' || ret=='thread') {
$.notify('<strong>작업을 시작하였습니다. ('+ret+')</strong>', {
type: 'success'
});
} else if (ret == 'is_running') {
$.notify('<strong>작업중입니다.</strong>', {
type: 'warning'
});
} else {
$.notify('<strong>작업 시작에 실패하였습니다.</strong>', {
type: 'warning'
});
}
}
});
});
$("#global_one_execute_sub_btn").click(function(e) {
//$("body").on('click', '#one_execute_btn', function(e){
e.preventDefault();
$.ajax({
url: '/' + package_name + '/ajax/one_execute',
type: "POST",
cache: false,
data: {sub:sub},
dataType: "json",
success: function (ret) {
if (ret=='scheduler' || ret=='thread') {
$.notify('<strong>작업을 시작하였습니다. ('+ret+')</strong>', {
type: 'success'
});
} else if (ret == 'is_running') {
$.notify('<strong>작업중입니다.</strong>', {
type: 'warning'
});
} else {
$.notify('<strong>작업 시작에 실패하였습니다.</strong>', {
type: 'warning'
});
}
}
});
});
$("body").on('click', '#global_immediately_execute_sub_btn', function(e){
e.preventDefault();
$.ajax({
url: '/' + package_name + '/ajax/immediately_execute',
type: "POST",
cache: false,
data: {sub:sub},
dataType: "json",
success: function (ret) {
if (ret.msg != null) notify(ret.msg, ret.ret);
}
});
});
$("body").on('click', '#global_reset_db_btn', function(e){
e.preventDefault();
document.getElementById("confirm_title").innerHTML = "DB 삭제";
document.getElementById("confirm_body").innerHTML = "전체 목록을 삭제 하시겠습니까?";
$('#confirm_button').attr('onclick', "global_db_delete();");
$("#confirm_modal").modal();
return;
});
function global_db_delete() {
$.ajax({
url: '/' + package_name + '/ajax/reset_db',
type: "POST",
cache: false,
data: {},
dataType: "json",
success: function (data) {
if (data) {
$.notify('<strong>삭제하였습니다.</strong>', {
type: 'success'
});
} else {
$.notify('<strong>삭제에 실패하였습니다.</strong>',{
type: 'warning'
});
}
}
});
}
$("body").on('click', '#global_reset_db_sub_btn', function(e){
e.preventDefault();
document.getElementById("confirm_title").innerHTML = "DB 삭제";
document.getElementById("confirm_body").innerHTML = "전체 목록을 삭제 하시겠습니까?";
$('#confirm_button').attr('onclick', "global_db_delete_sub();");
$("#confirm_modal").modal();
return;
});
function global_db_delete_sub() {
$.ajax({
url: '/' + package_name + '/ajax/reset_db',
type: "POST",
cache: false,
data: {sub:sub},
dataType: "json",
success: function (data) {
if (data) {
$.notify('<strong>삭제하였습니다.</strong>', {
type: 'success'
});
} else {
$.notify('<strong>삭제에 실패하였습니다.</strong>',{
type: 'warning'
});
}
}
});
}
function global_sub_request_search(page, move_top=true) {
var formData = get_formdata('#form_search')
formData += '&page=' + page;
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/web_list',
type: "POST",
cache: false,
data: formData,
dataType: "json",
success: function (data) {
current_data = data;
if (move_top)
window.scrollTo(0,0);
make_list(data.list)
make_page_html(data.paging)
}
});
}
$("body").on('click', '#global_json_btn', function(e){
e.preventDefault();
var id = $(this).data('id');
for (i in current_data.list) {
if (current_data.list[i].id == id) {
m_modal(current_data.list[i])
}
}
});
$("body").on('click', '#global_reset_btn', function(e){
e.preventDefault();
document.getElementById("order").value = 'desc';
document.getElementById("option").value = 'all';
document.getElementById("search_word").value = '';
global_sub_request_search('1')
});
$("body").on('click', '#global_remove_btn', function(e) {
e.preventDefault();
id = $(this).data('id');
$.ajax({
url: '/'+package_name+'/ajax/'+sub+ '/db_remove',
type: "POST",
cache: false,
data: {id:id},
dataType: "json",
success: function (data) {
if (data) {
$.notify('<strong>삭제되었습니다.</strong>', {
type: 'success'
});
global_sub_request_search(current_data.paging.current_page, false)
} else {
$.notify('<strong>삭제 실패</strong>', {
type: 'warning'
});
}
}
});
});
//#######################################################
//플러그인 - 모듈 - 서브 구조하에서 서브 관련 함수
function global_send_command_sub(command, arg1, arg2, arg3, modal_title, callback) {
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/' + sub2 + '/command',
type: "POST",
cache: false,
data:{command:command, arg1:arg1, arg2:arg2, arg3},
dataType: "json",
success: function (ret) {
console.log(ret);
if (ret.msg != null) notify(ret.msg, ret.ret);
if (ret.modal != null) m_modal(ret.modal, modal_title, false);
if (ret.json != null) m_modal(ret.json, modal_title, true);
if (callback != null) callback(ret);
}
});
}
$("body").on('click', '#global_one_execute_sublogic_btn', function(e){
e.preventDefault();
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/' + sub2 + '/one_execute',
type: "POST",
cache: false,
data: {},
dataType: "json",
success: function (ret) {
if (ret=='scheduler' || ret=='thread') {
$.notify('<strong>작업을 시작하였습니다. ('+ret+')</strong>', {
type: 'success'
});
} else if (ret == 'is_running') {
$.notify('<strong>작업중입니다.</strong>', {
type: 'warning'
});
} else {
$.notify('<strong>작업 시작에 실패하였습니다.</strong>', {
type: 'warning'
});
}
}
});
});
$("body").on('click', '#global_immediately_execute_sublogic_btn', function(e){
e.preventDefault();
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/' + sub2 + '/immediately_execute',
type: "POST",
cache: false,
data: {},
dataType: "json",
success: function (ret) {
if (ret.msg != null) notify(ret.msg, ret.ret);
}
});
});
$('#global_scheduler_sublogic').change(function() {
var ret = $(this).prop('checked');
$.ajax({
url: '/'+package_name+'/ajax/' + sub + '/' + sub2 + '/scheduler',
type: "POST",
cache: false,
data: {scheduler : ret},
dataType: "json",
success: function (list) {
}
});
});
// 이동한 함수
function global_send_command(command, data={}) {
data['command'] = command;
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/command',
type: "POST",
cache: false,
data:data,
dataType: "json",
success: function (data) {
notify(data['msg'], data['ret']);
}
});
}
function global_send_command2(command, arg1, arg2, arg3, modal_title, callback) {
$.ajax({
url: '/' + package_name + '/ajax/' + sub + '/command',
type: "POST",
cache: false,
data:{command:command, arg1:arg1, arg2:arg2, arg3},
dataType: "json",
success: function (ret) {
if (ret.msg != null) notify(ret.msg, ret.ret);
if (ret.modal != null) m_modal(ret.modal, modal_title, false);
if (ret.json != null) m_modal(ret.json, modal_title, true);
if (callback != null) callback(ret);
}
});
}
+227
View File
@@ -0,0 +1,227 @@
function m_row_start(padding='10', align='center') {
var str = '<div class="row" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_start_hover(padding='10', align='center') {
var str = '<div class="row my_hover" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_start_top(padding='10') {
return m_row_start(padding, 'top');
}
function m_row_start_color(padding='10', align='center', color='') {
var str = '<div class="row" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+'; background-color:'+color+'">';
return str;
}
function m_row_start_color2(padding='10', align='center') {
var str = '<div class="row bg-dark text-white" style="padding-top: '+padding+'px; padding-bottom:'+padding+'px; align-items:'+align+';">';
return str;
}
function m_row_end() {
var str = '</div>';
return str;
}
//border
function m_col(w, h, align='left') {
var str = '<div class="col-sm-' + w + ' " style="text-align: '+align+'; word-break:break-all;">';
str += h
str += '</div>';
return str
}
function m_col2(w, h, align='left') {
var str = '<div class="col-sm-' + w + ' " style="padding:5px; margin:0px; text-align: '+align+'; word-break:break-all;">';
str += h
str += '</div>';
return str
}
function m_button_group(h) {
var str = '<div class="btn-group btn-group-sm flex-wrap mr-2" role="group">';
str += h
str += '</div>';
return str;
}
function m_button(id, text, data) {
var str = '<button id="'+id+'" name="'+id+'" class="btn btn-sm btn-outline-success" '
for ( var i in data) {
str += ' data-' + data[i].key + '="' + data[i].value+ '" '
}
str += '>' + text + '</button>';
return str;
}
function m_button2(id, text, data, outline_color) {
var str = '<button id="'+id+'" name="'+id+'" class="btn btn-sm btn-outline-'+outline_color+'" '
for ( var i in data) {
str += ' data-' + data[i].key + '="' + data[i].value+ '" '
}
str += '>' + text + '</button>';
return str;
}
function m_hr(margin='5') {
var str = '<hr style="width: 100%; margin:'+margin+'px;" />';
return str;
}
function m_hr_black() {
var str = '<hr style="width: 100%; color: black; height: 2px; background-color:black;" />';
return str;
}
// 체크박스는 자바로 하면 on/off 스크립트가 안먹힘.
function m_modal(data='EMPTY', title='JSON', json=true) {
document.getElementById("modal_title").innerHTML = title;
if (json) {
data = JSON.stringify(data, null, 2);
}
document.getElementById("modal_body").innerHTML = "<pre>"+ data + "</pre>";;
$("#large_modal").modal();
}
function m_tab_head(name, active) {
if (active) {
var str = '<a class="nav-item nav-link active" id="id_'+name+'" data-toggle="tab" href="#'+name+'" role="tab">'+name+'</a>';
} else {
var str = '<a class="nav-item nav-link" id="id_'+name+'" data-toggle="tab" href="#'+name+'" role="tab">'+name+'</a>';
}
return str;
}
function m_tab_content(name, content, active) {
if (active) {
var str = '<div class="tab-pane fade show active" id="'+name+'" role="tabpanel" >';
} else {
var str = '<div class="tab-pane fade show" id="'+name+'" role="tabpanel" >';
}
str += content;
str += '</div>'
return str;
}
function m_progress(id, width, label) {
var str = '';
str += '<div class="progress" style="height: 25px;">'
str += '<div id="'+id+'" class="progress-bar" style="background-color:yellow;width:'+width+'%"></div>';
str += '<div id="'+id+'_label" class="justify-content-center d-flex w-100 position-absolute" style="margin-top:2px">'+label+'</div>';
str += '</div>'
return str;
}
function m_progress2(id, width, label) {
var str = '';
str += '<div class="progress" style="height: 25px;">'
str += '<div id="'+id+'" class="progress-bar" style="background-color:yellow;width:'+width+'%"></div>';
str += '<div id="'+id+'_label" class="justify-content-center d-flex w-100 position-absolute" style="margin:0px; margin-top:2px">'+label+'</div>';
str += '</div>'
return str;
}
function make_page_html(data) {
str = ' \
<div class="d-inline-block"></div> \
<div class="row mb-3"> \
<div class="col-sm-12"> \
<div class="btn-toolbar" style="justify-content: center;" role="toolbar" aria-label="Toolbar with button groups" > \
<div class="btn-group btn-group-sm mr-2" role="group" aria-label="First group">'
if (data.prev_page) {
str += '<button id="page" data-page="' + (data.start_page-1) + '" type="button" class="btn btn-secondary">&laquo;</button>'
}
for (var i = data.start_page ; i <= data.last_page ; i++) {
str += '<button id="page" data-page="' + i +'" type="button" class="btn btn-secondary" ';
if (i == data.current_page) {
str += 'disabled';
}
str += '>'+i+'</button>';
}
if (data.next_page) {
str += '<button id="page" data-page="' + (data.last_page+1) + '" type="button" class="btn btn-secondary">&raquo;</button>'
}
str += '</div> \
</div> \
</div> \
</div> \
'
document.getElementById("page1").innerHTML = str;
document.getElementById("page2").innerHTML = str;
}
function use_collapse(div, reverse=false) {
var ret = $('#' + div).prop('checked');
if (reverse) {
if (ret) {
$('#' + div + '_div').collapse('hide')
} else {
$('#' + div + '_div').collapse('show')
}
} else {
if (ret) {
$('#' + div + '_div').collapse('show')
} else {
$('#' + div + '_div').collapse('hide')
}
}
}
document.addEventListener("DOMContentLoaded", function(){
/////// Prevent closing from click inside dropdown
document.querySelectorAll('.dropdown-menu').forEach(function(element){
element.addEventListener('click', function (e) {
e.stopPropagation();
});
})
// make it as accordion for smaller screens
if (window.innerWidth < 992) {
// close all inner dropdowns when parent is closed
document.querySelectorAll('.navbar .dropdown').forEach(function(everydropdown){
everydropdown.addEventListener('hidden.bs.dropdown', function () {
// after dropdown is hidden, then find all submenus
this.querySelectorAll('.submenu').forEach(function(everysubmenu){
// hide every submenu as well
everysubmenu.style.display = 'none';
});
})
});
document.querySelectorAll('.dropdown-menu a').forEach(function(element){
element.addEventListener('click', function (e) {
let nextEl = this.nextElementSibling;
if(nextEl && nextEl.classList.contains('submenu')) {
// prevent opening link if link needs to open dropdown
e.preventDefault();
console.log(nextEl);
if(nextEl.style.display == 'block'){
nextEl.style.display = 'none';
} else {
nextEl.style.display = 'block';
}
}
});
})
}
// end if innerWidth
});
// DOMContentLoaded end
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+68
View File
@@ -0,0 +1,68 @@
{% from "macro_menu.html" import menu, menu_module, menu_page with context %}
{% from "macro_include.html" import apply_theme, modals with context %}
{% import "macro.html" as macros %}
<!DOCTYPE html>
<html>
<head lang="ko">
{% block head %}
<title>{{get_web_title()}}</title>
{% endblock %}
<meta name="google" value="notranslate">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">-->
<!--
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
-->
<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}">
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
{% set theme = get_theme() %}
<link href="{{ url_for('static', filename='css/theme/'+theme+'_bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/animate.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/custom.css') }}" rel="stylesheet">
<link href="https://unpkg.com/balloon-css/balloon.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!--<script src="{{ url_for('static', filename='js/popper.min.js') }}"></script>-->
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap-notify.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/sjva_ui14.js') }}"></script>
<script src="{{ url_for('static', filename='js/ff_common1.js') }}"></script>
<script src="{{ url_for('static', filename='js/ff_ui1.js') }}"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js"></script>
<!-- 토글 -->
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.4.0/css/bootstrap4-toggle.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.4.0/js/bootstrap4-toggle.min.js"></script>
<!-- end 토글 -->
</head>
<body class="body ">
{{ menu() }}
{{ menu_module() }}
<!--</nav>-->
<main id="main_container" role="main" class="container">
<div class="d-inline-block"></div>
{{ menu_page() }}
<div>
{% block content %}{% endblock %}
</div>
</main>
<div class="loading" id="loading">
<img src="/static/img/loading.gif" />
</div>
{{ modals() }}
</body>
</html>
<!-- 글로벌 버튼이 모두 나오고 처리-->
<script src="{{ url_for('static', filename='js/sjva_global1.js') }}"></script>
<script src="{{ url_for('static', filename='js/ff_global1.js') }}"></script>
+81
View File
@@ -0,0 +1,81 @@
{% extends "base.html" %}
{% block content %}
<div>
<nav>
{{ macros.m_tab_head_start() }}
{{ macros.m_tab_head('이전', true) }}
{{ macros.m_tab_head('실시간', false) }}
{{ macros.m_tab_head_end() }}
</nav>
<div class="tab-content" id="nav-tabContent">
{{ macros.m_tab_content_start('이전', true) }}
<div>
<textarea id="log" class="col-md-12" rows="30" charswidth="23" disabled style="background-color:#ffffff;visibility:hidden"></textarea>
</div>
{{ macros.m_tab_content_end() }}
{{ macros.m_tab_content_start('실시간', false) }}
<div>
<textarea id="add" class="col-md-12" rows="30" charswidth="23" disabled style="background-color:#ffffff;visibility:visible"></textarea>
</div>
<div class="form-inline">
<label class="form-check-label" for="auto_scroll">자동 스크롤</label>
<input id="auto_scroll" name="auto_scroll" class="form-control form-control-sm" type="checkbox" data-toggle="toggle" checked>
<span class='text-left' style="padding-left:25px; padding-top:0px">
<button id="clear" class="btn btn-sm btn-outline-success">리셋</button>
</span>
</div>
{{ macros.m_tab_content_end() }}
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
setWide();
$('#loading').show();
ResizeTextArea()
})
function ResizeTextArea() {
ClientHeight = window.innerHeight
$("#log").height(ClientHeight-240);
$("#add").height(ClientHeight-260);
}
$(window).resize(function() {
ResizeTextArea();
});
var protocol = window.location.protocol;
var socket = io.connect(protocol + "//" + document.domain + ":" + location.port + "/log");
socket.emit("start", {'package':'{{package}}'} );
socket.on('on_start', function(data){
document.getElementById("log").innerHTML += data.data;
document.getElementById("log").scrollTop = document.getElementById("log").scrollHeight;
document.getElementById("log").style.visibility = 'visible';
$('#loading').hide();
});
socket.on('add', function(data){
if (data.package == "{{package}}") {
var chk = $('#auto_scroll').is(":checked");
document.getElementById("add").innerHTML += data.data;
if (chk) document.getElementById("add").scrollTop = document.getElementById("add").scrollHeight;
}
});
$("#clear").click(function(e) {
e.preventDefault();
document.getElementById("add").innerHTML = '';
});
$("#auto_scroll").click(function(){
var chk = $(this).is(":checked");//.attr('checked');
});
</script>
{% endblock %}
File diff suppressed because it is too large Load Diff
+125
View File
@@ -0,0 +1,125 @@
{% import "macro.html" as macros %}
{% macro modals() %}
<!-- Modal -->
<div class="modal fade" id="large_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="modal_title"></h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body" id="modal_body" style="word-break:break-all;">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">닫기</button>
<!--<button type="button" class="btn btn-primary">Save changes</button>-->
</div>
</div>
</div>
</div>
<div class="modal fade" id="normal_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="normal_modal_title"></h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body" id="normal_modal_body" style="word-break:break-all;">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">닫기</button>
<!--<button type="button" class="btn btn-primary">Save changes</button>-->
</div>
</div>
</div>
</div>
<div id="confirm_modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="confirm_title" class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div id="confirm_body" class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button id="confirm_button" type="button" class="btn btn-primary" data-dismiss="modal">확인</button>
<button id="confirm_cancel_button" type="button" class="btn btn-secondary" data-dismiss="modal">취소</button>
</div>
</div>
</div>
</div>
<!-- 경로 선택 모달 -->
<div class="modal fade" id="select_local_file_modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="select_local_file_modal_title">경로 선택</h4>
</div>
<div class="modal-body" id="modal_body" style="word-break: break-all;">
<div class="row" style="padding-top: 10px; padding-bottom:10px; align-items: center;">
<div class="col-sm-1 set-left"><strong>Path</strong></div>
<div class="col-sm-11">
<div class="input-group col-sm-12">
<input id="select_local_file_modal_path" name="select_local_file_modal_path" type="text" class="form-control form-control-sm" value="/">
<div class="btn-group btn-group-sm flex-wrap mr-2" role="group" style="padding-left:5px; padding-top:0px">
<button id="global_select_local_file_load_btn" class="btn btn-sm btn-outline-success">Load</button>
<button type="button" id='select_local_file_modal_confirm_btn' class="btn btn-success" data-dismiss="modal">선택
</div>
</div>
<div style="padding-left:20px; padding-top:5px;"><em>입력한 경로를 불러옵니다.</em></div>
</div>
</div>
<input type="hidden" id="select_local_file_modal_only_dir" value="true" />
<input type="hidden" id="select_local_file_modal_callback" value="" />
<div class="list-group" id="select_local_file_modal_list_group">
</div>
</div>
<div class="modal-footer">
<button type="button" id='select_local_file_modal_confirm_btn' class="btn btn-success" data-dismiss="modal">선택
</button>
<button type="button" id='select_local_file_modal_cancel_btn' class="btn btn-default" data-dismiss="modal">닫기
</button>
</div>
</div>
</div>
</div>
<!-- 예고편 Player Modal: START -->
<div class="modal fade" id="video_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="video_modal_title" style="word-break:break-all;">예고편 재생: </h4>
</div>
<div class="modal-body" id="modal_body" style="word-break:break-all;">
<span id="video_player_body"></span>
</div>
<!--</div>-->
<div class="modal-footer">
<button type="button" id='video_close_btn' class="btn btn-default" data-dismiss="modal">닫기</button>
</div>
</div>
</div>
</div>
<!-- 예고편 Player Modal: END -->
<!--command modal-->
{{ macros.m_modal_start('command_modal', '', 'modal-lg') }}
<div>
<textarea id="command_modal_textarea" class="col-md-12" rows="30" disabled style="visibility:visible"></textarea>
</div>
{{ macros.m_modal_end() }}
<!--command modal end-->
<!-- Modal end -->
{% endmacro %}
+144
View File
@@ -0,0 +1,144 @@
{% macro menu() %}
<div id="menu_div">
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark flex-md-nowrap shadow" role="navigation">
<a class="navbar-brand" href="/">{{get_web_title()}}</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample01" aria-controls="navbarsExample01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
{% for i in range(1) %}
<div class="collapse navbar-collapse w-100" id="navbarsExample01">
{% if i == 0 %}
<ul class="nav navbar-nav mr-auto">
{% else %}
<ul class="nav navbar-nav ml-auto">
{% endif %}
{% set menu = request.full_path | get_menu %}
{% set menu_map = get_menu_map() %}
{% for category in menu_map %}
{% if 'uri' in category and category['uri'].startswith('http') %}
<li class="nav-item"> <a class="nav-link" href="{{ category['uri']}}" target="_blank">{{category['name']}}</a></li>
{% else %}
<!--{{ category }}-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{category['name']}}</a>
<ul class="dropdown-menu">
{% for category_child in category['list'] %}
{% if category_child['uri'] == 'setting' %}
<li><a class="dropdown-item" href="#" style="font-size: .850rem; font-weight:bold">{{category_child['name']}}</a>
<ul class="submenu dropdown-menu">
{% for item in category_child['list'] %}
<li><a class="dropdown-item" href="/{{item['uri']}}" style="font-size: .850rem; font-weight:bold">{{item['name']}}</a></li>
{% endfor %}
</ul>
</li>
{% elif category_child['uri'] == '-' %}
<div class="dropdown-divider"></div>
{% elif 'uri' in category_child and category_child['uri'].startswith('http') %}
{% if 'target' not in category_child or category_child['target'] == '_blank' %}
<a class="dropdown-item" href="{{ category_child['uri'] }}" target="_blank" style="font-size: .850rem; font-weight:bold">{{ category_child['name'] }}</a>
{% else %}
<a class="dropdown-item" href="{{ category_child['uri'] }}" style="font-size: .850rem; font-weight:bold">{{ category_child['name'] }}</a>
{% endif %}
{% else %}
{% if category_child['uri'] == menu[0] %}
<a class="dropdown-item active" href="/{{ category_child['uri'] }}" style="font-size: .850rem; font-weight:bold">{{ category_child['name'] }}</a>
{% else %}
<a class="dropdown-item" href="/{{ category_child['uri'] }}" style="font-size: .850rem; font-weight:bold">{{ category_child['name'] }}</a>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</li>
{% endif %}
{% endfor %}<!---->
</ul>
</div>
{% endfor %}
</nav>
</div>
{% endmacro %}
{% macro menu_module() %}
<div id='menu_module_div'>
{% set current_menu = request.full_path | get_menu %}
<!--{{ current_menu }}-->
{% set menu_map = get_menu_map() %}
{% for category in menu_map %}
{% for category_child in category['list'] %}
{% if current_menu[0] == category_child['uri'] %}
<ul class="nav nav-pills bg-light shadow text-dark" >
<li class="nav-item"><span class="nav-link">{{category['name']}} ➤ {{category_child['name']}}</span></li>
{% for module in category_child['list'] %}
<!--{{ module }}-->
{% if current_menu[0] == 'setting' %}
{% if current_menu[1] == module['uri'] or (current_menu[2] is not none and current_menu[1] + "/" + current_menu[2] == module['uri']) %}
<li class="nav-item"><a class="nav-link active" href="/{{module['uri']}}">{{module['name']}}</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="/{{module['uri']}}">{{module['name']}}</a></li>
{% endif %}
{% else %}
{% if current_menu[1] == module['uri'] or (current_menu[2] is not none and current_menu[1] + "/" + current_menu[2] == module['uri']) %}
<li class="nav-item"><a class="nav-link active" href="/{{current_menu[0]}}/{{module['uri']}}">{{module['name']}}</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="/{{current_menu[0]}}/{{module['uri']}}">{{module['name']}}</a></li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endmacro %}
{% macro menu_page() %}
<div id='menu_page_div'>
{% set current_menu = request.full_path | get_menu %}
{% set menu_map = get_menu_map() %}
{% for category in menu_map %}
{% for plugin in category['list'] %}
{% if current_menu[0] == plugin['uri'] and 'list' in plugin %}
{% for module in plugin['list'] %}
{% if module['uri'] == current_menu[1] and 'list' in module%}
<!--{{ module }}-->
<ul class="nav nav-pills bg-light shadow text-dark">
{% for page in module['list'] %}
{% if current_menu[2] == page['uri'] %}
<li class="nav-item"><a class="nav-link active" href="/{{ current_menu[0] }}/{{ current_menu[1] }}/{{ page['uri'] }}">{{page['name']}}</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="/{{ current_menu[0] }}/{{ current_menu[1] }}/{{ page['uri'] }}">{{page['name']}}</a></li>
{% endif %}
{% endfor %}
</ul>
<div class="d-inline-block"></div>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endmacro %}
+30
View File
@@ -0,0 +1,30 @@
{% extends "base.html" %}
{% block content %}
{% filter markdown %}
{{ data }}
{% endfilter %}
<style type="text/css">
img{
display: block;
max-width: 100%;
margin-right: auto;
}
</style>
<div id="md_div" data-url="{{ arg }}"></div>
<div id="content_div" data-url="{{ arg }}"></div>
<script type="text/javascript">
$(document).ready(function(){
//$('#main_container').attr('class', 'container-fluid');
});
</script>
{% endblock %}
+6
View File
@@ -0,0 +1,6 @@
{% extends "base.html" %}
{% block content %}
<div class="">
<h3>{{title}}</h3>
</div>
{% endblock %}
+65
View File
@@ -0,0 +1,65 @@
<title>{{data['play_title']}}</title>
<script src="https://vjs.zencdn.net/7.11.4/video.min.js"></script>
<link href="https://vjs.zencdn.net/7.11.4/video-js.css" rel="stylesheet" />
<body bgcolor='black'>
<video id=player width=960 height=540 class="video-js vjs-default-skin vjs-16-9" autoplay controls>
<source
src="{{data['play_source_src']}}"
type="{{data['play_source_type']}}" />
</video>
</body>
<script>
var subtitle_src = "{{data['play_subtitle_src']}}";
let options = {
html5: {
nativeTextTracks: false
},
playbackRates: [.5, .75, 1, 1.5, 2],
controls: true,
preload: "auto",
controlBar: {
playToggle: false,
pictureInPictureToggle: false,
remainingTimeDisplay: true,
qualitySelector: true,
}
};
let player = videojs('player', options);
player.ready(function(){
// set subtitle track
console.log(subtitle_src);
if (subtitle_src != "") {
var suburl = subtitle_src.replace(/&amp;/g, '&');
console.log(suburl);
let captionOption = {
kind: 'captions',
srclang: 'ko',
label: 'Korean',
src: suburl,
mode: 'showing'
};
player.addRemoteTextTrack(captionOption);
var settings = this.textTrackSettings;
settings.setValues({
"backgroundColor": "#000",
"backgroundOpacity": "0",
"edgeStyle": "uniform",
});
settings.updateDisplay();
}
else {
var tracks = player.textTracks();
console.log(tracks.length);
for (var i = 0; i < tracks.length; i++) {
var track = tracks[i];
console.log(track);
}
}
});
player.play();
</script>
+138
View File
@@ -0,0 +1,138 @@

# -*- coding: utf-8 -*-
#########################################################
# python
import os
import json
import traceback
import platform
import subprocess
# third-party
from sqlalchemy.ext.declarative import DeclarativeMeta
# sjva 공용
from framework import app, logger
#########################################################
class Util(object):
@staticmethod
def db_list_to_dict(db_list):
"""
세팅DB에서 사용, (key, value) dict로 변환
"""
ret = {}
for item in db_list:
ret[item.key] = item.value
return ret
@staticmethod
def db_to_dict(db_list):
ret = []
for item in db_list:
ret.append(item.as_dict())
return ret
@staticmethod
def get_paging_info(count, current_page, page_size):
try:
paging = {}
paging['prev_page'] = True
paging['next_page'] = True
if current_page <= 10:
paging['prev_page'] = False
paging['total_page'] = int(count / page_size) + 1
if count % page_size == 0:
paging['total_page'] -= 1
paging['start_page'] = int((current_page-1)/10) * 10 + 1
paging['last_page'] = paging['total_page'] if paging['start_page'] + 9 > paging['total_page'] else paging['start_page'] + 9
if paging['last_page'] == paging['total_page']:
paging['next_page'] = False
paging['current_page'] = current_page
paging['count'] = count
logger.debug('paging : c:%s %s %s %s %s %s', count, paging['total_page'], paging['prev_page'], paging['next_page'] , paging['start_page'], paging['last_page'])
return paging
except Exception as exception:
logger.debug('Exception:%s', exception)
logger.debug(traceback.format_exc())
@staticmethod
def save_from_dict_to_json(d, filename):
from tool_base import ToolUtil
ToolUtil.save_dict(d, filename)
@staticmethod
def change_text_for_use_filename(text):
from tool_base import ToolBaseFile
return ToolBaseFile.text_for_filename(text)
# 토렌트 인포에서 최대 크기 파일과 폴더명을 리턴한다
@staticmethod
def get_max_size_fileinfo(torrent_info):
try:
ret = {}
max_size = -1
max_filename = None
for t in torrent_info['files']:
if t['size'] > max_size:
max_size = t['size']
max_filename = str(t['path'])
t = max_filename.split('/')
ret['filename'] = t[-1]
if len(t) == 1:
ret['dirname'] = ''
elif len(t) == 2:
ret['dirname'] = t[0]
else:
ret['dirname'] = max_filename.replace('/%s' % ret['filename'], '')
ret['max_size'] = max_size
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
# 압축할 폴더 경로를 인자로 받음. 폴더명.zip 생성
@staticmethod
def makezip(zip_path, zip_extension='zip'):
import zipfile
try:
if os.path.isdir(zip_path):
zipfilename = os.path.join(os.path.dirname(zip_path), '%s.%s' % (os.path.basename(zip_path), zip_extension))
fantasy_zip = zipfile.ZipFile(zipfilename, 'w')
for f in os.listdir(zip_path):
#if f.endswith('.jpg') or f.endswith('.png'):
src = os.path.join(zip_path, f)
fantasy_zip.write(src, os.path.basename(src), compress_type = zipfile.ZIP_DEFLATED)
fantasy_zip.close()
import shutil
shutil.rmtree(zip_path)
return True
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return False
@staticmethod
def make_apikey(url):
from framework import SystemModelSetting
url = url.format(ddns=SystemModelSetting.get('ddns'))
if SystemModelSetting.get_bool('auth_use_apikey'):
if url.find('?') == -1:
url += '?'
else:
url += '&'
url += 'apikey=%s' % SystemModelSetting.get('auth_apikey')
return url
+14
View File
@@ -0,0 +1,14 @@
from framework import logger
from .model_setting import get_model_setting
from .logic import Logic
from .route import default_route, default_route_socketio_module, default_route_socketio_page, default_route_single_module
from .logic_module_base import PluginModuleBase, PluginPageBase
from .ffmpeg_queue import FfmpegQueueEntity, FfmpegQueue
from .model_base import ModelBase
from .create_plugin import create_plugin_instance
import os, sys, traceback, re, threading, time
from datetime import datetime, timedelta
from flask import Blueprint, render_template, jsonify, redirect, request
from framework import *
+7
View File
@@ -0,0 +1,7 @@
"""
import os, sys, traceback, re, threading, time
from datetime import datetime, timedelta
from flask import Blueprint, render_template, jsonify, redirect, request
from framework import frame, F, login_required, check_api, Job, SystemModelSetting
from plugin import PluginModuleBase, get_model_setting, Logic, default_route, create_plugin_instance
"""
+87
View File
@@ -0,0 +1,87 @@
import os, traceback
from flask import Blueprint
from framework import F
from support.base.yaml import SupportYaml
from . import get_model_setting, Logic, default_route, default_route_single_module
class PluginBase(object):
package_name = None
logger = None
blueprint = None
menu = None
plugin_info = None
ModelSetting = None
logic = None
module_list = None
home_module = None
def __init__(self, setting):
try:
is_system = ('system' == os.path.basename(os.path.dirname(setting['filepath'])))
self.status = ""
self.setting = setting
info_filepath = os.path.join(os.path.dirname(setting['filepath']), 'info.yaml')
if os.path.exists(info_filepath) == False and is_system == False:
return
if is_system:
self.package_name = 'system'
else:
self.plugin_info = SupportYaml.read_yaml(info_filepath)
self.package_name = self.plugin_info['package_name']
self.logger = F.get_logger(self.package_name)
self.blueprint = Blueprint(self.package_name, self.package_name, url_prefix=f'/{self.package_name}', template_folder=os.path.join(os.path.dirname(setting['filepath']), 'templates'), static_folder=os.path.join(os.path.dirname(setting['filepath']), 'static'))
self.menu = setting['menu']
self.setting_menu = setting['setting_menu']
self.ModelSetting = None
if setting.get('use_db', True):
db_path = os.path.join(F.config['path_data'], 'db', f'{self.package_name}.db')
F.app.config['SQLALCHEMY_BINDS'][self.package_name] = f"sqlite:///{db_path}"
if setting.get('use_default_setting', True):
self.ModelSetting = get_model_setting(self.package_name, self.logger)
self.module_list = []
self.home_module = setting.get('home_module')
self.status = "init_success"
self.config = {}
except Exception as e:
self.logger.error(f'Exception:{str(e)}')
self.logger.error(traceback.format_exc())
self.status = 'init_fail'
def set_module_list(self, mod_list):
try:
for mod in mod_list:
mod_ins = mod(self)
self.module_list.append(mod_ins)
except Exception as e:
F.logger.error(f'Exception:{str(e)}')
F.logger.error(traceback.format_exc())
self.logic = Logic(self)
route_mode = self.setting.get('default_route', 'normal')
if route_mode == 'normal':
default_route(self)
elif route_mode == 'single':
default_route_single_module(self)
def plugin_load(self):
self.logic.plugin_load()
def plugin_unload(self):
self.logic.plugin_unload()
def get_first_manual_path(self):
for __ in self.menu['list']:
if __['uri'] == 'manual' and len(__['list']) > 0:
return __['list'][0]['uri']
def create_plugin_instance(config):
ins = PluginBase(config)
return ins
+300
View File
@@ -0,0 +1,300 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os, sys, traceback
import threading, time
from datetime import datetime
import abc
# third-party
# sjva 공용
#########################################################
class FfmpegQueueEntity(abc.ABCMeta('ABC', (object,), {'__slots__': ()})):
def __init__(self, P, module_logic, info):
self.P = P
self.module_logic = module_logic
self.entity_id = -1 #FfmpegQueueEntity.static_index
self.info = info
self.url = None
self.ffmpeg_status = -1
self.ffmpeg_status_kor = u'대기중'
self.ffmpeg_percent = 0
self.ffmpeg_arg = None
self.cancel = False
self.created_time = datetime.now().strftime('%m-%d %H:%M:%S')
self.savepath = None
self.filename = None
self.filepath = None
self.quality = None
self.headers = None
#FfmpegQueueEntity.static_index += 1
#FfmpegQueueEntity.entity_list.append(self)
def get_video_url(self):
return self.url
def get_video_filepath(self):
return self.filepath
@abc.abstractmethod
def refresh_status(self):
pass
@abc.abstractmethod
def info_dict(self, tmp):
pass
def donwload_completed(self):
pass
def as_dict(self):
tmp = {}
tmp['entity_id'] = self.entity_id
tmp['url'] = self.url
tmp['ffmpeg_status'] = self.ffmpeg_status
tmp['ffmpeg_status_kor'] = self.ffmpeg_status_kor
tmp['ffmpeg_percent'] = self.ffmpeg_percent
tmp['ffmpeg_arg'] = self.ffmpeg_arg
tmp['cancel'] = self.cancel
tmp['created_time'] = self.created_time#.strftime('%m-%d %H:%M:%S')
tmp['savepath'] = self.savepath
tmp['filename'] = self.filename
tmp['filepath'] = self.filepath
tmp['quality'] = self.quality
#tmp['current_speed'] = self.ffmpeg_arg['current_speed'] if self.ffmpeg_arg is not None else ''
tmp = self.info_dict(tmp)
return tmp
class FfmpegQueue(object):
def __init__(self, P, max_ffmpeg_count):
self.P = P
self.static_index = 1
self.entity_list = []
self.current_ffmpeg_count = 0
self.download_queue = None
self.download_thread = None
self.max_ffmpeg_count = max_ffmpeg_count
if self.max_ffmpeg_count is None or self.max_ffmpeg_count == '':
self.max_ffmpeg_count = 1
def queue_start(self):
try:
if self.download_queue is None:
self.download_queue = queue.Queue()
if self.download_thread is None:
self.download_thread = threading.Thread(target=self.download_thread_function, args=())
self.download_thread.daemon = True
self.download_thread.start()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def download_thread_function(self):
while True:
try:
while True:
try:
if self.current_ffmpeg_count < self.max_ffmpeg_count:
break
time.sleep(5)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
self.P.logger.error('current_ffmpeg_count : %s', self.current_ffmpeg_count)
self.P.logger.error('max_ffmpeg_count : %s', self.max_ffmpeg_count)
break
entity = self.download_queue.get()
if entity.cancel:
continue
#from .logic_ani24 import LogicAni24
#entity.url = LogicAni24.get_video_url(entity.info['code'])
video_url = entity.get_video_url()
if video_url is None:
entity.ffmpeg_status_kor = 'URL실패'
entity.refresh_status()
#plugin.socketio_list_refresh()
continue
import ffmpeg
#max_pf_count = 0
#save_path = ModelSetting.get('download_path')
#if ModelSetting.get('auto_make_folder') == 'True':
# program_path = os.path.join(save_path, entity.info['filename'].split('.')[0])
# save_path = program_path
#try:
# if not os.path.exists(save_path):
# os.makedirs(save_path)
#except:
# logger.debug('program path make fail!!')
# 파일 존재여부 체크
filepath = entity.get_video_filepath()
if os.path.exists(filepath):
entity.ffmpeg_status_kor = '파일 있음'
entity.ffmpeg_percent = 100
entity.refresh_status()
#plugin.socketio_list_refresh()
continue
dirname = os.path.dirname(filepath)
if not os.path.exists(dirname):
os.makedirs(dirname)
f = ffmpeg.Ffmpeg(video_url, os.path.basename(filepath), plugin_id=entity.entity_id, listener=self.ffmpeg_listener, call_plugin=self.P.package_name, save_path=dirname, headers=entity.headers)
f.start()
self.current_ffmpeg_count += 1
self.download_queue.task_done()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def ffmpeg_listener(self, **arg):
import ffmpeg
entity = self.get_entity_by_entity_id(arg['plugin_id'])
if entity is None:
return
if arg['type'] == 'status_change':
if arg['status'] == ffmpeg.Status.DOWNLOADING:
pass
elif arg['status'] == ffmpeg.Status.COMPLETED:
entity.donwload_completed()
elif arg['status'] == ffmpeg.Status.READY:
pass
elif arg['type'] == 'last':
self.current_ffmpeg_count += -1
elif arg['type'] == 'log':
pass
elif arg['type'] == 'normal':
pass
entity.ffmpeg_arg = arg
entity.ffmpeg_status = int(arg['status'])
entity.ffmpeg_status_kor = str(arg['status'])
entity.ffmpeg_percent = arg['data']['percent']
entity.ffmpeg_arg['status'] = str(arg['status'])
#self.P.logger.debug(arg)
#import plugin
#arg['status'] = str(arg['status'])
#plugin.socketio_callback('status', arg)
entity.refresh_status()
#FfmpegQueueEntity.static_index += 1
#FfmpegQueueEntity.entity_list.append(self)
def add_queue(self, entity):
try:
#entity = QueueEntity.create(info)
#if entity is not None:
# LogicQueue.download_queue.put(entity)
# return True
entity.entity_id = self.static_index
self.static_index += 1
self.entity_list.append(entity)
self.download_queue.put(entity)
return True
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
return False
def set_max_ffmpeg_count(self, max_ffmpeg_count):
self.max_ffmpeg_count = max_ffmpeg_count
def get_max_ffmpeg_count(self):
return self.max_ffmpeg_count
def command(self, cmd, entity_id):
self.P.logger.debug('command :%s %s', cmd, entity_id)
ret = {}
try:
if cmd == 'cancel':
self.P.logger.debug('command :%s %s', cmd, entity_id)
entity = self.get_entity_by_entity_id(entity_id)
if entity is not None:
if entity.ffmpeg_status == -1:
entity.cancel = True
entity.ffmpeg_status_kor = "취소"
#entity.refresh_status()
ret['ret'] = 'refresh'
elif entity.ffmpeg_status != 5:
ret['ret'] = 'notify'
ret['log'] = '다운로드중 상태가 아닙니다.'
else:
idx = entity.ffmpeg_arg['data']['idx']
import ffmpeg
ffmpeg.Ffmpeg.stop_by_idx(idx)
entity.refresh_status()
ret['ret'] = 'refresh'
elif cmd == 'reset':
if self.download_queue is not None:
with self.download_queue.mutex:
self.download_queue.queue.clear()
for _ in self.entity_list:
if _.ffmpeg_status == 5:
import ffmpeg
idx = _.ffmpeg_arg['data']['idx']
ffmpeg.Ffmpeg.stop_by_idx(idx)
self.entity_list = []
ret['ret'] = 'refresh'
elif cmd == 'delete_completed':
new_list = []
for _ in self.entity_list:
if _.ffmpeg_status_kor in [u'파일 있음', u'취소', u'사용자중지']:
continue
if _.ffmpeg_status != 7:
new_list.append(_)
self.entity_list = new_list
ret['ret'] = 'refresh'
elif cmd == 'remove':
new_list = []
for _ in self.entity_list:
if _.entity_id == entity_id:
continue
new_list.append(_)
self.entity_list = new_list
ret['ret'] = 'refresh'
return ret
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def get_entity_by_entity_id(self, entity_id):
for _ in self.entity_list:
if _.entity_id == entity_id:
return _
return None
def get_entity_list(self):
ret = []
for x in self.entity_list:
tmp = x.as_dict()
ret.append(tmp)
return ret
+236
View File
@@ -0,0 +1,236 @@
import traceback, time, threading
from framework import F, Job
#########################################################
class Logic(object):
db_default = {
'recent_menu_plugin' : '',
}
def __init__(self, P):
self.P = P
def plugin_load(self):
try:
#self.P.logger.debug('%s plugin_load', self.P.package_name)
self.db_init()
for module in self.P.module_list:
module.migration()
for module in self.P.module_list:
module.plugin_load()
if module.page_list is not None:
for page_instance in module.page_list:
page_instance.plugin_load()
if self.P.ModelSetting is not None:
for module in self.P.module_list:
key = f'{module.name}_auto_start'
if self.P.ModelSetting.has_key(key) and self.P.ModelSetting.get_bool(key):
self.scheduler_start(module.name)
if module.page_list is not None:
for page_instance in module.page_list:
key = f'{module.name}_{page_instance.name}_auto_start'
if self.P.ModelSetting.has_key(key) and self.P.ModelSetting.get_bool(key):
self.scheduler_start_sub(module.name, page_instance.name)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def db_init(self):
try:
if self.P.ModelSetting is None:
return
for key, value in Logic.db_default.items():
if F.db.session.query(self.P.ModelSetting).filter_by(key=key).count() == 0:
F.db.session.add(self.P.ModelSetting(key, value))
for module in self.P.module_list:
if module.page_list is not None:
for page_instance in module.page_list:
if page_instance.db_default is not None:
for key, value in page_instance.db_default.items():
if F.db.session.query(self.P.ModelSetting).filter_by(key=key).count() == 0:
F.db.session.add(self.P.ModelSetting(key, value))
if module.db_default is not None:
for key, value in module.db_default.items():
if F.db.session.query(self.P.ModelSetting).filter_by(key=key).count() == 0:
F.db.session.add(self.P.ModelSetting(key, value))
F.db.session.commit()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def plugin_unload(self):
try:
self.P.logger.debug('%s plugin_unload', self.P.package_name)
for module in self.P.module_list:
module.plugin_unload()
if module.page_list is not None:
for page_instance in module.page_list:
page_instance.plugin_unload()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def scheduler_start(self, sub):
try:
job_id = '%s_%s' % (self.P.package_name, sub)
module = self.get_module(sub)
job = Job(self.P.package_name, job_id, module.get_scheduler_interval(), self.scheduler_function, module.get_scheduler_desc(), args=sub)
F.scheduler.add_job_instance(job)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def scheduler_stop(self, sub):
try:
job_id = '%s_%s' % (self.P.package_name, sub)
F.scheduler.remove_job(job_id)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def scheduler_function(self, sub):
try:
module = self.get_module(sub)
module.scheduler_function()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def reset_db(self,sub):
try:
module = self.get_module(sub)
return module.reset_db()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def one_execute(self, sub):
self.P.logger.debug('one_execute :%s', sub)
try:
job_id = '%s_%s' % (self.P.package_name, sub)
if F.scheduler.is_include(job_id):
if F.scheduler.is_running(job_id):
ret = 'is_running'
else:
F.scheduler.execute_job(job_id)
ret = 'scheduler'
else:
def func():
time.sleep(2)
self.scheduler_function(sub)
threading.Thread(target=func, args=()).start()
ret = 'thread'
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
ret = 'fail'
return ret
def immediately_execute(self, sub):
self.P.logger.debug('immediately_execute :%s', sub)
try:
def func():
time.sleep(1)
self.scheduler_function(sub)
threading.Thread(target=func, args=()).start()
ret = {'ret':'success', 'msg':'실행합니다.'}
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
ret = {'ret' : 'danger', 'msg':str(exception)}
return ret
def get_module(self, sub):
try:
for module in self.P.module_list:
if module.name == sub:
return module
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def process_telegram_data(self, data, target=None):
try:
for module in self.P.module_list:
if target is None or target.startswith(module.name):
module.process_telegram_data(data, target=target)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
#######################################################
# 플러그인 - 모듈 - 페이지 구조하에서 서브 관련 함수
def scheduler_start_sub(self, module_name, page_name):
try:
#self.P.logger.warning('scheduler_start_sub')
job_id = f'{self.P.package_name}_{module_name}_{page_name}'
ins_module = self.get_module(module_name)
ins_page = ins_module.get_page(page_name)
job = Job(self.P.package_name, job_id, ins_page.get_scheduler_interval(), ins_page.scheduler_function, ins_page.get_scheduler_desc(), args=None)
F.scheduler.add_job_instance(job)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def scheduler_stop_sub(self, module_name, page_name):
try:
job_id = f'{self.P.package_name}_{module_name}_{page_name}'
F.scheduler.remove_job(job_id)
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def scheduler_function_sub(self, module_name, page_name):
try:
ins_module = self.get_module(module_name)
ins_sub = ins_module.get_page(page_name)
ins_sub.scheduler_function()
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
def one_execute_sub(self, module_name, page_name):
try:
job_id = f'{self.P.package_name}_{module_name}_{page_name}'
if F.scheduler.is_include(job_id):
if F.scheduler.is_running(job_id):
ret = 'is_running'
else:
F.scheduler.execute_job(job_id)
ret = 'scheduler'
else:
def func():
time.sleep(2)
self.scheduler_function_sub(module_name, page_name)
threading.Thread(target=func, args=()).start()
ret = 'thread'
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
ret = 'fail'
return ret
def immediately_execute_sub(self, module_name, page_name):
self.P.logger.debug(f'immediately_execute : {module_name} {page_name}')
try:
def func():
time.sleep(1)
self.scheduler_function_sub(module_name, page_name)
threading.Thread(target=func, args=()).start()
ret = {'ret':'success', 'msg':'실행합니다.'}
except Exception as exception:
self.P.logger.error('Exception:%s', exception)
self.P.logger.error(traceback.format_exc())
ret = {'ret' : 'danger', 'msg':str(exception)}
return ret
+171
View File
@@ -0,0 +1,171 @@
import traceback
class PluginModuleBase(object):
db_default = None
def __init__(self, P, first_menu=None, name=None, scheduler_desc=None):
self.P = P
self.scheduler_desc = scheduler_desc
self.first_menu = first_menu
self.name = name
self.socketio_list = None
self.page_list = None
# set_module_list 대응
def set_page_list(self, page_list):
try:
self.page_list = []
for mod in page_list:
mod_ins = mod(self.P, self)
self.page_list.append(mod_ins)
except Exception as e:
self.P.logger.error(f'Exception:{str(e)}')
self.P.logger.error(traceback.format_exc())
def get_page(self, page_name):
try:
for page in self.page_list:
if page_name == page.name:
return page
except Exception as e:
self.P.logger.error(f'Exception:{str(e)}')
self.P.logger.error(traceback.format_exc())
def process_menu(self, sub):
pass
def process_ajax(self, sub, req):
pass
def process_command(self, command, arg1, arg2, arg3, req):
pass
def process_api(self, sub, req):
pass
def process_normal(self, sub, req):
pass
def scheduler_function(self):
pass
def reset_db(self):
pass
def plugin_load(self):
pass
def plugin_unload(self):
pass
def setting_save_after(self, change_list):
pass
def process_telegram_data(self, data, target=None):
pass
def migration(self):
pass
#################################################################
def get_scheduler_desc(self):
return self.scheduler_desc
def get_scheduler_interval(self):
if self.P is not None and self.P.ModelSetting is not None and self.name is not None:
return self.P.ModelSetting.get('{module_name}_interval'.format(module_name=self.name))
def get_first_menu(self):
return self.first_menu
def get_scheduler_name(self):
return '%s_%s' % (self.P.package_name, self.name)
def dump(self, data):
if type(data) in [type({}), type([])]:
import json
return '\n' + json.dumps(data, indent=4, ensure_ascii=False)
else:
return str(data)
def socketio_connect(self):
pass
def socketio_disconnect(self):
pass
class PluginPageBase(object):
db_default = None
def __init__(self, P, parent, name=None, scheduler_desc=None):
self.P = P
self.parent = parent
self.name = name
self.scheduler_desc = scheduler_desc
self.socketio_list = None
def process_ajax(self, sub, req):
pass
def scheduler_function(self):
pass
def plugin_load(self):
pass
def plugin_unload(self):
pass
def get_scheduler_desc(self):
return self.scheduler_desc
def get_scheduler_interval(self):
if self.P is not None and self.P.ModelSetting is not None and self.parent.name is not None and self.name is not None:
return self.P.ModelSetting.get(f'{self.parent.name}_{self.name}_interval')
def get_scheduler_name(self):
return f'{self.P.package_name}_{self.parent.name}_{self.name}'
def process_api(self, sub, req):
pass
def process_normal(self, sub, req):
pass
def reset_db(self):
pass
def setting_save_after(self, change_list):
pass
def process_telegram_data(self, data, target=None):
pass
def migration(self):
pass
#################################################################
def process_menu(self, sub):
pass
+145
View File
@@ -0,0 +1,145 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import traceback
from datetime import datetime
# third-party
# sjva 공용
from framework import db
from framework.util import Util
#########################################################
class ModelBase(db.Model):
__abstract__ = True
__table_args__ = {'mysql_collate': 'utf8_general_ci'}
model_setting = None
logger = None
def __repr__(self):
return repr(self.as_dict())
def as_dict(self):
return {x.name: getattr(self, x.name).strftime('%m-%d %H:%M:%S') if isinstance(getattr(self, x.name), datetime) else getattr(self, x.name) for x in self.__table__.columns}
def save(self):
try:
db.session.add(self)
db.session.commit()
except Exception as e:
self.logger.error(f'Exception:{str(e)}')
self.logger.error(traceback.format_exc())
@classmethod
def get_paging_info(cls, count, current_page, page_size):
try:
paging = {}
paging['prev_page'] = True
paging['next_page'] = True
if current_page <= 10:
paging['prev_page'] = False
paging['total_page'] = int(count / page_size) + 1
if count % page_size == 0:
paging['total_page'] -= 1
paging['start_page'] = int((current_page-1)/10) * 10 + 1
paging['last_page'] = paging['total_page'] if paging['start_page'] + 9 > paging['total_page'] else paging['start_page'] + 9
if paging['last_page'] == paging['total_page']:
paging['next_page'] = False
paging['current_page'] = current_page
paging['count'] = count
cls.logger.debug('paging : c:%s %s %s %s %s %s', count, paging['total_page'], paging['prev_page'], paging['next_page'] , paging['start_page'], paging['last_page'])
return paging
except Exception as e:
cls.logger.error(f'Exception:{str(e)}')
cls.logger.error(traceback.format_exc())
@classmethod
def get_by_id(cls, id):
try:
return db.session.query(cls).filter_by(id=id).first()
except Exception as e:
cls.logger.error(f'Exception:{str(e)}')
cls.logger.error(traceback.format_exc())
@classmethod
def get_list(cls, by_dict=False):
try:
tmp = db.session.query(cls).all()
if by_dict:
tmp = [x.as_dict() for x in tmp]
return tmp
except Exception as e:
cls.logger.error(f'Exception:{str(e)}')
cls.logger.error(traceback.format_exc())
@classmethod
def delete_by_id(cls, id):
try:
db.session.query(cls).filter_by(id=id).delete()
db.session.commit()
return True
except Exception as e:
cls.logger.error(f'Exception:{str(e)}')
cls.logger.error(traceback.format_exc())
return False
@classmethod
def delete_all(cls):
try:
db.session.query(cls).delete()
db.session.commit()
return True
except Exception as e:
cls.logger.error(f'Exception:{str(e)}')
cls.logger.error(traceback.format_exc())
return False
@classmethod
def web_list(cls, req):
try:
ret = {}
page = 1
page_size = 30
search = ''
if 'page' in req.form:
page = int(req.form['page'])
if 'keyword' in req.form:
search = req.form['keyword']
option1 = req.form.get('option1', 'all')
option2 = req.form.get('option2', 'all')
order = req.form['order'] if 'order' in req.form else 'desc'
query = cls.make_query(order=order, search=search, option1=option1, option2=option2)
count = query.count()
query = query.limit(page_size).offset((page-1)*page_size)
cls.logger.debug('cls count:%s', count)
lists = query.all()
ret['list'] = [item.as_dict() for item in lists]
ret['paging'] = cls.get_paging_info(count, page, page_size)
try:
if cls.model_setting is not None and cls.__tablename__ is not None:
cls.model_setting.set(f'{cls.__tablename__}_last_list_option', f'{order}|{page}|{search}|{option1}|{option2}')
except Exception as e:
cls.logger.error('Exception:%s', e)
cls.logger.error(traceback.format_exc())
cls.logger.error(f'{cls.__tablename__}_last_list_option ERROR!' )
return ret
except Exception as e:
cls.logger.error('Exception:%s', e)
cls.logger.error(traceback.format_exc())
# 오버라이딩
@classmethod
def make_query(cls, order='desc', search='', option1='all', option2='all'):
query = db.session.query(cls)
return query
+138
View File
@@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os, traceback
# third-party
# sjva 공용
from framework import frame, db
from framework.util import Util
#########################################################
def get_model_setting(package_name, logger, table_name=None):
class ModelSetting(db.Model):
__tablename__ = '%s_setting' % package_name if table_name is None else table_name
__table_args__ = {'mysql_collate': 'utf8_general_ci'}
__bind_key__ = package_name
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String, unique=True, nullable=False)
value = db.Column(db.String, nullable=False)
def __init__(self, key, value):
self.key = key
self.value = value
def __repr__(self):
return repr(self.as_dict())
def as_dict(self):
return {x.name: getattr(self, x.name) for x in self.__table__.columns}
@staticmethod
def get(key):
try:
ret = db.session.query(ModelSetting).filter_by(key=key).first()
if ret is not None:
return ret.value.strip()
return None
except Exception as exception:
logger.error('Exception:%s %s', exception, key)
logger.error(traceback.format_exc())
@staticmethod
def has_key(key):
return (db.session.query(ModelSetting).filter_by(key=key).first() is not None)
@staticmethod
def get_int(key):
try:
return int(ModelSetting.get(key))
except Exception as exception:
logger.error('Exception:%s %s', exception, key)
logger.error(traceback.format_exc())
@staticmethod
def get_bool(key):
try:
return (ModelSetting.get(key) == 'True')
except Exception as exception:
logger.error('Exception:%s %s', exception, key)
logger.error(traceback.format_exc())
@staticmethod
def set(key, value):
try:
item = db.session.query(ModelSetting).filter_by(key=key).with_for_update().first()
if item is not None:
item.value = value.strip() if value is not None else value
db.session.commit()
else:
db.session.add(ModelSetting(key, value.strip()))
db.session.commit()
except Exception as exception:
logger.error('Exception:%s %s', exception, key)
logger.error(traceback.format_exc())
@staticmethod
def to_dict():
try:
ret = Util.db_list_to_dict(db.session.query(ModelSetting).all())
ret['package_name'] = package_name
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def setting_save(req):
try:
change_list = []
for key, value in req.form.items():
if key in ['scheduler', 'is_running']:
continue
if key.startswith('global_') or key.startswith('tmp_') or key.startswith('_'):
continue
#logger.debug('Key:%s Value:%s', key, value)
if ModelSetting.get(key) != value:
change_list.append(key)
entity = db.session.query(ModelSetting).filter_by(key=key).with_for_update().first()
entity.value = value
db.session.commit()
return True, change_list
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.debug('Error Key:%s Value:%s', key, value)
return False, []
@staticmethod
def get_list(key, delimeter='\n', comment=' #'):
try:
value = ModelSetting.get(key).replace('\n', delimeter)
if comment is None:
values = [x.strip() for x in value.split(delimeter)]
else:
values = [x.split(comment)[0].strip() for x in value.split(delimeter)]
values = ModelSetting.get_list_except_empty(values)
return values
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('Error Key:%s Value:%s', key, value)
@staticmethod
def get_list_except_empty(source):
tmp = []
for _ in source:
if _.strip().startswith('#'):
continue
if _.strip() != '':
tmp.append(_.strip())
return tmp
return ModelSetting
+423
View File
@@ -0,0 +1,423 @@
# -*- coding: utf-8 -*-
# python
import traceback, os
import json
# third-party
from flask import Blueprint, request, render_template, redirect, jsonify
from flask_login import login_required
from flask_socketio import SocketIO, emit, send
# sjva 공용
from framework import socketio, check_api
from support.base.util import AlchemyEncoder
# 패키지
#########################################################
def default_route(P):
@P.blueprint.route('/')
def home():
if P.ModelSetting is not None:
tmp = P.ModelSetting.get('recent_menu_plugin')
if tmp is not None and tmp != '':
tmps = tmp.split('|')
if len(tmps) == 2:
return redirect('/{package_name}/{sub}/{sub2}'.format(package_name=P.package_name, sub=tmps[0], sub2=tmps[1]))
elif len(tmps) == 1 and not (P.package_name =='system' and tmps[0] == 'logout'):
return redirect('/{package_name}/{sub}'.format(package_name=P.package_name, sub=tmps[0]))
return redirect('/{package_name}/{home_module}'.format(package_name=P.package_name, home_module=P.home_module))
@P.blueprint.route('/<sub>', methods=['GET', 'POST'])
@login_required
def first_menu(sub):
try:
if P.ModelSetting is not None and (P.package_name == 'system' and sub != 'home'):
P.ModelSetting.set('recent_menu_plugin', '{}'.format(sub))
for module in P.module_list:
if sub == module.name:
first_menu = module.get_first_menu()
if first_menu:
return redirect('/{package_name}/{sub}/{first_menu}'.format(package_name=P.package_name, sub=sub, first_menu=module.get_first_menu()))
else:
return module.process_menu(None, request)
if sub == 'log':
return render_template('log.html', package=P.package_name)
elif sub == 'manual':
#return redirect(f"/{P.package_name}/manual/{P.menu['second']['manual'][0][0]}")
try:
return redirect(f"/{P.package_name}/manual/{P.menu['sub2']['manual'][0][0]}")
except:
return redirect(f"/{P.package_name}/manual/{P.get_first_manual_path()}")
return render_template('sample.html', title='%s - %s' % (P.package_name, sub))
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/manual/<path:path>', methods=['GET', 'POST'])
@login_required
def manual(path):
try:
plugin_root = os.path.dirname(P.blueprint.template_folder)
filepath = os.path.join(plugin_root, *path.split('/'))
from tool_base import ToolBaseFile
data = ToolBaseFile.read(filepath)
return render_template('manual.html', data=data)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/<sub>/<sub2>', methods=['GET', 'POST'])
@login_required
def second_menu(sub, sub2):
if P.ModelSetting is not None:
P.ModelSetting.set('recent_menu_plugin', '{}|{}'.format(sub, sub2))
try:
for module in P.module_list:
if sub == module.name:
return module.process_menu(sub2, request)
if sub == 'log':
return render_template('log.html', package=P.package_name)
return render_template('sample.html', title='%s - %s' % (P.package_name, sub))
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
#########################################################
# For UI
#########################################################
@P.blueprint.route('/ajax/<sub>', methods=['GET', 'POST'])
@login_required
def ajax(sub):
P.logger.debug('AJAX %s %s', P.package_name, sub)
try:
# global
if sub == 'setting_save':
ret, change_list = P.ModelSetting.setting_save(request)
for module in P.module_list:
module.setting_save_after(change_list)
return jsonify(ret)
elif sub == 'scheduler':
sub = request.form['sub']
go = request.form['scheduler']
P.logger.debug('scheduler :%s', go)
if go == 'true':
P.logic.scheduler_start(sub)
else:
P.logic.scheduler_stop(sub)
return jsonify(go)
elif sub == 'reset_db':
sub = request.form['sub']
ret = P.logic.reset_db(sub)
return jsonify(ret)
elif sub == 'one_execute':
sub = request.form['sub']
ret = P.logic.one_execute(sub)
return jsonify(ret)
elif sub == 'immediately_execute':
sub = request.form['sub']
ret = P.logic.immediately_execute(sub)
return jsonify(ret)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/ajax/<mod>/<cmd>', methods=['GET', 'POST'])
@login_required
def second_ajax(mod, cmd):
try:
for module in P.module_list:
if mod == module.name:
if cmd == 'command':
return module.process_command(request.form['command'], request.form.get('arg1'), request.form.get('arg2'), request.form.get('arg3'), request)
else:
return module.process_ajax(cmd, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/ajax/<module_name>/<page_name>/<command>', methods=['GET', 'POST'])
@login_required
def sub_ajax(module_name, page_name, command):
try:
ins_module = P.get_module(module_name)
ins_page = ins_module.get_page(page_name)
if ins_page != None:
if command == 'scheduler':
#sub = page_name
go = request.form['scheduler']
P.logger.debug('scheduler :%s', go)
if go == 'true':
P.logic.scheduler_start_sub(module_name, page_name)
else:
P.logic.scheduler_stop_sub(module_name, page_name)
return jsonify(go)
#elif command == 'reset_db':
# sub = request.form['sub']
# ret = P.logic.reset_db(sub)
# return jsonify(ret)
elif command == 'one_execute':
ret = P.logic.one_execute_sub(module_name, page_name)
return jsonify(ret)
elif command == 'immediately_execute':
ret = P.logic.immediately_execute_sub(module_name, page_name)
return jsonify(ret)
else:
return ins_page.process_ajax(command, request)
P.logger.error(f"not process ajax : {P.package_name} {module_name} {page_name} {command}")
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
#########################################################
# API - 외부
#########################################################
# 단일 모듈인 경우 모듈이름을 붙이기 불편하여 추가.
@P.blueprint.route('/api/<sub2>', methods=['GET', 'POST'])
@check_api
def api_first(sub2):
try:
for module in P.module_list:
return module.process_api(sub2, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/api/<sub>/<sub2>', methods=['GET', 'POST'])
@check_api
def api(sub, sub2):
try:
for module in P.module_list:
if sub == module.name:
return module.process_api(sub2, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/normal/<sub>/<sub2>', methods=['GET', 'POST'])
def normal(sub, sub2):
try:
for module in P.module_list:
if sub == module.name:
return module.process_normal(sub2, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
def default_route_single_module(P):
@P.blueprint.route('/')
def home():
return redirect('/{package_name}/{home_module}'.format(package_name=P.package_name, home_module=P.home_module))
@P.blueprint.route('/<sub>', methods=['GET', 'POST'])
@login_required
def first_menu(sub):
if sub == 'log':
return render_template('log.html', package=P.package_name)
return P.module_list[0].process_menu(sub, request)
@P.blueprint.route('/ajax/<sub>', methods=['GET', 'POST'])
@login_required
def ajax(sub):
P.logger.debug('AJAX %s %s', P.package_name, sub)
try:
# global
if sub == 'setting_save':
ret, change_list = P.ModelSetting.setting_save(request)
if ret:
P.module_list[0].setting_save_after(change_list)
return jsonify(ret)
elif sub == 'scheduler':
sub = request.form['sub']
go = request.form['scheduler']
P.logger.debug('scheduler :%s', go)
if go == 'true':
P.logic.scheduler_start(sub)
else:
P.logic.scheduler_stop(sub)
return jsonify(go)
elif sub == 'reset_db':
sub = request.form['sub']
ret = P.logic.reset_db(sub)
return jsonify(ret)
elif sub == 'one_execute':
sub = request.form['sub']
ret = P.logic.one_execute(sub)
return jsonify(ret)
else:
return P.module_list[0].process_ajax(sub, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/api/<sub>', methods=['GET', 'POST'])
@check_api
def api(sub):
try:
return P.module_list[0].process_api(sub, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@P.blueprint.route('/normal/<sub>', methods=['GET', 'POST'])
def normal(sub):
try:
return P.module_list[0].process_normal(sub, request)
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
def default_route_socketio_module(module):
P = module.P
if module.socketio_list is None:
module.socketio_list = []
@socketio.on('connect', namespace=f'/{P.package_name}/{module.name}')
def connect():
try:
P.logger.debug(f'socket_connect : {P.package_name} - {module.name}')
module.socketio_list.append(request.sid)
socketio_callback('start', '')
module.socketio_connect()
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
@socketio.on('disconnect', namespace='/{package_name}/{sub}'.format(package_name=P.package_name, sub=module.name))
def disconnect():
try:
P.logger.debug('socket_disconnect : %s - %s', P.package_name, module.name)
module.socketio_list.remove(request.sid)
module.socketio_disconnect()
except Exception as exception:
P.logger.error('Exception:%s', exception)
P.logger.error(traceback.format_exc())
def socketio_callback(cmd, data, encoding=True):
if module.socketio_list:
if encoding:
data = json.dumps(data, cls=AlchemyEncoder)
data = json.loads(data)
socketio.emit(cmd, data, namespace='/{package_name}/{sub}'.format(package_name=P.package_name, sub=module.name), broadcast=True)
module.socketio_callback = socketio_callback
def default_route_socketio_page(page):
module = page.parent
P = page.P
if page.socketio_list is None:
page.socketio_list = []
@socketio.on('connect', namespace=f'/{P.package_name}/{module.name}/{page.name}')
def connect():
try:
P.logger.debug(f'socket_connect : {P.package_name}/{module.name}/{page.name}')
page.socketio_list.append(request.sid)
socketio_callback('start', '')
except Exception as exception:
P.logger.error(f'Exception:{str(exception)}', exception)
P.logger.error(traceback.format_exc())
@socketio.on('disconnect', namespace=f'/{P.package_name}/{module.name}/{page.name}')
def disconnect():
try:
P.logger.debug(f'socket_disconnect : {P.package_name}/{module.name}/{page.name}')
page.socketio_list.remove(request.sid)
except Exception as exception:
P.logger.error(f'Exception:{str(exception)}', exception)
P.logger.error(traceback.format_exc())
def socketio_callback(cmd, data, encoding=True):
if page.socketio_list:
if encoding:
data = json.dumps(data, cls=AlchemyEncoder)
data = json.loads(data)
socketio.emit(cmd, data, namespace=f'/{P.package_name}/{module.name}/{page.name}', broadcast=True)
page.socketio_callback = socketio_callback
+17
View File
@@ -0,0 +1,17 @@
def d(data):
if type(data) in [type({}), type([])]:
import json
return '\n' + json.dumps(data, indent=4, ensure_ascii=False)
else:
return str(data)
from .logger import get_logger
logger = get_logger()
def set_logger(l):
global logger
logger = l
# 일반 cli 사용 겸용이다.
# set_logger 로 인한 진입이 아니고 import가 되면 기본 경로로 로그파일을
# 생성하기 때문에, set_logger 전에 import가 되지 않도록 주의.
+13
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
+80
View File
@@ -0,0 +1,80 @@
import os, sys, logging, logging.handlers
from datetime import datetime
from pytz import timezone, utc
"""
ConsoleColor.Black => "\x1B[30m",
ConsoleColor.DarkRed => "\x1B[31m",
ConsoleColor.DarkGreen => "\x1B[32m",
ConsoleColor.DarkYellow => "\x1B[33m",
ConsoleColor.DarkBlue => "\x1B[34m",
ConsoleColor.DarkMagenta => "\x1B[35m",
ConsoleColor.DarkCyan => "\x1B[36m",
ConsoleColor.Gray => "\x1B[37m",
ConsoleColor.Red => "\x1B[1m\x1B[31m",
ConsoleColor.Green => "\x1B[1m\x1B[32m",
ConsoleColor.Yellow => "\x1B[1m\x1B[33m",
ConsoleColor.Blue => "\x1B[1m\x1B[34m",
ConsoleColor.Magenta => "\x1B[1m\x1B[35m",
ConsoleColor.Cyan => "\x1B[1m\x1B[36m",
ConsoleColor.White => "\x1B[1m\x1B[37m",
"""
class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""
grey = "\x1b[38;21m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
green = "\x1B[32m"
# pathname filename
#format = "[%(asctime)s|%(name)s|%(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
format = '[{yellow}%(asctime)s{reset}|{color}%(levelname)s{reset}|{green}%(name)s{reset}|%(pathname)s:%(lineno)s] {color}%(message)s{reset}'
FORMATS = {
logging.DEBUG: format.format(color=grey, reset=reset, yellow=yellow, green=green),
logging.INFO: format.format(color=green, reset=reset, yellow=yellow, green=green),
logging.WARNING: format.format(color=yellow, reset=reset, yellow=yellow, green=green),
logging.ERROR: format.format(color=red, reset=reset, yellow=yellow, green=green),
logging.CRITICAL: format.format(color=bold_red, reset=reset, yellow=yellow, green=green)
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
def get_logger(name=None, log_path=None):
if name == None:
name = sys.argv[0].rsplit('.', 1)[0]
logger = logging.getLogger(name)
if not logger.handlers:
level = logging.DEBUG
logger.setLevel(level)
formatter = logging.Formatter(u'[%(asctime)s|%(levelname)s|%(filename)s:%(lineno)s] %(message)s')
def customTime(*args):
utc_dt = utc.localize(datetime.utcnow())
my_tz = timezone("Asia/Seoul")
converted = utc_dt.astimezone(my_tz)
return converted.timetuple()
formatter.converter = customTime
file_max_bytes = 1 * 1024 * 1024
if log_path == None:
log_path = os.path.join(os.getcwd(), 'tmp')
#os.makedirs(log_path, exist_ok=True)
else:
os.makedirs(log_path, exist_ok=True)
fileHandler = logging.handlers.RotatingFileHandler(filename=os.path.join(log_path, f'{name}.log'), maxBytes=file_max_bytes, backupCount=5, encoding='utf8', delay=True)
streamHandler = logging.StreamHandler()
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(CustomFormatter())
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
return logger
View File
+497
View File
@@ -0,0 +1,497 @@
import os, sys, traceback, time, urllib.parse, requests, json, base64, re, platform
if __name__ == '__main__':
if platform.system() == 'Windows':
sys.path += ["C:\SJVA3\lib2", "C:\SJVA3\data\custom", "C:\SJVA3_DEV"]
else:
sys.path += ["/root/SJVA3/lib2", "/root/SJVA3/data/custom"]
from support import d, logger
apikey = '1e7952d0917d6aab1f0293a063697610'
#apikey = '95a64ebcd8e154aeb96928bf34848826'
class SupportTving:
default_param = f'&screenCode=CSSD0100&networkCode=CSND0900&osCode=CSOD0900&teleCode=CSCD0900&apiKey={apikey}'
headers = {
'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',
'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',
'Referer' : '',
}
# 같은 코드가 여러군에 있는게 불편하여 그냥 sjva안에서는 ins를 가져와서 사용하는 것으로 한다.
# sjva외에서는 생성해서 사용.
# ins를 만드는 것은 system plugin
ins = None
def __init__(self, token=None, proxy=None, user=None, password=None, deviceid=None, uuid=None):
self.token = token
if self.token and '_tving_token=' in self.token:
self.token = self.token.split('=')[1]
self.proxies = None
self.proxy = proxy
if self.proxy != None:
self.proxies = {"https": proxy, 'http':proxy}
self.user = user
self.password = password
self.deviceid = deviceid
self.uuid = uuid
def do_login(self, user_id, user_pw, login_type):
try:
url = 'https://user.tving.com/user/doLogin.tving'
if login_type == '0':
login_type_value = '10'
else:
login_type_value = '20'
params = {
'userId' : user_id,
'password' : user_pw,
'loginType' : login_type_value
}
res = requests.post(url, data=params)
cookie = res.headers['Set-Cookie']
for c in cookie.split(','):
c = c.strip()
if c.startswith('_tving_token'):
ret = c.split(';')[0]
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
def get_device_list(self):
url = f"http://api.tving.com/v1/user/device/list?{self.default_param[1:]}"
return self.api_get(url)
def get_info(self, mediacode, streamcode):
ts = str(int(time.time()))
try:
tmp_param = self.default_param
if streamcode == 'stream70':
tmp_param = self.default_param.replace('CSSD0100', 'CSSD1200')
url = f"http://api.tving.com/v2/media/stream/info?info=y{tmp_param}&noCache={ts}&mediaCode={mediacode}&streamCode={streamcode}&deviceId={self.deviceid}"
#logger.warning(url)
if self.token != None:
self.headers['Cookie'] = f"_tving_token={self.token}"
info = self.api_get(url)
if streamcode == 'stream70':
for stream in info['content']['info']['stream']:
if stream['code'] == 'stream70':
break
else:
#logger.debug("stream70이 없어서 50으로 재요청")
return self.get_info(mediacode, 'stream50')
#logger.debug(d(self.headers))
#logger.debug(d(info))
#logger.error(mediacode)
if info['result']['code'] == "000":
info['avaliable'] = True
else:
info['avaliable'] = False
return info
#logger.error(info['stream']['drm_yn'])
if 'drm_yn' in info['stream'] and info['stream']['drm_yn'] == 'Y' and '4k_nondrm_url' not in info['stream']['broadcast']:
info['drm'] = True
info['play_info'] = {
'uri' : self.__decrypt2(mediacode, ts, info['stream']['broadcast']['widevine']['broad_url']),
'drm_scheme' : 'widevine',
'drm_license_uri' : 'http://cj.drmkeyserver.com/widevine_license',
'drm_key_request_properties': {
'origin' : 'https://www.tving.com',
'sec-fetch-site' : 'cross-site',
'sec-fetch-mode' : 'cors',
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
'Host' : 'cj.drmkeyserver.com',
'referer' : 'https://www.tving.com/',
'AcquireLicenseAssertion' : info['stream']['drm_license_assertion'],
}
}
info['url'] = info['play_info']['uri']
#info['play_info']['url'] = info['play_info']['uri']
else:
if '4k_nondrm_url' in info['stream']['broadcast']:
url = info['stream']['broadcast']['4k_nondrm_url']
else:
url = info['stream']['broadcast']['broad_url']
decrypted_url = self.__decrypt2(mediacode, ts, url)
#logger.error(decrypted_url)
#if decrypted_url.find('m3u8') == -1:
# decrypted_url = decrypted_url.replace('rtmp', 'http')
# decrypted_url = decrypted_url.replace('?', '/playlist.m3u8?')
#2020-06-12
# 2022-05-26
# smil/playlist.m3u8 이거 영화만 탐??
#logger.error(decrypted_url)
if decrypted_url.find('smil/playlist.m3u8') != -1 and decrypted_url.find('content_type=VOD') != -1 :
tmps = decrypted_url.split('playlist.m3u8')
r = requests.get(decrypted_url, headers=self.headers, proxies=self.proxies)
lines = r.text.split('\n')
#logger.debug(d(lines))
# 2022-05-26 이전까지는 고화질이 마지막에 나왔을텐데 영화에서 맨 처음에 나온다고 함. 당연히 확인했을테니 마지막이었겠지?
#i = -1
#last = ''
#while len(last) == 0:
# last = lines[i].strip()
# i -= 1
max_bandwidth = 0
max_url = None
while len(lines) > 0: #for line in lines:
line = lines.pop(0)
match = re.search('BANDWIDTH=(?P<bw>\d+)', line)
if match:
bw = int(match.group('bw'))
if bw > max_bandwidth:
max_bandwidth = bw
max_url = lines.pop(0)
decrypted_url = '%s%s' % (tmps[0], max_url)
#logger.debug(f"VOD : {decrypted_url}")
if 'manifest.m3u8' in decrypted_url: #QVOD
r = requests.get(decrypted_url, headers=self.headers, proxies=self.proxies)
lines = r.text.split('\n')
i = -1
last = ''
while len(last) == 0:
last = lines[i].strip()
i -= 1
tmps = decrypted_url.split('//')
tmps2 = tmps[1].split('/', 1)
tmps3 = tmps2[1].rsplit('/', 1)
tmps3[1] = re.sub(r'manifest\.m3u8\?start=(\d|-|:)+&end=(\d|-|:)+', '', tmps3[1])
decrypted_url = f"{tmps[0]}//{tmps2[0]}{last}{tmps3[1]}"
info['broad_url'] = decrypted_url
info['drm'] = False
info['url'] = decrypted_url
info['play_info'] = {
'hls': decrypted_url,
}
if mediacode[0] in ['E', 'M']:
info['filename'] = self.get_filename(info)
#logger.warning(d(info))
return info
except Exception as e:
logger.error(f"Exception:{str(e)}")
logger.error(traceback.format_exc())
# list_type : all, live, vod
def get_live_list(self, list_type='live', order='rating', include_drm=False):
def func(param, page, order='rating', include_drm=True):
has_more = 'N'
try:
result = []
url = f'https://api.tving.com/v2/media/lives?cacheType=main&pageNo={page}&pageSize=20&order={order}&adult=all&free=all&guest=all&scope=all{param}{self.default_param}'
data = self.api_get(url)
#logger.debug(url)
for item in data["result"]:
try:
# 2020-11-10 현재 /v1 에서는 drm채널인지 알려주지않고, 방송이 drm 적용인지 알려줌. 그냥 fix로..
info = {'is_drm':self.is_drm_channel(item['live_code'])}
if include_drm == False and info['is_drm']:
continue
info['id'] = item["live_code"]
info['title'] = item['schedule']['channel']['name']['ko']
info['episode_title'] = ' '
info['img'] = 'http://image.tving.com/upload/cms/caic/CAIC1900/%s.png' % item["live_code"]
if item['schedule']['episode'] is not None:
info['episode_title'] = item['schedule']['episode']['name']['ko']
if info['title'].startswith('CH.') and len(item['schedule']['episode']['image']) > 0:
info['img'] = 'http://image.tving.com' + item['schedule']['episode']['image'][0]['url']
#info['free'] = (item['schedule']['broadcast_url'][0]['broad_url1'].find('drm') == -1)
info['summary'] = info['episode_title']
result.append(info)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
has_more = data["has_more"]
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return has_more, result
ret = []
if list_type == 'live':
params = ['&channelType=CPCS0100,CPCS0400']
elif list_type == 'vod':
params = ['&channelType=CPCS0300']
elif list_type == 'all':
params = ['&channelType=CPCS0100,CPCS0400', '&channelType=CPCS0300']
else:
params = ['&channelType=CPCS0100,CPCS0400']
for param in params:
page = 1
while True:
hasMore, data = func(param, page, order=order, include_drm=include_drm)
ret += data
if hasMore == 'N':
break
page += 1
return ret
def get_vod_list(self, program_code=None, page=1):
url = f'http://api.tving.com/v2/media/episodes?pageNo={page}&pageSize=18&adult=all&guest=all&scope=all&personal=N{self.default_param}'
if program_code is not None:
url += f'&free=all&order=frequencyDesc&programCode={program_code}'
else:
url += "&free=all&lastFrequency=n&order=broadDate"
return self.api_get(url)
def get_vod_list_genre(self, genre, page=1):
url = f'http://api.tving.com/v2/media/episodes?pageNo={page}&pageSize=18&adult=all&guest=all&scope=all&personal=N{self.default_param}'
if genre != None and genre != 'all':
url += f"&free=all&lastFrequency=y&order=broadDate&categoryCode={genre}"
else:
url += "&free=all&lastFrequency=y&order=broadDate"
return self.api_get(url)
def get_movie_list(self, page=1, category='all'):
url = f'https://api.tving.com/v2/media/movies?pageNo={page}&pageSize=24&order=viewDay&free=all&adult=all&guest=all&scope=all&productPackageCode=338723&personal=N&diversityYn=N{self.default_param}'
if category != 'all':
url += f'&multiCategoryCode={category}'
return self.api_get(url)
def get_frequency_programid(self, programid, page=1):
url = f'https://api.tving.com/v2/media/frequency/program/{programid}?pageNo={page}&pageSize=10&order=new&free=all&adult=all&scope=all{self.default_param}'
return self.api_get(url)
def get_schedules(self, code, date, start_time, end_time):
url = f"https://api.tving.com/v2/media/schedules?pageNo=1&pageSize=20&order=chno&scope=all&adult=n&free=all&broadDate={date}&broadcastDate={date}&startBroadTime={start_time}&endBroadTime={end_time}&channelCode={','.join(code)}{self.default_param}"
return self.api_get(url)
def get_program_programid(self, programid):
url = f'https://api.tving.com/v2/media/program/{programid}?pageNo=1&pageSize=10&order=name{self.default_param}'
return self.api_get(url)
def search(self, keyword):
# gubun VODBC, VODMV
try:
import urllib.parse
url = 'https://search.tving.com/search/common/module/getAkc.jsp?kwd=' + urllib.parse.quote(str(keyword))
data = requests.get(url, headers=self.headers).json()
#logger.debug(d(data))
if 'dataList' in data['akcRsb']:
return data['akcRsb']['dataList']
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
def api_get(self, url):
try:
if self.token != None:
self.headers['Cookie'] = f"_tving_token={self.token}"
data = requests.get(url, headers=self.headers, proxies=self.proxies).json()
try:
if type(data['body']['result']) == type({}) and data['body']['result']['message'] != None:
logger.debug(f"tving api message : {data['body']['result']['message']}")
except:
pass
if data['header']['status'] == 200:
return data['body']
except Exception as e:
logger.error(f'url: {url}')
logger.error(f"Exception:{str(e)}")
logger.error(traceback.format_exc())
def is_drm_channel(self, code):
# C07381:ocn C05661:디즈니채널 C44441:koon C04601:ocn movie C07382:ocn thrill
return (code in ['C07381', 'C05661', 'C44441', 'C04601', 'C07382'])
def get_filename(self, episode_data):
try:
title = episode_data["content"]["program_name"]
title = title.replace("<", "").replace(">", "").replace("\\", "").replace("/", "").replace(":", "").replace("*", "").replace("\"", "").replace("|", "").replace("?", "").replace(" ", " ").strip()
currentQuality = None
if episode_data["stream"]["quality"] is None:
currentQuality = "stream40"
else:
qualityCount = len(episode_data["stream"]["quality"])
for i in range(qualityCount):
if episode_data["stream"]["quality"][i]["selected"] == "Y":
currentQuality = episode_data["stream"]["quality"][i]["code"]
break
if currentQuality is None:
return
qualityRes = self.__get_quality_to_res(currentQuality)
if 'frequency' in episode_data["content"]:
episodeno = episode_data["content"]["frequency"]
airdate = str(episode_data["content"]["info"]["episode"]["broadcast_date"])[2:]
if episodeno > 0:
ret = f"{title}.E{str(episodeno).zfill(2)}.{airdate}.{qualityRes}-ST.mp4"
else:
ret = f"{title}.{airdate}.{qualityRes}-ST.mp4"
else:
ret = f"{title}.{qualityRes}-ST.mp4"
#if episode_data['drm']:
# ret = ret.replace('.mp4', '.mkv')
from support.base import SupportFile
return SupportFile.text_for_filename(ret)
except Exception as e:
logger.error(f"Exception:{str(e)}")
logger.error(traceback.format_exc())
def __get_quality_to_res(self, quality):
if quality == 'stream50':
return '1080p'
elif quality == 'stream40':
return '720p'
elif quality == 'stream30':
return '480p'
elif quality == 'stream70':
return '2160p'
elif quality == 'stream25':
return '270p'
return '1080p'
def get_quality_to_tving(self, quality):
if quality == 'FHD':
return 'stream50'
elif quality == 'HD':
return 'stream40'
elif quality == 'SD':
return 'stream30'
elif quality == 'UHD':
return 'stream70'
return 'stream50'
def __decrypt2(self, mediacode, ts, url):
try:
#raise Exception('test')
import sc
ret = sc.td1(mediacode, str(ts), url).strip()
#data = sc.td1(code, ts, url)
ret = re.sub('[^ -~]+', '', ret)
#logger.error(f"[{ret}]")
return ret
except Exception as e:
logger.error(f"Exception:{str(e)}")
#logger.error(traceback.format_exc())
data = {'url':url, 'code':mediacode, 'ts':ts}
ret = requests.post('https://sjva.me/sjva/tving.php', data=data).json()
return ret['url']
if __name__ == '__main__':
import argparse
#from support.base import d, get_logger
from lib_wvtool import WVDownloader
parser = argparse.ArgumentParser()
parser.add_argument('--code', required=True, help='컨텐츠 코드')
parser.add_argument('--quality', required=False, default='stream50', help='화질')
parser.add_argument('--token', required=True,)
parser.add_argument('--proxy', default=None)
parser.add_argument('--deviceid', default=None)
parser.add_argument('--folder_tmp', default=None)
parser.add_argument('--folder_output', default=None)
args = parser.parse_args()
info = SupportTving(token=args.token, proxy=args.proxy, deviceid=args.deviceid).get_info(args.code, args.quality)
logger.debug(d(info['play_info']))
if info['drm']:
SupportTving.headers['Cookie'] = f"_tving_token={args.token}"
downloader = WVDownloader({
'logger' : logger,
'mpd_url' : info['play_info']['uri'],
'code' : args.code,
'output_filename' : info['filename'],
'license_headers' : info['play_info']['drm_key_request_properties'],
'license_url' : info['play_info']['drm_license_uri'],
'clean' : True,
'folder_output': args.folder_output,
'folder_tmp': args.folder_tmp,
'mpd_headers' : SupportTving.headers
})
downloader.download()
else:
logger.error("DRM 영상이 아닙니다.")
#print(args)
+1
View File
@@ -0,0 +1 @@
from .gsheet_base import GoogleSheetBase
+1
View File
@@ -0,0 +1 @@
{"installed":{"client_id":"78061934091-l4m6ba5jip749lb4stk00jg8vf2tcsmq.apps.googleusercontent.com","project_id":"sjva-plex-scan-200106","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"qb0NiC8JahlPggbHZJSF7xVJ","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
+212
View File
@@ -0,0 +1,212 @@
import os, sys, traceback
try:
import oauth2client
except:
os.system('pip install oauth2client')
import oauth2client
from oauth2client.file import Storage
from oauth2client import tools
from oauth2client.client import flow_from_clientsecrets
try:
from apiclient.discovery import build
except:
os.system('pip install google-api-python-client')
from apiclient.discovery import build
try:
import gspread, time
from gspread_formatting import cellFormat, textFormat, color, format_cell_range
except:
os.system('pip3 install gspread')
os.system('pip3 install gspread_formatting')
import gspread, time
from gspread_formatting import cellFormat, textFormat, color, format_cell_range
from support.base import get_logger, d
logger = get_logger()
class GoogleSheetBase:
current_flow = None
color_format = {
'green' : cellFormat(
backgroundColor=color(0, 1, 0), #set it to yellow
textFormat=textFormat(foregroundColor=color(0, 0, 0)),
),
'yellow' : cellFormat(
backgroundColor=color(1, 1, 0), #set it to yellow
textFormat=textFormat(foregroundColor=color(0, 0, 0)),
),
'white' : cellFormat(
backgroundColor=color(1, 1, 1), #set it to yellow
textFormat=textFormat(foregroundColor=color(0, 0, 0)),
)
}
def __init__(self, doc_id, credentials_filepath, tab_index, unique_header):
self.credentials_filepath = credentials_filepath
self.credentials = self.get_credentials()
self.doc_id = doc_id
doc_url = f'https://docs.google.com/spreadsheets/d/{doc_id}'
gsp = gspread.authorize(self.credentials)
doc = gsp.open_by_url(doc_url)
self.tab_index = tab_index
self.ws = doc.get_worksheet(tab_index)
self.header_info = None
self.header_info_reverse = None
self.unique_header = unique_header
def get_credentials(self, project_filepath=None):
if os.path.exists(self.credentials_filepath) == False:
logger.info(f"credentials_filepath : {self.credentials_filepath}")
url = self.__make_token_cli(project_filepath)
logger.debug(f"Auth URL : {url}")
code = input("Input Code : ")
self.__save_token(self.credentials_filepath, code)
store = Storage(self.credentials_filepath)
credentials = store.get()
if not credentials or credentials.invalid:
logger.warning('credentials error')
#flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
#creds = tools.run_flow(flow, store)
os.remove(self.credentials_filepath)
return self.get_credentials(self.credentials_filepath)
return credentials
def __make_token_cli(self, project_filepath):
try:
if project_filepath == None:
project_filepath = os.path.join(os.path.dirname(__file__), 'cs.json')
self.current_flow = flow_from_clientsecrets(
project_filepath, # downloaded file
'https://www.googleapis.com/auth/drive', # scope
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
return self.current_flow.step1_get_authorize_url()
except Exception as e:
logger.error(f"Exception: {e}")
logger.error(traceback.format_exc())
def __save_token(self, credentials_filepath, code):
try:
credentials = self.current_flow.step2_exchange(code)
storage = Storage(credentials_filepath)
storage.put(credentials)
return True
except Exception as e:
logger.error(f"Exception: {e}")
logger.error(traceback.format_exc())
return False
def get_sheet_data(self):
tmp = self.ws.get_all_values()#[:-1]
self.set_sheet_header(tmp[0])
rows = tmp[1:]
ret = []
for row in rows:
item = {}
for idx, col in enumerate(row):
item[self.header_info_reverse[idx+1]] = col
ret.append(item)
return ret
def set_sheet_header(self, row):
self.header_info = {}
self.header_info_reverse = {}
for idx, col in enumerate(row):
self.header_info[col] = idx + 1
self.header_info_reverse[idx+1] = col
logger.debug(self.header_info)
def find_row_index(self, total_data, data):
find_row_index = -1
#find = False
#data['IDX'] = len(total_data)+1
for idx, item in enumerate(total_data):
if item[self.unique_header] == str(data[self.unique_header]):
#find = True
find_row_index = idx
#data['IDX'] = find_row_index + 1
break
if find_row_index == -1:
data['IDX'] = len(total_data)+1
return find_row_index
def sleep(self):
time.sleep(0.5)
def sleep_exception(self):
time.sleep(10)
def after_update_cell(self, sheet_row_index, sheet_col_index, key, value, old_value):
pass
def set_color(self, sheet_row, sheet_col1, sheet_col2, color):
format_cell_range(self.ws, gspread.utils.rowcol_to_a1(sheet_row,sheet_col1)+':' + gspread.utils.rowcol_to_a1(sheet_row,sheet_col2), color)
def set_color_row(self, sheet_row, color):
format_cell_range(self.ws, gspread.utils.rowcol_to_a1(sheet_row,1)+':' + gspread.utils.rowcol_to_a1(sheet_row,len(self.header_info)), color)
def set_color_cell(self, sheet_row, sheet_col, color):
format_cell_range(self.ws, gspread.utils.rowcol_to_a1(sheet_row,sheet_col)+':' + gspread.utils.rowcol_to_a1(sheet_row,sheet_col), color)
def write_data(self, total_data, data):
find_row_index = self.find_row_index(total_data, data)
write_count = 0
for key, value in data.items():
if key.startswith('_'):
continue
if value == None:
continue
if key not in self.header_info:
continue
while True:
try:
if find_row_index != -1 and str(total_data[find_row_index][key]) != str(value):
logger.warning(f"업데이트 : {key} {total_data[find_row_index][key]} ==> {value}")
self.ws.update_cell(find_row_index+2, self.header_info[key], value)
self.after_update_cell(find_row_index+2, self.header_info[key], key, value, total_data[find_row_index][key])
write_count += 1
self.sleep()
elif find_row_index == -1 and value != '':
logger.warning(f"추가 : {key} {value}")
self.ws.update_cell(len(total_data)+2, self.header_info[key], value)
self.after_update_cell(len(total_data)+2, self.header_info[key], key, value, None)
write_count += 1
self.sleep()
break
except gspread.exceptions.APIError:
self.sleep_exception()
except Exception as exception:
logger.error(f"{key} - {value}")
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error(self.header_info)
self.sleep_exception()
if find_row_index == -1:
total_data.append(data)
else:
total_data[find_row_index] = data
return write_count
+275
View File
@@ -0,0 +1,275 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import logging
from datetime import datetime
import string
import random
import json
# third-party
import requests
from flask import Blueprint, request, Response, send_file, render_template, redirect, jsonify
from flask_login import login_user, logout_user, current_user, login_required
from framework import F, frame, app, db, scheduler, VERSION, path_app_root, logger, Job, User
from framework.util import Util
# 패키지
from .model import ModelSetting
import system
#########################################################
class SystemLogic(object):
point = 0
db_default = {
'db_version' : '1',
'port' : '9999',
'ddns' : 'http://localhost:9999',
#'url_filebrowser' : 'http://localhost:9998',
#'url_celery_monitoring' : 'http://localhost:9997',
'id' : 'admin',
'pw' : '//nCv0/YkVI3U2AAgYwOuJ2hPlQ7cDYIbuaCt4YJupY=',
'system_start_time' : '',
'repeat' : '',
'auto_restart_hour' : '12',
#'unique' : '',
'theme' : 'Cerulean',
'log_level' : '10',
'use_login' : 'False',
'link_json' : '[{"type":"link","title":"위키","url":"https://sjva.me/wiki/public/start"}]',
'plugin_dev_path': '',
'plugin_tving_level2' : 'False',
'web_title' : 'Home',
'my_ip' : '',
'wavve_guid' : '',
#인증
'auth_use_apikey' : 'False',
'auth_apikey' : '',
#'hide_menu' : 'True',
#Selenium
'selenium_remote_url' : '',
'selenium_remote_default_option' : '--no-sandbox\n--disable-gpu',
'selenium_binary_default_option' : '',
# notify
'notify_telegram_use' : 'False',
'notify_telegram_token' : '',
'notify_telegram_chat_id' : '',
'notify_telegram_disable_notification' : 'False',
'notify_discord_use' : 'False',
'notify_discord_webhook' : '',
'notify_advaned_use' : 'False',
'notify_advaned_policy' : u"# 각 플러그인 설정 설명에 명시되어 있는 ID = 형식\n# DEFAULT 부터 주석(#) 제거 후 작성\n\n# DEFAULT = ",
# telegram
'telegram_bot_token' : '',
'telegram_bot_auto_start' : 'False',
'telegram_resend' : 'False',
'telegram_resend_chat_id' : '',
# 홈페이지 연동 2020-06-07
'sjva_me_user_id' : '',
'auth_status' : '',
'sjva_id' : '',
# memo
'memo' : '',
# tool - decrypt
'tool_crypt_use_user_key' : 'False',
'tool_crypt_user_key' : '',
'tool_crypt_encrypt_word' : '',
'tool_crypt_decrypt_word' : '',
'use_beta' : 'False',
}
@staticmethod
def get_info():
info = {}
import platform
info['platform'] = platform.platform()
info['processor'] = platform.processor()
import sys
info['python_version'] = sys.version
info['version'] = VERSION
info['recent_version'] = SystemLogic.recent_version
info['path_app_root'] = path_app_root
info['running_type'] = u'%s. 비동기 작업 : %s' % (frame.config['running_type'], u"사용" if frame.config['use_celery'] else "미사용")
import system
info['auth'] = frame.config['member']['auth_desc']
info['cpu_percent'] = 'not supported'
info['memory'] = 'not supported'
info['disk'] = 'not supported'
if frame.config['running_type'] != 'termux':
try:
import psutil
from framework.util import Util
info['cpu_percent'] = '%s %%' % psutil.cpu_percent()
tmp = psutil.virtual_memory()
#info['memory'] = [Util.sizeof_fmt(tmp[0], suffix='B'), Util.sizeof_fmt(tmp[3]), Util.sizeof_fmt(tmp[1]), tmp[2]]
info['memory'] = u'전체 : %s 사용량 : %s 남은량 : %s (%s%%)' % (Util.sizeof_fmt(tmp[0], suffix='B'), Util.sizeof_fmt(tmp[3], suffix='B'), Util.sizeof_fmt(tmp[1], suffix='B'), tmp[2])
except:
pass
try:
import platform
if platform.system() == 'Windows':
s = os.path.splitdrive(path_app_root)
root = s[0]
else:
root = '/'
tmp = psutil.disk_usage(root)
info['disk'] = u'전체 : %s 사용량 : %s 남은량 : %s (%s%%) - 드라이브 (%s)' % (Util.sizeof_fmt(tmp[0], suffix='B'), Util.sizeof_fmt(tmp[1], suffix='B'), Util.sizeof_fmt(tmp[2], suffix='B'), tmp[3], root)
except Exception as exception:
pass
try:
tmp = SystemLogic.get_setting_value('system_start_time')
#logger.debug('SYSTEM_START_TIME:%s', tmp)
tmp_datetime = datetime.strptime(tmp, '%Y-%m-%d %H:%M:%S')
timedelta = datetime.now() - tmp_datetime
info['time'] = u'시작 : %s 경과 : %s 재시작 : %s' % (tmp, str(timedelta).split('.')[0], frame.config['arg_repeat'])
except Exception as exception:
info['time'] = str(exception)
return info
@staticmethod
def setting_save_system(req):
try:
for key, value in req.form.items():
logger.debug('Key:%s Value:%s', key, value)
entity = db.session.query(ModelSetting).filter_by(key=key).with_for_update().first()
entity.value = value
#if key == 'theme':
# SystemLogic.change_theme(value)
db.session.commit()
lists = ModelSetting.query.all()
SystemLogic.setting_list = Util.db_list_to_dict(lists)
frame.users[db.session.query(ModelSetting).filter_by(key='id').first().value] = User(db.session.query(ModelSetting).filter_by(key='id').first().value, passwd_hash=db.session.query(ModelSetting).filter_by(key='pw').first().value)
SystemLogic.set_restart_scheduler()
frame.set_level(int(db.session.query(ModelSetting).filter_by(key='log_level').first().value))
return True
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return False
@staticmethod
def setting_save_after():
try:
frame.users[ModelSetting.get('id')] = User(ModelSetting.get('id'), passwd_hash=ModelSetting.get('pw'))
SystemLogic.set_restart_scheduler()
frame.set_level(int(db.session.query(ModelSetting).filter_by(key='log_level').first().value))
from .logic_site import SystemLogicSite
SystemLogicSite.get_daum_cookies(force=True)
SystemLogicSite.create_tving_instance()
return True
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return False
@staticmethod
def change_theme(theme):
try:
source = os.path.join(path_app_root, 'static', 'css', 'theme', '%s_bootstrap.min.css' % theme)
target = os.path.join(path_app_root, 'static', 'css', 'bootstrap.min.css')
os.remove(target)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return False
@staticmethod
def get_setting_value(key):
try:
#logger.debug('get_setting_value:%s', key)
entity = db.session.query(ModelSetting).filter_by(key=key).first()
if entity is None:
return None
else:
return entity.value
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('error key : %s', key)
return False
@staticmethod
def command_run(command_text):
try:
ret = {}
tmp = command_text.strip().split(' ')
if not tmp:
ret['ret'] = 'success'
ret['log'] = 'Empty..'
return ret
if tmp[0] == 'set':
if len(tmp) == 3:
if tmp[1] == 'token':
tmp[1] = 'unique'
entity = db.session.query(ModelSetting).filter_by(key=tmp[1]).with_for_update().first()
if entity is None:
ret['ret'] = 'fail'
ret['log'] = '%s not exist' % tmp[1]
return ret
entity.value = tmp[2] if tmp[2] != 'EMPTY' else ""
db.session.commit()
ret['ret'] = 'success'
ret['log'] = '%s - %s' % (tmp[1], tmp[2])
return ret
if tmp[0] == 'set2':
if tmp[1] == 'klive':
from klive import ModelSetting as KLiveModelSetting
if KLiveModelSetting.get(tmp[2]) is not None:
KLiveModelSetting.set(tmp[2], tmp[3])
ret['ret'] = 'success'
ret['log'] = f'KLive 설정 값 변경 : {tmp[2]} - {tmp[3]}'
return ret
ret['ret'] = 'fail'
ret['log'] = 'wrong command'
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
ret['ret'] = 'fail'
ret['log'] = str(exception)
return ret
@staticmethod
def link_save(link_data_str):
try:
data = json.loads(link_data_str)
entity = db.session.query(ModelSetting).filter_by(key='link_json').with_for_update().first()
entity.value = link_data_str
db.session.commit()
SystemLogic.apply_menu_link()
return True
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return False
+14
View File
@@ -0,0 +1,14 @@
"""
from .plugin import blueprint, menu, plugin_load, plugin_unload, restart, shutdown
from .logic import SystemLogic
from .model import ModelSetting
from .model import ModelSetting as SystemModelSetting
from .logic_plugin import LogicPlugin
from .logic_selenium import SystemLogicSelenium
from .logic_command import SystemLogicCommand
from .logic_site import SystemLogicSite
"""
+160
View File
@@ -0,0 +1,160 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import random
import json
import string
import codecs
# third-party
import requests
from flask import Blueprint, request, Response, send_file, render_template, redirect, jsonify
# sjva 공용
from framework import frame, path_app_root, app
from framework.util import Util
# 패키지
from .plugin import package_name, logger
from .model import ModelSetting
class SystemLogicAuth(object):
@staticmethod
def process_ajax(sub, req):
logger.debug(sub)
try:
if sub == 'apikey_generate':
ret = SystemLogicAuth.apikey_generate()
return jsonify(ret)
elif sub == 'do_auth':
ret = SystemLogicAuth.do_auth()
return jsonify(ret)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
##########################################################################
@staticmethod
def get_auth_status(retry=True):
try:
value = ModelSetting.get('auth_status')
ret = {'ret' : False, 'desc' : '', 'level' : 0, 'point': 0}
if value == '':
ret['desc'] = '미인증'
elif value == 'wrong_id':
ret['desc'] = '미인증 - 홈페이지 아이디가 없습니다.'
elif value == 'too_many_sjva':
ret['desc'] = '미인증 - 너무 많은 SJVA를 사용중입니다.'
elif value == 'wrong_apikey':
ret['desc'] = '미인증 - 홈페이지에 등록된 APIKEY와 다릅니다.'
elif value == 'auth_status':
ret['desc'] = '인증 실패'
else:
status = SystemLogicAuth.check_auth_status(value)
if status is not None and status['ret']:
ret['ret'] = status['ret']
ret['desc'] = '인증되었습니다. (회원등급:%s, 포인트:%s)' % (status['level'], status['point'])
ret['level'] = status['level']
ret['point'] = status['point']
else:
if retry:
SystemLogicAuth.do_auth()
#ModelSetting.set('auth_status', SystemLogicAuth.make_auth_status())
return SystemLogicAuth.get_auth_status(retry=False)
else:
ret['desc'] = '잘못된 값입니다. 다시 인증하세요.'
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def check_auth_status(value=None):
try:
from support.base.aes import SupportAES
mykey=(codecs.encode(SystemLogicAuth.get_ip().encode(), 'hex').decode() + codecs.encode(ModelSetting.get('auth_apikey').encode(), 'hex').decode()).zfill(32)[:32].encode()
logger.debug(mykey)
tmp = SupportAES.decrypt(value, mykey=mykey)
tmp = tmp.split('_')
ret = {}
ret['ret'] = (ModelSetting.get('sjva_id') == tmp[0])
ret['level'] = int(tmp[1])
ret['point'] = int(tmp[2])
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def make_auth_status(level, point):
try:
from support.base import SupportAES
mykey=(codecs.encode(SystemLogicAuth.get_ip().encode(), 'hex').decode() + codecs.encode(ModelSetting.get('auth_apikey').encode(), 'hex').decode()).zfill(32)[:32].encode()
ret = SupportAES.encrypt(str('%s_%s_%s' % (ModelSetting.get('sjva_id'), level, point)), mykey=mykey)
logger.debug(ret)
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def get_ip():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
logger.debug('IP:%s', IP)
return IP
@staticmethod
def do_auth():
try:
ret = {'ret':False, 'msg':'', 'level':0, 'point':0}
apikey = ModelSetting.get('auth_apikey')
user_id = ModelSetting.get('sjva_me_user_id')
if len(apikey) != 10:
ret['msg'] = 'APIKEY 문자 길이는 10자리여야합니다.'
return ret
if user_id == '':
ret['msg'] = '홈페이지 ID가 없습니다.'
return ret
data = requests.post(f"{frame.config['DEFINE']['WEB_DIRECT_URL']}/sjva/auth.php", data={'apikey':apikey,'user_id':user_id, 'sjva_id':ModelSetting.get('sjva_id')}).json()
if data['result'] == 'success':
ret['ret'] = True
ret['msg'] = u'%s개 등록<br>회원등급:%s, 포인트:%s' % (data['count'], data['level'], data['point'])
ret['level'] = int(data['level'])
ret['point'] = int(data['point'])
ModelSetting.set('auth_status', SystemLogicAuth.make_auth_status(ret['level'], ret['point']))
else:
ModelSetting.set('auth_status', data['result'])
tmp = SystemLogicAuth.get_auth_status(retry=False)
ret['ret'] = tmp['ret']
ret['msg'] = tmp['desc']
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
ret['msg'] = '인증 실패'
ret['level'] = -1
ret['point'] = -1
ModelSetting.set('auth_status', 'auth_fail')
return ret
+246
View File
@@ -0,0 +1,246 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import logging
import platform
import subprocess
import threading
import sys
import io
import time
import json
import queue
# third-party
# sjva 공용
from framework import path_app_root, socketio, app, logger
# 패키지
class SystemLogicCommand(object):
commands = None
process = None
stdout_queue = None
thread = None
send_to_ui_thread = None
return_log = None
@staticmethod
def start(title, commands, clear=True, wait=False, show_modal=True):
try:
if show_modal:
if clear:
socketio.emit("command_modal_clear", None, namespace='/framework', broadcast=True)
SystemLogicCommand.return_log = []
SystemLogicCommand.title = title
SystemLogicCommand.commands = commands
SystemLogicCommand.thread = threading.Thread(target=SystemLogicCommand.execute_thread_function, args=(show_modal,))
SystemLogicCommand.thread.setDaemon(True)
SystemLogicCommand.thread.start()
if wait:
time.sleep(1)
SystemLogicCommand.thread.join()
return SystemLogicCommand.return_log
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def execute_thread_function(show_modal):
try:
#if wait:
if show_modal:
socketio.emit("loading_hide", None, namespace='/framework', broadcast=True)
for command in SystemLogicCommand.commands:
#logger.debug('Command :%s', command)
if command[0] == 'msg':
if show_modal:
socketio.emit("command_modal_add_text", '%s\n\n' % command[1], namespace='/framework', broadcast=True)
elif command[0] == 'system':
if show_modal:
socketio.emit("command_modal_add_text", '$ %s\n\n' % command[1], namespace='/framework', broadcast=True)
os.system(command[1])
else:
show_command = True
if command[0] == 'hide':
show_command = False
command = command[1:]
#SystemLogicCommand.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1)
SystemLogicCommand.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf8')
SystemLogicCommand.start_communicate(command, show_command=show_command)
SystemLogicCommand.send_queue_start(show_modal)
if SystemLogicCommand.process is not None:
SystemLogicCommand.process.wait()
time.sleep(1)
except Exception as exception:
#logger.error('Exception:%s', exception)
#logger.error(traceback.format_exc())
if show_modal:
socketio.emit("command_modal_show", SystemLogicCommand.title, namespace='/framework', broadcast=True)
socketio.emit("command_modal_add_text", str(exception), namespace='/framework', broadcast=True)
socketio.emit("command_modal_add_text", str(traceback.format_exc()), namespace='/framework', broadcast=True)
@staticmethod
def start_communicate(current_command, show_command=True):
SystemLogicCommand.stdout_queue = queue.Queue()
if show_command:
SystemLogicCommand.stdout_queue.put('$ %s\n' % ' '.join(current_command))
sout = io.open(SystemLogicCommand.process.stdout.fileno(), 'rb', closefd=False)
#serr = io.open(process.stderr.fileno(), 'rb', closefd=False)
def Pump(stream):
queue = queue.Queue()
def rdr():
logger.debug('START RDR')
while True:
buf = SystemLogicCommand.process.stdout.read(1)
if buf:
queue.put( buf )
else:
queue.put( None )
break
logger.debug('END RDR')
queue.put( None )
time.sleep(1)
#Logic.command_close()
def clct():
active = True
logger.debug('START clct')
while active:
r = queue.get()
if r is None:
break
try:
while True:
r1 = queue.get(timeout=0.005)
if r1 is None:
active = False
break
else:
r += r1
except:
pass
if r is not None:
try:
r = r.decode('utf-8')
except Exception as exception:
#logger.error('Exception:%s', e)
#logger.error(traceback.format_exc())
try:
r = r.decode('cp949')
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
try:
r = r.decode('euc-kr')
except:
pass
SystemLogicCommand.stdout_queue.put(r)
#SystemLogicCommand.return_log.append(r)
SystemLogicCommand.return_log += r.split('\n')
logger.debug('IN:%s', r)
SystemLogicCommand.stdout_queue.put('<END>')
logger.debug('END clct')
#Logic.command_close()
for tgt in [rdr, clct]:
th = threading.Thread(target=tgt)
th.setDaemon(True)
th.start()
Pump(sout)
#Pump(serr, 'stderr')
@staticmethod
def send_queue_start(show_modal):
def send_to_ui_thread_function():
logger.debug('send_queue_thread_function START')
if show_modal:
socketio.emit("command_modal_show", SystemLogicCommand.title, namespace='/framework', broadcast=True)
while SystemLogicCommand.stdout_queue:
line = SystemLogicCommand.stdout_queue.get()
logger.debug('Send to UI :%s', line)
if line == '<END>':
if show_modal:
socketio.emit("command_modal_add_text", "\n", namespace='/framework', broadcast=True)
break
else:
if show_modal:
socketio.emit("command_modal_add_text", line, namespace='/framework', broadcast=True)
SystemLogicCommand.send_to_ui_thread = None
SystemLogicCommand.stdout_queue = None
SystemLogicCommand.process = None
logger.debug('send_to_ui_thread_function END')
if SystemLogicCommand.send_to_ui_thread is None:
SystemLogicCommand.send_to_ui_thread = threading.Thread(target=send_to_ui_thread_function, args=())
SystemLogicCommand.send_to_ui_thread.start()
@staticmethod
def plugin_unload():
try:
if SystemLogicCommand.process is not None and SystemLogicCommand.process.poll() is None:
import psutil
process = psutil.Process(SystemLogicCommand.process.pid)
for proc in SystemLogicCommand.process.children(recursive=True):
proc.kill()
SystemLogicCommand.process.kill()
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
##################################
# 외부 호출
@staticmethod
def execute_command_return(command, format=None, force_log=False):
from tool_base import ToolSubprocess
return ToolSubprocess.execute_command_return(command, format=format, force_log=force_log)
"""
try:
logger.debug('execute_command_return : %s', ' '.join(command))
if app.config['config']['running_type'] == 'windows':
command = ' '.join(command)
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf8')
ret = []
with process.stdout:
for line in iter(process.stdout.readline, ''):
ret.append(line.strip())
if force_log:
logger.debug(ret[-1])
process.wait() # wait for the subprocess to exit
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 exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('command : %s', command)
"""
+209
View File
@@ -0,0 +1,209 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import logging
import platform
import subprocess
import threading
import sys
import io
import time
import json
import queue
# third-party
# sjva 공용
from framework import path_app_root, socketio, logger, app
# 패키지
# 로그
package_name = __name__.split('.')[0]
#logger = get_logger(package_name)
#########################################################
class SystemLogicCommand2(object):
instance_list = []
def __init__(self, title, commands, clear=True, wait=False, show_modal=True):
self.title = title
self.commands = commands
self.clear = clear
self.wait = wait
self.show_modal = show_modal
self.process = None
self.stdout_queue = None
self.thread = None
self.send_to_ui_thread = None
self.return_log = []
SystemLogicCommand2.instance_list.append(self)
def start(self):
try:
if self.show_modal:
if self.clear:
socketio.emit("command_modal_clear", None, namespace='/framework', broadcast=True)
self.thread = threading.Thread(target=self.execute_thread_function, args=())
self.thread.setDaemon(True)
self.thread.start()
if self.wait:
time.sleep(1)
self.thread.join()
return self.return_log
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
def execute_thread_function(self):
try:
#if wait:
if self.show_modal:
socketio.emit("command_modal_show", self.title, namespace='/framework', broadcast=True)
socketio.emit("loading_hide", None, namespace='/framework', broadcast=True)
for command in self.commands:
if command[0] == 'msg':
if self.show_modal:
socketio.emit("command_modal_add_text", '%s\n\n' % command[1], namespace='/framework', broadcast=True)
elif command[0] == 'system':
if self.show_modal:
socketio.emit("command_modal_add_text", '$ %s\n\n' % command[1], namespace='/framework', broadcast=True)
os.system(command[1])
else:
show_command = True
if command[0] == 'hide':
show_command = False
command = command[1:]
#self.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1)
self.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf8')
self.start_communicate(command, show_command=show_command)
self.send_queue_start()
if self.process is not None:
self.process.wait()
time.sleep(1)
except Exception as exception:
if self.show_modal:
socketio.emit("command_modal_show", self.title, namespace='/framework', broadcast=True)
socketio.emit("command_modal_add_text", str(exception), namespace='/framework', broadcast=True)
socketio.emit("command_modal_add_text", str(traceback.format_exc()), namespace='/framework', broadcast=True)
def start_communicate(self, current_command, show_command=True):
self.stdout_queue = queue.Queue()
if show_command:
self.stdout_queue.put('$ %s\n' % ' '.join(current_command))
sout = io.open(self.process.stdout.fileno(), 'rb', closefd=False)
#serr = io.open(process.stderr.fileno(), 'rb', closefd=False)
def Pump(stream):
queue = queue.Queue()
def rdr():
#logger.debug('START RDR')
while True:
buf = self.process.stdout.read(1)
if buf:
queue.put( buf )
else:
queue.put( None )
break
#logger.debug('END RDR')
queue.put( None )
time.sleep(1)
#Logic.command_close()
def clct():
active = True
#logger.debug('START clct')
while active:
r = queue.get()
if r is None:
break
try:
while True:
r1 = queue.get(timeout=0.005)
if r1 is None:
active = False
break
else:
r += r1
except:
pass
if r is not None:
if app.config['config']['is_py2']:
try:
r = r.decode('utf-8')
except Exception as exception:
#logger.error('Exception:%s', e)
#logger.error(traceback.format_exc())
try:
r = r.decode('cp949')
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
try:
r = r.decode('euc-kr')
except:
pass
self.stdout_queue.put(r)
self.return_log += r.split('\n')
#logger.debug('IN:%s', r)
self.stdout_queue.put('<END>')
#logger.debug('END clct')
#Logic.command_close()
for tgt in [rdr, clct]:
th = threading.Thread(target=tgt)
th.setDaemon(True)
th.start()
Pump(sout)
#Pump(serr, 'stderr')
def send_queue_start(self):
def send_to_ui_thread_function():
#logger.debug('send_queue_thread_function START')
if self.show_modal:
socketio.emit("command_modal_show", self.title, namespace='/framework', broadcast=True)
while self.stdout_queue:
line = self.stdout_queue.get()
#logger.debug('Send to UI :%s', line)
if line == '<END>':
if self.show_modal:
socketio.emit("command_modal_add_text", "\n", namespace='/framework', broadcast=True)
break
else:
if self.show_modal:
socketio.emit("command_modal_add_text", line, namespace='/framework', broadcast=True)
self.send_to_ui_thread = None
self.stdout_queue = None
self.process = None
#logger.debug('send_to_ui_thread_function END')
if self.send_to_ui_thread is None:
self.send_to_ui_thread = threading.Thread(target=send_to_ui_thread_function, args=())
self.send_to_ui_thread.start()
@classmethod
def plugin_unload(cls):
for instance in cls.instance_list:
try:
if instance.process is not None and instance.process.poll() is None:
import psutil
process = psutil.Process(instance.process.pid)
for proc in instance.process.children(recursive=True):
proc.kill()
instance.process.kill()
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
finally:
try: instance.process.kill()
except: pass
+129
View File
@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
#########################################################
# python
import os
import traceback
import logging
import platform
import time
import threading
# third-party
from flask import Blueprint, request, Response, send_file, render_template, redirect, jsonify
# sjva 공용
from framework import F, path_app_root, path_data, celery, app
# 패키지
from .plugin import logger, package_name
from .model import ModelSetting
class SystemLogicEnv(object):
@staticmethod
def load_export():
try:
from support.base.file import SupportFile
f = os.path.join(path_app_root, 'export.sh')
if os.path.exists(f):
return SupportFile.read_file(f)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@staticmethod
def process_ajax(sub, req):
ret = False
try:
if sub == 'setting_save':
data = req.form['export']
#logger.debug(data)
data = data.replace("\r\n", "\n" ).replace( "\r", "\n" )
ret = False
if platform.system() != 'Windows':
f = os.path.join(path_app_root, 'export.sh')
with open(f, 'w') as f:
f.write(data)
#os.system("dos2unix export.sh")
ret = True
elif sub == 'ps':
def func():
import system
commands = [
['msg', u'잠시만 기다려주세요.'],
['ps', '-ef'],
['top', '-n1']
]
#commands.append(['msg', u'설치가 완료되었습니다.'])
system.SystemLogicCommand.start('ps', commands)
t = threading.Thread(target=func, args=())
t.setDaemon(True)
t.start()
elif sub == 'celery_test':
ret = SystemLogicEnv.celery_test()
elif sub == 'worker_start':
os.system('sh worker_start.sh &')
"""
def func():
import system
commands = [
['msg', u'잠시만 기다려주세요.'],
['sh', 'worker_start.sh'],
]
#commands.append(['msg', u'설치가 완료되었습니다.'])
system.SystemLogicCommand.start('ps', commands)
t = threading.Thread(target=func, args=())
t.setDaemon(True)
t.start()
"""
ret = True
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
return jsonify(ret)
@staticmethod
def celery_test():
if F.config['use_celery']:
from celery import Celery
from celery.exceptions import TimeoutError, NotRegistered
data = {}
try:
result = SystemLogicEnv.celery_test2.apply_async()
logger.debug(result)
try:
tmp = result.get(timeout=5, propagate=True)
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
#result = SystemLogicEnv.celery_test2.delay()
data['ret'] = 'success'
data['data'] = tmp
except TimeoutError:
data['ret'] = 'timeout'
data['data'] = u'celery가 동작중이 아니거나 모든 프로세스가 작업중입니다.'
except NotRegistered:
data['ret'] = 'not_registered'
data['data'] = u'Not Registered'
#logger.debug(data)
else:
data['ret'] = 'no_celery'
data['data'] = u'celery 실행환경이 아닙니다.'
return data
@staticmethod
@celery.task
def celery_test2():
try:
logger.debug('!!!! celery_test2222')
import time
time.sleep(1)
data = u'정상입니다. 이 메시지는 celery 에서 반환됩니다. '
return data
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())

Some files were not shown because too many files have changed in this diff Show More