示例#1
0
    def get_opds_navcatalog(self, which, page_url, up_url, offset=0):
        categories = self.db.get_categories()
        if which not in categories:
            raise web.HTTPError(404, reason='Category %r not found' % which)

        items = categories[which]
        updated = self.db.last_modified()
        category_meta = self.db.field_metadata
        meta = category_meta.get(which, {})
        category_name = meta.get('name', which)
        feed_title = default_feed_title + ' :: ' + _('By %s') % category_name

        id_ = 'calibre-category-feed:' + which

        MAX_ITEMS = CONF['max_opds_ungrouped_items']

        if len(items) <= MAX_ITEMS:
            max_items = CONF['max_opds_items']
            offsets = Offsets(offset, max_items, len(items))
            items = list(items)[offsets.offset:offsets.offset + max_items]
            ans = CategoryFeed(items,
                               which,
                               id_,
                               updated,
                               offsets,
                               page_url,
                               up_url,
                               self.db,
                               title=feed_title)
        else:

            class Group:
                def __init__(self, text, count):
                    self.text, self.count = text, count

            groups = defaultdict(int)
            for x in items:
                c = first_char(x)
                groups[c.upper()] += 1

            items = []
            for c in sorted(groups.keys(), key=sort_key):
                items.append(Group(c, groups[c]))

            max_items = CONF['max_opds_items']
            offsets = Offsets(offset, max_items, len(items))
            items = items[offsets.offset:offsets.offset + max_items]
            ans = CategoryGroupFeed(items,
                                    which,
                                    id_,
                                    updated,
                                    offsets,
                                    page_url,
                                    up_url,
                                    title=feed_title)

        self.set_header('Last-Modified', self.last_modified(updated))
        self.set_header('Content-Type', 'application/atom+xml; charset=UTF-8')

        return str(ans)
示例#2
0
    def get_opds_navcatalog(self,
                            which,
                            page_url,
                            up_url,
                            version=0,
                            offset=0):
        categories = self.categories_cache(
            self.get_opds_allowed_ids_for_version(version))
        if which not in categories:
            raise cherrypy.HTTPError(404, 'Category %r not found' % which)

        items = categories[which]
        updated = self.db.last_modified()

        id_ = 'calibre-category-feed:' + which

        MAX_ITEMS = self.opts.max_opds_ungrouped_items

        if len(items) <= MAX_ITEMS:
            max_items = self.opts.max_opds_items
            offsets = Offsets(offset, max_items, len(items))
            items = list(items)[offsets.offset:offsets.offset + max_items]
            ans = CategoryFeed(items, which, id_, updated, version, offsets,
                               page_url, up_url, self.db)
        else:

            class Group:
                def __init__(self, text, count):
                    self.text, self.count = text, count

            starts = set([])
            for x in items:
                val = getattr(x, 'sort', x.name)
                if not val:
                    val = 'A'
                starts.add(val[0].upper())
            category_groups = OrderedDict()
            for x in sorted(starts, key=sort_key):
                category_groups[x] = len([
                    y for y in items
                    if getattr(y, 'sort', y.name).startswith(x)
                ])
            items = [Group(x, y) for x, y in category_groups.items()]
            max_items = self.opts.max_opds_items
            offsets = Offsets(offset, max_items, len(items))
            items = items[offsets.offset:offsets.offset + max_items]
            ans = CategoryGroupFeed(items, which, id_, updated, version,
                                    offsets, page_url, up_url)

        cherrypy.response.headers['Last-Modified'] = self.last_modified(
            updated)
        cherrypy.response.headers['Content-Type'] = 'application/atom+xml'

        return str(ans)
示例#3
0
文件: opds.py 项目: tokot/calibre
 def get_opds_acquisition_feed(self,
                               ids,
                               offset,
                               page_url,
                               up_url,
                               id_,
                               sort_by='title',
                               ascending=True,
                               version=0,
                               feed_title=None):
     idx = self.db.FIELD_MAP['id']
     ids &= self.get_opds_allowed_ids_for_version(version)
     if not ids:
         raise cherrypy.HTTPError(404, 'No books found')
     items = [x for x in self.db.data.iterall() if x[idx] in ids]
     self.sort(items, sort_by, ascending)
     max_items = self.opts.max_opds_items
     offsets = Offsets(offset, max_items, len(items))
     items = items[offsets.offset:offsets.offset + max_items]
     updated = self.db.last_modified()
     cherrypy.response.headers['Last-Modified'] = self.last_modified(
         updated)
     cherrypy.response.headers[
         'Content-Type'] = 'application/atom+xml;profile=opds-catalog'
     return str(
         AcquisitionFeed(updated,
                         id_,
                         items,
                         offsets,
                         page_url,
                         up_url,
                         version,
                         self.db,
                         self.opts.url_prefix,
                         title=feed_title))
