~submarine/unity-scope-googlenews/trunk

« back to all changes in this revision

Viewing changes to src/unity_googlenews_daemon.py

  • Committer: Tarmac
  • Author(s): James Henstridge
  • Date: 2013-03-19 09:21:20 UTC
  • mfrom: (52.2.4 unity-scope-googlnews)
  • Revision ID: tarmac-20130319092120-kiinj9sb5s9t8a25
Test suite fixes: remove the LOC global variable, and don't hit the network for the location check.

Approved by PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import locale
25
25
import gettext
26
26
 
27
 
try:
28
 
    locale.setlocale(locale.LC_MESSAGES, '')
29
 
    LOC = locale.getlocale(locale.LC_MESSAGES)[0].split(".")[0].lower()
30
 
except:
31
 
    LOC = 'en_us'
 
27
locale.setlocale(locale.LC_MESSAGES, '')
32
28
 
33
29
APP_NAME = 'unity-scope-googlenews'
34
30
LOCAL_PATH = '/usr/share/locale/'
40
36
 
41
37
GROUP_NAME = 'com.canonical.Unity.Scope.News.Googlenews'
42
38
UNIQUE_PATH = '/com/canonical/unity/scope/news/googlenews'
 
39
GEOIPLOOKUP_URI = 'http://geoiplookup.wikimedia.org'
43
40
SEARCH_URI = 'https://news.google.com/news/feeds'
44
41
SEARCH_HINT = _('Search Google News')
45
42
NO_RESULTS_HINT = _('Sorry, there are no Google News articles that match your search.')
74
71
    try:
75
72
        #FIXME geoip.ubuntu.com can have format errors,
76
73
        # but it should eventually be the source here
77
 
        f = urllib.request.urlopen('http://geoiplookup.wikimedia.org')
 
74
        f = urllib.request.urlopen(GEOIPLOOKUP_URI)
78
75
        content = f.read()
79
76
    except:
80
77
        print("Location unavailable")
96
93
        print(error)
97
94
        return ['us', '']
98
95
 
99
 
parameters = get_location()
100
 
geo_ln = parameters[0]
 
96
country = None
 
97
city = None
101
98
 
102
99
def search(search, filters):
103
100
    '''
113
110
    result_type (Unity ResultType)
114
111
    extras metadata fields (variant)
115
112
    '''
 
113
    global country, city
 
114
    if country is None:
 
115
        country, city = get_location()
 
116
 
116
117
    results = []
117
118
    for cat in range(0,2):
118
119
        topic = ''
119
120
        scoring = "r"
120
 
        ln = geo_ln
121
121
        if search:
122
122
            search = urllib.parse.quote(search)
123
123
            if cat == 0:
124
 
                rss = "%s?output=rss&q=%s&num=50&scoring=%s&ned=%s&geo=%s&topic=%s" % (SEARCH_URI, search, scoring, ln, parameters[1], topic)
 
124
                rss = "%s?output=rss&q=%s&num=50&scoring=%s&ned=%s&geo=%s&topic=%s" % (SEARCH_URI, search, scoring, country, city, topic)
125
125
            else:
126
 
                rss = "%s?output=rss&q=%s&num=50&scoring=%s&ned=%s&topic=%s" % (SEARCH_URI, search, scoring, ln, topic)
 
126
                rss = "%s?output=rss&q=%s&num=50&scoring=%s&ned=%s&topic=%s" % (SEARCH_URI, search, scoring, country, topic)
127
127
        else:
128
128
            if cat == 0:
129
 
                rss = "%s?output=rss&num=50&scoring=%s&ned=%s&geo=%s&topic=%s" % (SEARCH_URI, scoring, ln, parameters[1], topic)
 
129
                rss = "%s?output=rss&num=50&scoring=%s&ned=%s&geo=%s&topic=%s" % (SEARCH_URI, scoring, country, city, topic)
130
130
            else:
131
 
                rss = "%s?output=rss&num=50&scoring=%s&ned=%s&topic=%s" % (SEARCH_URI, scoring, ln, topic)
 
131
                rss = "%s?output=rss&num=50&scoring=%s&ned=%s&topic=%s" % (SEARCH_URI, scoring, country, topic)
132
132
        print(rss)
133
133
#        try:
134
134
        feed = feedparser.parse(rss)
143
143
                            'icon':img,
144
144
                            'category':cat,
145
145
                            'title':f['title'],
146
 
                            'language':GLib.Variant('s',ln),
 
146
                            'language':GLib.Variant('s',country),
147
147
                            'published':GLib.Variant('s',f['published'])})
148
148
#        except Exception as error:
149
149
#            print( error)