~davidc3/onehundredscopes/youporn-precise

« back to all changes in this revision

Viewing changes to src/unity-scope-youporn

  • Committer: David Callé
  • Date: 2011-12-17 19:14:07 UTC
  • Revision ID: davidc@framli.eu-20111217191407-2cszgj46upa75ci0
Init + packaging

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
 
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
from lxml.html import fromstring
 
25
import urllib2
 
26
 
 
27
socket.setdefaulttimeout(3)
 
28
BUS_NAME = "net.launchpad.scope.video.youporn"
 
29
 
 
30
class Daemon:
 
31
        def __init__ (self):
 
32
                self.scope = Unity.Scope.new ("/net/launchpad/scope/video/youporn")
 
33
                self.scope.search_in_global = True
 
34
                self.scope.connect ("notify::active-search", self.on_search_changed)
 
35
                self.scope.connect ("filters-changed", self.on_search_changed);
 
36
#               self.scope.connect ("notify::active", self.on_search_changed);
 
37
                self.scope.connect ("activate-uri", self.on_activate_uri);
 
38
                self.scope.export()
 
39
 
 
40
        def on_activate_uri (self, scope, uri):
 
41
                print uri
 
42
                if uri.split('__')[0] == "page":
 
43
#                       self.scope.props.results_model.clear()
 
44
                        results = self.scope.props.results_model
 
45
                        results.remove(results.get_iter_at_row(results.get_n_rows()-1))
 
46
                        self.update_results_model (uri.split('__')[2], results, int(uri.split('__')[1])+1)
 
47
                        return Unity.ActivationResponse(handled=Unity.HandledType.SHOW_DASH, goto_uri='')
 
48
                else:
 
49
#               GLib.spawn_command_line_async("totem")
 
50
#               video = self.get_video_from_uri(uri)
 
51
#               if video != '':
 
52
#                       print 'Opening %s' % video
 
53
#                       GLib.spawn_command_line_async("totem '%s'" % video)
 
54
#               else:
 
55
#                       "No downloadable video can't be found at %s" % uri
 
56
                        GLib.spawn_command_line_async(uri)
 
57
                        return Unity.ActivationResponse(handled=Unity.HandledType.HIDE_DASH, goto_uri='')
 
58
 
 
59
        def get_search_string (self):
 
60
                
 
61
                search = self.scope.props.active_search
 
62
                return search.props.search_string if search else None
 
63
 
 
64
        def on_search_changed (self, scope, param_spec=None):
 
65
                        search = self.get_search_string()
 
66
                        print "Search changed to: '%s'" % search
 
67
                        results = self.scope.props.results_model
 
68
                        results.clear()
 
69
                        self.update_results_model (search, results, "1")
 
70
                        results.flush_revision_queue ()
 
71
 
 
72
        def update_results_model(self, search, model, page):
 
73
                try:
 
74
                        active_filter = self.scope.get_filter("porn_categories").get_active_option().props.id
 
75
                        if active_filter == "amateur":
 
76
                                cat_id = "1"
 
77
                        elif active_filter == "anal":
 
78
                                cat_id = "2"
 
79
                        elif active_filter == "blowjob":
 
80
                                cat_id = "9"
 
81
                        elif active_filter == "cunnilingus":
 
82
                                cat_id = "15"
 
83
                        elif active_filter == "gay":
 
84
                                cat_id = "20"
 
85
                        elif active_filter == "group-sex":
 
86
                                cat_id = "21"
 
87
                        elif active_filter == "lesbian":
 
88
                                cat_id = "26"
 
89
                        elif active_filter == "masturbate":
 
90
                                cat_id = "55"
 
91
                        elif active_filter == "dildos-toys":
 
92
                                cat_id = "44"
 
93
                        elif active_filter == "webcam":
 
94
                                cat_id = "35"
 
95
                        else:
 
96
                                cat_id = ""
 
97
                except:
 
98
                        cat_id = ""
 
99
                        active_filter = ""
 
100
                pages = [page]
 
101
                for page in pages:
 
102
                        if search == "None":
 
103
                                search = ''
 
104
                        print page
 
105
                        for i in self.youporn(search, active_filter, cat_id, page):
 
106
                                title = i[0]
 
107
                                comment = i[1]
 
108
                                uri = i[4]
 
109
                                icon_hint = i[3]
 
110
                                dnd_uri = i[2]
 
111
                                model.append (uri, icon_hint, 0,"text/html", title, comment, dnd_uri)
 
112
                        model.flush_revision_queue ()
 
113
                model.append ("page__"+str(page)+"__"+str(search), "add", 0,"text/html", "More results", "", "")
 
114
 
 
115
                if self.scope.props.active_search:
 
116
                        self.scope.props.active_search.emit('finished')
 
117
 
 
118
        def youporn(self, search, cat_name, cat_id, page):
 
