1 # Photo module for pyTivo by William McBrine <wmcbrine@users.sf.net>
2 # based partly on music.py and plugin.py
4 # After version 0.15, see git for the history
6 # Version 0.15, Dec. 29 -- allow Unicode; better error messages
7 # Version 0.14, Dec. 26 -- fix Random sort; handle ItemCount == 0
8 # Version 0.13, Dec. 19 -- more thread-safe; use draft mode always
9 # Version 0.12, Dec. 18 -- get date and orientation from Exif
10 # Version 0.11, Dec. 16 -- handle ItemCount, AnchorItem etc. correctly
11 # Version 0.10, Dec. 14 -- give full list if no ItemCount; use antialias
12 # mode always; allow larger thumbnails
13 # Version 0.9, Dec. 13 -- different sort types
14 # Version 0.8, Dec. 12 -- faster thumbnails, better quality full views
15 # Version 0.7, Dec. 11 -- fix missing item on thumbnail scroll up,
16 # better anchor and path handling
17 # Version 0.6, Dec. 10 -- cache recursive lookups for faster slide shows
18 # Version 0.5, Dec. 10 -- fix reboot problem by keeping directory names
19 # (vs. contents) out of "Recurse=Yes" lists
20 # Version 0.4, Dec. 10 -- drop the use of playable_cache, add path
21 # separator kludges for Windows
22 # Version 0.3, Dec. 8 -- revert to using PixelShape, workaround for
23 # Image.save() under Windows
24 # Version 0.2, Dec. 8 -- thumbnail caching, faster thumbnails
25 # Version 0.1, Dec. 7, 2007
34 from cStringIO
import StringIO
35 from xml
.sax
.saxutils
import escape
40 print 'Photo Plugin Error: The Python Imaging Library is not installed'
42 from Cheetah
.Template
import Template
43 from lrucache
import LRUCache
44 from plugin
import EncodeUnicode
, Plugin
, quote
, unquote
46 SCRIPTDIR
= os
.path
.dirname(__file__
)
50 # Match Exif date -- YYYY:MM:DD HH:MM:SS
51 exif_date
= re
.compile(r
'(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)').search
53 # Match Exif orientation, Intel and Motorola versions
55 re
.compile('\x12\x01\x03\x00\x01\x00\x00\x00(.)\x00\x00\x00').search
57 re
.compile('\x01\x12\x00\x03\x00\x00\x00\x01\x00(.)\x00\x00').search
59 # Preload the template
60 tname
= os
.path
.join(SCRIPTDIR
, 'templates', 'container.tmpl')
61 PHOTO_TEMPLATE
= file(tname
, 'rb').read()
65 CONTENT_TYPE
= 'x-container/tivo-photos'
67 class LockedLRUCache(LRUCache
):
68 def __init__(self
, num
):
69 LRUCache
.__init
__(self
, num
)
70 self
.lock
= threading
.RLock()
72 def acquire(self
, blocking
=1):
73 return self
.lock
.acquire(blocking
)
78 def __setitem__(self
, key
, obj
):
81 LRUCache
.__setitem
__(self
, key
, obj
)
85 def __getitem__(self
, key
):
89 item
= LRUCache
.__getitem
__(self
, key
)
94 media_data_cache
= LockedLRUCache(300) # info and thumbnails
95 recurse_cache
= LockedLRUCache(5) # recursive directory lists
96 dir_cache
= LockedLRUCache(10) # non-recursive lists
98 def send_file(self
, handler
, path
, query
):
101 handler
.send_response(200)
102 handler
.send_header('Content-Type', 'image/jpeg')
103 handler
.send_header('Content-Length', len(data
))
104 handler
.send_header('Connection', 'close')
105 handler
.end_headers()
106 handler
.wfile
.write(data
)
108 if 'Format' in query
and query
['Format'][0] != 'image/jpeg':
109 handler
.send_error(415)
113 attrs
= self
.media_data_cache
[path
]
119 rot
= attrs
['rotation']
123 if 'Rotation' in query
:
124 rot
= (rot
- int(query
['Rotation'][0])) % 360
126 attrs
['rotation'] = rot
131 width
= int(query
.get('Width', ['0'])[0])
132 height
= int(query
.get('Height', ['0'])[0])
134 # Return saved thumbnail?
135 if attrs
and 'thumb' in attrs
and 0 < width
< 100 and 0 < height
< 100:
136 send_jpeg(attrs
['thumb'])
141 pic
= Image
.open(unicode(path
, 'utf-8'))
142 except Exception, msg
:
143 handler
.server
.logger
.error('Could not open %s -- %s' %
145 handler
.send_error(404)
150 pic
.draft('RGB', (width
, height
))
151 except Exception, msg
:
152 handler
.server
.logger
.error('Failed to set draft mode ' +
153 'for %s -- %s' % (path
, msg
))
154 handler
.send_error(404)
157 # Read Exif data if possible
158 if 'exif' in pic
.info
:
159 exif
= pic
.info
['exif']
162 if attrs
and not 'odate' in attrs
:
163 date
= exif_date(exif
)
165 year
, month
, day
, hour
, minute
, second
= (int(x
)
166 for x
in date
.groups())
168 odate
= time
.mktime((year
, month
, day
, hour
,
169 minute
, second
, -1, -1, -1))
170 attrs
['odate'] = '%#x' % int(odate
)
173 if attrs
and 'exifrot' in attrs
:
174 rot
= (rot
+ attrs
['exifrot']) % 360
177 orient
= exif_orient_i(exif
)
179 orient
= exif_orient_m(exif
)
190 8: 90}.get(ord(orient
.group(1)), 0)
192 rot
= (rot
+ exifrot
) % 360
194 attrs
['exifrot'] = exifrot
199 pic
= pic
.rotate(rot
)
200 except Exception, msg
:
201 handler
.server
.logger
.error('Rotate failed on %s -- %s' %
203 handler
.send_error(404)
210 except Exception, msg
:
211 handler
.server
.logger
.error('Palette conversion failed ' +
212 'on %s -- %s' % (path
, msg
))
213 handler
.send_error(404)
217 oldw
, oldh
= pic
.size
219 if not width
: width
= oldw
220 if not height
: height
= oldh
222 # Correct aspect ratio
223 if 'PixelShape' in query
:
224 pixw
, pixh
= query
['PixelShape'][0].split(':')
229 ratio
= float(oldw
) / oldh
231 if float(width
) / height
< ratio
:
232 height
= int(width
/ ratio
)
234 width
= int(height
* ratio
)
237 pic
= pic
.resize((width
, height
), Image
.ANTIALIAS
)
238 except Exception, msg
:
239 handler
.server
.logger
.error('Resize failed on %s -- %s' %
241 handler
.send_error(404)
247 pic
.save(out
, 'JPEG')
248 encoded
= out
.getvalue()
250 except Exception, msg
:
251 handler
.server
.logger
.error('Encode failed on %s -- %s' %
253 handler
.send_error(404)
257 if attrs
and width
< 100 and height
< 100:
258 attrs
['thumb'] = encoded
263 def QueryContainer(self
, handler
, query
):
265 # Reject a malformed request -- these attributes should only
266 # appear in requests to send_file, but sometimes appear here
267 badattrs
= ('Rotation', 'Width', 'Height', 'PixelShape')
270 handler
.send_error(404)
273 subcname
= query
['Container'][0]
274 cname
= subcname
.split('/')[0]
275 local_base_path
= self
.get_local_base_path(handler
, query
)
276 if (not cname
in handler
.server
.containers
or
277 not self
.get_local_path(handler
, query
)):
278 handler
.send_error(404)
281 def ImageFileFilter(f
):
282 goodexts
= ('.jpg', '.gif', '.png', '.bmp', '.tif', '.xbm',
283 '.xpm', '.pgm', '.pbm', '.ppm', '.pcx', '.tga',
284 '.fpx', '.ico', '.pcd', '.jpeg', '.tiff')
285 return os
.path
.splitext(f
)[1].lower() in goodexts
288 if f
.name
in self
.media_data_cache
:
289 return self
.media_data_cache
[f
.name
]
292 item
['path'] = f
.name
293 item
['part_path'] = f
.name
.replace(local_base_path
, '', 1)
294 item
['name'] = os
.path
.split(f
.name
)[1]
295 item
['is_dir'] = f
.isdir
297 item
['cdate'] = '%#x' % f
.cdate
298 item
['mdate'] = '%#x' % f
.mdate
300 self
.media_data_cache
[f
.name
] = item
303 t
= Template(PHOTO_TEMPLATE
, filter=EncodeUnicode
)
306 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
,
308 t
.files
= map(media_data
, t
.files
)
313 handler
.send_response(200)
314 handler
.send_header('Content-Type', 'text/xml')
315 handler
.send_header('Content-Length', len(page
))
316 handler
.send_header('Connection', 'close')
317 handler
.end_headers()
318 handler
.wfile
.write(page
)
320 def get_files(self
, handler
, query
, filterFunction
):
323 def __init__(self
, name
, isdir
):
326 st
= os
.stat(unicode(name
, 'utf-8'))
327 self
.cdate
= int(st
.st_ctime
)
328 self
.mdate
= int(st
.st_mtime
)
331 def __init__(self
, files
):
336 self
.lock
= threading
.RLock()
338 def acquire(self
, blocking
=1):
339 return self
.lock
.acquire(blocking
)
344 def build_recursive_list(path
, recurse
=True):
346 path
= unicode(path
, 'utf-8')
348 for f
in os
.listdir(path
):
349 if f
.startswith('.'):
351 f
= os
.path
.join(path
, f
)
352 isdir
= os
.path
.isdir(f
)
353 f
= f
.encode('utf-8')
354 if recurse
and isdir
:
355 files
.extend(build_recursive_list(f
))
357 if isdir
or filterFunction(f
):
358 files
.append(FileData(f
, isdir
))
365 return cmp(x
.name
, y
.name
)
367 def cdate_sort(x
, y
):
368 return cmp(x
.cdate
, y
.cdate
)
370 def mdate_sort(x
, y
):
371 return cmp(x
.mdate
, y
.mdate
)
374 if x
.isdir
== y
.isdir
:
375 return sortfunc(x
, y
)
377 return y
.isdir
- x
.isdir
379 subcname
= query
['Container'][0]
380 cname
= subcname
.split('/')[0]
381 path
= self
.get_local_path(handler
, query
)
384 recurse
= query
.get('Recurse', ['No'])[0] == 'Yes'
387 rc
= self
.recurse_cache
393 updated
= os
.stat(unicode(path
, 'utf-8'))[8]
394 if path
in dc
and dc
.mtime(path
) >= updated
:
397 if path
.startswith(p
) and rc
.mtime(p
) < updated
:
401 filelist
= SortList(build_recursive_list(path
, recurse
))
413 sortby
= query
.get('SortOrder', ['Normal'])[0]
414 if 'Random' in sortby
:
415 if 'RandomSeed' in query
:
416 seed
= query
['RandomSeed'][0]
418 if 'RandomStart' in query
:
419 start
= query
['RandomStart'][0]
422 if filelist
.unsorted
or filelist
.sortby
!= sortby
:
423 if 'Random' in sortby
:
424 self
.random_lock
.acquire()
427 random
.shuffle(filelist
.files
)
428 self
.random_lock
.release()
430 local_base_path
= self
.get_local_base_path(handler
, query
)
431 start
= unquote(start
)
432 start
= start
.replace(os
.path
.sep
+ cname
,
434 filenames
= [x
.name
for x
in filelist
.files
]
436 index
= filenames
.index(start
)
437 i
= filelist
.files
.pop(index
)
438 filelist
.files
.insert(0, i
)
440 handler
.server
.logger
.warning('Start not found: ' +
443 if 'CaptureDate' in sortby
:
444 sortfunc
= cdate_sort
445 elif 'LastChangeDate' in sortby
:
446 sortfunc
= mdate_sort
451 filelist
.files
.sort(dir_sort
)
453 filelist
.files
.sort(sortfunc
)
455 filelist
.sortby
= sortby
456 filelist
.unsorted
= False
458 files
= filelist
.files
[:]
460 # Filter it -- this section needs work
461 if 'Filter' in query
:
462 usedir
= 'folder' in query
['Filter'][0]
463 useimg
= 'image' in query
['Filter'][0]
465 files
= [x
for x
in files
if not x
.isdir
]
466 elif usedir
and not useimg
:
467 files
= [x
for x
in files
if x
.isdir
]
469 files
, total
, start
= self
.item_count(handler
, query
, cname
, files
,
471 filelist
.last_start
= start
473 return files
, total
, start