This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
globalvoices.py
49 lines (43 loc) · 1.61 KB
/
globalvoices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import json
import feedparser
import urllib
from urllib2 import urlopen
import HTMLParser
# figure out what dir we are in (needed to load other files when deploying to a server)
basedir = os.path.dirname(os.path.abspath(__file__))
# read in mapping of country names to paths to RSS feeds on the Global Voices server
f = open(basedir+'/globalvoices-countrypaths.json', 'r')
path_lookup = json.loads(f.read())
def recent_stories_from(country):
'''
Return a list of the last 3 stories for a given country
'''
h = HTMLParser.HTMLParser()
raw_content = urlopen( _content_url_via_google_for( country ) ).read()
content = json.loads( raw_content )
stories = []
for details in content['responseData']['feed']['entries']:
stories.append( {
'title': details['title'],
'link': details['link'],
'author': details['author'],
'contentSnippet': h.unescape(details['contentSnippet'])
} )
return stories
def country_list():
'''
Return a list of all the countries with feeds on the Global Voices site
'''
return path_lookup.keys()
def _content_url_via_google_for(country):
'''
Return the URL to the RSS content for a country via the Google API, so we can get in JSON directly
(rather than in XML)
'''
return "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=3&q="+ urllib.quote( _rss_url_for(country).encode("utf-8") )
def _rss_url_for(country):
'''
Return the URL to the RSS feed of stories for a country
'''
return "http://globalvoicesonline.org" + path_lookup[country] + "feed";