def get_music_folders_view(): """Return all music folders.""" (u, p, v, c, f, callback) = map(request.args.get, ['u', 'p', 'v', 'c', 'f', 'callback']) return request.formatter({ 'musicFolders': { 'musicFolder': [{ 'id': MediaManager.uuid(d), 'name': d } for d in app.iposonic.get_music_folders() if isdir(d)] } })
def get_music_folders_view(): """Return all music folders.""" (u, p, v, c, f, callback) = map( request.args.get, ['u', 'p', 'v', 'c', 'f', 'callback']) return request.formatter( {'musicFolders': {'musicFolder': [ { 'id': MediaManager.uuid(d), 'name': d } for d in app.iposonic.get_music_folders() if isdir(d) ] }} )
def get_music_directory_view(): """Return the content of a directory. params: - id=-493506601 - xml response 1: <directory id="1" name="ABBA"> <child id="11" parent="1" title="Arrival" artist="ABBA" isDir="true" coverArt="22"/> <child id="12" parent="1" title="Super Trouper" artist="ABBA" isDir="true" coverArt="23"/> </directory> xml response 2: <directory id="11" parent="1" name="Arrival"> <child id="111" parent="11" title="Dancing Queen" isDir="false" album="Arrival" artist="ABBA" track="7" year="1978" genre="Pop" coverArt="24" size="8421341" contentType="audio/mpeg" suffix="mp3" duration="146" bitRate="128" path="ABBA/Arrival/Dancing Queen.mp3"/> <child id="112" parent="11" ... # se above contentType="audio/flac" suffix="flac" transcodedContentType="audio/mpeg" transcodedSuffix="mp3" duration="208" bitRate="128" /> </directory> jsonp response """ (u, p, v, c, f, callback, dir_id) = map(request.args.get, ['u', 'p', 'v', 'c', 'f', 'callback', 'id']) if not dir_id: raise SubsonicProtocolException( "Missing required parameter: 'id' in getMusicDirectory.view") (path, dir_path) = app.iposonic.get_directory_path_by_id(dir_id) mf = app.iposonic.db.music_folders[0] dir_path = os.path.join("/", mf, dir_path) log.info("Getting entries in path: %s" % dir_path) children = [] artist = app.iposonic.db.Artist(dir_path) # # if nothing changed before our last visit # or is a virtual path (eg. uniexistent) # don't rescan # try: last_modified = os.stat(dir_path).st_ctime except: last_modified = -1 if last_modified == -1: print("Getting items from valbum.") children = app.iposonic.get_songs(query={'albumId': dir_id}) elif fs_cache.get(dir_id, 0) == last_modified: print("Getting items from cache.") children = app.iposonic.get_songs(query={'parent': dir_id}) children.extend(app.iposonic.get_albums(query={'parent': dir_id})) else: for child in os.listdir(unicode(dir_path)): # TODO find a way to support non-unicode directories and # folders. The easiest way is to simply RENAME THEM! # ................ print("checking string type: ", type(child)) #child = to_unicode(child) if child[0] in ['.', '_']: continue # # To manage non-utf8 filenames # the easiest thing is to rename # paths in utf. # # This may cause issues for collections # stored on windows or vfat filesystem. # # This is the KISS-siest approach # that avoids continuously encode # and decode of the filenames. # if not isinstance(child, unicode): if not app.config.get('rename_non_utf8'): log.warn("skipping non unicode path: %s " % to_unicode(child)) continue child_new = to_unicode(child) os.rename( b'%s/%s' % (dir_path.encode('utf-8'), child), b'%s/%s' % (dir_path.encode('utf-8'), child_new.encode('utf-8'))) child = child_new path = join(dir_path, child) try: child_j = {} is_dir = isdir(path) # This is a Lazy Indexing. It should not be there # unless a cache is set # XXX eid = MediaManager.uuid(path) try: child_j = app.iposonic.get_entry_by_id(eid) except IposonicException: app.iposonic.add_path(path, album=is_dir) child_j = app.iposonic.get_entry_by_id(eid) children.append(child_j) except IposonicException as e: log.info(e) fs_cache.setdefault(dir_id, last_modified) def _track_or_die(x): try: return int(x['track']) except: return 0 # Sort songs by track id, if possible children = sorted(children, key=_track_or_die) return request.formatter({ 'directory': { 'id': dir_id, 'name': artist.get('name'), 'child': children } })
def get_music_directory_view(): """Return the content of a directory. params: - id=-493506601 - xml response 1: <directory id="1" name="ABBA"> <child id="11" parent="1" title="Arrival" artist="ABBA" isDir="true" coverArt="22"/> <child id="12" parent="1" title="Super Trouper" artist="ABBA" isDir="true" coverArt="23"/> </directory> xml response 2: <directory id="11" parent="1" name="Arrival"> <child id="111" parent="11" title="Dancing Queen" isDir="false" album="Arrival" artist="ABBA" track="7" year="1978" genre="Pop" coverArt="24" size="8421341" contentType="audio/mpeg" suffix="mp3" duration="146" bitRate="128" path="ABBA/Arrival/Dancing Queen.mp3"/> <child id="112" parent="11" ... # se above contentType="audio/flac" suffix="flac" transcodedContentType="audio/mpeg" transcodedSuffix="mp3" duration="208" bitRate="128" /> </directory> jsonp response """ (u, p, v, c, f, callback, dir_id) = map( request.args.get, ['u', 'p', 'v', 'c', 'f', 'callback', 'id']) if not dir_id: raise SubsonicProtocolException( "Missing required parameter: 'id' in getMusicDirectory.view") (path, dir_path) = app.iposonic.get_directory_path_by_id(dir_id) mf = app.iposonic.db.music_folders[0] dir_path = os.path.join("/", mf, dir_path) log.info("Getting entries in path: %s" % dir_path) children = [] artist = app.iposonic.db.Artist(dir_path) # # if nothing changed before our last visit # or is a virtual path (eg. uniexistent) # don't rescan # try: last_modified = os.stat(dir_path).st_ctime except: last_modified = -1 if last_modified == -1: print("Getting items from valbum.") children = app.iposonic.get_songs(query={'albumId': dir_id}) elif fs_cache.get(dir_id, 0) == last_modified: print("Getting items from cache.") children = app.iposonic.get_songs(query={'parent': dir_id}) children.extend(app.iposonic.get_albums(query={'parent': dir_id})) else: for child in os.listdir(unicode(dir_path)): # TODO find a way to support non-unicode directories and # folders. The easiest way is to simply RENAME THEM! # ................ print("checking string type: ", type(child)) #child = to_unicode(child) if child[0] in ['.', '_']: continue # # To manage non-utf8 filenames # the easiest thing is to rename # paths in utf. # # This may cause issues for collections # stored on windows or vfat filesystem. # # This is the KISS-siest approach # that avoids continuously encode # and decode of the filenames. # if not isinstance(child, unicode): if not app.config.get('rename_non_utf8'): log.warn( "skipping non unicode path: %s " % to_unicode(child)) continue child_new = to_unicode(child) os.rename( b'%s/%s' % (dir_path.encode('utf-8'), child), b'%s/%s' % ( dir_path.encode('utf-8'), child_new.encode('utf-8')) ) child = child_new path = join(dir_path, child) try: child_j = {} is_dir = isdir(path) # This is a Lazy Indexing. It should not be there # unless a cache is set # XXX eid = MediaManager.uuid(path) try: child_j = app.iposonic.get_entry_by_id(eid) except IposonicException: app.iposonic.add_path(path, album=is_dir) child_j = app.iposonic.get_entry_by_id(eid) children.append(child_j) except IposonicException as e: log.info(e) fs_cache.setdefault(dir_id, last_modified) def _track_or_die(x): try: return int(x['track']) except: return 0 # Sort songs by track id, if possible children = sorted(children, key=_track_or_die) return request.formatter( {'directory': { 'id': dir_id, 'name': artist.get('name'), 'child': children } })