2018-10-10 22:43:50 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
from shutil import copyfile
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
from django.conf import settings as dj_settings
|
2018-10-10 22:43:50 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from .models import UserSettings
|
|
|
|
from .utils.customconfigparser import ConfigParserWithEnv
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
__SETTINGS_FILE = 'config.ini'
|
|
|
|
__LOG_FILE = 'log.log'
|
|
|
|
__LOG_FORMAT = '%(asctime)s|%(process)d|%(thread)d|%(name)s|%(filename)s|%(lineno)d|%(levelname)s|%(message)s'
|
|
|
|
|
|
|
|
__DEFAULT_SETTINGS = {
|
|
|
|
'global': {
|
|
|
|
'YouTubeApiKey': 'AIzaSyBabzE4Bup77WexdLMa9rN9z-wJidEfNX8',
|
|
|
|
'SynchronizationSchedule': '0 * * * *',
|
|
|
|
'SchedulerConcurrency': '2',
|
|
|
|
},
|
|
|
|
'user': {
|
|
|
|
'MarkDeletedAsWatched': 'True',
|
|
|
|
'DeleteWatched': 'True',
|
|
|
|
'AutoDownload': 'True',
|
|
|
|
'DownloadMaxAttempts': '3',
|
|
|
|
'DownloadGlobalLimit': '',
|
|
|
|
'DownloadSubscriptionLimit': '5',
|
|
|
|
'DownloadOrder': 'playlist_index',
|
|
|
|
'DownloadPath': '${env:USERPROFILE}${env:HOME}/Downloads',
|
|
|
|
'DownloadFilePattern': '${channel}/${playlist}/S01E${playlist_index} - ${title} [${id}]',
|
|
|
|
'DownloadFormat': 'bestvideo+bestaudio',
|
|
|
|
'DownloadSubtitles': 'True',
|
|
|
|
'DownloadAutogeneratedSubtitles': 'False',
|
|
|
|
'DownloadSubtitlesAll': 'False',
|
|
|
|
'DownloadSubtitlesLangs': 'en,ro',
|
|
|
|
'DownloadSubtitlesFormat': '',
|
2018-10-10 22:43:50 +00:00
|
|
|
}
|
2018-10-13 20:01:45 +00:00
|
|
|
}
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
log_path = os.path.join(dj_settings.BASE_DIR, 'config', __LOG_FILE)
|
|
|
|
settings_path = os.path.join(dj_settings.BASE_DIR, 'config', __SETTINGS_FILE)
|
|
|
|
settings = ConfigParserWithEnv(defaults=__DEFAULT_SETTINGS, allow_no_value=True)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def __initialize_logger():
|
|
|
|
log_level_str = settings.get('global', 'LogLevel', fallback='INFO')
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
try:
|
|
|
|
log_level = getattr(logging, log_level_str)
|
|
|
|
logging.basicConfig(filename=log_path, level=log_level, format=__LOG_FORMAT)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
except AttributeError:
|
|
|
|
logging.basicConfig(filename=log_path, level=logging.INFO, format=__LOG_FORMAT)
|
|
|
|
logging.warning('Invalid log level "%s" in config file.', log_level_str)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def initialize_app_config():
|
|
|
|
load_settings()
|
|
|
|
__initialize_logger()
|
|
|
|
logging.info('Application started!')
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def load_settings():
|
|
|
|
if os.path.exists(settings_path):
|
|
|
|
with open(settings_path, 'r') as f:
|
|
|
|
settings.read_file(f)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def save_settings():
|
|
|
|
if os.path.exists(settings_path):
|
|
|
|
# Create a backup
|
|
|
|
copyfile(settings_path, settings_path + ".backup")
|
|
|
|
else:
|
|
|
|
# Ensure directory exists
|
|
|
|
settings_dir = os.path.dirname(settings_path)
|
|
|
|
os.makedirs(settings_dir, exist_ok=True)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
with open(settings_path, 'w') as f:
|
|
|
|
settings.write(f)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def get_user_config(user: User) -> ConfigParserWithEnv:
|
|
|
|
user_settings = UserSettings.find_by_user(user)
|
|
|
|
if user_settings is not None:
|
|
|
|
user_config = ConfigParserWithEnv(defaults=settings, allow_no_value=True)
|
|
|
|
user_config.read_dict({
|
|
|
|
'user': user_settings.to_dict()
|
|
|
|
})
|
|
|
|
return user_config
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
return settings
|