示例#4
0
 def get_opds_acquisition_feed(self,
                               ids,
                               offset,
                               page_url,
                               up_url,
                               id_,
                               sort_by='title',
                               ascending=True,
                               feed_title=None):
     idx = self.db.FIELD_MAP['id']
     if not ids:
         raise web.HTTPError(404, reason='No books found')
     items = [x for x in self.db.data.iterall() if x[idx] in ids]
     self.sort(items, sort_by, ascending)
     max_items = CONF['max_opds_items']
     offsets = Offsets(offset, max_items, len(items))
     items = items[offsets.offset:offsets.offset + max_items]
     updated = self.db.last_modified()
     self.set_header('Last-Modified', self.last_modified(updated))
     self.set_header('Content-Type',
                     'application/atom+xml; profile=opds-catalog')
     return str(
         AcquisitionFeed(updated,
                         id_,
                         items,
                         offsets,
                         page_url,
                         up_url,
                         self.db,
                         CONF['url_prefix'],
                         title=feed_title))
示例#5
0
    def opds_category_group(self, category=None, which=None, offset=0):
        try:
            offset = int(offset)
        except:
            raise web.HTTPError(404, reason='Not found')

        if not which or not category:
            raise web.HTTPError(404, reason='Not found')

        categories = self.db.get_categories()
        page_url = url_for('opdscategorygroup', category=category, which=which)

        category = unhexlify(category)
        if category not in categories:
            raise web.HTTPError(404, reason='Category %r not found' % which)
        category_meta = self.db.field_metadata
        meta = category_meta.get(category, {})
        category_name = meta.get('name', which)
        which = unhexlify(which)
        feed_title = default_feed_title + ' :: ' + (_('By {0} :: {1}').format(
            category_name, which))
        owhich = hexlify('N' + which)
        up_url = url_for('opdsnavcatalog', which=owhich)
        items = categories[category]

        def belongs(x, which):
            return first_char(x).lower() == which.lower()

        items = [x for x in items if belongs(x, which)]
        if not items:
            raise web.HTTPError(404,
                                reason='No items in group %r:%r' %
                                (category, which))
        updated = self.db.last_modified()

        id_ = 'calibre-category-group-feed:' + category + ':' + which
        max_items = CONF['max_opds_items']
        offsets = Offsets(offset, max_items, len(items))
        items = list(items)[offsets.offset:offsets.offset + max_items]

        self.set_header('Last-Modified', self.last_modified(updated))
        self.set_header('Content-Type', 'application/atom+xml; charset=UTF-8')

        return str(
            CategoryFeed(items,
                         category,
                         id_,
                         updated,
                         offsets,
                         page_url,
                         up_url,
                         self.db,
                         title=feed_title))
示例#6
0
    def opds_category_group(self,
                            category=None,
                            which=None,
                            version=0,
                            offset=0):
        try:
            offset = int(offset)
            version = int(version)
        except:
            raise cherrypy.HTTPError(404, 'Not found')

        if not which or not category or version not in BASE_HREFS:
            raise cherrypy.HTTPError(404, 'Not found')

        categories = self.categories_cache(
            self.get_opds_allowed_ids_for_version(version))
        page_url = url_for('opdscategorygroup',
                           version,
                           category=category,
                           which=which)

        category = unhexlify(category)
        if category not in categories:
            raise cherrypy.HTTPError(404, 'Category %r not found' % which)
        which = unhexlify(which)
        owhich = hexlify('N' + which)
        up_url = url_for('opdsnavcatalog', version, which=owhich)
        items = categories[category]

        def belongs(x, which):
            return getattr(x, 'sort', x.name).lower().startswith(which.lower())

        items = [x for x in items if belongs(x, which)]
        if not items:
            raise cherrypy.HTTPError(
                404, 'No items in group %r:%r' % (category, which))
        updated = self.db.last_modified()

        id_ = 'calibre-category-group-feed:' + category + ':' + which

        max_items = self.opts.max_opds_items
        offsets = Offsets(offset, max_items, len(items))
        items = list(items)[offsets.offset:offsets.offset + max_items]

        cherrypy.response.headers['Last-Modified'] = self.last_modified(
            updated)
        cherrypy.response.headers['Content-Type'] = 'application/atom+xml'

        return str(
            CategoryFeed(items, category, id_, updated, version, offsets,
                         page_url, up_url, self.db))