def __init__(self, config): self.config = config self.yt = Youtube(self.config.youtube_username, self.config.youtube_apikey)
class SonosMusicApi(object): def __init__(self, config): self.config = config self.yt = Youtube(self.config.youtube_username, self.config.youtube_apikey) def getmetadata(self, id, index, count): logging.debug("getmetadata args: " + id) mid = MediaId.parse(id) metadata = MetadataResult() if MediaId.PREFIX_ROOT == mid.prefix: metadata.mediacollection.append(MediaCollectionItem(MediaId(MediaId.PREFIX_PLAYLISTS), "Playlists")) metadata.mediacollection.append(MediaCollectionItem(MediaId(MediaId.PREFIX_SUBSCRIPTIONS), "Subscriptions")) metadata.mediacollection.extend(self.yt.getuserchannel()) metadata.mediacollection.append( MediaCollectionItem(MediaId(MediaId.PREFIX_CACHE), "Cache", itemtype="trackList")) elif MediaId.PREFIX_PLAYLISTS == mid.prefix: metadata.mediacollection.extend(self.yt.getuserplaylists()) elif MediaId.PREFIX_PLAYLIST == mid.prefix: metadata.mediametadatacollection.extend(self.yt.getplaylist(mid.key)) elif MediaId.PREFIX_SUBSCRIPTIONS == mid.prefix: metadata.mediacollection.extend(self.yt.getusersubscriptions()) elif MediaId.PREFIX_CHANNEL == mid.prefix: metadata.mediacollection.extend(self.yt.getchannel(mid.key)) elif MediaId.PREFIX_CACHE == mid.prefix: cached_videos = [] for file in os.listdir(self.config.abs_cache_path): print(file) if file.endswith(".m4a"): cached_videos.append(file.replace(".m4a", "")) metadata.mediametadatacollection.extend(self.yt.getvideos(cached_videos)) return metadata.todict() def getmediauri(self, id): logging.debug("getmediauri args: " + id) mid = MediaId.parse(id) mediauri = MediaUri(self.config.streamtube_baseuri, mid) logging.debug("uri: " + mediauri.uri) return mediauri.uri def getmediametadata(self, id): logging.debug("getmediametadata args: " + id) mid = MediaId.parse(id) return {"getMediaMetadataResult": self.yt.getvideo(mid.key).todict()} def getlastupdate(self): logging.debug("getlastupdate") update = LastUpdate() update.catalog = 0 return update.todict() def search(self, id, term, index, count): logging.debug("search - id: " + id) logging.debug("search - term: " + term) search = SearchResult() if term.endswith("?"): term = term.rstrip("?") if id == "tracks": search.mediametadatacollection.extend(self.yt.search_videos(term)) elif id == "playlists": search.mediacollection.extend(self.yt.search_playlists(term)) elif id == "people": search.mediacollection.extend(self.yt.search_channels(term)) elif id == "tags": search.mediametadatacollection.append(self.yt.getvideo(term)) return search.todict()