source: LinkExchange/trunk/linkexchange/clients/trustlink.py @ 259

Revision 259, 7.0 KB checked in by lostclus, 5 months ago (diff)

Fixed test links in TrustLinkClient?.

Line 
1# LinkExchange - Universal link exchange service client
2# Copyright (C) 2009-2011 Konstantin Korikov
3#
4# This library is free software; you can redistribute it and/or
5# modify it under the terms of the GNU Lesser General Public
6# License as published by the Free Software Foundation; either
7# version 2.1 of the License, or (at your option) any later version.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12# Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public
15# License along with this library; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17#
18# NOTE: In the context of the Python environment, I interpret "dynamic
19# linking" as importing -- thus the LGPL applies to the contents of
20# the modules, but make no requirements on code importing these
21# modules.
22
23import urlparse
24import logging
25
26from linkexchange.clients.sape import SapeLikeClient
27from linkexchange.clients.sape import SapeLikeTestServer
28from linkexchange.utils import is_plugin_specifier, load_plugin
29
30log = logging.getLogger('linkexchange.clients.trustlink')
31
32class TrustLinkClient(SapeLikeClient):
33    """
34    TrustLink.ru client.
35    """
36
37    server_list = [
38            'http://db.trustlink.ru/%(user)s/%(host)s/UTF-8']
39    server_format = 'php'
40    force_show_code = False
41    link_template = u"""
42    <ul>
43      <li><a href="%(href)s">%(anchor)s</a></li>
44      <li>%(text)s</li>
45      <li>%(host)s</li>
46    </ul>"""
47    link_template_no_anchor = u"""
48    <ul>
49      <li>%(text)s</li>
50      <li>%(host)s</li>
51    </ul>"""
52
53    def __init__(self, user, db_driver, **kw):
54        """
55        TrustLinkClient constructor.
56       
57        The user is hash code string that assigned to user on link exchange
58        service.
59
60        The db_driver argument is multihash database driver instance or plugin
61        specifier. In second case plugin specifier is used to create new
62        instance. The database driver instance is used to store links database.
63
64        @param user: user hash code string on link exchange service
65        @param db_driver: multihash database driver instance or plugin specifier
66        @keyword server_list: list of servers URLs
67        @keyword link_template: template to build particular link
68        @keyword link_template_no_anchor: template to build particular link
69                                          without anchor
70        """
71        super(TrustLinkClient, self).__init__(user, **kw)
72        if is_plugin_specifier(db_driver):
73            db_driver = load_plugin('linkexchange.multihash_drivers', db_driver)
74        self.db_driver = db_driver
75        for param in ('server_list', 'link_template', 'link_template_no_anchor'):
76            if param in kw:
77                setattr(self, param, kw[param])
78        log.debug("New %s instance:\n%s",
79                self.__class__.__name__,
80                '\n'.join(["    %s: %s" % (x, repr(getattr(self, x)))
81                    for x in (
82                        'user',
83                        'db_driver',
84                        'db_lifetime',
85                        'db_reloadtime',
86                        'socket_timeout',
87                        'force_show_code',
88                        'no_query_string',
89                        'server_list',
90                        'server_charset',
91                        'user_agent')]))
92
93    def load_links_data(self, request):
94        return self.load_data(self.db_driver, self.server_list,
95                self.server_format, request)
96
97    def refresh_db(self, request):
98        self.refresh_data(self.db_driver, self.server_list,
99                self.server_format, request)
100
101    def parse_param(self, name, value):
102        if name == '__trustlink_robots__':
103            if type(value) == dict:
104                value = value.values()
105        return super(TrustLinkClient, self).parse_param(name, value)
106
107    def get_links_for_page(self, data, request, uri):
108        def link_to_html(link):
109            if type(link) != dict:
110                log.error("Link must be a dictionary object: %s", repr(link))
111                return ''
112            if 'text' not in link or 'url' not in link:
113                log.error("Invalid link dictionary: %s", repr(link))
114                return ''
115            tpl_vars = link.copy()
116            tpl_vars['href'] = link.get('punicode_url', '') or link['url']
117            tpl_vars['host'] = urlparse.urlsplit(
118                    link['url'])[1].split(':')[0].lower()
119            if tpl_vars['host'].startswith('www.'):
120                tpl_vars['host'] = tpl_vars['host'][len('www.'):]
121            if link.get('anchor', ''):
122                return self.link_template % tpl_vars
123            return self.link_template_no_anchor % tpl_vars
124
125        if self.is_test(data, request):
126            try:
127                test_link = data['__test_tl_link__']
128            except KeyError:
129                links = []
130            else:
131                links = [test_link] * 4
132        else:
133            links = data.get(uri, [])
134        return map(link_to_html, links)
135
136    def get_links_new_page(self, data, request):
137        if self.is_check_code_visible(data, request):
138            if (data.get('__trustlink_start__', '') +
139                    data.get('__trustlink_end__', '')):
140                return ['']
141        return []
142
143    def get_delimiter(self, data, request):
144        return data.get('__trustlink_delimiter__', '')
145
146    def is_bot(self, data, request):
147        bot_ips = data.get('__trustlink_robots__', [])
148        return request.remote_addr and request.remote_addr in bot_ips
149
150    def is_test(self, data, request):
151        return request.meta.get('HTTP_TRUSTLINK', '') == self.user
152
153    def transform_code(self, data, request, code):
154        if self.is_check_code_visible(data, request):
155            start = data.get('__trustlink_start__', '')
156            end = data.get('__trustlink_end__', '')
157        else:
158            start = end = u''
159        return start + code + end
160
161class TrustLinkTestServer(SapeLikeTestServer):
162    """
163    Test server to test TrustLink client.
164    """
165    data = {
166        '/': [
167            dict(url="http://example1.com", anchor="anchor 1", text="text 1"),
168            dict(url="http://example2.com", anchor="anchor 2", text="text 2"),
169            ],
170        '/path/1': [
171            dict(url="http://example1.com", anchor="anchor 1", text="text 1"),
172            dict(url="http://example2.com", anchor="anchor 2", text="text 2"),
173            dict(url="http://example3.com", anchor="anchor 3", text="text 3"),
174            dict(url="http://example4.com", anchor="anchor 4", text="text 4"),
175            ],
176        '__trustlink_start__' : '<!--12345-->',
177        '__trustlink_end__' : '<!--12345-->',
178        '__trustlink_delimiter__' : '. ',
179        '__trustlink_robots__' : ['123.45.67.89'],
180        }
181    server_format = 'php'
182
183if __name__ == "__main__":
184    import doctest
185    doctest.testmod()
Note: See TracBrowser for help on using the repository browser.