Changeset 2901 in products


Ignore:
Timestamp:
Oct 20, 2010 1:14:13 PM (14 years ago)
Author:
mylan
Message:

#233: Check SEO keywords: Added error handling, return reasonable info in case of error

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/browser/keywords.py

    r2895 r2901  
    3434                                 msgid=_(u'Keywords list is empty!'), 
    3535                                 context=self.context) 
    36         # request html page of context object 
     36        # Get html page internally or with external request 
    3737        if isExternal: 
    3838            # Not pass timeout option because: 
     
    4040            # 2. timeout option added in python 2.6 (so acceptable only in plone4+) 
    4141            try: 
    42                 html = urllib2.urlopen(self.context.absolute_url()) 
    43             except urllib2.URLError: 
    44                 # In case of exceed timeout period 
    45                 # or other URL connection errors. 
    46                 html = unicode(self.context()).encode(enc) 
     42                try: 
     43                    resp = urllib2.urlopen(self.context.absolute_url()) 
     44                    html = resp.read() 
     45                finally: 
     46                    'resp' in locals().keys() and resp.close() 
     47            except Exception: 
     48                # In case of exceed timeout period or other URL connection errors. 
     49                html = None 
    4750        else: 
    4851            html = unicode(self.context()).encode(enc) 
    49         page_text = transforms.convert("html_to_text", html).getData() 
    50                                   
    51         # check every keyword on appearing in body of html page 
     52 
     53        # If no html - information about problem with page retrieval should be returned 
    5254        result = [] 
    53         for keyword in keywords: 
    54             keyword_on_page = unicode(len(re.findall(u'\\b%s\\b' % keyword, page_text, re.I|re.U))) 
    55             result.append(' - '.join((keyword, keyword_on_page))) 
     55        if html is not None: 
     56            page_text = transforms.convert("html_to_text", html).getData() 
     57            # check every keyword on appearing in body of html page 
     58            for keyword in keywords: 
     59                keyword_on_page = unicode(len(re.findall(u'\\b%s\\b' % keyword, page_text, re.I|re.U))) 
     60                result.append(' - '.join((keyword, keyword_on_page))) 
     61        else: 
     62            result.append("Problem with page retrieval") 
     63 
    5664        return ts.utranslate(domain='quintagroup.seoptimizer', 
    5765                             msgid=_(u'number_keywords', 
Note: See TracChangeset for help on using the changeset viewer.