Exemplo n.º 1
0
    def _check_file(self, file):
        with open(file) as fd:
            data = fd.read()

        if not data or is_html(data):
            remove_file(file)
            if RE_MAXIMUM_DOWNLOAD.search(data):
                update_rate(self.__module__.rsplit('.', 1)[-1], count=-1)
                logger.error('download limit reached')
                raise RateLimitReached('opensubtitles download limit reached')
            return
        return True
Exemplo n.º 2
0
def run():
    path = Settings.get_settings('paths')['finished_download']
    if os.path.exists(path):
        media_paths = Settings.get_settings('paths')['media']

        for download in downloads(path):
            if not check_download(download.file):
                if remove_file(download.file):
                    logger.info('removed %s (bad download)', download.filename)
                continue

            # Move the download
            if download.type not in media_paths:
                download.type = 'misc'
            dst = media_paths[download.type]
            res = move_file(download.file, dst)
            if res:
                Media.add_file(res)
                Download.insert({
                        'name': download.filename,
                        'category': download.type,
                        'path': dst,
                        'created': datetime.utcnow(),
                        }, safe=True)
                logger.info('moved %s to %s', download.filename, dst)
Exemplo n.º 3
0
def clean_nzbs():
    try:
        client = get_nzb_client()
        nzbs = client.list_nzbs(history=False) + client.list_nzbs(history=True)
        if nzbs:
            return

        config = client.get_config()
        base_path = str(Settings.get_settings('sabnzbd')['base_path'])
        for name in ('download_dir', 'complete_dir'):
            path = config['misc'].get(name)
            if not path:
                logger.error('failed to get sabnzbd %s', name)
                continue
            path = str(os.path.join(base_path, path))
            if not os.path.exists(path):
                logger.error('sabnzbd %s path %s does not exist', name, path)
                continue

            for file in glob(path + '/*'):
                if remove_file(file):
                    logger.info('removed obsolete sabnzbd path %s', file)

    except SabnzbdError, e:
        logger.error('nzb client error: %s', str(e))
Exemplo n.º 4
0
def clean_download_dir(path):
    '''Clean the download directories and files.
    '''
    for file in list(media.iter_files(path, incl_dirs=True)) + [path]:
        if os.path.isdir(file) and not os.listdir(file):
            media.remove_file(file)
        elif RE_DOWNLOAD_JUNK.search(file):
            media.remove_file(file)
        else:
            try:
                os.utime(file, None)
            except OSError:
                pass
            media.clean_file(file)

    if os.path.exists(path):
        return path
Exemplo n.º 5
0
def manage_nzb(client, nzb_id, dst):
    try:
        nzb = client.get_nzb(nzb_id, history=True)
        if not nzb or not nzb['storage']:
            return

        if not os.path.exists(nzb['storage']):
            client.remove_nzb(nzb['nzo_id'], history=True, delete_files=True)
            logger.error('failed to find finished nzb directory "%s" (%s)', nzb['storage'], repr(nzb))
            media.remove_file(nzb['path'])
            logger.error('removed nzb path "%s" (%s)', nzb['path'], repr(nzb))
            return

        if media.move_file(nzb['storage'], dst):
            client.remove_nzb(nzb['nzo_id'], history=True)
            logger.info('moved finished nzb %s to %s', nzb['storage'], dst)

    except SabnzbdError, e:
        logger.error('nzb client error: %s', str(e))
Exemplo n.º 6
0
def get_downloads(path_root):
    '''Clean and get download sub directories.

    :return: directories list
    '''
    paths = []
    for path in list(media.iter_files(path_root,
            incl_files=False, incl_dirs=True)) + [path_root]:

        if media.get_type(path) == 'audio':
            album = {}
            extra = []
            for file in media.files(path, recursive=False):
                # Get album files
                if file.type == 'audio' and file.ext.lower() not in ('.m3u',):
                    album[file.file] = file.get_file_info()
                # Get extra files
                elif file.type == 'video' or (file.type == 'image' \
                        and media.get_size(file.file) > SIZE_ALBUM_IMAGE_MIN):
                    extra.append(file.file)
                else:
                    media.remove_file(file.file)

            path_dst = path
            if album:
                stat = {}
                for info in album.values():
                    def set_stat(key, force=False):
                        stat.setdefault(key, [])
                        val = info.get(key)
                        if val and (force or val not in stat[key]):
                            stat[key].append(val)
                    set_stat('title', True)
                    for key in ('artist', 'album', 'date', 'track_number'):
                        set_stat(key)

                if len(stat['title']) == len(stat['track_number']) == len(album):
                    # Rename tracks files
                    for file, info in album.items():
                        track_name = '%02d-%s-%s' % (info.get('track_number', 0), info['artist'], info['title'])
                        track_name = re.sub(r'\s+', '_', track_name).lower()
                        file_dst = os.path.join(path, track_name + os.path.splitext(file)[1])
                        media.rename_file(file, file_dst)

                # Get album directory name
                if len(stat['artist']) == len(stat['album']) == 1:
                    date_str = '-%s' % stat['date'][0] if len(stat['date']) == 1 else ''
                    album_name = '%s-%s%s' % (stat['artist'][0].capitalize(), stat['album'][0].capitalize(), date_str)
                    album_name = re.sub(r'\s+', '_', album_name)

                    # Rename extra files using the album name
                    for file in extra:
                        filename_extra = os.path.basename(file)
                        if not filename_extra.startswith('00-'):
                            file_dst = os.path.join(path, '00-%s-%s' % (album_name.lower(), filename_extra.lower()))
                            media.rename_file(file, file_dst)

                    # Rename album directory
                    path_dst = media.rename_file(path,
                            os.path.join(os.path.dirname(path_root), album_name))
                    paths.append(path_dst)

    if os.path.exists(path_root) and path_root not in paths:
        paths.append(path_root)
    return paths