/
plugin.py
335 lines (273 loc) · 11.1 KB
/
plugin.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#encoding:utf-8
# Copyright (C) 2006 Amit Man <amit.man@gmail.com>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import gtk
import HTMLParser
import urllib2
import random
import threading
from gettext import gettext as _
from sentencesplitter import SentenceSplitter
import config
b = gtk.Button()
PLUGIN_ICON = b.render_icon('gtk-info', gtk.ICON_SIZE_MENU)
b.destroy()
class content(HTMLParser.HTMLParser,threading.Thread):
def __init__(self, the_page):
threading.Thread.__init__(self)
HTMLParser.HTMLParser.__init__(self)
self.sentencesplitter=SentenceSplitter()
self.in_paragraph=False
self.num_of_para=-1 # when we first meet tag p, we will be 0
self.paraString=[]
self.paraSentances=[]
self.end_of_intro=False
self.cant_find_page=False
self.gotimage=False
self.feed(the_page)
self.parse_to_sentances()
def handle_starttag(self,tag,attrs):
if tag == 'p':
#if not self.end_of_intro:
self.in_paragraph=True
self.num_of_para=self.num_of_para+1
self.paraString.append('')
#print 'len of parastring: %d' % len(self.paraString)
#print 'start paragraph: %d' % self.num_of_para
if tag =='table':
if attrs[0][0] == 'id' and attrs[0][1]=='toc':
self.end_of_intro=True
if tag =='img':
if not self.gotimage:
for desc in attrs:
if desc[0]=='src':
src=desc[1]
print src
if src[-3:] != 'jpg':
print 'breaking'
break
print 'good pic'
self.gotimage=True
print src
headers = { 'User-Agent' : config.USER_AGENT }
req = urllib2.Request(src, None, headers)
response = urllib2.urlopen(req)
image = response.read()
imagefile=file('didyouknow_tmp.img','w')
imagefile.write(image)
imagefile.close
def handle_endtag(self,tag):
if tag =='p':
self.in_paragraph=False
#if the last paragraph was empty
if not len(self.paraString[self.num_of_para]):
self.num_of_para=self.num_of_para-1
self.paraString.pop()
def handle_data(self,data):
if data=='Wikipedia does not have an article with this exact name.':
print "cant find wikipedia page"
self.no_page()
if self.in_paragraph: #and not self.end_of_intro:
self.paraString[self.num_of_para]=self.paraString[self.num_of_para]+data
def parse_to_sentances(self):
for s in self.paraString:
self.paraSentances.append(self.sentencesplitter.split(s))
def get_random_fact(self):
if self.cant_find_page:
return "Cant find info on wikipedia"
else:
s=''
random_para=random.randint(0,len(self.paraSentances)-1)
random_sen=random.randint(0,len(self.paraSentances[random_para])-1)
if random_sen > 0:
s=self.half_sentence(self.paraSentances[random_para][random_sen-1],'begin')+' '
s=s+self.paraSentances[random_para][random_sen]
if random_sen < len(self.paraSentances[random_para])-1:
s=s+' '+self.half_sentence(self.paraSentances[random_para][random_sen+1],'end')
return s
def half_sentence(self,sentence,side='begin'):
wa=sentence.split(' ')
word_count=len(wa)
if side=='end':
wa=wa[0:(word_count/2)]+['... ']
if side == 'begin':
wa=[' ...']+wa[(word_count/2):word_count]
return (' '.join(wa))
def no_page(self):
self.cant_find_page=True
ig=None
newSong=True
songName=''
APP = None
BUTTON = None
MENU_ITEM = None
TIPS = gtk.Tooltips()
D=None
L=None
def play_track(exaile, track):
"""
Called when playback on a track starts ("play-track" event)
"""
global ig
if track.type != 'stream':
ig.get_song_info(exaile,track)
def stop_track(exaile, track):
global ig
global songName
songName=''
def track_information_updated(exaile):
global ig
global songName
global enable_popups
track=exaile.player.current
if (ig.enable_popups):
#if track.type == 'stream':
if songName != track.title:
songName=track.title
print "TRYING: to find on %s " % track.artist
ig.get_song_info(exaile,track)
ig.show_random_info()
class InfoGiver:
def __init__(self,APP,toggle_button):
self.toggle_button=toggle_button
self.c=None
self.enable_popups=False
wtree=gtk.glade.xml_new_from_buffer(XML_STRING, len(XML_STRING))
self.D=wtree.get_widget('window')
self.L=wtree.get_widget('label')
self.image=wtree.get_widget('image')
self.vbox1=wtree.get_widget('vbox1')
self.title=wtree.get_widget('title')
self.event=wtree.get_widget('event')
self.sep_event=wtree.get_widget('sep_event')
self.title_event=wtree.get_widget('title_event')
self.event.connect('button_press_event', self.start_dragging)
self.event.connect('button_press_event', self.close_by_double_click)
self.event.connect('button_release_event', self.stop_dragging)
self.sep_event.connect('button_press_event', self.start_dragging)
self.sep_event.connect('button_press_event', self.close_by_double_click)
self.title_event.connect('button_press_event', self.close_by_double_click)
self.sep_event.connect('button_release_event', self.stop_dragging)
self.title_event.connect('button_press_event', self.start_dragging)
self.title_event.connect('button_release_event', self.stop_dragging)
self.__handler = None
self.B=wtree.get_widget('button')
self.D.hide();
self.D.connect('destroy',self.popup_destroyed)
self.B.connect('clicked',self.activated)
bg_color = gtk.gdk.color_parse('#567EA2');
self.D.modify_bg(gtk.STATE_NORMAL, bg_color)
self.title_event.modify_bg(gtk.STATE_NORMAL, bg_color)
self.sep_event.modify_bg(gtk.STATE_NORMAL, bg_color)
self.event.modify_bg(gtk.STATE_NORMAL, bg_color)
#------------------------------------------------
# dragging capability. copied from xlmisc.py
def close_by_double_click(self,widget,event):
if event.type == gtk.gdk._2BUTTON_PRESS:
bg_color = gtk.gdk.color_parse('white');
self.toggle_button.modify_bg(gtk.STATE_NORMAL, bg_color)
self.enable_popups = False
self.D.hide()
def start_dragging(self, widget, event):
"""
Called when the user starts dragging the window
"""
self.__start = event.x, event.y
self.__handler = self.D.connect('motion_notify_event',
self.dragging)
self.__timeout = None
def stop_dragging(self, widget, event):
"""
Called when the user stops dragging the mouse
"""
global POPUP
if self.__handler: self.D.disconnect(self.__handler)
self.__handler = None
# if self.start_timer:
# self.__timeout = gobject.timeout_add(4000, self.D.hide)
# settings = self.exaile.settings
# (w, h) = self..get_size()
# (x, y) = self.window.get_position()
# settings['osd/x'] = x
# settings['osd/y'] = y
# settings['osd/h'] = h
# settings['osd/w'] = w
# POPUP = OSDWindow(self.exaile, get_osd_settings(settings))
def dragging(self, widget, event):
"""
Called when the user drags the window
"""
self.D.move(int(event.x_root - self.__start[0]),
int(event.y_root - self.__start[1]))
#-------------------------------------------------------------
def toggle_popups(self,b):
if self.enable_popups == False:
self.enable_popups = True
bg_color = gtk.gdk.color_parse('red');
self.toggle_button.modify_bg(gtk.STATE_NORMAL, bg_color)
if APP.player.current != None:
self.get_song_info(APP,APP.player.current)
self.show_random_info()
#else:
# self.label.set_label('no track is playing yet..')
# self.dialog.show_all()
#moving from enable popups to diable them
elif self.enable_popups == True:
bg_color = gtk.gdk.color_parse('white');
self.toggle_button.modify_bg(gtk.STATE_NORMAL, bg_color)
self.enable_popups = False
self.D.hide()
def activated(self,b):
self.show_random_info()
def get_song_info(self,exaile,track):
if not self.enable_popups:
return
locale = exaile.settings.get_str('wikipedia_locale', 'en')
url = "http://%s.wikipedia.org/wiki/%s" % (locale, track.artist)
url = url.replace(" ", "_")
headers = { 'User-Agent' : config.USER_AGENT }
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req)
the_page = response.read()
self.c=content(the_page)
self.c.start()
#adding image
if self.c.gotimage:
self.image.set_from_file('didyouknow_tmp.img')
else:
raise "No Image found"
self.title.set_label(_('Artist: ')+ track.artist+'\n\n'+self.c.paraSentances[0][0]);
def show_random_info(self):
self.D.show()
self.L.show()
if not self.enable_popups:
return
self.L.set_label(_("Did you know...\n\n%s") % (self.c.get_random_fact()))
#self.dialog.show_all()
self.vbox1.resize_children()
def kill_window(self):
try:
self.D.destroy()
except:
pass
def popup_destroyed(self,w):
self.enable_popups=False
bg_color = gtk.gdk.color_parse('white');
self.toggle_button.modify_bg(gtk.STATE_NORMAL, bg_color)
XML_STRING = None
def load_data(zip):
global XML_STRING
XML_STRING = zip.get_data('gui.glade')