119
                if not search or search == '':
 
120
                        if cat_name != '':
 
121
                                url = "http://www.youporn.com/category/%s/%s/?page=%s" % (cat_id, cat_name, page)
 
122
                        else:
 
123
                                url = "http://www.youporn.com/?page=%s" % (page)
 
124
                else:
 
125
                        search = search.replace(" ", "+")
 
126
                        url = "http://www.youporn.com/search/time?query=%s&page=%s&category_id=%s" % (search, page, cat_id)
 
127
                print url
 
128
                try:
 
129
                        results = urllib2.urlopen(url).read()
 
130
                        tree = fromstring(results)
 
131
                        href = []
 
132
                        youporn_href = []
 
133
                        title = []
 
134
                        icons = []
 
135
                        duration = []
 
136
                        ratings = []
 
137
                
 
138
        # Parse for thumbnails
 
139
                        for img in tree.cssselect('a img'):
 
140
                                try:
 
141
                                        if 'thumb' in img.get('id'):
 
142
                                                icons.append(img.get('src'))
 
143
                                except:
 
144
                                        pass
 
145
                                
 
146
        # Parse for uris and titles
 
147
                        for a in tree.cssselect('h1 a'):
 
148
                                if a.get('href').startswith("/watch/"):
 
149
                                        youporn_href.append(a.get('href'))
 
150
                                        title.append(a.text_content())
 
151
                                        href.append(a.get('href'))
 
152
                                        
 
153
        # Parse for duration
 
154
                        for h2 in tree.cssselect('div h2'):
 
155
                                if ':' in h2.text_content():
 
156
                                        duration.append(h2.text_content())
 
157
                                        
 
158
        # Parse for rating
 
159
                        for p in tree.cssselect('div p'):
 
160
                                if '/' in p.text_content():
 
161
                                        p = p.text_content().replace('\n','').replace(' ','').split(' / ')[0]
 
162
                                        full_stars = int(round(float(p),0))
 
163
                                        empty_stars = 5 - full_stars
 
164
                                        rating = (u"\u2605" * full_stars) + (u"\u2606" * empty_stars)
 
165
                                        if full_stars < 1:
 
166
                                                rating = u"\u2606" * 5
 
167
                                        ratings.append(rating)
 
168
 
 
169
        # Parse results 
 
170
                        i = -1
 
171
                        result_list = []
 
172
                        for uri in href:
 
173
                                i = i+1
 
174
                # Add to the list
 
175
                                item = []
 
176
                                item.append(title[i])
 
177
#                               try:
 
178
#                                       item.append(rating[i]+'\n'+duration[i])
 
179
#                               except:
 
180
                                item.append(duration[i])
 
181
                                item.append("")
 
182
                                item.append(icons[i])
 
183
                                item.append("/usr/lin/unity-scope-youporn/tiny-video-parser 'http://youporn.com%s'" % uri)
 
184
                                result_list.append(item)
 
185
                        return result_list
 
186
                except (urllib2.URLError):
 
187
                        print "Connection to http://www.youporn.com is impossible"
 
188
                        return []
 
189
 
 
190
        def to_KiB(self, file_size):
 
191
                if 'KiB' in file_size:
 
192
                        file_unit = 'KiB'
 
193
                        file_in_KiB = float(file_size.strip(file_unit))
 
194
                elif 'MiB' in file_size:
 
195
                        file_unit = 'MiB'
 
196
                        file_in_KiB = float(file_size.strip(file_unit))*1000
 
197
                elif 'GiB' in file_size:
 
198
                        file_unit = 'GiB'
 
199
                        file_in_KiB = float(file_size.strip(file_unit))*1000000
 
200
                else:
 
201
                        file_in_KiB = 0
 
202
                return file_in_KiB
 
203
                
 
204
        def get_video_from_uri(self, uri):
 
205
                video = ''
 
206
                results = urllib2.urlopen(uri).read()
 
207
                tree = fromstring(results)
 
208
                for a in tree.cssselect('div ul li p a'):
 
209
                        if a.get('href').startswith("http://download.youporn.com/download/") and 'iPhone' in a.text_content():
 
210
                                video = a.get('href')
 
211
                return video
 
212
 
 
213
if __name__ == "__main__":
 
214
        session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
215
        session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
216
                                              'org.freedesktop.DBus',
 
217
                                              '/org/freedesktop/DBus',
 
218
                                              'org.freedesktop.DBus', None)
 
219
        result = session_bus.call_sync('RequestName',
 
220
                                       GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
221
                                       0, -1, None)
 
222
                                       
 
223
        # Unpack variant response with signature "(u)". 1 means we got it.
 
224
        result = result.unpack()[0]
 
225
        
 
226
        if result != 1 :
 
227
                print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
228
                raise SystemExit (1)
 
229
        
 
230
        daemon = Daemon()
 
231
        GObject.MainLoop().run()