comparison myrss/myrss_app.py @ 46:aca02ce71274

myrss_app.py: add urlopen timeout, and render time
author paulo
date Tue, 12 Feb 2013 00:43:11 -0700
parents c673e9e9c4ca
children 315afeb47e52
comparison
equal deleted inserted replaced
1:d73fc7405ce6 2:d39f15d9bc72
18 18
19 CACHE_LIFE = 1200 # [seconds] 19 CACHE_LIFE = 1200 # [seconds]
20 MAX_ITEMS = 30 20 MAX_ITEMS = 30
21 MAX_LINK_Z = 4 21 MAX_LINK_Z = 4
22 MAX_THREADS = 20 22 MAX_THREADS = 20
23 URLOPEN_TIMEOUT = 60 # [seconds]
23 24
24 25
25 _PARSE_ROOT_TAG_RE = re.compile(r"(\{(.+)\})?(.+)") 26 _PARSE_ROOT_TAG_RE = re.compile(r"(\{(.+)\})?(.+)")
26 27
27 def _parse_root_tag(root_tag): 28 def _parse_root_tag(root_tag):
105 p += " - " 106 p += " - "
106 107
107 p.a(it_title, href=it_link, klass="z%d" % (link_z % MAX_LINK_Z)) 108 p.a(it_title, href=it_link, klass="z%d" % (link_z % MAX_LINK_Z))
108 link_z += 1 109 link_z += 1
109 110
111 dtdelta = datetime.datetime.now() - dtnow
112 root.div("%.3f" % (dtdelta.days*86400 + dtdelta.seconds + dtdelta.microseconds/1e6), klass="debug")
113
110 return unicode(root).encode("utf-8") 114 return unicode(root).encode("utf-8")
111 115
112 116
113 def _process_url(url): 117 def _process_url(url):
114 ret = None 118 ret = None
115 119
116 try: 120 try:
117 logging.info("processing %s" % url) 121 logging.info("processing %s" % url)
118 feed = urllib2.urlopen(urllib2.Request(url, headers={"User-Agent": ''})) 122 feed = urllib2.urlopen(urllib2.Request(url, headers={"User-Agent": ''}), timeout=URLOPEN_TIMEOUT)
119 except urllib2.HTTPError as e: 123 except urllib2.HTTPError as e:
120 logging.info("(%s) %s" % (url, e)) 124 logging.info("(%s) %s" % (url, e))
121 return ret 125 return ret
122 126
123 elementTree = xml.etree.ElementTree.parse(feed) 127 elementTree = xml.etree.ElementTree.parse(feed)