def make_feed(values): """Create atom:feed element from dict which follow structure: { 'title': str, '_id': str, 'updated': datetime, 'language': str, 'links': list, -- use :func:`opds.make_link` to create each item 'entry': list -- see :func:`make_entry` doc for structure of each item, } :param values: Catalog feed fields. :type values: dict. :returns: lxml.etree._Element. """ atom = ElementMaker(namespace=Namespace.ATOM, nsmap={'atom': Namespace.ATOM}) updated = values.get('updated', datetime.now()) feed = atom.feed( atom.id(values.get('_id', u'http://books.scielo.org/opds/')), atom.title(values.get('title', u'SciELO Books')), atom.updated(updated.strftime('%Y-%m-%dT%H:%M:%SZ')), atom.author( atom.name(u'SciELO Books'), atom.uri(u'http://books.scielo.org'), atom.email(u'*****@*****.**') ), atom.link(type=ContentType.NAVIGATION, href=u'/opds/', rel=u'start') ) links = values.get('links', []) for link in links: feed.append(atom.link(type=link['type'], href=link['href'], rel=link['rel'])) entries = values.get('entry', []) for entry_values in entries: feed.append(make_entry(entry_values)) return feed
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)