File: httpserver.py

package info (click to toggle)
exaile 0.2.11.1%2Bdebian-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,464 kB
  • ctags: 2,403
  • sloc: python: 17,416; ansic: 224; makefile: 163; perl: 153; sh: 125; sql: 74
file content (303 lines) | stat: -rw-r--r-- 8,611 bytes parent folder | download
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
# Copyright (C) 2007 Mathieu Virbel <tito@bankiz.org>
#
# 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.

import gtk, threading, gobject, cgi
from gettext import gettext as _
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

PLUGIN_NAME = _('HTTP Server Control')
PLUGIN_AUTHORS = ['Mathieu Virbel <tito@bankiz.org>']
PLUGIN_VERSION = '0.2'
PLUGIN_DESCRIPTION = _('Open an HTTP Server to control exaile from a remote host')
PLUGIN_ENABLED = False
button = gtk.Button()
PLUGIN_ICON = button.render_icon('gtk-info', gtk.ICON_SIZE_MENU)
button.destroy()

DEFAULT_PORT = 10000

APP = None
eh_thread = None



# -----------------------------------------------------------------------------
# Utilities
# -----------------------------------------------------------------------------

def tag(tagname, value):
	"""
		Construct a xml tag
	"""
	return '<%s>%s</%s>' % (tagname, value, tagname)

class TrackInfo:
	"""
		Fetch some infos from a Track
		(In case we have no track, set some default value)
		(We also can have reduce information for display a playlist)
	"""

	def __init__(self, track = None, playlistmode=False):
		self.infos = {}
		self.infos['filename']	= ''
		self.infos['title']		= _('None')
		self.infos['artist']	= _('None')
		self.infos['album']		= _('None')
		self.infos['bitrate']	= _('None')
		self.infos['rating']	= _('Unknown')
		self.infos['len']		= '0:00'
		self.infos['disc']		= _('None')
		self.infos['genre']		= _('None')
		self.infos['duration']	= '0'
		self.infos['is_paused']	= APP.player.is_paused()
		self.infos['is_playing']= APP.player.is_playing()

		if track is not None:
			self.infos['filename'] = cgi.escape(track.get_filename())
			self.infos['title'] = cgi.escape(track.get_title())
			self.infos['artist'] = cgi.escape(track.get_artist())
			self.infos['album'] = cgi.escape(track.get_album())
			self.infos['bitrate'] = cgi.escape(track.get_bitrate())
			self.infos['rating'] = cgi.escape(track.get_rating())
			self.infos['len'] = cgi.escape(track.get_len())
			self.infos['disc'] = cgi.escape(track.get_disc())
			self.infos['genre'] = cgi.escape(track.get_genre())
			self.infos['duration'] = cgi.escape(str(track.get_duration()))

		if not playlistmode:
			self.infos['position'] = '0'
			if track is not None:
				self.infos['position'] = cgi.escape(str(APP.player.get_current_position()))



# -----------------------------------------------------------------------------
# Callbacks for HTTP Request
# -----------------------------------------------------------------------------

def eh_page_rpc_current(r):
	"""
		Get informations from current track
		Response is in XML.
	"""
	r.send_response(200, 'OK')
	r.send_header('Pragma', 'no-cache')
	r.send_header('Content-type', 'text/xml; charset=utf-8')
	r.end_headers()

	ti = TrackInfo(APP.player.current)
	r.wfile.write('<?xml version="1.0"?>')
	r.wfile.write('<track>')
	for key in ti.infos:
		r.wfile.write(tag(key, ti.infos[key]))
	r.wfile.write('</track>')


def eh_page_cover_current(r):
	"""
		Return the current track cover in a HTTP Response
		Support only jpeg or png file
	"""
	track = APP.player.current
	if track is None:
		r.send_error(404,'Not Found')
		return
	filename = APP.cover_manager.fetch_cover(track, 1)
	if filename is None or filename == '':
		r.send_error(404, 'Not Found')
		return

	handle = open(filename, 'rb')
	if handle is None:
		self.send_error(404, 'Not Found')
		return

	r.send_response(200, 'OK')
	r.send_header('Pragma', 'no-cache')
	if filename.rfind('.jpg') > 0:
		r.send_header('Content-type', 'image/jpeg')
	else:
		r.send_header('Content-type', 'image/png')
	r.end_headers()

	data = handle.read()
	handle.close()

	r.wfile.write(data)


def eh_page_file(r):
	"""
		Return a static file from zipfile
		Filter is done before.
	"""
	r.send_response(200, 'OK')
	r.send_header('Pragma', 'no-cache')
	if r.path.rfind('.css') > 0:
		r.send_header('Content-type', 'text/css')
	else:
		r.send_header('Content-type', 'text/html; charset=utf-8')
	r.end_headers()

	path = r.path
	if path == '/':
		path = '/index.html'

	data = ZIP.get_data('data%s' % path)
	r.wfile.write(data)


