Package elisa :: Package plugins :: Package bad :: Package ipod_plugin :: Module ipod_media
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.ipod_plugin.ipod_media

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  IpodMediaProvider component class 
 19  """ 
 20   
 21   
 22  __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>' 
 23   
 24  from elisa.base_components.media_provider import MediaProvider 
 25  from elisa.core.media_uri import MediaUri,quote 
 26  from elisa.core.bus.bus_message import ComponentsLoaded, MediaLocation 
 27  from elisa.core.component import InitializeFailure 
 28  from elisa.core.observers.dict import DictObservable 
 29  from elisa.core.utils import sorting 
 30   
 31  import os 
 32   
 33  from twisted.internet import defer, threads 
 34   
 35   
 36  """ 
 37  TODO: 
 38   
 39  - support artwork 
 40  - support photos (experimental in gpod) 
 41  - support videos (?) 
 42   
 43  """ 
 44   
 45  import gpod 
 46   
47 -class Artist:
48
49 - def __init__(self, name):
50 self.name = name 51 self.albums = {}
52
53 - def add_album(self, album):
54 if album.name: 55 self.albums[album.name] = album
56
57 -class Album:
58
59 - def __init__(self, name):
60 self.name = name 61 self.songs = {}
62
63 - def add_song(self, song):
64 self.songs[song.title] = song
65
66 -class Song:
67
68 - def __init__(self, nr, name, path, typ):
69 self.nr = nr 70 self.name = name 71 self.title = '%s - %s' % (nr, name) 72 self.path = path 73 self.filetype = typ
74
75 -def load_artists(db):
76 artists = {} 77 78 for track in db: 79 artist_name = track['artist'] 80 album_name = track['album'] 81 title = track['title'] 82 track_nb = str(track['track_nr']).rjust(2, '0') 83 track_type = track['filetype'] 84 song_path = track['ipod_path'].replace(':','/') 85 song_title = '%s - %s' % (track_nb, title) 86 87 88 if artist_name not in artists: 89 artist = Artist(artist_name) 90 artists[artist_name] = artist 91 else: 92 artist = artists[artist_name] 93 94 if album_name not in artist.albums: 95 album = Album(album_name) 96 artist.add_album(album) 97 else: 98 album = artist.albums[album_name] 99 100 if song_title not in album.songs: 101 song = Song(track_nb, title, song_path, track_type) 102 album.add_song(song) 103 104 return artists
105 106 107
108 -class IpodMedia(MediaProvider):
109 """ 110 This class implements IPod support 111 """
112 - def __init__(self):
113 MediaProvider.__init__(self) 114 self.dbs = {}
115
117 return {'ipod' : 0}
118
119 - def _blocking_get_media_type(self, uri):
120 return {'file_type' : 'directory', 'mime_type' : '' }
121
122 - def _blocking_is_directory(self, uri):
123 if uri.get_param('id'): 124 return False 125 return os.path.exists("%s%s" % (uri.host,uri.path))
126
127 - def _blocking_has_children_with_types(self, uri, media_types):
128 has_children = False 129 if not os.path.isfile(uri.path): 130 has_children = len(set(['audio', 131 'directory']).intersection(media_types)) > 0 132 return has_children
133
134 - def get_direct_children(self, uri, l):
135 d = threads.deferToThread(self._blocking_get_direct_children, uri, l) 136 return d
137
138 - def _blocking_get_direct_children(self, uri, children):
139 artist = uri.get_param('artist') 140 album = uri.get_param('album') 141 142 self.debug("Retrieving children for %s, artist: %s, album: %s", 143 uri, artist, album) 144 145 # libgpod doesn't support unicode.... 146 path = str(uri.path) 147 if path not in self.dbs: 148 # lazily fetch ipod db from dbs dict 149 try: 150 if os.path.isdir(path): 151 db = gpod.Database(path) 152 elif os.path.isfile(path): 153 # it's a iTunes database 154 db = gpod.Database('', local=True, localdb=path) 155 else: 156 return children 157 except gpod.DatabaseException,e: 158 self.warning("Could not read the Database: %s" % e) 159 return children 160 else: 161 artists = load_artists(db) 162 self.dbs[path] = (db, artists) 163 164 db, artists = self.dbs[path] 165 166 db_artist = None 167 db_album = None 168 169 if artist: 170 db_artist = artists.get(artist) 171 172 if album and db_artist: 173 db_album = db_artist.albums.get(album) 174 175 if db_album: 176 self.debug("Fetching songs for %s album", db_album.name) 177 # fetch songs of given album 178 179 # sort alpha-numerically 180 song_titles = db_album.songs.keys() 181 sorting.natural_sort(song_titles) 182 183 for song_title in song_titles: 184 song = db_album.songs[song_title] 185 metadata = DictObservable() 186 self.debug("Processing song: %s" % song_title) 187 child_uri = MediaUri("file://%s%s" % (str(uri.path), song.path)) 188 # FIXME: REMOVE THIS UGLY HACK !!! 189 metadata['song_artist']= artist 190 metadata['song_album'] = album 191 metadata['song'] = song_title 192 child_uri.label = song_title 193 children.append((child_uri, metadata)) 194 elif db_artist: 195 # fetch albums list of given artist 196 197 # sort alpha-numerically 198 album_titles = db_artist.albums.keys() 199 sorting.natural_sort(album_titles) 200 201 for album_title in album_titles: 202 album = db_artist.albums[album_title] 203 metadata = DictObservable() 204 child_uri = MediaUri("ipod://%s" % uri.path) 205 metadata['artist'] = artist 206 metadata['album'] = album.name 207 # Don't use a DictObservable for params! 208 child_uri.set_params({'artist':artist,'album':album.name}) 209 child_uri.label = album.name 210 children.append((child_uri, metadata)) 211 else: 212 # return artists list 213 214 # sort alpha-numerically 215 artist_names = artists.keys() 216 sorting.natural_sort(artist_names) 217 218 for artist_name in artist_names: 219 artist = artists[artist_name] 220 metadata = DictObservable() 221 child_uri = MediaUri("ipod://%s" % uri.path) 222 metadata['artist'] = artist.name 223 # Don't use a DictObservable for params! 224 child_uri.set_param('artist', artist.name) 225 child_uri.label = artist.name 226 children.append((child_uri, metadata)) 227 228 return children
229