[387] | 1 | import re, commands |
---|
| 2 | from zope.interface import implements |
---|
[408] | 3 | from zope.component import queryMultiAdapter |
---|
[387] | 4 | from Products.CMFCore.utils import getToolByName |
---|
| 5 | |
---|
[1313] | 6 | from quintagroup.seoptimizer.interfaces import IKeywords, IMappingMetaTags |
---|
| 7 | from quintagroup.seoptimizer.util import SortedDict |
---|
[387] | 8 | |
---|
[1313] | 9 | |
---|
[387] | 10 | class AdditionalKeywords(object): |
---|
| 11 | implements(IKeywords) |
---|
| 12 | |
---|
| 13 | def __init__(self, context): |
---|
| 14 | self.context = context |
---|
| 15 | |
---|
| 16 | def listKeywords(self): |
---|
| 17 | portal_props = getToolByName(self.context, 'portal_properties') |
---|
[1313] | 18 | seo_props = getToolByName(portal_props, 'seo_properties', None) |
---|
[387] | 19 | |
---|
| 20 | # now set type is not using because of it unordered behaviour |
---|
| 21 | #original = set(self.context.qSEO_Keywords()) |
---|
| 22 | #additional = set(seo_props.additional_keywords) |
---|
| 23 | #text = set(self.context.SearchableText().split()) |
---|
| 24 | #keywords = list(additional.intersection(text).union(original)) |
---|
| 25 | |
---|
[408] | 26 | request = self.context.REQUEST |
---|
| 27 | seo_context = queryMultiAdapter((self.context, request), name='seo_context') |
---|
[1313] | 28 | if seo_context: |
---|
| 29 | keywords = list(seo_context.seo_keywords()) |
---|
| 30 | lower_keywords = map(lambda x: x.lower(), keywords) |
---|
| 31 | additional = seo_props.additional_keywords |
---|
[387] | 32 | |
---|
[1313] | 33 | is_test = self.context.REQUEST.get('qseo_without_additional_keywords', None) |
---|
[387] | 34 | |
---|
[1313] | 35 | if additional and is_test is None: |
---|
| 36 | # extract words from url page using lynx browser |
---|
| 37 | text = commands.getoutput('lynx --dump --nolist %s?qseo_without_additional_keywords=1' % self.context.absolute_url()).lower() |
---|
| 38 | if text and text != 'sh: lynx: command not found': |
---|
| 39 | for keyword in additional: |
---|
| 40 | if keyword.lower() not in lower_keywords and re.compile(r'\b%s\b' % keyword, re.I).search(text): |
---|
| 41 | keywords.append(keyword) |
---|
| 42 | return ', '.join(keywords) |
---|
| 43 | return '' |
---|
[387] | 44 | |
---|
[1313] | 45 | |
---|
| 46 | class MappingMetaTags(object): |
---|
| 47 | implements(IMappingMetaTags) |
---|
| 48 | |
---|
| 49 | def __init__(self, context): |
---|
| 50 | self.context = context |
---|
| 51 | self.portal_props = getToolByName(self.context, 'portal_properties') |
---|
| 52 | self.seo_props = getToolByName(self.portal_props, 'seo_properties', None) |
---|
| 53 | |
---|
| 54 | def getMappingMetaTags(self): |
---|
| 55 | metadata_name = SortedDict() |
---|
| 56 | if self.seo_props: |
---|
| 57 | pmn = self.seo_props.getProperty('metatags_order') |
---|
| 58 | pmn = pmn and pmn or '' |
---|
| 59 | for mt in [mt.split(' ') for mt in pmn if len(mt.split(' '))==2]: |
---|
| 60 | metadata_name[mt[0]] = mt[1] |
---|
| 61 | return metadata_name |
---|