Merge lp:~chipaca/unity-lens-video/refresh-sources-and-cache-recommendations-and-refresh-them-too-why-not into lp:unity-lens-video/remote-videos-scope-trunk

Proposed by John Lenton
Status: Merged
Merged at revision: 35
Proposed branch: lp:~chipaca/unity-lens-video/refresh-sources-and-cache-recommendations-and-refresh-them-too-why-not
Merge into: lp:unity-lens-video/remote-videos-scope-trunk
Diff against target: 123 lines (+50/-9)
1 file modified
src/unity-scope-video-remote (+50/-9)
To merge this branch: bzr merge lp:~chipaca/unity-lens-video/refresh-sources-and-cache-recommendations-and-refresh-them-too-why-not
Reviewer Review Type Date Requested Status
David Callé Pending
Review via email: mp+94674@code.launchpad.net

Description of the change

Refreshing of sources, and early fetching, and caching, and refreshing of recommendations.

Apologies for earlier merge proposal, which was targeted at the wrong branch.

To post a comment you must log in.
37. By John Lenton

only update sources_list if it changes

38. By John Lenton

a bit of clenaup

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/unity-scope-video-remote'
2--- src/unity-scope-video-remote 2012-02-24 10:34:11 +0000
3+++ src/unity-scope-video-remote 2012-02-26 16:58:17 +0000
4@@ -20,7 +20,7 @@
5
6 import json
7 import sys
8-import urllib
9+from urllib import urlencode
10 import time
11
12 #pylint: disable=E0611
13@@ -45,6 +45,9 @@
14 BUS_NAME = "net.launchpad.scope.RemoteVideos"
15 SERVER = "http://video.u1.to/v0"
16
17+REFRESH_INTERVAL = 3600 # fetch sources & recommendations once an hour
18+RETRY_INTERVAL = 60 # retry sources/recommendations after a minute
19+
20 # Be cautious with Zeitgeist, don't assume it's ready
21 try:
22 ZGCLIENT = ZeitgeistClient()
23@@ -60,7 +63,8 @@
24 """Create the scope and listen to Unity signals"""
25
26 def __init__(self):
27- self.sources_list = None
28+ self.sources_list = []
29+ self.recommendations = []
30 self.scope = Unity.Scope.new("/net/launchpad/scope/remotevideos")
31 self.scope.search_in_global = False
32 self.scope.connect("search-changed", self.on_search_changed)
33@@ -74,20 +78,49 @@
34 self.session.user_agent = "Unity Video Lens Remote Scope v0"
35 self.session.add_feature_by_type(SoupGNOME.ProxyResolverGNOME)
36 self.query_list_of_sources()
37+ self.update_recommendations()
38 self.scope.export()
39
40+ def update_recommendations(self):
41+ """Query the server for 'recommendations'.
42+
43+ In v0, that means simply do an empty search.
44+ """
45+ recs = self.get_results('', [])
46+ if recs:
47+ self.recommendations = recs
48+ dt = REFRESH_INTERVAL
49+ else:
50+ dt = RETRY_INTERVAL
51+ GLib.timeout_add_seconds(dt, self.update_recommendations)
52+
53 def query_list_of_sources(self):
54 """Query the server for a list of sources that will be used
55 to build sources filter options and search queries."""
56 msg = Soup.Message.new("GET", SERVER + "/sources")
57 if self.session.send_message(msg) != 200:
58- self.sources_list = []
59 print "Error: Unable to query the server for a list of sources"
60 print " %d: %s" % (msg.status_code, msg.reason_phrase)
61 else:
62- self.sources_list = json.loads(msg.response_body.data)
63- for n, source in enumerate(self.sources_list):
64- self.scope.props.sources.add_option(str(n), source, None)
65+ try:
66+ sources_list = json.loads(msg.response_body.data)
67+ except ValueError:
68+ print "Error: got garbage json from the server"
69+ else:
70+ if sources_list != self.sources_list:
71+ self.sources_list = sources_list
72+ sources = self.scope.props.sources
73+ for opt in sources.options[:]:
74+ sources.remove_option(opt.props.id)
75+ for n, source in enumerate(self.sources_list):
76+ sources.add_option(str(n), source, None)
77+ if self.sources_list:
78+ # refresh later
79+ dt = REFRESH_INTERVAL
80+ else:
81+ # retry soon
82+ dt = RETRY_INTERVAL
83+ GLib.timeout_add_seconds(dt, self.query_list_of_sources)
84
85 def on_filtering_changed(self, *_):
86 """Run another search when a filter change is notified."""
87@@ -162,10 +195,12 @@
88
89 def get_results(self, search, sources):
90 """Query the server with the search string and the list of sources."""
91+ if not search and not sources and self.recommendations:
92+ return self.recommendations
93 query = dict(q=search)
94 if sources:
95 query['sources'] = sources.encode("utf-8")
96- query = urllib.urlencode(query)
97+ query = urlencode(query)
98 url = "%s/search?%s" % (SERVER, query)
99 print "Querying the server:", url
100 msg = Soup.Message.new("GET", url)
101@@ -174,7 +209,13 @@
102 print " %d: %s" % (msg.status_code, msg.reason_phrase)
103 return []
104 else:
105- return json.loads(msg.response_body.data)
106+ try:
107+ data = json.loads(msg.response_body.data)
108+ except ValueError:
109+ print "Error: got garbage json from the server"
110+ return []
111+ else:
112+ return data
113
114 def on_activate_uri (self, scope, rawuri):
115 """On item clicked, parse the custom uri items"""
116@@ -186,7 +227,7 @@
117 self.zeitgeist_insert_event(uri, title, icon)
118 # Use direct glib activation instead of Unity's own method
119 # to workaround an ActivationResponse bug.
120- GLib.spawn_command_line_async("gvfs-open "+uri)
121+ GLib.spawn_async(["/usr/bin/gvfs-open", uri])
122 # Then close the Dash
123 return Unity.ActivationResponse(goto_uri='', handled=2 )
124

Subscribers

People subscribed via source and target branches

to all changes: