mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-14 18:47:09 -07:00
Refactor identification utils from utils to utils.identification
This commit is contained in:
parent
e44c0bb56a
commit
03cb11dae3
2 changed files with 182 additions and 172 deletions
|
@ -3,7 +3,6 @@
|
|||
from __future__ import print_function, unicode_literals
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
import beets
|
||||
import guessit
|
||||
|
@ -23,6 +22,7 @@ from core.utils.files import (
|
|||
is_min_size,
|
||||
list_media_files,
|
||||
)
|
||||
from core.utils.identification import find_imdbid
|
||||
from core.utils.links import copy_link, replace_links
|
||||
from core.utils.naming import clean_file_name, is_sample, sanitize_name
|
||||
from core.utils.network import find_download, test_connection, wake_on_lan, wake_up
|
||||
|
@ -64,100 +64,6 @@ requests.packages.urllib3.disable_warnings()
|
|||
shutil_custom.monkey_patch()
|
||||
|
||||
|
||||
def category_search(input_directory, input_name, input_category, root, categories):
|
||||
tordir = False
|
||||
|
||||
try:
|
||||
input_name = input_name.encode(core.SYS_ENCODING)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
input_directory = input_directory.encode(core.SYS_ENCODING)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if input_directory is None: # =Nothing to process here.
|
||||
return input_directory, input_name, input_category, root
|
||||
|
||||
pathlist = os.path.normpath(input_directory).split(os.sep)
|
||||
|
||||
if input_category and input_category in pathlist:
|
||||
logger.debug('SEARCH: Found the Category: {0} in directory structure'.format(input_category))
|
||||
elif input_category:
|
||||
logger.debug('SEARCH: Could not find the category: {0} in the directory structure'.format(input_category))
|
||||
else:
|
||||
try:
|
||||
input_category = list(set(pathlist) & set(categories))[-1] # assume last match is most relevant category.
|
||||
logger.debug('SEARCH: Found Category: {0} in directory structure'.format(input_category))
|
||||
except IndexError:
|
||||
input_category = ''
|
||||
logger.debug('SEARCH: Could not find a category in the directory structure')
|
||||
if not os.path.isdir(input_directory) and os.path.isfile(input_directory): # If the input directory is a file
|
||||
if not input_name:
|
||||
input_name = os.path.split(os.path.normpath(input_directory))[1]
|
||||
return input_directory, input_name, input_category, root
|
||||
|
||||
if input_category and os.path.isdir(os.path.join(input_directory, input_category)):
|
||||
logger.info(
|
||||
'SEARCH: Found category directory {0} in input directory directory {1}'.format(input_category, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_category)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
if input_name and os.path.isdir(os.path.join(input_directory, input_name)):
|
||||
logger.info('SEARCH: Found torrent directory {0} in input directory directory {1}'.format(input_name, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_name)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isdir(os.path.join(input_directory, sanitize_name(input_name))):
|
||||
logger.info('SEARCH: Found torrent directory {0} in input directory directory {1}'.format(
|
||||
sanitize_name(input_name), input_directory))
|
||||
input_directory = os.path.join(input_directory, sanitize_name(input_name))
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isfile(os.path.join(input_directory, input_name)):
|
||||
logger.info('SEARCH: Found torrent file {0} in input directory directory {1}'.format(input_name, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_name)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isfile(os.path.join(input_directory, sanitize_name(input_name))):
|
||||
logger.info('SEARCH: Found torrent file {0} in input directory directory {1}'.format(
|
||||
sanitize_name(input_name), input_directory))
|
||||
input_directory = os.path.join(input_directory, sanitize_name(input_name))
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
|
||||
imdbid = [item for item in pathlist if '.cp(tt' in item] # This looks for the .cp(tt imdb id in the path.
|
||||
if imdbid and '.cp(tt' not in input_name:
|
||||
input_name = imdbid[0] # This ensures the imdb id is preserved and passed to CP
|
||||
tordir = True
|
||||
|
||||
if input_category and not tordir:
|
||||
try:
|
||||
index = pathlist.index(input_category)
|
||||
if index + 1 < len(pathlist):
|
||||
tordir = True
|
||||
logger.info('SEARCH: Found a unique directory {0} in the category directory'.format
|
||||
(pathlist[index + 1]))
|
||||
if not input_name:
|
||||
input_name = pathlist[index + 1]
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if input_name and not tordir:
|
||||
if input_name in pathlist or sanitize_name(input_name) in pathlist:
|
||||
logger.info('SEARCH: Found torrent directory {0} in the directory structure'.format(input_name))
|
||||
tordir = True
|
||||
else:
|
||||
root = 1
|
||||
if not tordir:
|
||||
root = 2
|
||||
|
||||
if root > 0:
|
||||
logger.info('SEARCH: Could not find a unique directory for this download. Assume a common directory.')
|
||||
logger.info('SEARCH: We will try and determine which files to process, individually')
|
||||
|
||||
return input_directory, input_name, input_category, root
|
||||
|
||||
|
||||
def flatten(output_destination):
|
||||
return flatten_dir(output_destination, list_media_files(output_destination))
|
||||
|
||||
|
@ -277,80 +183,3 @@ def get_dirs(section, subsection, link='hard'):
|
|||
logger.debug('No directories identified in {0}:{1} for post-processing'.format(section, subsection))
|
||||
|
||||
return list(set(to_return))
|
||||
|
||||
|
||||
def find_imdbid(dir_name, input_name, omdb_api_key):
|
||||
imdbid = None
|
||||
|
||||
logger.info('Attemping imdbID lookup for {0}'.format(input_name))
|
||||
|
||||
# find imdbid in dirName
|
||||
logger.info('Searching folder and file names for imdbID ...')
|
||||
m = re.search(r'(tt\d{7})', dir_name + input_name)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}]'.format(imdbid))
|
||||
return imdbid
|
||||
if os.path.isdir(dir_name):
|
||||
for file in os.listdir(text_type(dir_name)):
|
||||
m = re.search(r'(tt\d{7})', file)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}] via file name'.format(imdbid))
|
||||
return imdbid
|
||||
if 'NZBPR__DNZB_MOREINFO' in os.environ:
|
||||
dnzb_more_info = os.environ.get('NZBPR__DNZB_MOREINFO', '')
|
||||
if dnzb_more_info != '':
|
||||
regex = re.compile(r'^http://www.imdb.com/title/(tt[0-9]+)/$', re.IGNORECASE)
|
||||
m = regex.match(dnzb_more_info)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}] from DNZB-MoreInfo'.format(imdbid))
|
||||
return imdbid
|
||||
logger.info('Searching IMDB for imdbID ...')
|
||||
try:
|
||||
guess = guessit.guessit(input_name)
|
||||
except Exception:
|
||||
guess = None
|
||||
if guess:
|
||||
# Movie Title
|
||||
title = None
|
||||
if 'title' in guess:
|
||||
title = guess['title']
|
||||
|
||||
# Movie Year
|
||||
year = None
|
||||
if 'year' in guess:
|
||||
year = guess['year']
|
||||
|
||||
url = 'http://www.omdbapi.com'
|
||||
|
||||
if not omdb_api_key:
|
||||
logger.info('Unable to determine imdbID: No api key provided for ombdapi.com.')
|
||||
return
|
||||
|
||||
logger.debug('Opening URL: {0}'.format(url))
|
||||
|
||||
try:
|
||||
r = requests.get(url, params={'apikey': omdb_api_key, 'y': year, 't': title},
|
||||
verify=False, timeout=(60, 300))
|
||||
except requests.ConnectionError:
|
||||
logger.error('Unable to open URL {0}'.format(url))
|
||||
return
|
||||
|
||||
try:
|
||||
results = r.json()
|
||||
except Exception:
|
||||
logger.error('No json data returned from omdbapi.com')
|
||||
|
||||
try:
|
||||
imdbid = results['imdbID']
|
||||
except Exception:
|
||||
logger.error('No imdbID returned from omdbapi.com')
|
||||
|
||||
if imdbid:
|
||||
logger.info('Found imdbID [{0}]'.format(imdbid))
|
||||
return imdbid
|
||||
|
||||
logger.warning('Unable to find a imdbID for {0}'.format(input_name))
|
||||
return imdbid
|
||||
|
|
181
core/utils/identification.py
Normal file
181
core/utils/identification.py
Normal file
|
@ -0,0 +1,181 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
import guessit
|
||||
import requests
|
||||
from six import text_type
|
||||
|
||||
import core
|
||||
from core import logger
|
||||
from core.utils.naming import sanitize_name
|
||||
|
||||
|
||||
def find_imdbid(dir_name, input_name, omdb_api_key):
|
||||
imdbid = None
|
||||
|
||||
logger.info('Attemping imdbID lookup for {0}'.format(input_name))
|
||||
|
||||
# find imdbid in dirName
|
||||
logger.info('Searching folder and file names for imdbID ...')
|
||||
m = re.search(r'(tt\d{7})', dir_name + input_name)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}]'.format(imdbid))
|
||||
return imdbid
|
||||
if os.path.isdir(dir_name):
|
||||
for file in os.listdir(text_type(dir_name)):
|
||||
m = re.search(r'(tt\d{7})', file)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}] via file name'.format(imdbid))
|
||||
return imdbid
|
||||
if 'NZBPR__DNZB_MOREINFO' in os.environ:
|
||||
dnzb_more_info = os.environ.get('NZBPR__DNZB_MOREINFO', '')
|
||||
if dnzb_more_info != '':
|
||||
regex = re.compile(r'^http://www.imdb.com/title/(tt[0-9]+)/$', re.IGNORECASE)
|
||||
m = regex.match(dnzb_more_info)
|
||||
if m:
|
||||
imdbid = m.group(1)
|
||||
logger.info('Found imdbID [{0}] from DNZB-MoreInfo'.format(imdbid))
|
||||
return imdbid
|
||||
logger.info('Searching IMDB for imdbID ...')
|
||||
try:
|
||||
guess = guessit.guessit(input_name)
|
||||
except Exception:
|
||||
guess = None
|
||||
if guess:
|
||||
# Movie Title
|
||||
title = None
|
||||
if 'title' in guess:
|
||||
title = guess['title']
|
||||
|
||||
# Movie Year
|
||||
year = None
|
||||
if 'year' in guess:
|
||||
year = guess['year']
|
||||
|
||||
url = 'http://www.omdbapi.com'
|
||||
|
||||
if not omdb_api_key:
|
||||
logger.info('Unable to determine imdbID: No api key provided for ombdapi.com.')
|
||||
return
|
||||
|
||||
logger.debug('Opening URL: {0}'.format(url))
|
||||
|
||||
try:
|
||||
r = requests.get(url, params={'apikey': omdb_api_key, 'y': year, 't': title},
|
||||
verify=False, timeout=(60, 300))
|
||||
except requests.ConnectionError:
|
||||
logger.error('Unable to open URL {0}'.format(url))
|
||||
return
|
||||
|
||||
try:
|
||||
results = r.json()
|
||||
except Exception:
|
||||
logger.error('No json data returned from omdbapi.com')
|
||||
|
||||
try:
|
||||
imdbid = results['imdbID']
|
||||
except Exception:
|
||||
logger.error('No imdbID returned from omdbapi.com')
|
||||
|
||||
if imdbid:
|
||||
logger.info('Found imdbID [{0}]'.format(imdbid))
|
||||
return imdbid
|
||||
|
||||
logger.warning('Unable to find a imdbID for {0}'.format(input_name))
|
||||
return imdbid
|
||||
|
||||
|
||||
def category_search(input_directory, input_name, input_category, root, categories):
|
||||
tordir = False
|
||||
|
||||
try:
|
||||
input_name = input_name.encode(core.SYS_ENCODING)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
input_directory = input_directory.encode(core.SYS_ENCODING)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if input_directory is None: # =Nothing to process here.
|
||||
return input_directory, input_name, input_category, root
|
||||
|
||||
pathlist = os.path.normpath(input_directory).split(os.sep)
|
||||
|
||||
if input_category and input_category in pathlist:
|
||||
logger.debug('SEARCH: Found the Category: {0} in directory structure'.format(input_category))
|
||||
elif input_category:
|
||||
logger.debug('SEARCH: Could not find the category: {0} in the directory structure'.format(input_category))
|
||||
else:
|
||||
try:
|
||||
input_category = list(set(pathlist) & set(categories))[-1] # assume last match is most relevant category.
|
||||
logger.debug('SEARCH: Found Category: {0} in directory structure'.format(input_category))
|
||||
except IndexError:
|
||||
input_category = ''
|
||||
logger.debug('SEARCH: Could not find a category in the directory structure')
|
||||
if not os.path.isdir(input_directory) and os.path.isfile(input_directory): # If the input directory is a file
|
||||
if not input_name:
|
||||
input_name = os.path.split(os.path.normpath(input_directory))[1]
|
||||
return input_directory, input_name, input_category, root
|
||||
|
||||
if input_category and os.path.isdir(os.path.join(input_directory, input_category)):
|
||||
logger.info(
|
||||
'SEARCH: Found category directory {0} in input directory directory {1}'.format(input_category, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_category)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
if input_name and os.path.isdir(os.path.join(input_directory, input_name)):
|
||||
logger.info('SEARCH: Found torrent directory {0} in input directory directory {1}'.format(input_name, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_name)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isdir(os.path.join(input_directory, sanitize_name(input_name))):
|
||||
logger.info('SEARCH: Found torrent directory {0} in input directory directory {1}'.format(
|
||||
sanitize_name(input_name), input_directory))
|
||||
input_directory = os.path.join(input_directory, sanitize_name(input_name))
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isfile(os.path.join(input_directory, input_name)):
|
||||
logger.info('SEARCH: Found torrent file {0} in input directory directory {1}'.format(input_name, input_directory))
|
||||
input_directory = os.path.join(input_directory, input_name)
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
elif input_name and os.path.isfile(os.path.join(input_directory, sanitize_name(input_name))):
|
||||
logger.info('SEARCH: Found torrent file {0} in input directory directory {1}'.format(
|
||||
sanitize_name(input_name), input_directory))
|
||||
input_directory = os.path.join(input_directory, sanitize_name(input_name))
|
||||
logger.info('SEARCH: Setting input_directory to {0}'.format(input_directory))
|
||||
tordir = True
|
||||
|
||||
imdbid = [item for item in pathlist if '.cp(tt' in item] # This looks for the .cp(tt imdb id in the path.
|
||||
if imdbid and '.cp(tt' not in input_name:
|
||||
input_name = imdbid[0] # This ensures the imdb id is preserved and passed to CP
|
||||
tordir = True
|
||||
|
||||
if input_category and not tordir:
|
||||
try:
|
||||
index = pathlist.index(input_category)
|
||||
if index + 1 < len(pathlist):
|
||||
tordir = True
|
||||
logger.info('SEARCH: Found a unique directory {0} in the category directory'.format
|
||||
(pathlist[index + 1]))
|
||||
if not input_name:
|
||||
input_name = pathlist[index + 1]
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if input_name and not tordir:
|
||||
if input_name in pathlist or sanitize_name(input_name) in pathlist:
|
||||
logger.info('SEARCH: Found torrent directory {0} in the directory structure'.format(input_name))
|
||||
tordir = True
|
||||
else:
|
||||
root = 1
|
||||
if not tordir:
|
||||
root = 2
|
||||
|
||||
if root > 0:
|
||||
logger.info('SEARCH: Could not find a unique directory for this download. Assume a common directory.')
|
||||
logger.info('SEARCH: We will try and determine which files to process, individually')
|
||||
|
||||
return input_directory, input_name, input_category, root
|
Loading…
Add table
Add a link
Reference in a new issue