def eh_page_rpc_action(r):
	"""
		Handle some player action (play, next, pause...)
	"""
	r.send_response(200, 'OK')
	r.send_header('Pragma', 'no-cache')
	r.send_header('Content-type', 'text/plain')
	r.end_headers()
	r.wfile.write('OK')

	paths = r.path.split('?')
	path = paths[0]
	if path == '/rpc/action/play':
		if len(paths) > 1:
			args = cgi.parse_qs(paths[1])
			if args.has_key('f') and args['f'] != '' and APP.tracks is not None:
				songs = APP.tracks.get_songs()
				for track in songs:
					if track.get_filename() == args['f'][0]:
						print 'PLAYING filename=%s' % args['f'][0]
						gobject.idle_add(APP.player.play_track, track, False, False)
						return;
		if APP.player.is_paused():
			gobject.idle_add(APP.player.toggle_pause)
		else:
			gobject.idle_add(APP.player.play)
	elif path == '/rpc/action/next':
		gobject.idle_add(APP.player.next)
	elif path == '/rpc/action/previous':
		gobject.idle_add(APP.player.previous)
	elif path == '/rpc/action/pause':
		gobject.idle_add(APP.player.pause)
	elif path == '/rpc/action/stop':
		gobject.idle_add(APP.player.stop)
	elif path == '/rpc/action/seek':
		args = cgi.parse_qs(paths[1])
		if args.has_key('s') and args['s'] != '':
			gobject.idle_add(APP.player.seek, int(args['s'][0]), False)


def eh_page_playlist_list(r):
	"""
		Return track informations from current playlist
	"""
	r.send_response(200, 'OK')
	r.send_header('Pragma', 'no-cache')
	r.send_header('Content-type', 'text/xml; charset=utf-8')
	r.end_headers()

	r.wfile.write('<?xml version="1.0"?>')
	r.wfile.write('<playlist>')
	if APP.tracks is not None:
		songs = APP.tracks.get_songs()
		for song in songs:
			ti = TrackInfo(song, playlistmode=True)
			r.wfile.write('<track>')
			for key in ti.infos:
				r.wfile.write(tag(key, ti.infos[key]))
			r.wfile.write('</track>')
	r.wfile.write('</playlist>')



# -----------------------------------------------------------------------------
# HTTP server
# -----------------------------------------------------------------------------

eh_pages = {
		# Dynamic content
		'/image/cover/current':	eh_page_cover_current,

		# RPC url
		'/rpc/current':			eh_page_rpc_current,
		'/rpc/action/play':		eh_page_rpc_action,
		'/rpc/action/stop':		eh_page_rpc_action,
		'/rpc/action/next':		eh_page_rpc_action,
		'/rpc/action/previous':	eh_page_rpc_action,
		'/rpc/action/pause':	eh_page_rpc_action,
		'/rpc/action/seek':		eh_page_rpc_action,
		'/rpc/playlist/list':	eh_page_playlist_list,

		# Data
		'/':					eh_page_file,
		'/index.html':			eh_page_file,
		'/exaile.css':			eh_page_file,
		'/exaile.js':			eh_page_file,
		'/prototype.js':		eh_page_file,
		'/btn-play.png':		eh_page_file,
		'/btn-stop.png':		eh_page_file,
		'/btn-pause.png':		eh_page_file,
		'/btn-previous.png':	eh_page_file,
		'/btn-next.png':		eh_page_file,
		'/btn-reload.png':		eh_page_file,
		'/star.png':			eh_page_file,
		'/loading.gif':			eh_page_file,
		'/bg-trans.png':		eh_page_file,
	}

class ExaileHttpRequestHandler(BaseHTTPRequestHandler):

	def do_GET(self):

		path = self.path.split('?')[0]
		if eh_pages.has_key(path):
			eh_pages[path](self)
		else:
			self.send_error(404,'Not Found')
			return

		return

	@staticmethod
	def serve_forever(port):
		HTTPServer(('', port), ExaileHttpRequestHandler).serve_forever()

class ExaileHttpThread(threading.Thread):

	def __init__(self):
		threading.Thread.__init__(self)

	def run(self):
		print 'HTTP> http://127.0.0.1:%d/' % DEFAULT_PORT
		ExaileHttpRequestHandler.serve_forever(DEFAULT_PORT)



# -----------------------------------------------------------------------------
# Exaile interface
# -----------------------------------------------------------------------------

def initialize():
	eh_thread = ExaileHttpThread()
	eh_thread.setDaemon(True)
	eh_thread.start()
	return True

def destroy():
	if eh_thread is not None:
		del eh_thread
		eh_thread = None