source: products/quintagroup.plonegooglesitemaps/branches/sitemap_date/quintagroup/plonegooglesitemaps/utils.py @ 3565

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

fixed ping to google (timeout was set)

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1from urllib import quote as urlquote
2from DateTime import DateTime
3import logging
4import urllib2
5import socket
6
7from Globals import DevelopmentMode
8#from OFS.ObjectManager import BadRequestException
9from Products.CMFCore.utils import getToolByName
10
11from quintagroup.plonegooglesitemaps import config
12
13
14logger = logging.getLogger(__name__)
15
16
17# BBB: 'timeout' was added to urlopen in python2.6.
18# Method dedicated to compatibility with python2.4 and python2.5
19def urlopen(request, timeout=5.0, data=None, marker=[]):
20    global_timeout = marker
21    try:
22        try:
23            urllib2.urlopen(request, data, timeout=timeout)
24        except TypeError, e:
25            logger.info('TypeError: %s. You use python < 2.6. '
26                        'Ugly socket.setdefaulttimeout hack applied '
27                        'to avoid it upgrade your Plone.' % e)
28            # socket.getdefaulttimeout() -> None.
29            # It indicates that new socket objects have no timeout.
30            global_timeout = socket.getdefaulttimeout()
31            socket.setdefaulttimeout(timeout)
32            urllib2.urlopen(request)
33    finally:
34        if global_timeout is not marker:
35            socket.setdefaulttimeout(global_timeout)
36
37
38def ping_google(url, sitemap_id):
39    """Ping sitemap to Google"""
40
41    resurl = url + "/" + sitemap_id
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.