Exemplo n.º 1
0
    def __str__(self):
        """Returns the xml text for the podcast feed itself."""
        itunes_url = "http://www.itunes.com/dtds/podcast-1.0.dtd"
        E = ElementMaker(nsmap={'itunes': itunes_url})
        iE = ElementMaker(namespace=itunes_url, nsmap={'itunes': itunes_url})

        rss = E.rss({'version': '2.0'})
        channel = E.channel(E.title(self.title), E.link(self.link))
        rss.append(channel)
        if self.description:
            channel.append(E.description(self.description))
        if self.author:
            channel.append(iE.author(self.author))
        if self.owneremail:
            owner = iE.owner(iE.email(self.owneremail))
            channel.append(owner)
            if self.ownername:
                owner.append(iE.name(self.ownername))

        if self.imageurl:
            channel.append(iE.image({'href': self.imageurl}))

        for i in self.item_list:
            # i (iterator) vs item (lxml element) is confusing
            itemurl = self.baseurl + i.shortfilename
            item = E.item(E.title(i.title),
                    E.link(itemurl),
                    E.pubdate(i.pubdate),
                    E.enclosure({'url': itemurl,
                                 'length': i.filesize,
                                 'type': 'audio/mpeg'}),
                    iE.subtitle(i.subtitle),
                    iE.summary(i.summary),
                    iE.duration(i.duration),
                    )
            if i.author:
                item.append(iE.author(i.author))
            if i.imageurl:
                item.append(iE.image({'href': i.imageurl}))
            channel.append(item)

        return etree.tostring(rss, xml_declaration=True, encoding="UTF-8",
                                pretty_print=True)
Exemplo n.º 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