[2927] | 1 | import re, sys, urllib2 |
---|
[2139] | 2 | from xml.dom import Node |
---|
[387] | 3 | |
---|
| 4 | from zope.interface import implements |
---|
[2895] | 5 | from zope.component import queryAdapter |
---|
| 6 | from zope.component import queryMultiAdapter |
---|
[387] | 7 | from Products.Five.browser import BrowserView |
---|
| 8 | |
---|
[2139] | 9 | from Products.CMFPlone.utils import safe_unicode, getSiteEncoding |
---|
[387] | 10 | from Products.CMFCore.utils import getToolByName |
---|
| 11 | |
---|
| 12 | from interfaces import IValidateSEOKeywordsView |
---|
[1682] | 13 | from quintagroup.seoptimizer import SeoptimizerMessageFactory as _ |
---|
[2895] | 14 | from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema |
---|
[387] | 15 | |
---|
| 16 | class ValidateSEOKeywordsView(BrowserView): |
---|
| 17 | |
---|
| 18 | implements(IValidateSEOKeywordsView) |
---|
| 19 | |
---|
[2139] | 20 | def validateKeywords(self): |
---|
[387] | 21 | """ see interface """ |
---|
[2139] | 22 | text = self.request.get('text') |
---|
[1466] | 23 | ts = getToolByName(self.context, 'translation_service') |
---|
[2895] | 24 | transforms = getToolByName(self.context, 'portal_transforms') |
---|
| 25 | portal = getToolByName(self.context, 'portal_url').getPortalObject() |
---|
| 26 | isExternal = queryAdapter(portal, ISEOConfigletSchema).external_keywords_test |
---|
[387] | 27 | # extract keywords from text |
---|
[2139] | 28 | enc = getSiteEncoding(self.context) |
---|
[1466] | 29 | if text.lower().strip(): |
---|
[2139] | 30 | keywords = filter(None, map(lambda x: safe_unicode(x.strip(), enc), |
---|
| 31 | text.lower().strip().split('\n'))) |
---|
[1466] | 32 | else: |
---|
[2139] | 33 | return ts.utranslate(domain='quintagroup.seoptimizer', |
---|
| 34 | msgid=_(u'Keywords list is empty!'), |
---|
| 35 | context=self.context) |
---|
[2901] | 36 | # Get html page internally or with external request |
---|
[2927] | 37 | error_url = "" |
---|
[2895] | 38 | if isExternal: |
---|
| 39 | # Not pass timeout option because: |
---|
[2929] | 40 | # 1. its value get from the global default timeout settings. |
---|
[2895] | 41 | # 2. timeout option added in python 2.6 (so acceptable only in plone4+) |
---|
| 42 | try: |
---|
[2901] | 43 | try: |
---|
| 44 | resp = urllib2.urlopen(self.context.absolute_url()) |
---|
| 45 | html = resp.read() |
---|
| 46 | finally: |
---|
[2929] | 47 | if 'resp' in locals().keys(): |
---|
| 48 | resp.close() |
---|
[2927] | 49 | except (urllib2.URLError, urllib2.HTTPError), e: |
---|
[2901] | 50 | # In case of exceed timeout period or other URL connection errors. |
---|
[2927] | 51 | info = sys.exc_info() |
---|
| 52 | elog = getToolByName(self.context, "error_log") |
---|
| 53 | if elog: |
---|
| 54 | error_url = elog.raising(info) |
---|
[2901] | 55 | html = None |
---|
[387] | 56 | else: |
---|
[2895] | 57 | html = unicode(self.context()).encode(enc) |
---|
[2901] | 58 | |
---|
| 59 | # If no html - information about problem with page retrieval should be returned |
---|
[2139] | 60 | result = [] |
---|
[2929] | 61 | if html is None: |
---|
| 62 | sfx = error_url and ", details at %s." % error_url or "." |
---|
| 63 | result.append("Problem with page retrieval" + sfx) |
---|
| 64 | else: |
---|
[2901] | 65 | page_text = transforms.convert("html_to_text", html).getData() |
---|
| 66 | # check every keyword on appearing in body of html page |
---|
| 67 | for keyword in keywords: |
---|
| 68 | keyword_on_page = unicode(len(re.findall(u'\\b%s\\b' % keyword, page_text, re.I|re.U))) |
---|
| 69 | result.append(' - '.join((keyword, keyword_on_page))) |
---|
| 70 | |
---|
[2139] | 71 | return ts.utranslate(domain='quintagroup.seoptimizer', |
---|
| 72 | msgid=_(u'number_keywords', |
---|
| 73 | default=u'Number of keywords at page:\n${result}', |
---|
| 74 | mapping={'result':'\n'.join(result)}), |
---|
| 75 | context=self.context) |
---|