~davidc3/onehundredscopes/deviantart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/python
# -*- coding: latin-1 -*-

#		Copyright (c) 2011 David Calle <davidc@framli.eu>

#		This program is free software: you can redistribute it and/or modify
#		it under the terms of the GNU General Public License as published by
#		the Free Software Foundation, either version 3 of the License, or
#		(at your option) any later version.

#		This program is distributed in the hope that it will be useful,
#		but WITHOUT ANY WARRANTY; without even the implied warranty of
#		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#		GNU General Public License for more details.

#		You should have received a copy of the GNU General Public License
#		along with this program.  If not, see <http://www.gnu.org/licenses/>.
import sys, os
from gi.repository import GLib, GObject, Gio
from gi.repository import Dee
_m = dir(Dee.SequenceModel)
from gi.repository import Unity
import socket

socket.setdefaulttimeout(3)
BUS_NAME = "net.launchpad.scope.image.deviantart"

class Daemon:
	def __init__ (self):
		self.scope = Unity.Scope.new ("/net/launchpad/scope/image/deviantart")
		self.scope.search_in_global = False
		self.scope.connect ("notify::active-search", self.on_search_changed)
		self.scope.connect ("filters-changed", self.on_search_changed);
#		self.scope.connect ("notify::active", self.on_search_changed);
		self.scope.connect ("activate-uri", self.on_activate_uri);
		self.scope.export()

	def on_activate_uri (self, scope, uri):
		if uri.startswith("more"):
			page = uri.split('__')[2]
			search = uri.split('__')[1]
			results = self.scope.props.results_model
			results.remove(results.get_iter_at_row(results.get_n_rows()-1))
			self.update_results_model (search, results, page)
			return Unity.ActivationResponse(handled=Unity.HandledType.SHOW_DASH, goto_uri="")
		else:
#		GLib.spawn_command_line_async("eog %s" % uri)
			return Unity.ActivationResponse(Unity.HandledType.HIDE_DASH, uri)

	def get_search_string (self):
		search = self.scope.props.active_search
		return search.props.search_string if search else None

	def on_search_changed (self, scope, param_spec=None):
			search = self.get_search_string()
			print "Search changed to: '%s'" % search
			results = self.scope.props.results_model
			results.clear()
			self.update_results_model (search, results, 0)
			results.flush_revision_queue ()

	def update_results_model(self, search, model, page):
		if self.check_filters("art_categories") == "brushes" or self.check_filters("art_categories") is None:
			i = None
			for i in self.deviantart(search, page):
				title = i[0]
				comment = i[1]
				uri = i[2]
				icon_hint = i[3]
				model.append (uri, icon_hint, 3,"text/html", title, comment, uri)
			if i:
				if not search:
					search = ''
				page = int(page)+24
				model.append ("more__"+search+"__"+str(page), "add", 3,"text/html", "More results", '', '')




		if self.scope.props.active_search:
			self.scope.props.active_search.emit("finished")

	def check_filters(self, filter_name):
		try:
			filter = self.scope.get_filter(filter_name).get_active_option().props.id
		except (AttributeError):
			filter = None
		return filter



	def deviantart(self, search, page):
		url = None
		if search == '' or search is None or search == "None":
			url = "http://browse.deviantart.com/resources/applications/gimpbrushes/?order=9&offset=%s" % (page)
		else:
			url = "http://browse.deviantart.com/resources/applications/gimpbrushes/?order=9&q=%s&offset=%s" % (search.strip('#'), page)
		if url:
			print url
			from lxml.html import fromstring
			import urllib2
			results = urllib2.urlopen(url).read()
			tree = fromstring(results)
			href = []
			icons = []
			title = []
			counter = 0
		
	# Parse for href
			for a in tree.cssselect('div span span a'):
				try:
					if 'deviantart.com/art/' in a.get('href') and "GIMP Brushes" in a.get('title'):
						counter = counter + 1
					
						href.append(a.get('href'))
						title.append(a.get('title'))
				except:
					pass

	# Parse for thumbnails
			for img in tree.cssselect('div span span a img'):
				try:
					if '.deviantart.net/' in img.get('src'):
						icons.append(img.get('src'))
				except:
					pass
				
				
	# Parse results 
			i = 0
			result_list = []
			for uri in href:
		# Add to the list
				item = []
				item.append(title[i].split(' by ')[0])
				item.append("")
				item.append(uri)
				item.append(icons[i])
				result_list.append(item)
				i = i+1
			return result_list
		else:
			return []

	def rgb(self,c):
		split = (c[0:2], c[2:4], c[4:6])
		return [int(x, 16) for x in split]

if __name__ == "__main__":
	session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
	session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
																			  'org.freedesktop.DBus',
																			  '/org/freedesktop/DBus',
																			  'org.freedesktop.DBus', None)
	result = session_bus.call_sync('RequestName',
															   GLib.Variant ("(su)", (BUS_NAME, 0x4)),
															   0, -1, None)
															   
	# Unpack variant response with signature "(u)". 1 means we got it.
	result = result.unpack()[0]
	
	if result != 1 :
		print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
		raise SystemExit (1)
	
	daemon = Daemon()
	GObject.MainLoop().run()