def createNZBString(file_elements, xmlns): """ :param file_elements: first element :param xmlns: xmlns :return: :rtype: AnyStr """ rootElement = etree.Element("nzb") if xmlns: rootElement.set("xmlns", xmlns) for curFile in file_elements: rootElement.append(stripNS(curFile, xmlns)) return etree.tostring(rootElement, encoding='utf-8')
def _show_data(self, show_obj): # type: (sickbeard.tv.TVShow) -> Optional[Union[bool, etree.Element]] """ Creates an elementTree XML structure for a Kodi-style tvshow.nfo and returns the resulting data object. show_obj: a TVShow instance to create the NFO for """ show_ID = show_obj.prodid show_lang = show_obj.lang tvinfo_config = sickbeard.TVInfoAPI(show_obj.tvid).api_params.copy() tvinfo_config['actors'] = True if show_lang and not 'en' == show_lang: tvinfo_config['language'] = show_lang if 0 != show_obj.dvdorder: tvinfo_config['dvdorder'] = True t = sickbeard.TVInfoAPI(show_obj.tvid).setup(**tvinfo_config) tv_node = etree.Element('tvshow') try: show_info = t[int(show_ID)] except BaseTVinfoShownotfound as e: logger.log( 'Unable to find show with id %s on %s, skipping it' % (show_ID, sickbeard.TVInfoAPI(show_obj.tvid).name), logger.ERROR) raise e except BaseTVinfoError as e: logger.log( '%s is down, can\'t use its data to add this show' % sickbeard.TVInfoAPI(show_obj.tvid).name, logger.ERROR) raise e if not self._valid_show(show_info, show_obj): return # check for title and id if None is getattr(show_info, 'seriesname', None) or None is getattr( show_info, 'id', None): logger.log( 'Incomplete info for show with id %s on %s, skipping it' % (show_ID, sickbeard.TVInfoAPI(show_obj.tvid).name), logger.ERROR) return False title = etree.SubElement(tv_node, 'title') if None is not getattr(show_info, 'seriesname', None): title.text = '%s' % show_info['seriesname'] # year = etree.SubElement(tv_node, 'year') premiered = etree.SubElement(tv_node, 'premiered') premiered_text = self.get_show_year(show_obj, show_info, year_only=False) if premiered_text: premiered.text = '%s' % premiered_text has_id = False tvdb_id = None for tvid, slug in map_iter( lambda _tvid: (_tvid, sickbeard.TVInfoAPI(_tvid).config.get('kodi_slug')), list(sickbeard.TVInfoAPI().all_sources)): mid = slug and show_obj.ids[tvid].get('id') if mid: has_id = True kwargs = dict(type=slug) if TVINFO_TVDB == tvid: kwargs.update(dict(default='true')) tvdb_id = str(mid) uniqueid = etree.SubElement(tv_node, 'uniqueid', **kwargs) uniqueid.text = '%s%s' % (('', 'tt')[TVINFO_IMDB == tvid], mid) if not has_id: logger.log( 'Incomplete info for show with id %s on %s, skipping it' % (show_ID, sickbeard.TVInfoAPI(show_obj.tvid).name), logger.ERROR) return False ratings = etree.SubElement(tv_node, 'ratings') if None is not getattr(show_info, 'rating', None): # todo: name dynamic depending on source rating = etree.SubElement(ratings, 'rating', name='thetvdb', max='10') rating_value = etree.SubElement(rating, 'value') rating_value.text = '%s' % show_info['rating'] if None is not getattr(show_info, 'siteratingcount', None): ratings_votes = etree.SubElement(rating, 'votes') ratings_votes.text = '%s' % show_info['siteratingcount'] plot = etree.SubElement(tv_node, 'plot') if None is not getattr(show_info, 'overview', None): plot.text = '%s' % show_info['overview'] episodeguide = etree.SubElement(tv_node, 'episodeguide') episodeguideurl = etree.SubElement(episodeguide, 'url', post='yes', cache='auth.json') if tvdb_id: episodeguideurl.text = sickbeard.TVInfoAPI( TVINFO_TVDB).config['epg_url'].replace('{MID}', tvdb_id) mpaa = etree.SubElement(tv_node, 'mpaa') if None is not getattr(show_info, 'contentrating', None): mpaa.text = '%s' % show_info['contentrating'] genre = etree.SubElement(tv_node, 'genre') if None is not getattr(show_info, 'genre', None): if isinstance(show_info['genre'], string_types): genre.text = ' / '.join([ x.strip() for x in show_info['genre'].split('|') if x.strip() ]) studio = etree.SubElement(tv_node, 'studio') if None is not getattr(show_info, 'network', None): studio.text = '%s' % show_info['network'] self.add_actor_element(show_info, etree, tv_node) # Make it purdy sg_helpers.indent_xml(tv_node) # output valid xml # data = etree.ElementTree(tv_node) # output non valid xml that Kodi accepts data = decode_str(etree.tostring(tv_node)) parts = data.split('episodeguide') if 3 == len(parts): data = 'episodeguide'.join( [parts[0], parts[1].replace('"', '"'), parts[2]]) return data