def create_feed(req_params, feed_data, get_callback, parse_callback): """Create an Atom feed based on provided data and callbacks. This function will retrieve the results, parse them and create the Atom feed. The result retrieving is done calling the `get_callback` function, and the parsing calling the `parse_callback` function. :param req_params: The parameters to be passed the the `get_callback` function to retrieve the results from the backend. :type req_params: list :param feed_data: All the necessary data for the feed. :type feed_data: dict :param get_callback: Function that will be called to retrieve the results from the backend. Only `req_params` will be passed to it. :type get_callback: function :param parse_callback: Function that will be called to parse the results obtained from the backend. It must accept two parameters: `results` and `feed_data`. """ d_get = feed_data.get feed = app.cache.get(d_get("cache_key")) if not feed: feed = watom.AtomFeed( d_get("title"), title_type="text", feed_url=d_get("feed_url"), url=d_get("host_url"), subtitle=d_get("subtitle"), generator=(AUTHOR_NAME, d_get("host_url"), __version__), author=AUTHOR_NAME ) results = get_callback(req_params) if results: feed_categories = d_get("feed_categories") for parsed in parse_callback(results, feed_data): p_get = parsed.get feed.add( categories=feed_categories, content=p_get("content"), content_type="html", links=p_get("links"), published=p_get("published"), title=p_get("title"), title_type="text", updated=p_get("updated"), url=p_get("url") ) return feed
def blog_feed(bl_filter_type=None, bl_filter=None): def make_external(url): return urllib.parse.urljoin(flask.request.url_root, url) # Build the filter. _filter = bl_filter_function(bl_filter_type, bl_filter) if _filter is None: flask.abort(404) # Create Atom feed. feed = atom.AtomFeed( title="Cyphar's Blog", title_type="text", author="Aleksa Sarai", rights= "Copyright (C) 2014-2017 Aleksa Sarai. Licensed under CC-BY-SA 4.0.", rights_type="text", subtitle="The wild ramblings of Aleksa Sarai.", subtitle_type="text", feed_url=flask.request.url, url=make_external( flask.url_for("blog", bl_filter_type=bl_filter_type, bl_filter=bl_filter))) # Get latest posts. posts = _get_posts(_filter)[:ATOM_FEED_SIZE] # Add posts to feed. for post in posts: feed.add(title=post.meta["title"], title_type="text", author=post.meta["author"], url=make_external(post.meta["url"]), summary=post.meta["description"], summary_type="html", updated=post.meta["updated"], published=post.meta["published"], categories=[{ "term": tag } for tag in post.meta["tags"]]) # Generate Atom response. return feed.get_response()
def atom_feed(): """Returns an atom feed with the latest items. Based on http://flask.pocoo.org/snippets/10/ """ db_session = database.get_session() feed = atom.AtomFeed('Recently added items', feed_url=flask.request.url, url=flask.request.url_root) items = db_session.query(database.Item).order_by( database.Item.datetime.desc()).limit(20).all() for item in items: feed.add(item.name, unicode(item.description), content_type='html', url='%scatalog/%s/%d' % (flask.request.url_root, item.category.name, item.id), updated=item.datetime, published=item.datetime) return feed.get_response()
def all_build_feed(): """Create a daily Atom feed for available builds. :return The Atom feed as an XML string. """ build_feed = None host_url = request.host_url feed_url = request.url cache_key = hashlib.md5(feed_url).digest() cached = app.cache.get(cache_key) if cached: build_feed = cached else: feed = watom.AtomFeed(u"Kernel CI \u2014 Latest Builds", title_type="text", feed_url=feed_url, url=host_url, subtitle=u"Latest builds available", generator=(AUTHOR_NAME, host_url, __version__), author=AUTHOR) status, results = _get_build_results() if status == 200: if results: for data in _parse_build_results(results): feed.add(title=data[0], title_type="text", content=data[1], url=data[2], content_type="html", updated=data[4], published=data[4], categories=BUILD_FEED_CATEGORIES, links=[{ "href": data[2], "rel": "alternate" }, { "href": data[3], "rel": "related" }]) else: error_title = u"Error loading builds data" error_str = ( "<p>" + "No builds data could be loaded from the server (Error: %d)." "<br />" "Please report this problem." "</p>" % status) now = datetime.datetime.now(tz=bson.tz_util.utc) feed.add(author=AUTHOR, title=error_title, title_type="text", url=feed_url, content=error_str, content_type="html", updated=now, published=now) build_feed = feed.get_response() app.cache.set(cache_key, build_feed, timeout=60 * 60) return build_feed