Пример #1
0
 def get_page_state(self, user, archive_path):
     """return a tuple (current page, total pages) for an archive for the user."""
     key = unicode(archive_path)
     if user not in Dict['read_states'] or key not in Dict['read_states'][user]:
         cur, total = (-1, -1)
     else:
         cur, total = Dict['read_states'][user][key]
     if total <= 0:
         a = archives.get_archive(archive_path)
         total = len([x for x in a.namelist() if utils.splitext(x)[-1] in utils.IMAGE_FORMATS])
     return (int(cur), int(total))
Пример #2
0
def get_image(archive_path, filename, user):
    """Return the contents of `filename` from within `archive_path`. also do some other stuff."""
    archive = archives.get_archive(archive_path)

    x, total_pages = DATABASE.get_page_state(user, archive_path)

    m = utils.PAGE_NUM_REGEX.search(filename)
    cur_page = int(m.group(1)) if m else 0
    Log.Info('{}: <{}> ({}/{})'.format(user, os.path.basename(archive_path), cur_page, total_pages))

    if cur_page > 0:
        DATABASE.set_page_state(user, archive_path, cur_page)

    return utils.data_object(archive, filename)
Пример #3
0
def get_image(archive_path, filename, user):
    """Return the contents of `filename` from within `archive_path`. also do some other stuff."""
    archive = archives.get_archive(archive_path)

    x, total_pages = DATABASE.get_page_state(user, archive_path)

    m = utils.PAGE_NUM_REGEX.search(filename)
    cur_page = int(m.group(1)) if m else 0
    Log.Info('{}: <{}> ({}/{})'.format(user, os.path.basename(archive_path), cur_page, total_pages))

    if cur_page > 0:
        DATABASE.set_page_state(user, archive_path, cur_page)

    return utils.data_object(archive, filename)
Пример #4
0
 def get_page_state(self, user, archive_path):
     """return a tuple (current page, total pages) for an archive for the user."""
     key = unicode(archive_path)
     if user not in Dict['read_states'] or key not in Dict['read_states'][
             user]:
         cur, total = (-1, -1)
     else:
         cur, total = Dict['read_states'][user][key]
     if total <= 0 or total == 1:
         try:
             a = archives.get_archive(archive_path)
         except archives.ArchiveError:
             total = 1
         else:
             total = len([
                 x for x in a.namelist()
                 if utils.splitext(x)[-1] in utils.IMAGE_FORMATS
             ])
     return (int(cur), int(total))
Пример #5
0
def Comic(archive_path, user=None, page=0):
    """Return an oc with all pages in archive_path. if page > 0 return pages [page - Prefs['resume_length']:]"""
    oc = ObjectContainer(title2=unicode(os.path.basename(archive_path)), no_cache=True)
    try:
        archive = archives.get_archive(archive_path)
    except archives.ArchiveError as e:
        Log.Error(e)
        return error_message('bad archive', 'unable to open archive: {}'.format(archive_path))
    for f in utils.sorted_nicely(archive.namelist()):
        page_title, ext = utils.splitext(f)
        if not ext or ext not in utils.IMAGE_FORMATS:
            continue
        decoration = None
        if page > 0:
            m = utils.PAGE_NUM_REGEX.search(f)
            if m:
                page_num = int(m.group(1))
                if page_num < page - int(Prefs['resume_length']):
                    continue
                if page_num <= page:
                    decoration = '>'
        page_title = utils.basename(page_title)
        if decoration is not None:
            page_title = '{} {}'.format(decoration, page_title)

        if type(page_title) != unicode:
            try:
                page_title = page_title.decode('cp437')
            except Exception:
                try:
                    page_title = unicode(page_title, errors='replace')
                except Exception:
                    pass

        oc.add(CreatePhotoObject(
            media_key=Callback(GetImage, archive_path=String.Encode(archive_path),
                               filename=String.Encode(f), user=user, extension=ext.lstrip('.'),
                               time=int(time.time()) if bool(Prefs['prevent_caching']) else 0),
            rating_key=hashlib.sha1('{}{}{}'.format(archive_path, f, user)).hexdigest(),
            title=page_title,
            thumb=utils.thumb_transcode(Callback(get_thumb, archive_path=archive_path,
                                                 filename=f))))
    return oc
Пример #6
0
def Comic(archive_path, user=None, page=0):
    """Return an oc with all pages in archive_path. if page > 0 return pages [page - Prefs['resume_length']:]"""
    oc = ObjectContainer(title2=unicode(os.path.basename(archive_path)), no_cache=True)
    try:
        archive = archives.get_archive(archive_path)
    except archives.ArchiveError as e:
        Log.Error(e)
        return error_message('bad archive', 'unable to open archive: {}'.format(archive_path))
    for f in utils.sorted_nicely(archive.namelist()):
        page_title, ext = utils.splitext(f)
        if not ext or ext.lower() not in utils.IMAGE_FORMATS:
            continue
        decoration = None
        if page > 0:
            m = utils.PAGE_NUM_REGEX.search(f)
            if m:
                page_num = int(m.group(1))
                if page_num < page - int(Prefs['resume_length']):
                    continue
                if page_num <= page:
                    decoration = '>'
        page_title = utils.basename(page_title)
        if decoration is not None:
            page_title = '{} {}'.format(decoration, page_title)

        if type(page_title) != unicode:
            try:
                page_title = page_title.decode('cp437')
            except Exception:
                try:
                    page_title = unicode(page_title, errors='replace')
                except Exception:
                    pass

        oc.add(CreatePhotoObject(
            media_key=Callback(GetImage, archive_path=String.Encode(archive_path),
                               filename=String.Encode(f), user=user, extension=ext.lstrip('.'),
                               time=int(time.time()) if bool(Prefs['prevent_caching']) else 0),
            rating_key=hashlib.sha1('{}{}{}'.format(archive_path, f, user)).hexdigest(),
            title=page_title,
            thumb=utils.thumb_transcode(Callback(get_thumb, archive_path=archive_path,
                                                 filename=f))))
    return oc
Пример #7
0
def get_cover(archive_path):
    """Return the contents of the first file in `archive_path`."""
    archive = archives.get_archive(archive_path)
    x = sorted([x for x in archive.namelist() if utils.splitext(x)[-1] in utils.IMAGE_FORMATS])
    if x:
        return utils.data_object(archive, x[0])
Пример #8
0
def get_thumb(archive_path, filename):
    """Return the contents of `filename` from within `archive_path`."""
    archive = archives.get_archive(archive_path)
    return utils.data_object(archive, filename)
Пример #9
0
def get_cover(archive_path):
    """Return the contents of the first file in `archive_path`."""
    archive = archives.get_archive(archive_path)
    x = sorted([x for x in archive.namelist() if utils.splitext(x)[-1].lower() in utils.IMAGE_FORMATS])
    if x:
        return utils.data_object(archive, x[0])
Пример #10
0
def get_thumb(archive_path, filename):
    """Return the contents of `filename` from within `archive_path`."""
    archive = archives.get_archive(archive_path)
    return utils.data_object(archive, filename)