示例#1
0
def make_opds(monograph_dict):
    # title, id, au_name, updated, link are mandatory
    atom = ElementMaker(namespace = ATOM_NAMESPACE,
                 nsmap={'atom' : ATOM_NAMESPACE})
    dc = ElementMaker(namespace = OPDS_NAMESPACE,
                 nsmap={'dc' : OPDS_NAMESPACE})

    atom_id = 'http://www.feedbooks.com/book/{_id}'.format(**monograph_dict)
    xml = atom.entry(
        atom.title(monograph_dict['title']),
        atom.id('http://www.feedbooks.com/book/35'),
        atom.author(atom.name('H. G. Wells')
        ),
        atom.updated('2011-12-10T02:18:33Z'),
        atom.link(type='text/html', title='View on Feedbooks',
            href=atom_id, rel='alternate')
    )
    if 'language' in monograph_dict:
        xml.append(dc.language(monograph_dict['language']))
    if 'year' in monograph_dict:
        xml.append(dc.issued(monograph_dict['year']))

    return etree.tostring(xml, pretty_print=True)
示例#2
0
def make_entry(values):
    """Create atom:entry element from dict which follow structure:
    {
        'title': str,           # REQUIRED
        '_id': str,             # REQUIRED
        'updated': datetime,
        'language': str,
        'year': str,
        'publisher': str,
        'eisbn': str,
        'links': list, -- use :func:`opds.make_link` to create each item
        'pdf_file': str,
        'epub_file': str,
        'cover_thumbnail': str,
        'cover': str,
        'content': str,
        'synopsis': str,
        'creators': dict
    }

    :param values: Catalog entry fields.
    :type values: dict.
    :returns: lxml.etree._Element.
    """
    atom = ElementMaker(namespace=Namespace.ATOM,
        nsmap={'atom': Namespace.ATOM})
    dc = ElementMaker(namespace=Namespace.OPDS,
        nsmap={'dc': Namespace.OPDS})

    entry = atom.entry(
        atom.title(values['title']),
        atom.id(values['_id']))

    updated = values.get('updated', datetime.now())
    entry.append(atom.updated(updated.strftime('%Y-%m-%dT%H:%M:%SZ')))

    if 'language' in values:
        entry.append(dc.language(values['language']))

    if 'year' in values:
        entry.append(dc.issued(values['year']))

    if 'publisher' in values:
        entry.append(dc.publisher(values['publisher']))

    if 'eisbn' in values:
        entry.append(dc.identifier('urn:isbn:%s' % format(values['eisbn'])))

    links = values.get('links', [])
    for link in links:
        entry.append(atom.link(type=link['type'],
            href=link['href'], rel=link['rel']))

    if 'pdf_file' in values:
        link = values['pdf_file']
        entry.append(atom.link(type=link.get('type', 'application/pdf'),
            href=link['uri'], rel=LinkRel.ACQUISITION))

    if 'epub_file' in values:
        link = values['epub_file']
        entry.append(atom.link(type=link.get('type', 'application/epub+zip'),
            href=link['uri'], rel=LinkRel.ACQUISITION))

    if 'cover_thumbnail' in values:
        link = values['cover_thumbnail']
        entry.append(atom.link(type=link.get('type', 'image/jpeg'),
            href=link['uri'], rel=LinkRel.THUMBNAIL))

    if 'cover' in values:
        link = values['cover']
        entry.append(atom.link(type=link.get('type', 'image/jpeg'),
            href=link['uri'], rel=LinkRel.IMAGE))

    if 'content' in values:
        entry.append(atom.content(values['content']['value'],
            type=values['content'].get('type', 'text')))

    if 'synopsis' in values:
        entry.append(atom.summary(values['synopsis']))

    creators = values.get('creators', {})
    for author_key in ('individual_author', 'corporate_author', 'organizer'):
        for author in creators.get(author_key, []):
            new_author = atom.author(atom.name(author[0]))
            if author[1]:
                new_author.append(atom.uri(author[1]))
            entry.append(new_author)

    for contributor_key in ('editor', 'translator', 'collaborator', 'other',
        'coordinator'):
        for contributor in creators.get(contributor_key, []):
            new_contrib = atom.contributor(atom.name(contributor[0]))
            if contributor[1]:
                new_contrib.append(atom.uri(contributor[1]))
            entry.append(new_contrib)
    return entry