def add_category(self, source, cat_name): category = Session.query(WebradioCategory)\ .filter(WebradioCategory.source == source)\ .filter(WebradioCategory.name == cat_name)\ .one_or_none() if category is not None: raise DeejaydError(_("Category %s already exists") % cat_name) category = WebradioCategory(source=source, name=cat_name) Session.add(category) return category.to_json()
def add_directory(self, path, name): parent = self._get_folder(path) if parent is not None: dir_path = os.path.join(path, name) folder = LibraryFolder(name=name, parent_folder=parent, path=dir_path, library_id=self.library_id) Session.add(folder) self.watcher.watch_dir(dir_path, self)
def load_state(self): st_name = "%s_state" % self.state_name s = Session.query(State) \ .filter(State.name == st_name) \ .one_or_none() if s is None: s = State(name=st_name) s.state = self.initial_state Session.add(s) Session.commit() self.state = s.state
def __init__(self): super(_BaseWebradioSource, self).__init__() source = Session.query(WebradioSource)\ .filter(WebradioSource.name == self.NAME)\ .one_or_none() if source is None: source = WebradioSource(name=self.NAME) Session.add(source) Session.commit() self.load_state()
def add_webradio(self, source, name, urls, cat=None): provided_urls = [] for url in urls: if url.lower().startswith("http://") or \ url.lower().startswith("https://"): try: if url.lower().endswith(".pls"): provided_urls.extend(get_uris_from_pls(url)) elif url.lower().endswith(".m3u"): provided_urls.extend(get_uris_from_m3u(url)) else: provided_urls.append(url) except IOError: log.err(_("Could not parse %s") % url) pass needed_urls = [] for url in provided_urls: try: protocol = url.split(':')[0] if protocol not in ( 'http', 'https', 'rtsp', ): raise ValueError except ValueError: log.err( _("Discarding %s : webradio protocol not supported.") % url) else: if url not in needed_urls: needed_urls.append(url) if len(needed_urls) < 1: raise DeejaydError( _("Given urls %s is not " "supported") % ",".join(urls)) cats = cat is not None and [cat] or [] webradio = Session.query(Webradio)\ .filter(Webradio.source == source)\ .filter(Webradio.name == name)\ .one_or_none() if webradio is not None: raise DeejaydError(_("Webradio %s already exists") % name) webradio = Webradio(source=source, name=name) Session.add(webradio) for c in cats: webradio.categories.append(Session.query(WebradioCategory).get(c)) for url in needed_urls: webradio.entries.append(WebradioEntry(url=url))
def save(self, pls_name): pls = Session.query(StaticMediaList)\ .filter(StaticMediaList.name == pls_name)\ .one_or_none() if pls is None: pls = StaticMediaList(name=pls_name) Session.add(pls) pls.items = [ StaticMediaListItem(media_id=it["m_id"]) for it in self._playlist.get() ] Session.commit() self.dispatch_signame('recpls.listupdate') return {"playlist_id": pls.id}
def create(self, name, p_type): if name == "": raise DeejaydError(_("Set a playlist name")) if p_type not in ("static", "magic"): raise DeejaydError(_("playlist type has to be 'static' or 'magic'")) pls = Session.query(MediaList)\ .filter(MediaList.name == name)\ .one_or_none() if pls is not None: raise DeejaydError(_("This playlist already exists")) pl_cls = p_type == "static" and StaticMediaList or MagicMediaList pls = pl_cls(name=name) Session.add(pls) Session.commit() self.dispatch_signame('recpls.listupdate') return {"pl_id": pls.id, "name": name, "type": p_type}
def walk_directory(self, walk_root, force=False): walk_root = os.path.join(self.root_path, walk_root) walk_root = walk_root.rstrip("/") walked_folders = [] # cache all medias recorded in database media_dict = {} for m in Session.query(self.OBJECT_CLASS).all(): media_dict[m.get_path()] = m for root, subdirs, files in pathutils.walk(walk_root): root = root.rstrip("/") log.debug('library: update crawling %s' % root) if os.path.basename(root).startswith("."): continue # skip hidden folder folder = self._get_folder(root) if folder is None: folder = LibraryFolder(library_id=self.library_id, path=root, name=os.path.basename(root)) Session.add(folder) self.parse_files(folder, files, media_dict, force) subdirs_obj = self.get_subdirs(folder, subdirs) for sd in subdirs_obj: sd.parent_folder = folder walked_folders.append(root) # clean library Session.query(LibraryFolder) \ .filter(LibraryFolder.library_id == self.library_id) \ .filter(LibraryFolder.path.notin_(walked_folders)) \ .delete(synchronize_session='fetch') if len(media_dict) > 0: db_media_ids = [media_dict[k].m_id for k in media_dict] erased_files = Session.query(self.OBJECT_CLASS) \ .filter(self.OBJECT_CLASS.m_id.in_(db_media_ids)) \ .all() for f in erased_files: Session.delete(f) Session.commit()
def __init__(self, source, pl_id): self.source = source self.repeat = False self.plmedia_id = 0 self.pl_id = pl_id self.time_length = 0 # load recorded playlist if exist db_list = Session.query(StaticMediaList)\ .filter(StaticMediaList.name == "__"+source)\ .one_or_none() if db_list is None: db_list = StaticMediaList(name="__" + source) Session.add(db_list) Session.commit() self.medialist = [ PlaylistEntry(self, self.__get_next_id(), i.media, self.source) for i in db_list.items if i.media is not None ] self.time_length = reduce(lambda t, m: t + m["length"], self.medialist, 0)
def __init__(self, path): super(BaseLibrary, self).__init__() # get root path for this library path = os.path.abspath(path).rstrip("/") self.root_path = path # test library path if not os.path.isdir(self.root_path): msg = _("Unable to find '%s' folder in library") % self.root_path raise DeejaydError(msg) library = Session.query(Library) \ .filter(Library.name == self.TYPE) \ .one_or_none() if library is None: library = Library(name=self.TYPE, path=self.root_path) Session.add(library) Session.commit() self.library_id = library.id self.updating_state = {"id": 0, "running": False, "error": None} self.parser = self.PARSER(self) self.watcher = None self.load_state()
def update_file(self, path, filename): file_path = os.path.join(path, filename) try: file_mtime = os.stat(file_path).st_mtime except OSError: log.debug("library: file %s is gone, skipping." % file_path) return folder = self._get_folder(path) if folder is not None: file_obj = self._get_file(folder, filename) if file_obj is None: file_obj = self.OBJECT_CLASS(filename=filename, folder=folder, last_modified=-1) if file_mtime > file_obj.last_modified: file_obj = self._get_file_info(file_obj) if file_obj is None: self.update_extrainfo_file(file_path) return Session.add(file_obj) else: log.info("Inotify: a media has been updated in an unknown folder")
def __reload_list(self): log.msg(_("Start to reload icecast webradio source")) url = DeejaydConfig().get("webradio", "icecast_url") try: page_handle = urllib.request.urlopen(url, timeout=TIMEOUT) xml_page = page_handle.read() except Exception: raise DeejaydError(_("Unable to connect to icecast website")) # try to parse result try: root = ET.fromstring(xml_page) except ET.XMLSyntaxError: raise DeejaydError(_("Unable to parse icecast webradio list")) except Exception: raise DeejaydError( _("Unable to read result from icecast " "webradio list")) finally: page_handle.close() DatabaseLock.acquire() session = Session() source = session.query(WebradioSource)\ .filter(WebradioSource.name == self.NAME)\ .one() # delete old entries from the database session.query(Webradio)\ .filter(Webradio.source_id == source.id)\ .delete(synchronize_session='fetch') session.query(WebradioCategory)\ .filter(WebradioCategory.source_id == source.id)\ .delete(synchronize_session='fetch') categories = {} webradios = {} for station in root: try: server_type = station.find("server_type").text listen_url = station.find("listen_url").text genres = station.find("genre").text name = station.find("server_name").text except TypeError: continue if server_type.startswith("audio") or \ (server_type == "application/ogg" and not listen_url.endswith("ogv")): if name not in webradios: genres = genres.split(" ") webradios[name] = Webradio(source=source, name=name) for genre in genres: if len(genre) <= 2 or genre.startswith("."): continue genre = genre.capitalize() if genre not in categories: categories[genre] = WebradioCategory(name=genre, source=source) webradios[name].categories.append(categories[genre]) session.add(webradios[name]) webradios[name].entries.append(WebradioEntry(url=listen_url)) log.debug('Added icecast webradio %s' % name) session.commit() Session.remove() DatabaseLock.release() log.msg(_("Finish to reload icecast webradio source")) return { "wb_count": len(webradios), "cat_count": len(categories), }