Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param torrent_name: The relevant torrent name.
:param file_path: The file path to process.
"""
filename = os.path.basename(file_path)
category_path = get_categorized_path(os.path.join(torrent_name, filename))
if category_path is not None:
destination_dir = os.path.join(category_path, torrent_name)
# Creates target directory (of category path).
_create_destination_path(destination_dir)
destination_path = os.path.join(destination_dir, filename)
try:
# Move\Copy all relevant files to their location (keep original files for uploading).
handler(file_path, destination_path)
logger.info('{} {} to {}'.format(handler.__name__, file_path, destination_path))
if os.name != 'nt':
subprocess.check_output(['chmod', config.EXTRACTION_FILES_MASK, '-R', destination_dir])
# Get subtitles.
subtitles_paths = None
if config.SHOULD_FIND_SUBTITLES:
subtitles_paths = find_file_subtitles(destination_path)
# Upload files to Amazon.
if config.SHOULD_UPLOAD:
upload_file(destination_path)
if subtitles_paths:
for subtitles_path in subtitles_paths:
upload_file(subtitles_path)
except OSError as ex:
logger.exception('Failed to {} {}: {}'.format(handler.__name__, file_path, ex))
import sys
import logbook
from logbook import Logger
from pyexpander import config
file_handler = logbook.RotatingFileHandler(config.LOGFILE, level=logbook.DEBUG)
console_handler = logbook.StreamHandler(sys.stdout, level=logbook.INFO, bubble=True)
file_handler.push_application()
console_handler.push_application()
def get_logger(name):
"""
Return the logger for the given name.
:param name: The name of the logger.
:return: A logbook Logger.
"""
logger = Logger(name, level=logbook.DEBUG)
return logger
def expand_torrent_main():
"""
This main function is designed to be called from commandline.
If an argument (either as the full path, or as a base dir and a file) is provided,
the script will try to expand it.
Else, we assume transmission is calling the script.
"""
logger.info("Py-expander started!")
try:
if len(sys.argv) == 3:
folder = sys.argv[1]
filename = sys.argv[2]
if folder == config.DEFAULT_PATH:
torrent_path = os.path.join(folder, filename)
logger.info("Input is a file: %s" % torrent_path)
else:
torrent_path = folder
logger.info("Input is a dir: %s" % torrent_path)
expand_torrent(torrent_path)
elif len(sys.argv) == 2:
expand_torrent(sys.argv[1])
else:
expand_torrent_from_transmission()
except:
logger.exception("Critical exception occurred: ")
raise