Using logging for output and now is a functional prototype.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -98,5 +98,5 @@ ENV/
|
|||||||
# mypy
|
# mypy
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
|
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
**/.DS_Store
|
||||||
|
|||||||
@@ -1,25 +1,28 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3.6
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# @Author: KevinMidboe
|
# @Author: KevinMidboe
|
||||||
# @Date: 2017-08-25 23:22:27
|
# @Date: 2017-08-25 23:22:27
|
||||||
# @Last Modified by: KevinMidboe
|
# @Last Modified by: KevinMidboe
|
||||||
# @Last Modified time: 2017-09-09 14:57:54
|
# @Last Modified time: 2017-09-29 12:35:24
|
||||||
|
|
||||||
from guessit import guessit
|
from guessit import guessit
|
||||||
import os, errno
|
import os, errno
|
||||||
import logging
|
import logging
|
||||||
import tvdb_api
|
import tvdb_api
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
import env_variables as env
|
||||||
|
|
||||||
from video import VIDEO_EXTENSIONS, Episode, Movie, Video
|
from video import VIDEO_EXTENSIONS, Episode, Movie, Video
|
||||||
from subtitle import SUBTITLE_EXTENSIONS, Subtitle, get_subtitle_path
|
from subtitle import SUBTITLE_EXTENSIONS, Subtitle, get_subtitle_path
|
||||||
|
from utils import sanitize
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logging.basicConfig(filename=env.logfile, level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
#: Supported archive extensions
|
#: Supported archive extensions
|
||||||
ARCHIVE_EXTENSIONS = ('.rar',)
|
ARCHIVE_EXTENSIONS = ('.rar',)
|
||||||
|
|
||||||
@profile
|
|
||||||
def scan_video(path):
|
def scan_video(path):
|
||||||
"""Scan a video from a `path`.
|
"""Scan a video from a `path`.
|
||||||
|
|
||||||
@@ -37,12 +40,12 @@ 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)
|
||||||
logger.info('Scanning video %r in %r', filename, dirpath)
|
logging.info('Scanning video %r in %r', filename, dirpath)
|
||||||
|
|
||||||
# guess
|
# guess
|
||||||
parent_path = path.strip(filename)
|
parent_path = path.strip(filename)
|
||||||
# video = Video.fromguess(filename, parent_path, guessit(path))
|
video = Video.fromguess(filename, parent_path, guessit(path))
|
||||||
video = Video('test')
|
# video = Video('test')
|
||||||
# guessit(path)
|
# guessit(path)
|
||||||
|
|
||||||
return video
|
return video
|
||||||
@@ -53,7 +56,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)
|
||||||
logger.info('Scanning video %r in %r', filename, dirpath)
|
logging.info('Scanning subtitle %r in %r', filename, dirpath)
|
||||||
|
|
||||||
# guess
|
# guess
|
||||||
parent_path = path.strip(filename)
|
parent_path = path.strip(filename)
|
||||||
@@ -63,7 +66,6 @@ def scan_subtitle(path):
|
|||||||
return subtitle
|
return subtitle
|
||||||
|
|
||||||
|
|
||||||
@profile
|
|
||||||
def scan_files(path, age=None, archives=True):
|
def scan_files(path, age=None, archives=True):
|
||||||
"""Scan `path` for videos and their subtitles.
|
"""Scan `path` for videos and their subtitles.
|
||||||
|
|
||||||
@@ -87,12 +89,12 @@ def scan_files(path, age=None, archives=True):
|
|||||||
# walk the path
|
# walk the path
|
||||||
mediafiles = []
|
mediafiles = []
|
||||||
for dirpath, dirnames, filenames in os.walk(path):
|
for dirpath, dirnames, filenames in os.walk(path):
|
||||||
logger.debug('Walking directory %r', dirpath)
|
logging.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('.'):
|
||||||
logger.debug('Skipping hidden dirname %r in %r', dirname, dirpath)
|
logging.debug('Skipping hidden dirname %r in %r', dirname, dirpath)
|
||||||
dirnames.remove(dirname)
|
dirnames.remove(dirname)
|
||||||
|
|
||||||
# scan for videos
|
# scan for videos
|
||||||
@@ -103,7 +105,7 @@ def scan_files(path, age=None, archives=True):
|
|||||||
|
|
||||||
# skip hidden files
|
# skip hidden files
|
||||||
if filename.startswith('.'):
|
if filename.startswith('.'):
|
||||||
logger.debug('Skipping hidden filename %r in %r', filename, dirpath)
|
logging.debug('Skipping hidden filename %r in %r', filename, dirpath)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# reconstruct the file path
|
# reconstruct the file path
|
||||||
@@ -111,12 +113,12 @@ def scan_files(path, age=None, archives=True):
|
|||||||
|
|
||||||
# skip links
|
# skip links
|
||||||
if os.path.islink(filepath):
|
if os.path.islink(filepath):
|
||||||
logger.debug('Skipping link %r in %r', filename, dirpath)
|
logging.debug('Skipping link %r in %r', filename, dirpath)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# skip old files
|
# skip old files
|
||||||
if age and datetime.utcnow() - datetime.utcfromtimestamp(os.path.getmtime(filepath)) > age:
|
if age and datetime.utcnow() - datetime.utcfromtimestamp(os.path.getmtime(filepath)) > age:
|
||||||
logger.debug('Skipping old file %r in %r', filename, dirpath)
|
logging.debug('Skipping old file %r in %r', filename, dirpath)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# scan
|
# scan
|
||||||
@@ -125,21 +127,21 @@ def scan_files(path, age=None, archives=True):
|
|||||||
video = scan_video(filepath)
|
video = scan_video(filepath)
|
||||||
mediafiles.append(video)
|
mediafiles.append(video)
|
||||||
except ValueError: # pragma: no cover
|
except ValueError: # pragma: no cover
|
||||||
logger.exception('Error scanning video')
|
logging.exception('Error scanning video')
|
||||||
continue
|
continue
|
||||||
elif archives and filename.endswith(ARCHIVE_EXTENSIONS): # archive
|
elif archives and filename.endswith(ARCHIVE_EXTENSIONS): # archive
|
||||||
try:
|
try:
|
||||||
video = scan_archive(filepath)
|
video = scan_archive(filepath)
|
||||||
mediafiles.append(video)
|
mediafiles.append(video)
|
||||||
except (NotRarFile, RarCannotExec, ValueError): # pragma: no cover
|
except (NotRarFile, RarCannotExec, ValueError): # pragma: no cover
|
||||||
logger.exception('Error scanning archive')
|
logging.exception('Error scanning archive')
|
||||||
continue
|
continue
|
||||||
elif filename.endswith(SUBTITLE_EXTENSIONS): # subtitle
|
elif filename.endswith(SUBTITLE_EXTENSIONS): # subtitle
|
||||||
try:
|
try:
|
||||||
subtitle = scan_subtitle(filepath)
|
subtitle = scan_subtitle(filepath)
|
||||||
mediafiles.append(subtitle)
|
mediafiles.append(subtitle)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logger.exception('Error scanning subtitle')
|
logging.exception('Error scanning subtitle')
|
||||||
continue
|
continue
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
raise ValueError('Unsupported file %r' % filename)
|
raise ValueError('Unsupported file %r' % filename)
|
||||||
@@ -148,7 +150,6 @@ def scan_files(path, age=None, archives=True):
|
|||||||
return mediafiles
|
return mediafiles
|
||||||
|
|
||||||
|
|
||||||
@profile
|
|
||||||
def organize_files(path):
|
def organize_files(path):
|
||||||
hashList = {}
|
hashList = {}
|
||||||
mediafiles = scan_files(path)
|
mediafiles = scan_files(path)
|
||||||
@@ -219,12 +220,12 @@ def save_subtitles(files, single=False, directory=None, encoding=None):
|
|||||||
# for subtitle in files:
|
# for subtitle in files:
|
||||||
# # check content
|
# # check content
|
||||||
# if subtitle.name is None:
|
# if subtitle.name is None:
|
||||||
# logger.error('Skipping subtitle %r: no content', subtitle)
|
# logging.error('Skipping subtitle %r: no content', subtitle)
|
||||||
# continue
|
# continue
|
||||||
|
|
||||||
# # check language
|
# # check language
|
||||||
# if subtitle.language in set(s.language for s in saved_subtitles):
|
# if subtitle.language in set(s.language for s in saved_subtitles):
|
||||||
# logger.debug('Skipping subtitle %r: language already saved', subtitle)
|
# logging.debug('Skipping subtitle %r: language already saved', subtitle)
|
||||||
# continue
|
# continue
|
||||||
|
|
||||||
# # create subtitle path
|
# # create subtitle path
|
||||||
@@ -233,7 +234,7 @@ def save_subtitles(files, single=False, directory=None, encoding=None):
|
|||||||
# subtitle_path = os.path.join(directory, os.path.split(subtitle_path)[1])
|
# subtitle_path = os.path.join(directory, os.path.split(subtitle_path)[1])
|
||||||
|
|
||||||
# # save content as is or in the specified encoding
|
# # save content as is or in the specified encoding
|
||||||
# logger.info('Saving %r to %r', subtitle, subtitle_path)
|
# logging.info('Saving %r to %r', subtitle, subtitle_path)
|
||||||
# if encoding is None:
|
# if encoding is None:
|
||||||
# with io.open(subtitle_path, 'wb') as f:
|
# with io.open(subtitle_path, 'wb') as f:
|
||||||
# f.write(subtitle.content)
|
# f.write(subtitle.content)
|
||||||
@@ -250,12 +251,13 @@ def save_subtitles(files, single=False, directory=None, encoding=None):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
episodePath = '/Volumes/media/tv/Black Mirror/Black Mirror Season 01/'
|
# episodePath = '/Volumes/media/tv/Black Mirror/Black Mirror Season 01/'
|
||||||
|
episodePath = '/Volumes/media/tv/Black Mirror/'
|
||||||
|
|
||||||
t = tvdb_api.Tvdb()
|
t = tvdb_api.Tvdb()
|
||||||
|
|
||||||
hashList = organize_files(episodePath)
|
hashList = organize_files(episodePath)
|
||||||
pprint(hashList)
|
pprint(hashList)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user