Added custom logger instance to core and import in video.
This commit is contained in:
37
src/core.py
37
src/core.py
@@ -22,6 +22,17 @@ from subtitle import SUBTITLE_EXTENSIONS, Subtitle, get_subtitle_path
|
|||||||
from utils import sanitize, refine
|
from utils import sanitize, refine
|
||||||
|
|
||||||
logging.basicConfig(filename=env.logfile, level=logging.INFO)
|
logging.basicConfig(filename=env.logfile, level=logging.INFO)
|
||||||
|
logger = logging.getLogger('seasonedParser_core')
|
||||||
|
fh = logging.FileHandler(env.logfile)
|
||||||
|
fh.setLevel(logging.INFO)
|
||||||
|
ch = logging.StreamHandler()
|
||||||
|
ch.setLevel(logging.ERROR)
|
||||||
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
fh.setFormatter(formatter)
|
||||||
|
ch.setFormatter(formatter)
|
||||||
|
|
||||||
|
logger.addHandler(fh)
|
||||||
|
logger.addHandler(ch)
|
||||||
|
|
||||||
|
|
||||||
def search_external_subtitles(path, directory=None):
|
def search_external_subtitles(path, directory=None):
|
||||||
@@ -40,10 +51,10 @@ def search_external_subtitles(path, directory=None):
|
|||||||
try:
|
try:
|
||||||
language = Language.fromietf(language_code)
|
language = Language.fromietf(language_code)
|
||||||
except (ValueError, LanguageReverseError):
|
except (ValueError, LanguageReverseError):
|
||||||
logging.error('Cannot parse language code %r', language_code)
|
logger.error('Cannot parse language code %r', language_code)
|
||||||
|
|
||||||
subtitles[p] = language
|
subtitles[p] = language
|
||||||
logging.debug('Found subtitles %r', subtitles)
|
logger.debug('Found subtitles %r', subtitles)
|
||||||
|
|
||||||
return subtitles
|
return subtitles
|
||||||
|
|
||||||
@@ -64,7 +75,7 @@ def scan_video(path):
|
|||||||
raise ValueError('%r is not a valid video extension' % os.path.splitext(path)[1])
|
raise ValueError('%r is not a valid video extension' % os.path.splitext(path)[1])
|
||||||
|
|
||||||
dirpath, filename = os.path.split(path)
|
dirpath, filename = os.path.split(path)
|
||||||
logging.info('Scanning video %r in %r', filename, dirpath)
|
logger.info('Scanning video %r in %r', filename, dirpath)
|
||||||
|
|
||||||
# guess
|
# guess
|
||||||
video = Video.fromguess(path, guessit(path))
|
video = Video.fromguess(path, guessit(path))
|
||||||
@@ -93,7 +104,7 @@ def scan_subtitle(path):
|
|||||||
raise ValueError('Path does not exist')
|
raise ValueError('Path does not exist')
|
||||||
|
|
||||||
dirpath, filename = os.path.split(path)
|
dirpath, filename = os.path.split(path)
|
||||||
logging.info('Scanning subtitle %r in %r', filename, dirpath)
|
logger.info('Scanning subtitle %r in %r', filename, dirpath)
|
||||||
|
|
||||||
# guess
|
# guess
|
||||||
parent_path = path.strip(filename)
|
parent_path = path.strip(filename)
|
||||||
@@ -128,30 +139,30 @@ def scan_videos(path):
|
|||||||
# walk the path
|
# walk the path
|
||||||
videos = []
|
videos = []
|
||||||
for dirpath, dirnames, filenames in os.walk(path):
|
for dirpath, dirnames, filenames in os.walk(path):
|
||||||
logging.debug('Walking directory %r', dirpath)
|
logger.debug('Walking directory %r', dirpath)
|
||||||
|
|
||||||
# remove badly encoded and hidden dirnames
|
# remove badly encoded and hidden dirnames
|
||||||
for dirname in list(dirnames):
|
for dirname in list(dirnames):
|
||||||
if dirname.startswith('.'):
|
if dirname.startswith('.'):
|
||||||
logging.debug('Skipping hidden dirname %r in %r', dirname, dirpath)
|
logger.debug('Skipping hidden dirname %r in %r', dirname, dirpath)
|
||||||
dirnames.remove(dirname)
|
dirnames.remove(dirname)
|
||||||
|
|
||||||
# scan for videos
|
# scan for videos
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
if not (filename.endswith(VIDEO_EXTENSIONS)):
|
if not (filename.endswith(VIDEO_EXTENSIONS)):
|
||||||
logging.debug('Skipping non-video file %s', filename)
|
logger.debug('Skipping non-video file %s', filename)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# skip hidden files
|
# skip hidden files
|
||||||
if filename.startswith('.'):
|
if filename.startswith('.'):
|
||||||
logging.debug('Skipping hidden filename %r in %r', filename, dirpath)
|
logger.debug('Skipping hidden filename %r in %r', filename, dirpath)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# reconstruct the file path
|
# reconstruct the file path
|
||||||
filepath = os.path.join(dirpath, filename)
|
filepath = os.path.join(dirpath, filename)
|
||||||
|
|
||||||
if os.path.islink(filepath):
|
if os.path.islink(filepath):
|
||||||
logging.debug('Skipping link %r in %r', filename, dirpath)
|
logger.debug('Skipping link %r in %r', filename, dirpath)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# scan
|
# scan
|
||||||
@@ -159,7 +170,7 @@ def scan_videos(path):
|
|||||||
try:
|
try:
|
||||||
video = scan_video(filepath)
|
video = scan_video(filepath)
|
||||||
except ValueError: # pragma: no cover
|
except ValueError: # pragma: no cover
|
||||||
logging.exception('Error scanning video')
|
logger.exception('Error scanning video')
|
||||||
continue
|
continue
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
raise ValueError('Unsupported file %r' % filename)
|
raise ValueError('Unsupported file %r' % filename)
|
||||||
@@ -232,14 +243,14 @@ def scan_folder(path):
|
|||||||
videos = []
|
videos = []
|
||||||
ignored_videos = []
|
ignored_videos = []
|
||||||
errored_paths = []
|
errored_paths = []
|
||||||
logging.debug('Collecting path %s', path)
|
logger.debug('Collecting path %s', path)
|
||||||
|
|
||||||
# non-existing
|
# non-existing
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
try:
|
try:
|
||||||
video = Video.fromname(path)
|
video = Video.fromname(path)
|
||||||
except:
|
except:
|
||||||
logging.exception('Unexpected error while collecting non-existing path %s', path)
|
logger.exception('Unexpected error while collecting non-existing path %s', path)
|
||||||
errored_paths.append(path)
|
errored_paths.append(path)
|
||||||
|
|
||||||
video.subtitles |= set(search_external_subtitles(video.name, directory=path))
|
video.subtitles |= set(search_external_subtitles(video.name, directory=path))
|
||||||
@@ -253,7 +264,7 @@ def scan_folder(path):
|
|||||||
try:
|
try:
|
||||||
scanned_videos = scan_videos(path)
|
scanned_videos = scan_videos(path)
|
||||||
except:
|
except:
|
||||||
logging.exception('Unexpected error while collecting directory path %s', path)
|
logger.exception('Unexpected error while collecting directory path %s', path)
|
||||||
errored_paths.append(path)
|
errored_paths.append(path)
|
||||||
|
|
||||||
# Iterates over our scanned videos
|
# Iterates over our scanned videos
|
||||||
|
|||||||
@@ -7,9 +7,12 @@
|
|||||||
|
|
||||||
from guessit import guessit
|
from guessit import guessit
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
from titlecase import titlecase
|
from titlecase import titlecase
|
||||||
import hashlib, tvdb_api
|
import hashlib, tvdb_api
|
||||||
|
|
||||||
|
logger = logging.getLogger('seasonedParser_core')
|
||||||
|
|
||||||
#: Video extensions
|
#: Video extensions
|
||||||
VIDEO_EXTENSIONS = ('.3g2', '.3gp', '.3gp2', '.3gpp', '.60d', '.ajp', '.asf', '.asx', '.avchd', '.avi', '.bik',
|
VIDEO_EXTENSIONS = ('.3g2', '.3gp', '.3gp2', '.3gpp', '.60d', '.ajp', '.asf', '.asx', '.avchd', '.avi', '.bik',
|
||||||
'.bix', '.box', '.cam', '.dat', '.divx', '.dmf', '.dv', '.dvr-ms', '.evo', '.flc', '.fli',
|
'.bix', '.box', '.cam', '.dat', '.divx', '.dmf', '.dv', '.dvr-ms', '.evo', '.flc', '.fli',
|
||||||
@@ -190,7 +193,7 @@ class Episode(Video):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.year is None:
|
if self.year is None:
|
||||||
return '<%s [%r, %dx%s]>' % (self.__class__.__name__, self.series, self.season, self.episode)
|
return '<%s [%r, %dx%s]>' % (self.__class__.__name__, self.series, self.season, self.episode)
|
||||||
if self.subtitles is not None:
|
if self.subtitles is not (None or set):
|
||||||
return '<%s [%r, %dx%s] %s>' % (self.__class__.__name__, self.series, self.season, self.episode, self.subtitles)
|
return '<%s [%r, %dx%s] %s>' % (self.__class__.__name__, self.series, self.season, self.episode, self.subtitles)
|
||||||
|
|
||||||
return '<%s [%r, %d, %dx%d]>' % (self.__class__.__name__, self.series, self.year, self.season, self.episode)
|
return '<%s [%r, %d, %dx%d]>' % (self.__class__.__name__, self.series, self.year, self.season, self.episode)
|
||||||
|
|||||||
Reference in New Issue
Block a user