home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-gdata / examples / webmastertools / AddDeleteExampleDotCom.py next >
Encoding:
Python Source  |  2008-10-15  |  3.2 KB  |  104 lines

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2008 Yu-Jie Lin
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #      http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17.  
  18. import urllib
  19.  
  20. import gdata.webmastertools.service
  21. import gdata.service
  22. try:
  23.   from xml.etree import ElementTree
  24. except ImportError:
  25.   from elementtree import ElementTree
  26. import atom
  27. import getpass
  28.  
  29.  
  30. username = ''
  31. password = ''
  32.  
  33. username = raw_input('Please enter your username: ')
  34. password = getpass.getpass()
  35.  
  36. client = gdata.webmastertools.service.GWebmasterToolsService(
  37.     email=username, 
  38.     password=password, source='PythonWebmasterToolsSample-1')
  39.  
  40. EXAMPLE_SITE = 'http://www.example.com/'
  41. EXAMPLE_SITEMAP = 'http://www.example.com/sitemap-index.xml'
  42.  
  43.  
  44. def safeElementText(element):
  45.   if hasattr(element, 'text'):
  46.      return element.text
  47.   return ''
  48.  
  49.  
  50. print 'Logging in'
  51. client.ProgrammaticLogin()
  52.  
  53. print
  54. print 'Adding site: %s' % EXAMPLE_SITE
  55. entry = client.AddSite(EXAMPLE_SITE)
  56.  
  57. print
  58. print "%-25s %25s %25s" % ('Site', 'Last Updated', 'Last Crawled')
  59. print '='*80
  60. print "%-25s %25s %25s" % (
  61.   entry.title.text.replace('http://', '')[:25], entry.updated.text[:25],
  62.   safeElementText(entry.crawled)[:25])
  63. print "  Preferred: %-23s Indexed: %5s        GeoLoc: %10s" % (
  64.   safeElementText(entry.preferred_domain)[:30], entry.indexed.text[:5],
  65.   safeElementText(entry.geolocation)[:10])
  66. print "  Crawl rate: %-10s            Verified: %5s" % (
  67.   safeElementText(entry.crawl_rate)[:10], entry.verified.text[:5])
  68.  
  69. # Verifying a site. This sample won't do this since we don't own example.com
  70. #client.VerifySite(EXAMPLE_SITE, 'htmlpage')
  71.  
  72. # The following needs the ownership of the site
  73. #client.UpdateGeoLocation(EXAMPLE_SITE, 'US')
  74. #client.UpdateCrawlRate(EXAMPLE_SITE, 'normal')
  75. #client.UpdatePreferredDomain(EXAMPLE_SITE, 'preferwww')
  76. #client.UpdateEnhancedImageSearch(EXAMPLE_SITE, 'true')
  77.  
  78. print
  79. print 'Adding sitemap: %s' % EXAMPLE_SITEMAP
  80. entry = client.AddSitemap(EXAMPLE_SITE, EXAMPLE_SITEMAP)
  81.  
  82. print entry.title.text.replace('http://', '')[:80]
  83. print "  Last Updated   : %29s              Status: %10s" % (
  84.     entry.updated.text[:29], entry.sitemap_status.text[:10])
  85. print "  Last Downloaded: %29s           URL Count: %10s" % (
  86.     safeElementText(entry.sitemap_last_downloaded)[:29],
  87.     safeElementText(entry.sitemap_url_count)[:10])
  88.  
  89. # Add a mobile sitemap
  90. #entry = client.AddMobileSitemap(EXAMPLE_SITE, 'http://.../sitemap-mobile-example.xml', 'XHTML')
  91.  
  92. # Add a news sitemap, your site must be included in Google News.
  93. # See also http://google.com/support/webmasters/bin/answer.py?answer=42738
  94. #entry = client.AddNewsSitemap(EXAMPLE_SITE, 'http://.../sitemap-news-example.xml', 'Label')
  95.  
  96. print
  97. print 'Deleting sitemap: %s' % EXAMPLE_SITEMAP
  98. client.DeleteSitemap(EXAMPLE_SITE, EXAMPLE_SITEMAP)
  99.  
  100. print
  101. print 'Deleting site: %s' % EXAMPLE_SITE
  102. client.DeleteSite(EXAMPLE_SITE)
  103. print
  104.