Ejemplo n.º 1
0
def unpack_download(download, passes=UNPACK_PASSES):
    '''Move download file into a directory and unpack the archives.

    :return: directory
    '''
    download = media.clean_file(download, strip_extra=True)
    if os.path.isfile(download):
        # Move file into a directory
        path_dst = media.get_unique(os.path.splitext(download)[0])
        path, filename, ext = media.fsplit(download)
        file_dst = media.rename_file(download,
                os.path.join(path_dst, filename + ext))
        download = os.path.dirname(file_dst)

    to_skip = []
    for i in range(passes):
        for file in sorted(list(media.iter_files(download))):  # sort for multipart archives
            if file in to_skip:
                continue
            res = media.get_file(file)
            if res.type == 'archive':
                to_skip += res.unpack(remove_src=True)
            else:
                to_skip.append(file)

    return download
Ejemplo n.º 2
0
 def get_bases(cls, id, dirs_only=False):
     '''Get the Media base directories or files.
     '''
     media = cls.get(id) or {}
     files = media.get('files', [])
     res = [get_file(f).get_base() for f in files if os.path.exists(f)]
     if dirs_only:
         res = [f for f in res if os.path.isdir(f)]
     return list(set(res))
Ejemplo n.º 3
0
def search_subtitles(media_id):
    media = Media.get(media_id)
    if not media:
        return

    search_langs = Settings.get_settings('subtitles_langs')
    temp_dir = Settings.get_settings('paths')['tmp']
    if not search_langs:
        logger.error('missing subtitles search langs')
        return

    root_path = Settings.get_settings('paths')['media']['video'].rstrip('/') + '/'

    info = media['info']
    if info['subtype'] == 'tv':
        name = clean(info.get('name'), 6)
        season = info.get('season')
        episode = info.get('episode')
        date = None
    else:
        name = info.get('full_name')
        season = None
        episode = None
        date = media.get('extra', {}).get('imdb', {}).get('date')

    subtitles_langs = []
    plugins = {
        'subscene': Subscene(),
        'opensubtitles': Opensubtitles(**Settings.get_settings('opensubtitles')),
        }

    stat = []
    for file in media['files']:
        if not validate_file(file, root_path):
            continue

        file_ = get_file(file)
        dst = file_.get_subtitles_path()

        processed = False
        for lang in search_langs:
            logger.debug('searching %s subtitles for "%s" (%s)', lang, media['name'], file)

            for obj_name, obj in plugins.items():
                if not obj.accessible:
                    continue
                processed = True
                lang_ = LANGS_DEF[obj_name].get(lang)
                if not lang_:
                    continue

                for url in obj.results(name, season, episode, date, lang_):
                    doc = {'url': url, 'file': file_.file}
                    if Subtitles.find_one(doc):
                        continue
                    try:
                        files_dst = obj.download(url, dst, temp_dir)
                    except RateLimitReached:
                        break
                    if not files_dst:
                        continue
                    for file_dst in files_dst:
                        logger.info('downloaded %s on %s', file_dst, obj_name)

                    doc['created'] = datetime.utcnow()
                    Subtitles.insert(doc, safe=True)

        for lang in search_langs:
            if file_.set_subtitles(lang):
                subtitles_langs.append(lang)

        stat.append(processed)

    if False not in stat:
        media['updated_subs'] = datetime.utcnow()
    media['subtitles'] = sorted(list(set(subtitles_langs)))
    Media.save(media, safe=True)
Ejemplo n.º 4
0
def check_download_file(file, finished_file=None, finished=False):
    '''Check the file and its meta data.

    :param file: download file finished or incomplete
        (if incomplete, also pass the finished_file param)
    :param finished_file: finished file if the incomplete file
        has a different name (e.g.: '.part' extension)
    :param finished: True if the download is finished

    :return: True if the file is valid
    '''
    if not os.path.exists(file):
        return True

    file = media.get_file(file, real_file=finished_file)
    ext = getattr(file, 'real_ext', file.ext).lower()

    if file.type == 'archive':
        if file.is_main_file() and file.is_protected():
            logger.info('invalid archive %s: password protected', file.file)
            return False

    elif file.type == 'video':
        if not media.check_size(file.file, size_min=100):
            return True

        info = file.get_file_info()

        # Check extension
        if ext in ('.wmv', '.asf'):
            if info['season'] and info['episode']:
                logger.info('invalid extension "%s" for tvshow %s', ext, file.file)
                return False

        # Check duration
        if info.get('duration'):
            if not 15 < info['duration'] / 60 < 240:
                logger.info('invalid duration "%s" for %s', info['duration'], file.file)
                return False
        elif finished:
            logger.info('failed to get duration for %s', file.file)
            return False

        # Check bitrate
        if info.get('video_bitrate') and info.get('audio_bitrate'):
            if not 300 < info['video_bitrate'] / 1024 < 10000:
                logger.info('invalid video bitrate "%s" for %s', info['video_bitrate'], file.file)
                return False
            if not 30 < info['audio_bitrate'] / 1024 < 1000:
                logger.info('invalid audio bitrate "%s" for %s', info['audio_bitrate'], file.file)
                return False

        elif info.get('bitrate'):
            if not 300 < info['bitrate'] / 1024 < 10000:
                logger.info('invalid bitrate "%s" for %s', info['bitrate'], file.file)
                return False

        elif finished:
            logger.info('failed to get bitrate for %s', file.file)
            return False

    return True