opml.py (1295B)
1 # 2 # See LICENSE for licensing details. 3 # 4 # Copy me if you can. 5 # by 20h 6 # 7 8 from lxml import etree 9 from datetime import datetime 10 11 def read(ostr): 12 parser = etree.XMLParser(recover=True, encoding='utf-8') 13 xml = etree.fromstring(ostr, parser) 14 15 rssfeeds = [] 16 17 feeds = xml.xpath("//outline") 18 for feed in feeds: 19 if "xmlUrl" in feed.attrib: 20 rssfeeds.append(feed.attrib["xmlUrl"]) 21 elif "text" in feed.attrib: 22 rssfeeds.append(feed.attrib["text"]) 23 24 return rssfeeds 25 26 def write(rssfeeds): 27 opmle = etree.Element("opml") 28 29 heade = etree.SubElement(opmle, "head") 30 titlee = etree.SubElement(heade, "title") 31 32 daten = datetime.now().strftime("%Y-%m-%dT%H:%M:%S%Z") 33 datece = etree.SubElement(heade, "dateCreated") 34 datece.text = daten 35 dateme = etree.SubElement(heade, "dateModified") 36 dateme.text = daten 37 ownerne = etree.SubElement(heade, "ownerName") 38 ownerne.text = "Me" 39 docse = etree.SubElement(heade, "docs") 40 docse.text = "http://dev.opml.org/spec2.html" 41 42 bodye = etree.SubElement(opmle, "body") 43 44 for rss in rssfeeds: 45 outlinee = etree.SubElement(bodye, "outline") 46 outlinee.attrib["type"] = "rss" 47 outlinee.attrib["text"] = rss 48 outlinee.attrib["xmlUrl"] = rss 49 50 return etree.tostring(opmle, encoding="utf-8", \ 51 pretty_print=True, \ 52 xml_declaration=True).decode("utf-8") 53