@access.public
    def getRelevantAds(self, params):
        AD_LIMIT = 20
        def sort_documents_by_url(documents):
            return sorted(documents, key=lambda x: x['url'])

        # @todo this assumes all URLs returned from Solr will properly urlparse
        def group_documents_by_domain(documents):
            return itertools.groupby(sort_documents_by_url(documents),
                                     lambda doc: urlparse(doc['url']).netloc)

        try:
            result = requests.get(os.environ['IMAGE_SPACE_SOLR'] + '/select', params={
                'wt': 'json',
                'q': 'outpaths:"%s"' % params['solr_image_id'],
                'fl': 'id,url',
                'rows': str(AD_LIMIT)
            }, verify=False).json()
        except __HOLE__:
            return {
                'numFound': 0,
                'docs': []
            }

        response = {
            'numFound': result['response']['numFound'],
            'docs': result['response']['docs'],
            'groupedDocs': []
        }

        for (domain, documents) in group_documents_by_domain(response['docs']):
            response['groupedDocs'].append([domain, list(documents)])

        # Display the domain with the largest number of documents first
        response['groupedDocs'] = sorted(response['groupedDocs'],
                                         key=lambda (_, docs): len(docs),
                                         reverse=True)

        return response
dataset/ETHPy150Open memex-explorer/image_space/imagespace/server/imagesearch_rest.py/ImageSearch.getRelevantAds