source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/utils.py @ 3568

Last change on this file since 3568 was 3568, checked in by potar, 7 years ago

fixed url for sitemap which is not in a site root; set absolute path for sitemap instead of id

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1from urllib import quote as urlquote
2from DateTime import DateTime
3from urlparse import urljoin
4import logging
5import urllib2
6import socket
7
8from Globals import DevelopmentMode
9#from OFS.ObjectManager import BadRequestException
10from Products.CMFCore.utils import getToolByName
11
12from quintagroup.plonegooglesitemaps import config
13
14
15logger = logging.getLogger(__name__)
16
17
18# BBB: 'timeout' was added to urlopen in python2.6.
19# Method dedicated to compatibility with python2.4 and python2.5
20def urlopen(request, timeout=5.0, data=None, marker=[]):
21    global_timeout = marker
22    try:
23        try:
24            urllib2.urlopen(request, data, timeout=timeout)
25        except TypeError, e:
26            logger.info('TypeError: %s. You use python < 2.6. '
27                        'Ugly socket.setdefaulttimeout hack applied '
28                        'to avoid it upgrade your Plone.' % e)
29            # socket.getdefaulttimeout() -> None.
30            # It indicates that new socket objects have no timeout.
31            global_timeout = socket.getdefaulttimeout()
32            socket.setdefaulttimeout(timeout)
33            urllib2.urlopen(request)
34    finally:
35        if global_timeout is not marker:
36            socket.setdefaulttimeout(global_timeout)
37
38
39def ping_google(plone_home, sitemap_relative_path):
40    """Ping sitemap to Google"""
41    resurl = urljoin(plone_home, sitemap_relative_path)
42
43    if DevelopmentMode or config.testing:
44        #prevent pinging in debug or testing mode
45        print "Pinged %s sitemap to Google" % resurl
46        return 0
47
48    request = 'http://www.google.com/webmasters/tools/ping?sitemap=' +\
49              urlquote(resurl)
50    try:
51        # BBB: Change urlopen -> socket.urlopen when
52        # compatibility with python2.4 is not important
53        g = urlopen(request)
54    except urllib2.URLError, e:
55        if hasattr(e, 'reason'):
56            logger.error('We failed to reach a server. '
57                         'Request: %s. '
58                         'Reason: %s' % (request, e.reason))
59        elif hasattr(e, 'code'):
60            logger.error('The server couldn\'t fulfill the request. '
61                         'Request: %s '
62                         'Error code: %s. ' % (request, e.code))
63    else:
64        # Reading single byte should be enough for server pinged
65        # to get our request, process it and send some response.
66        g.read(1)
67        g.close()
68
69    return 0
70
71
72def getDefaultPage(obj):
73    """ Method gets default page for object (folderish) """
74    plone_tool = getToolByName(obj, 'plone_utils')
75    return plone_tool.getDefaultPage(obj)
76
77
78def isDefaultPage(obj):
79    """ If object is default page then return True"""
80    plone_tool = getToolByName(obj, 'plone_utils')
81    return plone_tool.isDefaultPage(obj)
82
83
84def dateTime(obj):
85    """ Method gets modification date """
86    return DateTime(obj.ModificationDate())
Note: See TracBrowser for help on using the repository browser.