source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/adapters.py @ 1519

Last change on this file since 1519 was 1509, checked in by liebster, 14 years ago

Added doc strings.

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