annotate myrss/myrss_parser.py @ 42:a1456ecd25b9

fix bug with threadpool; create WSGI app and test server; use logging
author paulo
date Mon, 04 Feb 2013 23:58:02 -0800
parents 5f9bc02e9caf
children
rev   line source
paulo@39 1 import os
paulo@40 2 import sys
paulo@39 3 import re
paulo@40 4 import urllib2
paulo@40 5 import threading
paulo@40 6 import Queue
paulo@41 7 import datetime
paulo@41 8 import time
paulo@42 9 import logging
paulo@42 10 logging.basicConfig(level=logging.INFO)
paulo@39 11
paulo@39 12 import html
paulo@39 13 import xml.etree.ElementTree
paulo@39 14
paulo@39 15
paulo@41 16 FEEDS_FILE = "FEEDS"
paulo@41 17 CACHE_HTML_FILE = "__cache__.html"
paulo@41 18
paulo@41 19 #CACHE_LIFE = 1200 # [seconds]
paulo@41 20 CACHE_LIFE = 30 # [seconds]
paulo@39 21 MAX_ITEMS = 30
paulo@39 22 MAX_LINK_Z = 4
paulo@40 23 MAX_THREADS = 20
paulo@39 24
paulo@39 25
paulo@39 26 _PARSE_ROOT_TAG_RE = re.compile(r"(\{(.+)\})?(.+)")
paulo@39 27
paulo@39 28 def _parse_root_tag(root_tag):
paulo@39 29 re_match = _PARSE_ROOT_TAG_RE.match(root_tag)
paulo@39 30
paulo@39 31 if re_match is None:
paulo@39 32 return (None, None)
paulo@39 33 else:
paulo@39 34 return re_match.group(2, 3)
paulo@39 35
paulo@39 36
paulo@39 37 def _go_rss(elementTree):
paulo@39 38 title = elementTree.find("channel/title").text.strip()
paulo@39 39 link = elementTree.find("channel/link").text
paulo@39 40
paulo@39 41 items = []
paulo@39 42
paulo@39 43 for i in elementTree.findall("channel/item")[:MAX_ITEMS]:
paulo@39 44 it_title = i.find("title").text.strip()
paulo@39 45 it_link = i.find("link").text
paulo@39 46
paulo@39 47 items.append((it_title, it_link))
paulo@39 48
paulo@39 49 return (title, link, items)
paulo@39 50
paulo@39 51
paulo@39 52 def _go_atom(elementTree):
paulo@39 53 ns = "http://www.w3.org/2005/Atom"
paulo@39 54
paulo@39 55 title = elementTree.find("{%s}title" % ns).text.strip()
paulo@39 56 link = ''
paulo@39 57
paulo@39 58 for i in elementTree.findall("{%s}link" % ns):
paulo@39 59 if i.get("type") == "text/html" and i.get("rel") == "alternate":
paulo@39 60 link = i.get("href")
paulo@39 61 break
paulo@39 62
paulo@39 63 items = []
paulo@39 64
paulo@39 65 for i in elementTree.findall("{%s}entry" % ns)[:MAX_ITEMS]:
paulo@39 66 it_title = i.find("{%s}title" % ns).text.strip()
paulo@39 67 it_link = ''
paulo@39 68
paulo@39 69 for j in i.findall("{%s}link" % ns):
paulo@39 70 if j.get("type") == "text/html" and j.get("rel") == "alternate":
paulo@39 71 it_link = j.get("href")
paulo@39 72 break
paulo@39 73
paulo@39 74 items.append((it_title, it_link))
paulo@39 75
paulo@39 76 return (title, link, items)
paulo@39 77
paulo@39 78
paulo@41 79 def _to_html(dtnow, docstruct):
paulo@41 80 datetime_str = dtnow.strftime("%Y-%m-%d %H:%M %Z")
paulo@41 81 page_title = "myrss -- %s" % datetime_str
paulo@41 82
paulo@42 83 root = html.HTML("html")
paulo@39 84
paulo@39 85 header = root.header
paulo@41 86 header.title(page_title)
paulo@39 87 header.link(rel="stylesheet", type="text/css", href="index.css")
paulo@39 88
paulo@41 89 body = root.body
paulo@41 90 body.h1(page_title)
paulo@41 91
paulo@39 92 link_z = 0
paulo@39 93
paulo@39 94 for feed in docstruct:
paulo@40 95 if feed is None:
paulo@40 96 continue
paulo@40 97
paulo@39 98 (title, link, items) = feed
paulo@39 99
paulo@41 100 body.h2.a(title, href=link, klass="z%d" % (link_z % MAX_LINK_Z))
paulo@39 101 link_z += 1
paulo@41 102 p = body.p
paulo@39 103
paulo@39 104 for (i, (it_title, it_link)) in enumerate(items):
paulo@39 105 if i > 0:
paulo@39 106 p += " - "
paulo@39 107
paulo@39 108 p.a(it_title, href=it_link, klass="z%d" % (link_z % MAX_LINK_Z))
paulo@39 109 link_z += 1
paulo@39 110
paulo@39 111 return unicode(root).encode("utf-8")
paulo@39 112
paulo@39 113
paulo@40 114 def _process_url(url):
paulo@40 115 ret = None
paulo@40 116
paulo@40 117 try:
paulo@42 118 logging.info("processing %s" % url)
paulo@41 119 feed = urllib2.urlopen(urllib2.Request(url, headers={"User-Agent": ''}))
paulo@40 120 except urllib2.HTTPError as e:
paulo@42 121 logging.info("(%s) %s" % (url, e))
paulo@40 122 return ret
paulo@40 123
paulo@40 124 elementTree = xml.etree.ElementTree.parse(feed)
paulo@40 125 root = elementTree.getroot()
paulo@40 126
paulo@40 127 parsed_root_tag = _parse_root_tag(root.tag)
paulo@40 128
paulo@40 129 if parsed_root_tag == (None, "rss"):
paulo@40 130 version = float(root.get("version", 0.0))
paulo@40 131 if version >= 2.0:
paulo@40 132 ret = _go_rss(elementTree)
paulo@40 133 else:
paulo@40 134 raise NotImplementedError("Unsupported rss version")
paulo@40 135 elif parsed_root_tag == ("http://www.w3.org/2005/Atom", "feed"):
paulo@40 136 ret = _go_atom(elementTree)
paulo@40 137 else:
paulo@40 138 raise NotImplementedError("Unknown root tag")
paulo@40 139
paulo@40 140 return ret
paulo@40 141
paulo@40 142
paulo@40 143 class WorkerThread(threading.Thread):
paulo@40 144 def __init__(self, *args, **kwargs):
paulo@40 145 self._input_queue = kwargs.pop("input_queue")
paulo@40 146 self._output_queue = kwargs.pop("output_queue")
paulo@40 147 threading.Thread.__init__(self, *args, **kwargs)
paulo@40 148 self.daemon = True
paulo@40 149
paulo@40 150 def run(self):
paulo@40 151 while True:
paulo@40 152 (idx, url) = self._input_queue.get()
paulo@40 153 docfeed = None
paulo@40 154 try:
paulo@40 155 docfeed = _process_url(url)
paulo@40 156 except Exception as e:
paulo@42 157 logging.info("(%s) exception: %s" % (url, e))
paulo@40 158 self._output_queue.put((idx, docfeed))
paulo@40 159
paulo@40 160
paulo@42 161 def main(input_queue, output_queue):
paulo@41 162 ret = ''
paulo@41 163
paulo@41 164 epoch_now = time.time()
paulo@41 165 dtnow = datetime.datetime.fromtimestamp(epoch_now)
paulo@41 166
paulo@41 167 if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE):
paulo@41 168 with open(CACHE_HTML_FILE) as cache_html_file:
paulo@41 169 ret = cache_html_file.read()
paulo@41 170
paulo@41 171 else:
paulo@41 172 with open(FEEDS_FILE) as feeds_file:
paulo@41 173 feedlines = feeds_file.readlines()
paulo@41 174
paulo@41 175 docstruct = [None]*len(feedlines)
paulo@42 176 num_input = 0
paulo@41 177 for (i, l) in enumerate(feedlines):
paulo@41 178 if l[0] != '#':
paulo@41 179 l = l.strip()
paulo@42 180 input_queue.put((i, l))
paulo@42 181 num_input += 1
paulo@41 182
paulo@42 183 for _ in range(num_input):
paulo@42 184 (idx, docfeed) = output_queue.get()
paulo@42 185 docstruct[idx] = docfeed
paulo@41 186
paulo@41 187 ret = _to_html(dtnow, docstruct)
paulo@41 188
paulo@41 189 with open(CACHE_HTML_FILE, 'w') as cache_html_file:
paulo@41 190 cache_html_file.write(ret)
paulo@41 191
paulo@41 192 return ret
paulo@41 193
paulo@41 194
paulo@42 195 class MyRssApp:
paulo@42 196 def __init__(self):
paulo@42 197 self._iq = Queue.Queue(MAX_THREADS)
paulo@42 198 self._oq = Queue.Queue(MAX_THREADS)
paulo@39 199
paulo@42 200 for _ in range(MAX_THREADS):
paulo@42 201 WorkerThread(input_queue=self._iq, output_queue=self._oq).start()
paulo@42 202
paulo@42 203 def __call__(self, environ, start_response):
paulo@42 204 response_body = main(self._iq, self._oq)
paulo@42 205 response_headers = [
paulo@42 206 ("Content-Type", "text/html"),
paulo@42 207 ("Content-Length", str(len(response_body))),
paulo@42 208 ]
paulo@42 209 start_response("200 OK", response_headers)
paulo@42 210
paulo@42 211 return [response_body]
paulo@42 212
paulo@42 213