1 | import re, commands |
---|
2 | from zope.interface import implements |
---|
3 | from zope.component import queryMultiAdapter |
---|
4 | from Products.CMFCore.utils import getToolByName |
---|
5 | from quintagroup.seoptimizer.interfaces import IKeywords |
---|
6 | |
---|
7 | |
---|
8 | class AdditionalKeywords(object): |
---|
9 | implements(IKeywords) |
---|
10 | |
---|
11 | def __init__(self, context): |
---|
12 | self.context = context |
---|
13 | |
---|
14 | def listKeywords(self): |
---|
15 | portal_props = getToolByName(self.context, 'portal_properties') |
---|
16 | seo_props = getToolByName(portal_props, 'seo_properties') |
---|
17 | |
---|
18 | # now set type is not using because of it unordered behaviour |
---|
19 | #original = set(self.context.qSEO_Keywords()) |
---|
20 | #additional = set(seo_props.additional_keywords) |
---|
21 | #text = set(self.context.SearchableText().split()) |
---|
22 | #keywords = list(additional.intersection(text).union(original)) |
---|
23 | |
---|
24 | request = self.context.REQUEST |
---|
25 | seo_context = queryMultiAdapter((self.context, request), name='seo_context') |
---|
26 | keywords = list(seo_context.seo_keywords()) |
---|
27 | lower_keywords = map(lambda x: x.lower(), keywords) |
---|
28 | additional = seo_props.additional_keywords |
---|
29 | |
---|
30 | is_test = self.context.REQUEST.get('qseo_without_additional_keywords', None) |
---|
31 | |
---|
32 | if additional and is_test is None: |
---|
33 | # extract words from url page using lynx browser |
---|
34 | text = commands.getoutput('lynx --dump --nolist %s?qseo_without_additional_keywords=1' % self.context.absolute_url()).lower() |
---|
35 | if text and text != 'sh: lynx: command not found': |
---|
36 | for keyword in additional: |
---|
37 | if keyword.lower() not in lower_keywords and re.compile(r'\b%s\b' % keyword, re.I).search(text): |
---|
38 | keywords.append(keyword) |
---|
39 | |
---|
40 | return ', '.join(keywords) |
---|