| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | PyLucid RSS plugin |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | Include external RSS Feeds directly into a CMS page. |
|---|
| 8 | Used feedparser by Mark Pilgrim: http://feedparser.org |
|---|
| 9 | http://feedparser.googlecode.com/svn/trunk/LICENSE |
|---|
| 10 | |
|---|
| 11 | Last commit info: |
|---|
| 12 | ~~~~~~~~~~~~~~~~~ |
|---|
| 13 | $LastChangedDate$ |
|---|
| 14 | $Rev$ |
|---|
| 15 | $Author$ |
|---|
| 16 | |
|---|
| 17 | :copyleft: 2007 by the PyLucid team, see AUTHORS for more details. |
|---|
| 18 | :license: GNU GPL v3 or above, see LICENSE for more details. |
|---|
| 19 | """ |
|---|
| 20 | |
|---|
| 21 | __version__= "$Rev$" |
|---|
| 22 | |
|---|
| 23 | import time, socket |
|---|
| 24 | |
|---|
| 25 | from PyLucid.system.BasePlugin import PyLucidBasePlugin |
|---|
| 26 | from PyLucid.tools.utils import escape |
|---|
| 27 | from PyLucid.tools import feedparser |
|---|
| 28 | |
|---|
| 29 | from django.core.cache import cache |
|---|
| 30 | from django.utils.safestring import mark_safe |
|---|
| 31 | |
|---|
| 32 | UPDATE_INFO = mark_safe( |
|---|
| 33 | 'Warning, title argument for RSS Plugin is obsolete! See:' |
|---|
| 34 | ' <a href="http://pylucid.org/_goto/121/changes/#18-07-2008-RSS-plugin-updates">' |
|---|
| 35 | 'Backwards-incompatible changes</a>' |
|---|
| 36 | ) |
|---|
| 37 | |
|---|
| 38 | class RSS(PyLucidBasePlugin): |
|---|
| 39 | |
|---|
| 40 | def _debug(self, url, feed): |
|---|
| 41 | from pprint import pformat |
|---|
| 42 | self.response.write("<h2>RSS debug for '%s':</h2>\n" % url) |
|---|
| 43 | self.response.write("<pre>\n") |
|---|
| 44 | self.response.write(escape(pformat(feed))) |
|---|
| 45 | self.response.write("</pre>\n") |
|---|
| 46 | |
|---|
| 47 | def lucidTag(self, url, internal_page=None, debug=None, pref_id=None, title=None): |
|---|
| 48 | |
|---|
| 49 | if self.request.debug: |
|---|
| 50 | if title: |
|---|
| 51 | # title tag is obsolete |
|---|
| 52 | self.page_msg(UPDATE_INFO) |
|---|
| 53 | |
|---|
| 54 | # Get the preferences from the database: |
|---|
| 55 | if pref_id: |
|---|
| 56 | preferences = self.get_preferences(id = pref_id) |
|---|
| 57 | else: |
|---|
| 58 | # get the default entry |
|---|
| 59 | preferences = self.get_preferences() |
|---|
| 60 | |
|---|
| 61 | if preferences == None: |
|---|
| 62 | self.page_msg.red("Can't get preferences from database.") |
|---|
| 63 | return |
|---|
| 64 | |
|---|
| 65 | if internal_page == None: |
|---|
| 66 | internal_page = preferences["internal_page"] |
|---|
| 67 | if debug == None: |
|---|
| 68 | debug = preferences["debug"] |
|---|
| 69 | |
|---|
| 70 | # rss_page = cache.get(url) |
|---|
| 71 | # if rss_page: |
|---|
| 72 | # self.response.write(self.info_txt % "[Used cached data]") |
|---|
| 73 | # else: |
|---|
| 74 | # Was not cached |
|---|
| 75 | |
|---|
| 76 | start_time = time.time() |
|---|
| 77 | |
|---|
| 78 | timeout = preferences.get("timeout", 1) |
|---|
| 79 | try: |
|---|
| 80 | old_timeout = socket.getdefaulttimeout() |
|---|
| 81 | socket.setdefaulttimeout(timeout) |
|---|
| 82 | feed = feedparser.parse(url) |
|---|
| 83 | socket.setdefaulttimeout(old_timeout) |
|---|
| 84 | except Exception, e: |
|---|
| 85 | self.response.write( |
|---|
| 86 | "[Can't get RSS feed '%s' Error:'%s']" % (url, e ) |
|---|
| 87 | ) |
|---|
| 88 | return |
|---|
| 89 | |
|---|
| 90 | # cache.set(url, rss_page) |
|---|
| 91 | |
|---|
| 92 | duration = time.time() - start_time |
|---|
| 93 | |
|---|
| 94 | if debug: |
|---|
| 95 | self._debug(url, feed) |
|---|
| 96 | |
|---|
| 97 | context = { |
|---|
| 98 | "url": url, |
|---|
| 99 | "feed": feed, |
|---|
| 100 | "duration": duration, |
|---|
| 101 | } |
|---|
| 102 | self._render_template(internal_page, context)#, debug=True) |
|---|