示例#1
0
def get_entity_references_(url):
    data = geturl('http://api.evri.com/v1/media/related.json?includeTopEntities=true&entityURI=' + urllib.quote(url))
    data = json.loads(data)
    data = data['evriThing']['mediaResult']['articleList']['article']
    def get_path(article):
        link = article.get('link', {})
        try:
            return 'http://%s%s' % (link['@hostName'], link['@path'])
        except KeyError:
            return ''

    def getentities(article):
        try:
            entities = mklist(article['topEntities']['entity'])
        except KeyError: return []
        return [dict(
                     name=getname(entity),
                     ref=gethref(entity),
                    ) for entity in entities]
        
    return [dict(
                 author=article.get('author', {}).get('$', ''),
                 content=article.get('content', {}).get('$', ''),
                 title=article.get('title', {}).get('$', ''),
                 url=get_path(article),
                 entities=getentities(article),
            ) for article in data]
示例#2
0
 def get(self):
     search = self.request.get('search', '')
     gurl = ('http://content.guardianapis.com/search?q=%s&format=json' %
             urllib.quote(search))
     results = geturl(gurl)
     results = json.loads(results)
     try:
         results = results['response']['results']
     except KeyError:
         results = []
     context = dict(search=search)
     fullresults = []
     count = 0
     for result in results:
         url = result['webUrl']
         if count < 1:
             entities = getentities.get_entities(url)
         else:
             entities = {}
         fullresults.append(dict(url=url.encode('utf8'),
                                 entities=entities.get('entities'),
                                 categories=entities.get('categories'),
                                 title=result['webTitle'],
                                 author='Guardian',
                            ))
         count += 1
     context['results'] = fullresults
     self.render("topic.html", context)
示例#3
0
def get_entities_(url):
    data = geturl('http://api.evri.com/v1/media/entities.json?uri=' + urllib.quote(url))
    data = json.loads(data)
    graph = data['evriThing']['graph']
    def getfacet(item):
        try:
            return item['facets']['facet']['$']
        except KeyError: pass
        return ''

    try:
        category_list=mklist(graph['categories']['category'])
        categories=[item['$'] for item in category_list]
    except KeyError:
        categories=[]

    try:
        entity_list = mklist(graph['entities']['entity'])
    except KeyError:
        entity_list = []

    return dict(
        categories=categories,
        entities=[dict(
                       name=getname(item),
                       facet=getfacet(item),
                       ref=gethref(item)
                      )
                  for item in entity_list],
    )