'The Serendipity button'

@require_oauth2_scope('read')
@api_doc(api_section.listings, uses_site=True)
def GET_random(self):

    sort = rand.choice(('new', 'hot'))
    q = c.site.get_links(sort, 'all')
    if isinstance(q, thing.Query):
        q._limit = g.num_serendipity
        names = [link._fullname for link in q]
    else:
        names = list(q)[:g.num_serendipity]
    rand.shuffle(names)
    def keep_fn(item):
        return (item.fresh and item.keep_item(item) and item.subreddit.discoverable)
    builder = IDBuilder(names, skip=True, keep_fn=keep_fn, num=1)
    (links, first, last, before, after) = builder.get_items()
    if links:
        redirect_url = links[0].make_permalink_slow(force_domain=True)
        return self.redirect(redirect_url)
    else:
        return self.redirect(add_sr('/'))
