Extensible RSS 2.0 Feed Generator written in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

775 lines
29 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
  1. __name__ = "rfeed"
  2. __version__ = (1, 0, 0)
  3. __author__ = "Santiago L. Valdarrama - https://blog.svpino.com"
  4. _generator = __name__ + " v" + ".".join(map(str, __version__))
  5. _docs = "https://github.com/svpino/rfeed/blob/master/README.md"
  6. import itertools
  7. import sys
  8. from xml.sax import saxutils
  9. if sys.version_info[0] == 3:
  10. basestring = str
  11. from io import StringIO
  12. else:
  13. try:
  14. from cStringIO import StringIO
  15. except ImportError:
  16. from StringIO import StringIO
  17. class Serializable:
  18. """ Represents an object that can be serialized as part of the feed.
  19. """
  20. def __init__(self):
  21. """ Initializes the extension. In your implementation, make sure you always call this base class method
  22. before adding your own code.
  23. """
  24. self.handler = None
  25. def publish(self, handler):
  26. """ This method produces the XML representation of the object to be included in the feed. In your implementation,
  27. make sure you always call this base class method before adding your own code.
  28. Keyword arguments:
  29. handler -- An xml.sax.saxutils.XMLGenerator instance that you can use to create the XML representation of the object.
  30. """
  31. self.handler = handler
  32. def _date(self, date):
  33. """ Converts a datetime into an RFC 2822 formatted date.
  34. Returns None if None is provided as an argument.
  35. Keyword arguments:
  36. date -- A datetime object in GMT format.
  37. """
  38. # Alright, I admit it: this method looks hideous. The thing is that RFC 822 requires a specific format for dates, and strftime is
  39. # locale dependent, so I can't use it to create the final date unless I force change the system locale.
  40. #
  41. # I looked into that (locale.setlocale, then restore), but I got the feeling that I was doing things that I was going to regret later.
  42. # Maybe it's just me, but it doesn't feel right to force change the locale just to create a simple date.
  43. #
  44. # So, not having a better solution, I went ahead and used the original method from the PyRSS2Gen library.
  45. if date is None:
  46. return None
  47. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][date.weekday()], date.day,
  48. ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date.month-1], date.year, date.hour, date.minute, date.second)
  49. def _write_element(self, name, value, attributes = {}):
  50. def parse_cdata(string):
  51. cdata_begin = string.find("<![CDATA[")
  52. if cdata_begin != -1:
  53. cdata_end = string[cdata_begin:].find("]]>")
  54. if cdata_end != -1:
  55. return {"begin": cdata_begin,
  56. "end": cdata_begin + cdata_end + 3}
  57. else:
  58. return None
  59. else:
  60. return None
  61. if value is not None or attributes != {}:
  62. self.handler.startElement(name, attributes)
  63. if value is not None:
  64. str_value = value if isinstance(value, basestring) else str(value)
  65. while len(str_value):
  66. cdata_section = parse_cdata(str_value)
  67. if cdata_section is not None:
  68. self.handler.characters(str_value[:cdata_section["begin"]])
  69. self.handler.ignorableWhitespace(
  70. str_value[cdata_section["begin"]:cdata_section["end"]])
  71. str_value = str_value[cdata_section["end"]:]
  72. else:
  73. self.handler.characters(str_value)
  74. break
  75. self.handler.endElement(name)
  76. class Extension(Serializable):
  77. def get_namespace(self):
  78. """ Returns the namespace (if any) for this extension. The namespace information is added as an attribute in
  79. the <rss> element of the feed. The return value should be a dictionary.
  80. For example, here is the code for this method on the iTunes extension: return {"xmlns:itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd"}
  81. """
  82. pass
  83. class Host(Serializable):
  84. """ Represents an object that can be host to other extensions.
  85. """
  86. def __init__(self, extensions = None):
  87. Serializable.__init__(self)
  88. self.extensions = [] if extensions is None else extensions
  89. def add_extension(self, extension):
  90. """ You can use this method to add new extensions to the feed.
  91. To create new extensions, make sure you inherit from the Serializable or Extension class.
  92. """
  93. if not isinstance(extension, Serializable):
  94. raise TypeError("The provided extension should be a subclass of the Serializable class")
  95. self.extensions.append(extension)
  96. class Category(Serializable):
  97. """ A Category object specifies one or more categories that the channel or item belongs to.
  98. More information at http://cyber.law.harvard.edu/rss/rss.html#ltcategorygtSubelementOfLtitemgt
  99. """
  100. def __init__(self, category, domain = None):
  101. """ Keyword arguments:
  102. category -- The name of the category
  103. domain -- Optional. A string that identifies a categorization taxonomy.
  104. """
  105. Serializable.__init__(self)
  106. if category is None: raise ElementRequiredError("category")
  107. self.category = category
  108. self.domain = domain
  109. def publish(self, handler):
  110. Serializable.publish(self, handler)
  111. self._write_element("category", self.category, { "domain": self.domain } if self.domain is not None else {})
  112. class Cloud(Serializable):
  113. """ A Cloud object specifies a web service that supports the rssCloud interface which can be implemented in HTTP-POST, XML-RPC or SOAP 1.1.
  114. More information at http://cyber.law.harvard.edu/rss/rss.html#ltcloudgtSubelementOfLtchannelgt
  115. """
  116. def __init__(self, domain, port, path, registerProcedure, protocol):
  117. """ Keyword arguments:
  118. domain -- The domain name or IP address of the cloud.
  119. port -- TCP port that the cloud is running on.
  120. path -- The location of its responder.
  121. registerProcedure -- The name of the procedure to call to request notification.
  122. protocol -- Indication of which protocol is to be used.
  123. """
  124. Serializable.__init__(self)
  125. if domain is None: raise ElementRequiredError("domain")
  126. if port is None: raise ElementRequiredError("port")
  127. if path is None: raise ElementRequiredError("path")
  128. if registerProcedure is None: raise ElementRequiredError("registerProcedure")
  129. if protocol is None: raise ElementRequiredError("protocol")
  130. self.domain = domain
  131. self.port = port
  132. self.path = path
  133. self.registerProcedure = registerProcedure
  134. self.protocol = protocol
  135. def publish(self, handler):
  136. Serializable.publish(self, handler)
  137. self._write_element("cloud", None, { "domain": self.domain, "port": str(self.port), "path": self.path, "registerProcedure": self.registerProcedure, "protocol": self.protocol })
  138. class Image(Serializable):
  139. """ An Image object specifies a GIF, JPEG or PNG image that can be displayed with the channel.
  140. More information at http://cyber.law.harvard.edu/rss/rss.html#ltimagegtSubelementOfLtchannelgt
  141. """
  142. def __init__(self, url, title, link, width = None, height = None, description = None):
  143. """ Keyword arguments:
  144. url -- The URL of the image that represents the channel.
  145. title -- Describes the image. It's used in the ALT attribute of the HTML <img> tag when the channel is rendered in HTML.
  146. link -- The URL of the site. When the channel is rendered the image is a link to the site.
  147. width -- Optional. The width of the image in pixels.
  148. height -- Optional. The height of the image in pixels.
  149. description -- Optional. Contains text that is included in the TITLE attribute of the link formed around the image in the HTML rendering.
  150. """
  151. Serializable.__init__(self)
  152. if url is None: raise ElementRequiredError("url")
  153. if title is None: raise ElementRequiredError("title")
  154. if link is None: raise ElementRequiredError("link")
  155. self.url = url
  156. self.title = title
  157. self.link = link
  158. self.width = width
  159. self.height = height
  160. self.description = description
  161. def publish(self, handler):
  162. Serializable.publish(self, handler)
  163. self.handler.startElement("image", {})
  164. self._write_element("url", self.url)
  165. self._write_element("title", self.title)
  166. self._write_element("link", self.link)
  167. self._write_element("width", self.width)
  168. self._write_element("height", self.height)
  169. self._write_element("description", self.description)
  170. self.handler.endElement("image")
  171. class TextInput(Serializable):
  172. """ A TextInput object specifies a text input box that can be displayed with the channel.
  173. More information at http://cyber.law.harvard.edu/rss/rss.html#lttextinputgtSubelementOfLtchannelgt
  174. """
  175. def __init__(self, title, description, name, link):
  176. """ Keyword arguments:
  177. title -- The label of the submit button in the text input area.
  178. description -- Explains the text input area.
  179. name -- The name of the text object in the text input area.
  180. link -- The URL of the CGI script that processes text input requests.
  181. """
  182. Serializable.__init__(self)
  183. if title is None: raise ElementRequiredError("title")
  184. if description is None: raise ElementRequiredError("description")
  185. if name is None: raise ElementRequiredError("name")
  186. if link is None: raise ElementRequiredError("link")
  187. self.title = title
  188. self.description = description
  189. self.name = name
  190. self.link = link
  191. def publish(self, handler):
  192. Serializable.publish(self, handler)
  193. self.handler.startElement("textInput", {})
  194. self._write_element("title", self.title)
  195. self._write_element("description", self.description)
  196. self._write_element("name", self.name)
  197. self._write_element("link", self.link)
  198. self.handler.endElement("textInput")
  199. class SkipHours(Serializable):
  200. """ A SkipHours object is a hint for aggregators telling them which hours they can skip.
  201. More information at http://cyber.law.harvard.edu/rss/skipHoursDays.html#skiphours
  202. """
  203. def __init__(self, hours):
  204. """ Keyword arguments:
  205. hours -- A list containing up to 24 values between 0 and 23, representing a time in GMT.
  206. """
  207. Serializable.__init__(self)
  208. if hours is None: raise ElementRequiredError("hours")
  209. self.hours = hours
  210. def publish(self, handler):
  211. Serializable.publish(self, handler)
  212. if self.hours:
  213. self.handler.startElement("skipHours", {})
  214. for hour in self.hours:
  215. self._write_element("hour", hour)
  216. self.handler.endElement("skipHours")
  217. class SkipDays(Serializable):
  218. """ A SkipDays object is a hint for aggregators telling them which days they can skip.
  219. More information at http://cyber.law.harvard.edu/rss/skipHoursDays.html#skipdays
  220. """
  221. def __init__(self, days):
  222. """ Keyword arguments:
  223. days -- A list containing up to 7 values. Possible values are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.
  224. """
  225. Serializable.__init__(self)
  226. if days is None: raise ElementRequiredError("days")
  227. self.days = days
  228. def publish(self, handler):
  229. Serializable.publish(self, handler)
  230. if self.days:
  231. self.handler.startElement("skipDays", {})
  232. for day in self.days:
  233. self._write_element("day", day)
  234. self.handler.endElement("skipDays")
  235. class Enclosure(Serializable):
  236. """ An Enclosure object describes a media object that is attached to the item.
  237. More information at http://cyber.law.harvard.edu/rss/rss.html#ltenclosuregtSubelementOfLtitemgt
  238. """
  239. def __init__(self, url, length, type):
  240. """ Keyword arguments:
  241. url -- Indicates where the enclosure is located.
  242. length -- Specifies how big the enclosure is in bytes.
  243. type -- Specifies the standard MIME type of the enclosure.
  244. """
  245. Serializable.__init__(self)
  246. if url is None: raise ElementRequiredError("url")
  247. if length is None: raise ElementRequiredError("length")
  248. if type is None: raise ElementRequiredError("type")
  249. self.url = url
  250. self.length = length
  251. self.type = type
  252. def publish(self, handler):
  253. Serializable.publish(self, handler)
  254. self._write_element("enclosure", None, { "url": self.url, "length": str(self.length), "type": self.type })
  255. class Guid(Serializable):
  256. """ A Guid object represents a string that uniquely identifies the item.
  257. More information at http://cyber.law.harvard.edu/rss/rss.html#ltguidgtSubelementOfLtitemgt
  258. """
  259. def __init__(self, guid, isPermaLink = True):
  260. """ Keyword arguments:
  261. guid -- This is a string that uniquely identifies the item. When present, an aggregator may choose to use this string to determine if an item is new.
  262. isPermaLink -- Indicates whether the guid is a url that points to the item.
  263. """
  264. Serializable.__init__(self)
  265. if guid is None: raise ElementRequiredError("guid")
  266. self.guid = guid
  267. self.isPermaLink = True if isPermaLink is None else isPermaLink
  268. def publish(self, handler):
  269. Serializable.publish(self, handler)
  270. self._write_element("guid", self.guid, { "isPermaLink": "true" if self.isPermaLink else "false" })
  271. class Source(Serializable):
  272. """ A Source object represents the RSS channel that the item came from.
  273. More information at http://cyber.law.harvard.edu/rss/rss.html#ltsourcegtSubelementOfLtitemgt
  274. """
  275. def __init__(self, name, url):
  276. """ Keyword arguments:
  277. name -- The name of the RSS channel that the item came from.
  278. url -- Links to the XMLization of the source.
  279. """
  280. Serializable.__init__(self)
  281. if name is None: raise ElementRequiredError("name")
  282. if url is None: raise ElementRequiredError("url")
  283. self.name = name
  284. self.url = url
  285. def publish(self, handler):
  286. Serializable.publish(self, handler)
  287. self._write_element("source", self.name, { "url": self.url })
  288. class iTunesOwner(Serializable):
  289. """ An iTunesOwner object contains contact information for the owner of the podcast intended to be used for administrative communication.
  290. More information at https://www.apple.com/itunes/podcasts/specs.html#owner
  291. """
  292. def __init__(self, name, email):
  293. """ Keyword arguments
  294. name -- The name of the owner.
  295. email -- The email address of the owner.
  296. """
  297. Serializable.__init__(self)
  298. if name is None: raise ElementRequiredError("name")
  299. if email is None: raise ElementRequiredError("email")
  300. self.name = name
  301. self.email = email
  302. def publish(self, handler):
  303. Serializable.publish(self, handler)
  304. self.handler.startElement("itunes:owner", {})
  305. self._write_element("itunes:name", self.name)
  306. self._write_element("itunes:email", self.email)
  307. self.handler.endElement("itunes:owner")
  308. class iTunesCategory(Serializable):
  309. """ An iTunesCategory object specified the browsing category of the feed.
  310. More information at https://www.apple.com/itunes/podcasts/specs.html#category
  311. """
  312. def __init__(self, name, subcategory = None):
  313. """ Keyword arguments
  314. name -- The name of the category
  315. subcategory -- Optional. The name of the subcategory.
  316. """
  317. Serializable.__init__(self)
  318. if name is None: raise ElementRequiredError("name")
  319. self.name = name
  320. self.subcategory = subcategory
  321. def publish(self, handler):
  322. Serializable.publish(self, handler)
  323. self.handler.startElement("itunes:category", { "text": self.name })
  324. if self.subcategory is not None:
  325. self._write_element("itunes:category", None, { "text": self.subcategory })
  326. self.handler.endElement("itunes:category")
  327. class iTunes(Extension):
  328. """ Extension for iTunes metatags.
  329. More information at https://www.apple.com/itunes/podcasts/specs.html
  330. """
  331. def __init__(self, author = None, block = None, categories = None, image = None, explicit = None, complete = None, owner = None, subtitle = None,
  332. summary = None, new_feed_url = None, type=None):
  333. """ Keyword arguments:
  334. author -- The author of the podcast. Visible under podcast title and in iTunes Store Browse.
  335. block -- Whether the podcast should appear in the iTunes Store podcast directory.
  336. categories -- The browsing categories for this podcast.
  337. image -- The URL of the artwork of your podcast.
  338. explicit -- Whether your podcast contains explicit material.
  339. complete -- Whether your podcast has been completed and no further episodes will be posted in the future.
  340. owner -- Contains contact information for the owner of the podcast.
  341. subtitle -- A few words that represent the description of the podcast.
  342. summary -- An extended summary of the podcast.
  343. new_feed_url -- When changing the podcast RSS URL, this is the new URL where the podcast is located.
  344. type -- The type of podcast.
  345. """
  346. Extension.__init__(self)
  347. self.author = author
  348. self.block = True if (isinstance(block, basestring) and block.lower() == 'yes') else block
  349. self.image = image
  350. self.explicit = True if (isinstance(explicit, basestring) and explicit.lower() == 'yes') else explicit
  351. self.complete = True if (isinstance(complete, basestring) and complete.lower() == 'yes') else complete
  352. self.owner = owner
  353. self.subtitle = subtitle
  354. self.summary = summary
  355. self.new_feed_url = new_feed_url
  356. self.type = type
  357. self.categories = [] if categories is None else categories
  358. if isinstance(self.categories, iTunesCategory):
  359. self.categories = [self.categories]
  360. elif isinstance(self.categories, basestring):
  361. self.categories = [iTunesCategory(self.categories)]
  362. def get_namespace(self):
  363. return {"xmlns:itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd"}
  364. def publish(self, handler):
  365. Extension.publish(self, handler)
  366. self._write_element("itunes:author", self.author)
  367. if self.block is not None:
  368. self._write_element("itunes:block", "yes" if self.block is True else "no")
  369. if self.image is not None:
  370. self._write_element("itunes:image", None, {"href" : self.image })
  371. if self.explicit is not None:
  372. self._write_element("itunes:explicit", "yes" if self.explicit is True else "clean")
  373. if self.complete is not None:
  374. self._write_element("itunes:complete", "yes" if self.complete is True else "no")
  375. if self.owner is not None:
  376. self.owner.publish(self.handler)
  377. self._write_element("itunes:subtitle", self.subtitle)
  378. self._write_element("itunes:summary", self.summary)
  379. self._write_element("itunes:new-feed-url", self.new_feed_url)
  380. self._write_element("itunes:type", self.type)
  381. for category in self.categories:
  382. if isinstance(category, basestring):
  383. category = iTunesCategory(category)
  384. category.publish(self.handler)
  385. class iTunesItem(Serializable):
  386. """ Extension for iTunes Item metatags.
  387. More information at https://www.apple.com/itunes/podcasts/specs.html
  388. """
  389. def __init__(self, author = None, block = None, image = None, duration = None, explicit = None, is_closed_captioned = None, order = None, subtitle = None, summary = None,
  390. title=None, episode=None, episodeType=None, season=None):
  391. """ Keyword arguments:
  392. author -- The author of the episode.
  393. block -- Whether the episode should appear in the iTunes Store podcast directory.
  394. image -- The URL of the artwork of your podcast.
  395. duration -- Specifies the duration of the podcast episode.
  396. explicit -- Whether your episode contains explicit material.
  397. is_closed_captioned -- Whether your episode has embedded closed captioning.
  398. order -- Used to override the default ordering of episodes in the iTunes Store.
  399. subtitle -- A few words that represent the description of the episode.
  400. summary -- An extended summary of the episode.
  401. title -- An episode title.
  402. episode -- An episode number.
  403. episodeType -- The episode type.
  404. season -- The episode season number.
  405. """
  406. Serializable.__init__(self)
  407. self.author = author
  408. self.block = True if (isinstance(block, basestring) and block.lower() == 'yes') else block
  409. self.image = image
  410. self.duration = duration
  411. self.explicit = True if (isinstance(explicit, basestring) and explicit.lower() == 'yes') else explicit
  412. self.is_closed_captioned = True if (isinstance(is_closed_captioned, basestring) and is_closed_captioned.lower() == 'yes') else is_closed_captioned
  413. self.order = order
  414. self.subtitle = subtitle
  415. self.summary = summary
  416. self.title = title
  417. self.episode = episode
  418. self.episodeType = episodeType
  419. self.season = season
  420. def publish(self, handler):
  421. Serializable.publish(self, handler)
  422. self._write_element("itunes:author", self.author)
  423. if self.block is not None:
  424. self._write_element("itunes:block", "yes" if self.block is True else "no")
  425. if self.image is not None:
  426. self._write_element("itunes:image", None, {"href" : self.image })
  427. self._write_element("itunes:duration", self.duration)
  428. if self.explicit is not None:
  429. self._write_element("itunes:explicit", "yes" if self.explicit is True else "clean")
  430. if self.is_closed_captioned is not None:
  431. self._write_element("itunes:is_closed_captioned", "yes" if self.is_closed_captioned is True else "no")
  432. if self.order is not None:
  433. self._write_element("itunes:order", str(self.order))
  434. self._write_element("itunes:subtitle", self.subtitle)
  435. self._write_element("itunes:summary", self.summary)
  436. self._write_element("itunes:title", self.title)
  437. self._write_element("itunes:episode", self.episode)
  438. self._write_element("itunes:episodeType", self.episodeType)
  439. self._write_element("itunes:season", self.season)
  440. class Item(Host):
  441. """ An Item object may represent a "story" - much like a story in a newspaper or magazine; if so its description is a synopsis of the story, and the link points to the full story.
  442. An item may also be complete in itself, if so, the description contains the text, and the link and title may be omitted. All elements of an item are optional, however at least one
  443. of title or description must be present.
  444. More information at http://cyber.law.harvard.edu/rss/rss.html#hrelementsOfLtitemgt
  445. """
  446. def __init__(self, title = None, link = None, description = None, author = None,
  447. creator = None, categories = None, comments = None, enclosure = None,
  448. guid = None, pubDate = None, source = None, extensions = None):
  449. """ Keyword arguments:
  450. title -- Optional. The title of the item.
  451. link -- Optional. The URL of the item.
  452. description -- Optional. The item synopsis.
  453. author -- Optional. Email address of the author of the item.
  454. creator -- Optional. Identifies the person or entity who wrote an item.
  455. categories -- Optional. Includes the item in one or more categories.
  456. comments -- Optional. URL of a page for comments relating to the item.
  457. enclosure -- Optional. Describes a media object that is attached to the item.
  458. guid -- Optional. A string that uniquely identifies the item.
  459. pubDate -- Optional. Indicates when the item was published.
  460. source -- Optional. The RSS channel that the item came from.
  461. extensions -- Optional. The list of extensions added to the item.
  462. """
  463. Host.__init__(self, extensions)
  464. if title is None and description is None:
  465. raise ElementRequiredError("title", "description")
  466. self.title = title
  467. self.link = link
  468. self.description = description
  469. self.author = author
  470. self.creator = creator
  471. self.comments = comments
  472. self.enclosure = enclosure
  473. self.guid = guid
  474. self.pubDate = pubDate
  475. self.source = source
  476. self.categories = [] if categories is None else categories
  477. if isinstance(self.categories, Category):
  478. self.categories = [self.categories]
  479. elif isinstance(self.categories, basestring):
  480. self.categories = [Category(self.categories)]
  481. def publish(self, handler):
  482. Serializable.publish(self, handler)
  483. self.handler.startElement("item", {})
  484. self._write_element("title", self.title)
  485. self._write_element("link", self.link)
  486. self._write_element("description", self.description)
  487. self._write_element("author", self.author)
  488. self._write_element("dc:creator", self.creator)
  489. self._write_element("comments", self.comments)
  490. self._write_element("pubDate", self._date(self.pubDate))
  491. for category in self.categories:
  492. if isinstance(category, basestring):
  493. category = Category(category)
  494. category.publish(self.handler)
  495. if self.enclosure is not None:
  496. self.enclosure.publish(self.handler)
  497. if self.guid is not None:
  498. self.guid.publish(self.handler)
  499. if self.source is not None:
  500. self.source.publish(self.handler)
  501. for extension in self.extensions:
  502. extension.publish(self.handler)
  503. self.handler.endElement("item")
  504. class Feed(Host):
  505. def __init__(self, title, link, description, language = None, copyright = None, managingEditor = None, webMaster = None, pubDate = None,
  506. lastBuildDate = None, categories = None, generator = None, docs = None, cloud = None, ttl = None, image = None, rating = None,
  507. textInput = None, skipHours = None, skipDays = None, items = None, extensions = None):
  508. """ Keyword arguments:
  509. title -- The name of the channel.
  510. link -- The URL to the HTML website corresponding to the channel.
  511. description -- Phrase or sentence describing the channel.
  512. language -- Optional. The language the channel is written in.
  513. copyright -- Optional. Copyright notice for content in the channel.
  514. managingEditor -- Optional. Email address for person responsible for editorial content.
  515. webMaster -- Optional. Email address for person responsible for technical issues relating to channel.
  516. pubDate -- Optional. The publication date for the content in the channel. This should be a datetime in GMT format.
  517. lastBuildDate -- Optional. The last time the content of the channel changed. This should be a datetime in GMT format.
  518. categories -- Optional. Specify one or more categories that the channel belongs to.
  519. generator -- Optional. A string indicating the program used to generate the channel.
  520. docs -- Optional. A URL that points to the documentation for the format used in the RSS file.
  521. cloud -- Optional. Allows processes to register with a cloud to be notified of updates to the channel. This is a Cloud object.
  522. ttl -- Optional. The number of minutes that indicates how long a channel can be cached before refreshing from the source. This should be an integer value.
  523. image -- Optional. Specifies an image that can be displayed with the channel. This is an Image object.
  524. rating -- Optional. The PICS rating for the channel. See http://www.w3.org/PICS/.
  525. textInput -- Optional. Specifies a text input box that can be displayed with the channel.
  526. skipHours -- Optional. A hint for aggregators telling them which hours they can skip.
  527. skipDays -- Optional. A hint for aggregators telling them which days they can skip.
  528. items -- Optional. The list of items included in this channel.
  529. extensions -- Optional. The list of extensions added to the feed.
  530. """
  531. Host.__init__(self, extensions)
  532. if title is None: raise ElementRequiredError("title")
  533. if link is None: raise ElementRequiredError("link")
  534. if description is None: raise ElementRequiredError("description")
  535. self.title = title
  536. self.link = link
  537. self.description = description
  538. self.language = language
  539. self.copyright = copyright
  540. self.managingEditor = managingEditor
  541. self.webMaster = webMaster
  542. self.pubDate = pubDate
  543. self.lastBuildDate = lastBuildDate
  544. self.generator = _generator if generator is None else generator
  545. self.docs = _docs if docs is None else docs
  546. self.cloud = cloud
  547. self.ttl = ttl
  548. self.image = image
  549. self.rating = rating
  550. self.textInput = textInput
  551. self.skipHours = skipHours
  552. self.skipDays = skipDays
  553. self.categories = [] if categories is None else categories
  554. if isinstance(self.categories, Category):
  555. self.categories = [self.categories]
  556. elif isinstance(self.categories, basestring):
  557. self.categories = [Category(self.categories)]
  558. self.items = [] if items is None else items
  559. def rss(self):
  560. output = StringIO()
  561. handler = saxutils.XMLGenerator(output, 'UTF-8')
  562. handler.startDocument()
  563. handler.startElement("rss", self._get_attributes())
  564. self.publish(handler)
  565. handler.endElement("rss")
  566. handler.endDocument()
  567. return output.getvalue()
  568. def publish(self, handler):
  569. Serializable.publish(self, handler)
  570. handler.startElement("channel", {})
  571. self._write_element("title", self.title)
  572. self._write_element("link", self.link)
  573. self._write_element("description", self.description)
  574. self._write_element("language", self.language)
  575. self._write_element("copyright", self.copyright)
  576. self._write_element("managingEditor", self.managingEditor)
  577. self._write_element("webMaster", self.webMaster)
  578. self._write_element("pubDate", self._date(self.pubDate))
  579. self._write_element("lastBuildDate", self._date(self.lastBuildDate))
  580. self._write_element("generator", self.generator)
  581. self._write_element("docs", self.docs)
  582. self._write_element("ttl", self.ttl)
  583. self._write_element("rating", self.rating)
  584. for category in self.categories:
  585. if isinstance(category, basestring):
  586. category = Category(category)
  587. category.publish(self.handler)
  588. if self.cloud is not None:
  589. self.cloud.publish(self.handler)
  590. if self.image is not None:
  591. self.image.publish(self.handler)
  592. if self.textInput is not None:
  593. self.textInput.publish(self.handler)
  594. if self.skipHours is not None:
  595. self.skipHours.publish(self.handler)
  596. if self.skipDays is not None:
  597. self.skipDays.publish(self.handler)
  598. for extension in self.extensions:
  599. extension.publish(self.handler)
  600. for item in self.items:
  601. item.publish(self.handler)
  602. handler.endElement("channel")
  603. def _get_attributes(self):
  604. attributes = {"version": "2.0", "xmlns:dc" : "http://purl.org/dc/elements/1.1/"}
  605. for extension in self.extensions:
  606. if isinstance(extension, Extension):
  607. namespace = extension.get_namespace()
  608. if namespace is not None:
  609. attributes = dict(itertools.chain(attributes.items(), namespace.items()))
  610. return attributes
  611. class ElementRequiredError(Exception):
  612. def __init__(self, element1, element2 = None):
  613. self.element1 = element1
  614. self.element2 = element2
  615. def __str__(self):
  616. if self.element2 is not None:
  617. return 'Either "' + self.element1 + '" or "' + self.element2 + '" must be defined'
  618. return '"' + self.element1 + '" must be defined'