~davidc3/onehundredscopes/rottentomatoes-reloaded

« back to all changes in this revision

Viewing changes to src/unity-scope-rottentomatoes

  • Committer: David Call√©
  • Date: 2012-06-11 20:00:35 UTC
  • Revision ID: davidc@framli.eu-20120611200035-weoar3bjiietxa7f
Branch init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
#    Copyright (c) 2011 David Calle <davidc@framli.eu>
 
4
 
 
5
#    This program is free software: you can redistribute it and/or modify
 
6
#    it under the terms of the GNU General Public License as published by
 
7
#    the Free Software Foundation, either version 3 of the License, or
 
8
#    (at your option) any later version.
 
9
 
 
10
#    This program is distributed in the hope that it will be useful,
 
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#    GNU General Public License for more details.
 
14
 
 
15
#    You should have received a copy of the GNU General Public License
 
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
import sys
 
19
from gi.repository import GLib, GObject, Gio
 
20
from gi.repository import Dee
 
21
from gi.repository import Unity
 
22
import socket, urllib, urllib2, simplejson
 
23
 
 
24
BUS_NAME = "net.launchpad.scope.rottentomatoes"
 
25
 
 
26
class Daemon:
 
27
 
 
28
    def __init__ (self):
 
29
        # The path for the Lens *must* also match the one in our .lens file
 
30
        self.scope = Unity.Scope.new ("/net/launchpad/scope/rottentomatoes")
 
31
        self.scope.connect("search-changed", self.on_search_changed)
 
32
        svg_dir = "/usr/share/icons/unity-icon-theme/places/svg/"
 
33
        self.scope.export()
 
34
        print "babar"
 
35
 
 
36
    def on_search_changed (self, scope, search, search_type, *_):
 
37
        print "toto"
 
38
        if search_type is Unity.SearchType.GLOBAL:
 
39
            search_string = search.props.search_string.strip()
 
40
            print "Search changed to \"%s\"" % search_string
 
41
            model = search.props.results_model
 
42
            model.clear ()
 
43
            self.update_results_model(scope, model, search_string)
 
44
            self.error_message = "Sorry, there are no movies that match your search."
 
45
            search.set_reply_hint ("no-results-hint", GLib.Variant.new_string(self.error_message))
 
46
        search.finished ()
 
47
        
 
48
    def update_results_model (self, scope, model, search_string):
 
49
        if search_string:
 
50
            data_list = []
 
51
            for i in self.rottentomatoes_search(search_string)['movies']:
 
52
                item_list = []
 
53
                url = i['links']['alternate']
 
54
                icon_hint = i['posters']['thumbnail']
 
55
                authors = str(i['year'])
 
56
                title = i['title']
 
57
                if i['ratings']['critics_score']:
 
58
                    full_stars = int(i['ratings']['critics_score'])/20
 
59
                    empty_stars = 5 - full_stars
 
60
                    authors = (u"\u2605" * full_stars) + (u"\u2606" * empty_stars)
 
61
                    if full_stars < 1:
 
62
                        authors = u"\u2606" * 5
 
63
                    try:
 
64
                        authors = authors + "\n" + i['critics_consensus']
 
65
                    except (KeyError):
 
66
                        pass
 
67
 
 
68
                item_list.append(title)
 
69
                item_list.append(authors)
 
70
                item_list.append(url)
 
71
                item_list.append(icon_hint)
 
72
                data_list.append(item_list)
 
73
            for i in data_list:
 
74
                title = i[0]
 
75
                comment = i[1]
 
76
                uri = i[2]
 
77
                icon_hint = i[3]
 
78
                model.append (uri, icon_hint, 2,"text/html", title, comment, uri)
 
79
 
 
80
    #
 
81
    # Rotten Tomatoes search
 
82
    # <http://developer.rottentomatoes.com/docs>
 
83
    #
 
84
    def rottentomatoes_search(self, q):
 
85
        try:
 
86
            query = urllib.urlencode({'q': q })
 
87
            if query != '' or query != 'q=' or query != 'q':
 
88
                url = ('http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=mfdgjzhrg7jmzj4vhsd7bh7e&%s'
 
89
                  % (query))
 
90
                print url
 
91
                f = urllib2.urlopen(url)
 
92
                content = f.read()
 
93
                results = simplejson.loads(content)
 
94
                if results['total'] != "0":
 
95
                    return results
 
96
                else:
 
97
                    return []
 
98
            else:
 
99
                return
 
100
        except (IOError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
 
101
            print "rottentomatoes_search_is_error"
 
102
            return
 
103
 
 
104
        
 
105
if __name__ == "__main__":
 
106
    session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
107
    session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
108
                                          'org.freedesktop.DBus',
 
109
                                          '/org/freedesktop/DBus',
 
110
                                          'org.freedesktop.DBus', None)
 
111
    result = session_bus.call_sync('RequestName',
 
112
                                   GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
113
                                   0, -1, None)
 
114
                                   
 
115
    # Unpack variant response with signature "(u)". 1 means we got it.
 
116
    result = result.unpack()[0]
 
117
    
 
118
    if result != 1 :
 
119
        print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
120
        raise SystemExit (1)
 
121
    
 
122
    daemon = Daemon()
 
123
    GObject.MainLoop().run()