~davidc3/onehundredscopes/9gag

« back to all changes in this revision

Viewing changes to src/unity-scope-9gag

  • Committer: David Call√©
  • Date: 2011-12-30 09:25:59 UTC
  • Revision ID: davidc@framli.eu-20111230092559-l1afxt1tkcp8bf0o
Branch init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
# -*- coding: latin-1 -*-
 
3
 
 
4
#               Copyright (c) 2011 David Calle <davidc@framli.eu>
 
5
 
 
6
#               This program is free software: you can redistribute it and/or modify
 
7
#               it under the terms of the GNU General Public License as published by
 
8
#               the Free Software Foundation, either version 3 of the License, or
 
9
#               (at your option) any later version.
 
10
 
 
11
#               This program is distributed in the hope that it will be useful,
 
12
#               but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#               GNU General Public License for more details.
 
15
 
 
16
#               You should have received a copy of the GNU General Public License
 
17
#               along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
import sys, os
 
19
from gi.repository import GLib, GObject, Gio
 
20
from gi.repository import Dee
 
21
_m = dir(Dee.SequenceModel)
 
22
from gi.repository import Unity
 
23
import socket
 
24
 
 
25
socket.setdefaulttimeout(3)
 
26
BUS_NAME = "net.launchpad.scope.misc.ninegag"
 
27
 
 
28
class Daemon:
 
29
        def __init__ (self):
 
30
                self.scope = Unity.Scope.new ("/net/launchpad/scope/misc/ninegag")
 
31
                self.scope.search_in_global = False
 
32
                self.scope.connect ("notify::active-search", self.on_search_changed)
 
33
                self.scope.connect ("filters-changed", self.on_search_changed);
 
34
#               self.scope.connect ("notify::active", self.on_search_changed);
 
35
                self.scope.connect ("activate-uri", self.on_activate_uri);
 
36
                self.scope.export()
 
37
 
 
38
        def on_activate_uri (self, scope, uri):
 
39
                if uri.startswith("more"):
 
40
                        page = uri.split('__')[2]
 
41
                        search = uri.split('__')[1]
 
42
                        results = self.scope.props.results_model
 
43
                        results.remove(results.get_iter_at_row(results.get_n_rows()-1))
 
44
                        self.update_results_model (search, results, page)
 
45
                        return Unity.ActivationResponse(handled=Unity.HandledType.SHOW_DASH, goto_uri="")
 
46
                else:
 
47
#               GLib.spawn_command_line_async("eog %s" % uri)
 
48
                        return Unity.ActivationResponse(Unity.HandledType.HIDE_DASH, uri)
 
49
 
 
50
        def get_search_string (self):
 
51
                search = self.scope.props.active_search
 
52
                return search.props.search_string if search else None
 
53
 
 
54
        def on_search_changed (self, scope, param_spec=None):
 
55
                        search = self.get_search_string()
 
56
                        print "Search changed to: '%s'" % search
 
57
                        results = self.scope.props.results_model
 
58
                        results.clear()
 
59
                        self.update_results_model (search, results, '')
 
60
                        results.flush_revision_queue ()
 
61
 
 
62
        def update_results_model(self, search, model, page):
 
63
                if self.check_filters("art_categories") == "brushes" or self.check_filters("art_categories") is None:
 
64
                        i = None
 
65
                        for i in self.deviantart(search, page):
 
66
                                title = i[0]
 
67
                                comment = i[1]
 
68
                                uri = i[2]
 
69
                                icon_hint = i[3]
 
70
                                model.append (uri, icon_hint, 0,"text/html", title, comment, uri)
 
71
                        if i:
 
72
                                if not search:
 
73
                                        search = ''
 
74
                                page = int(comment)-1
 
75
                                model.append ("more__"+search+"__"+str(page), "add", 0,"text/html", "More results", '', '')
 
76
 
 
77
 
 
78
 
 
79
 
 
80
                if self.scope.props.active_search:
 
81
                        self.scope.props.active_search.emit("finished")
 
82
 
 
83
        def check_filters(self, filter_name):
 
84
                try:
 
85
                        filter = self.scope.get_filter(filter_name).get_active_option().props.id
 
86
                except (AttributeError):
 
87
                        filter = None
 
88
                return filter
 
89
 
 
90
 
 
91
        def deviantart(self, search, page):
 
92
                url = None
 
93
                if search == '' or search is None or search == "None":
 
94
                        url = "http://9gag.com/hot/%s" % (page)
 
95
                else:
 
96
                        url = "http://9gag.com/hot/%s" % (page)
 
97
                if url:
 
98
                        print url
 
99
                        from lxml.html import fromstring
 
100
                        import urllib2
 
101
                        results = urllib2.urlopen(url).read()
 
102
                        tree = fromstring(results)
 
103
                        href = []
 
104
                        icons = []
 
105
                        title = []
 
106
                        counter = 0
 
107
                        page = ''
 
108
                
 
109
        # Parse for href
 
110
                        for img in tree.cssselect('ul li div div a img'):
 
111
                                try:
 
112
                                        icons.append(img.get('src'))
 
113
                                        title.append(img.get('alt'))
 
114
                                        print img.get('href')
 
115
                                        print img.get('alt')
 
116
                                except:
 
117
                                        pass
 
118
                                        
 
119
                        for a in tree.cssselect('ul li div div a'):
 
120
                                try:
 
121
                                        if a.get('href').startswith('/gag/'):
 
122
                                                href.append(a.get('href'))
 
123
                                except:
 
124
                                        pass
 
125
                                        
 
126
                        for a in tree.cssselect('div a'):
 
127
                                print "toto"
 
128
                                if a.get('class') == "next":
 
129
                                        page = a.get('href').strip('/hot/')
 
130
                                                
 
131
 
 
132
 
 
133
                                
 
134
                                
 
135
        # Parse results 
 
136
                        i = 0
 
137
                        result_list = []
 
138
                        for icon in icons:
 
139
                # Add to the list
 
140
                                item = []
 
141
                                item.append(title[i])
 
142
                                item.append(page)
 
143
                                item.append('http://9gag.com%s' % (href[i]))
 
144
                                item.append(icon)
 
145
                                result_list.append(item)
 
146
                                i = i+1
 
147
                        return result_list
 
148
                else:
 
149
                        return []
 
150
 
 
151
        def rgb(self,c):
 
152
                split = (c[0:2], c[2:4], c[4:6])
 
153
                return [int(x, 16) for x in split]
 
154
 
 
155
if __name__ == "__main__":
 
156
        session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
157
        session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
158
                                                                                                                                                          'org.freedesktop.DBus',
 
159
                                                                                                                                                          '/org/freedesktop/DBus',
 
160
                                                                                                                                                          'org.freedesktop.DBus', None)
 
161
        result = session_bus.call_sync('RequestName',
 
162
                                                                                                                           GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
163
                                                                                                                           0, -1, None)
 
164
                                                                                                                           
 
165
        # Unpack variant response with signature "(u)". 1 means we got it.
 
166
        result = result.unpack()[0]
 
167
        
 
168
        if result != 1 :
 
169
                print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
170
                raise SystemExit (1)
 
171
        
 
172
        daemon = Daemon()
 
173
        GObject.MainLoop().run()