/gdata/webmastertools/service.py
Python | 516 lines | 387 code | 33 blank | 96 comment | 38 complexity | 8fac78df7ce752ecf19dcce04a3e941a MD5 | raw file
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"""GWebmasterToolsService extends the GDataService to streamline 18Google Webmaster Tools operations. 19 20 GWebmasterToolsService: Provides methods to query feeds and manipulate items. 21 Extends GDataService. 22""" 23 24__author__ = 'livibetter (Yu-Jie Lin)' 25 26import urllib 27import gdata 28import atom.service 29import gdata.service 30import gdata.webmastertools as webmastertools 31import atom 32 33 34FEED_BASE = 'https://www.google.com/webmasters/tools/feeds/' 35SITES_FEED = FEED_BASE + 'sites/' 36SITE_TEMPLATE = SITES_FEED + '%s' 37SITEMAPS_FEED_TEMPLATE = FEED_BASE + '%(site_id)s/sitemaps/' 38SITEMAP_TEMPLATE = SITEMAPS_FEED_TEMPLATE + '%(sitemap_id)s' 39 40 41class Error(Exception): 42 pass 43 44 45class RequestError(Error): 46 pass 47 48 49class GWebmasterToolsService(gdata.service.GDataService): 50 """Client for the Google Webmaster Tools service.""" 51 52 def __init__(self, email=None, password=None, source=None, 53 server='www.google.com', **kwargs): 54 """Creates a client for the Google Webmaster Tools service. 55 56 Args: 57 email: string (optional) The user's email address, used for 58 authentication. 59 password: string (optional) The user's password. 60 source: string (optional) The name of the user's application. 61 server: string (optional) The name of the server to which a connection 62 will be opened. Default value: 'www.google.com'. 63 **kwargs: The other parameters to pass to gdata.service.GDataService 64 constructor. 65 """ 66 gdata.service.GDataService.__init__( 67 self, email=email, password=password, service='sitemaps', source=source, 68 server=server, **kwargs) 69 70 def GetSitesFeed(self, uri=SITES_FEED, 71 converter=webmastertools.SitesFeedFromString): 72 """Gets sites feed. 73 74 Args: 75 uri: str (optional) URI to retrieve sites feed. 76 converter: func (optional) Function which is executed on the server's 77 response before it is returned. Usually this is a function like 78 SitesFeedFromString which will parse the response and turn it into 79 an object. 80 81 Returns: 82 If converter is defined, the results of running converter on the server's 83 response. Otherwise, it will be a SitesFeed object. 84 """ 85 return self.Get(uri, converter=converter) 86 87 def AddSite(self, site_uri, uri=SITES_FEED, 88 url_params=None, escape_params=True, converter=None): 89 """Adds a site to Google Webmaster Tools. 90 91 Args: 92 site_uri: str URI of which site to add. 93 uri: str (optional) URI to add a site. 94 url_params: dict (optional) Additional URL parameters to be included 95 in the insertion request. 96 escape_params: boolean (optional) If true, the url_parameters will be 97 escaped before they are included in the request. 98 converter: func (optional) Function which is executed on the server's 99 response before it is returned. Usually this is a function like 100 SitesEntryFromString which will parse the response and turn it into 101 an object. 102 103 Returns: 104 If converter is defined, the results of running converter on the server's 105 response. Otherwise, it will be a SitesEntry object. 106 """ 107 108 site_entry = webmastertools.SitesEntry() 109 site_entry.content = atom.Content(src=site_uri) 110 response = self.Post(site_entry, uri, 111 url_params=url_params, 112 escape_params=escape_params, converter=converter) 113 if not converter and isinstance(response, atom.Entry): 114 return webmastertools.SitesEntryFromString(response.ToString()) 115 return response 116 117 def DeleteSite(self, site_uri, uri=SITE_TEMPLATE, 118 url_params=None, escape_params=True): 119 """Removes a site from Google Webmaster Tools. 120 121 Args: 122 site_uri: str URI of which site to remove. 123 uri: str (optional) A URI template to send DELETE request. 124 Default SITE_TEMPLATE. 125 url_params: dict (optional) Additional URL parameters to be included 126 in the insertion request. 127 escape_params: boolean (optional) If true, the url_parameters will be 128 escaped before they are included in the request. 129 130 Returns: 131 True if the delete succeeded. 132 """ 133 134 return self.Delete( 135 uri % urllib.quote_plus(site_uri), 136 url_params=url_params, escape_params=escape_params) 137 138 def VerifySite(self, site_uri, verification_method, uri=SITE_TEMPLATE, 139 url_params=None, escape_params=True, converter=None): 140 """Requests a verification of a site. 141 142 Args: 143 site_uri: str URI of which site to add sitemap for. 144 verification_method: str The method to verify a site. Valid values are 145 'htmlpage', and 'metatag'. 146 uri: str (optional) URI template to update a site. 147 Default SITE_TEMPLATE. 148 url_params: dict (optional) Additional URL parameters to be included 149 in the insertion request. 150 escape_params: boolean (optional) If true, the url_parameters will be 151 escaped before they are included in the request. 152 converter: func (optional) Function which is executed on the server's 153 response before it is returned. Usually this is a function like 154 SitemapsEntryFromString which will parse the response and turn it into 155 an object. 156 157 Returns: 158 If converter is defined, the results of running converter on the server's 159 response. Otherwise, it will be a SitesEntry object. 160 """ 161 162 site_entry = webmastertools.SitesEntry( 163 atom_id=atom.Id(text=site_uri), 164 category=atom.Category( 165 scheme='http://schemas.google.com/g/2005#kind', 166 term='http://schemas.google.com/webmasters/tools/2007#sites-info'), 167 verification_method=webmastertools.VerificationMethod( 168 type=verification_method, in_use='true') 169 ) 170 response = self.Put( 171 site_entry, 172 uri % urllib.quote_plus(site_uri), 173 url_params=url_params, 174 escape_params=escape_params, converter=converter) 175 if not converter and isinstance(response, atom.Entry): 176 return webmastertools.SitesEntryFromString(response.ToString()) 177 return response 178 179 180 def UpdateGeoLocation(self, site_uri, geolocation, uri=SITE_TEMPLATE, 181 url_params=None, escape_params=True, converter=None): 182 """Updates geolocation setting of a site. 183 184 Args: 185 site_uri: str URI of which site to add sitemap for. 186 geolocation: str The geographic location. Valid values are listed in 187 http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 188 uri: str (optional) URI template to update a site. 189 Default SITE_TEMPLATE. 190 url_params: dict (optional) Additional URL parameters to be included 191 in the insertion request. 192 escape_params: boolean (optional) If true, the url_parameters will be 193 escaped before they are included in the request. 194 converter: func (optional) Function which is executed on the server's 195 response before it is returned. Usually this is a function like 196 SitemapsEntryFromString which will parse the response and turn it into 197 an object. 198 199 Returns: 200 If converter is defined, the results of running converter on the server's 201 response. Otherwise, it will be a SitesEntry object. 202 """ 203 204 site_entry = webmastertools.SitesEntry( 205 atom_id=atom.Id(text=site_uri), 206 category=atom.Category( 207 scheme='http://schemas.google.com/g/2005#kind', 208 term='http://schemas.google.com/webmasters/tools/2007#sites-info'), 209 geolocation=webmastertools.GeoLocation(text=geolocation) 210 ) 211 response = self.Put( 212 site_entry, 213 uri % urllib.quote_plus(site_uri), 214 url_params=url_params, 215 escape_params=escape_params, converter=converter) 216 if not converter and isinstance(response, atom.Entry): 217 return webmastertools.SitesEntryFromString(response.ToString()) 218 return response 219 220 def UpdateCrawlRate(self, site_uri, crawl_rate, uri=SITE_TEMPLATE, 221 url_params=None, escape_params=True, converter=None): 222 """Updates crawl rate setting of a site. 223 224 Args: 225 site_uri: str URI of which site to add sitemap for. 226 crawl_rate: str The crawl rate for a site. Valid values are 'slower', 227 'normal', and 'faster'. 228 uri: str (optional) URI template to update a site. 229 Default SITE_TEMPLATE. 230 url_params: dict (optional) Additional URL parameters to be included 231 in the insertion request. 232 escape_params: boolean (optional) If true, the url_parameters will be 233 escaped before they are included in the request. 234 converter: func (optional) Function which is executed on the server's 235 response before it is returned. Usually this is a function like 236 SitemapsEntryFromString which will parse the response and turn it into 237 an object. 238 239 Returns: 240 If converter is defined, the results of running converter on the server's 241 response. Otherwise, it will be a SitesEntry object. 242 """ 243 244 site_entry = webmastertools.SitesEntry( 245 atom_id=atom.Id(text=site_uri), 246 category=atom.Category( 247 scheme='http://schemas.google.com/g/2005#kind', 248 term='http://schemas.google.com/webmasters/tools/2007#sites-info'), 249 crawl_rate=webmastertools.CrawlRate(text=crawl_rate) 250 ) 251 response = self.Put( 252 site_entry, 253 uri % urllib.quote_plus(site_uri), 254 url_params=url_params, 255 escape_params=escape_params, converter=converter) 256 if not converter and isinstance(response, atom.Entry): 257 return webmastertools.SitesEntryFromString(response.ToString()) 258 return response 259 260 def UpdatePreferredDomain(self, site_uri, preferred_domain, uri=SITE_TEMPLATE, 261 url_params=None, escape_params=True, converter=None): 262 """Updates preferred domain setting of a site. 263 264 Note that if using 'preferwww', will also need www.example.com in account to 265 take effect. 266 267 Args: 268 site_uri: str URI of which site to add sitemap for. 269 preferred_domain: str The preferred domain for a site. Valid values are 'none', 270 'preferwww', and 'prefernowww'. 271 uri: str (optional) URI template to update a site. 272 Default SITE_TEMPLATE. 273 url_params: dict (optional) Additional URL parameters to be included 274 in the insertion request. 275 escape_params: boolean (optional) If true, the url_parameters will be 276 escaped before they are included in the request. 277 converter: func (optional) Function which is executed on the server's 278 response before it is returned. Usually this is a function like 279 SitemapsEntryFromString which will parse the response and turn it into 280 an object. 281 282 Returns: 283 If converter is defined, the results of running converter on the server's 284 response. Otherwise, it will be a SitesEntry object. 285 """ 286 287 site_entry = webmastertools.SitesEntry( 288 atom_id=atom.Id(text=site_uri), 289 category=atom.Category( 290 scheme='http://schemas.google.com/g/2005#kind', 291 term='http://schemas.google.com/webmasters/tools/2007#sites-info'), 292 preferred_domain=webmastertools.PreferredDomain(text=preferred_domain) 293 ) 294 response = self.Put( 295 site_entry, 296 uri % urllib.quote_plus(site_uri), 297 url_params=url_params, 298 escape_params=escape_params, converter=converter) 299 if not converter and isinstance(response, atom.Entry): 300 return webmastertools.SitesEntryFromString(response.ToString()) 301 return response 302 303 def UpdateEnhancedImageSearch(self, site_uri, enhanced_image_search, 304 uri=SITE_TEMPLATE, url_params=None, escape_params=True, converter=None): 305 """Updates enhanced image search setting of a site. 306 307 Args: 308 site_uri: str URI of which site to add sitemap for. 309 enhanced_image_search: str The enhanced image search setting for a site. 310 Valid values are 'true', and 'false'. 311 uri: str (optional) URI template to update a site. 312 Default SITE_TEMPLATE. 313 url_params: dict (optional) Additional URL parameters to be included 314 in the insertion request. 315 escape_params: boolean (optional) If true, the url_parameters will be 316 escaped before they are included in the request. 317 converter: func (optional) Function which is executed on the server's 318 response before it is returned. Usually this is a function like 319 SitemapsEntryFromString which will parse the response and turn it into 320 an object. 321 322 Returns: 323 If converter is defined, the results of running converter on the server's 324 response. Otherwise, it will be a SitesEntry object. 325 """ 326 327 site_entry = webmastertools.SitesEntry( 328 atom_id=atom.Id(text=site_uri), 329 category=atom.Category( 330 scheme='http://schemas.google.com/g/2005#kind', 331 term='http://schemas.google.com/webmasters/tools/2007#sites-info'), 332 enhanced_image_search=webmastertools.EnhancedImageSearch( 333 text=enhanced_image_search) 334 ) 335 response = self.Put( 336 site_entry, 337 uri % urllib.quote_plus(site_uri), 338 url_params=url_params, 339 escape_params=escape_params, converter=converter) 340 if not converter and isinstance(response, atom.Entry): 341 return webmastertools.SitesEntryFromString(response.ToString()) 342 return response 343 344 def GetSitemapsFeed(self, site_uri, uri=SITEMAPS_FEED_TEMPLATE, 345 converter=webmastertools.SitemapsFeedFromString): 346 """Gets sitemaps feed of a site. 347 348 Args: 349 site_uri: str (optional) URI of which site to retrieve its sitemaps feed. 350 uri: str (optional) URI to retrieve sites feed. 351 converter: func (optional) Function which is executed on the server's 352 response before it is returned. Usually this is a function like 353 SitemapsFeedFromString which will parse the response and turn it into 354 an object. 355 356 Returns: 357 If converter is defined, the results of running converter on the server's 358 response. Otherwise, it will be a SitemapsFeed object. 359 """ 360 return self.Get(uri % {'site_id': urllib.quote_plus(site_uri)}, 361 converter=converter) 362 363 def AddSitemap(self, site_uri, sitemap_uri, sitemap_type='WEB', 364 uri=SITEMAPS_FEED_TEMPLATE, 365 url_params=None, escape_params=True, converter=None): 366 """Adds a regular sitemap to a site. 367 368 Args: 369 site_uri: str URI of which site to add sitemap for. 370 sitemap_uri: str URI of sitemap to add to a site. 371 sitemap_type: str Type of added sitemap. Valid types: WEB, VIDEO, or CODE. 372 uri: str (optional) URI template to add a sitemap. 373 Default SITEMAP_FEED_TEMPLATE. 374 url_params: dict (optional) Additional URL parameters to be included 375 in the insertion request. 376 escape_params: boolean (optional) If true, the url_parameters will be 377 escaped before they are included in the request. 378 converter: func (optional) Function which is executed on the server's 379 response before it is returned. Usually this is a function like 380 SitemapsEntryFromString which will parse the response and turn it into 381 an object. 382 383 Returns: 384 If converter is defined, the results of running converter on the server's 385 response. Otherwise, it will be a SitemapsEntry object. 386 """ 387 388 sitemap_entry = webmastertools.SitemapsEntry( 389 atom_id=atom.Id(text=sitemap_uri), 390 category=atom.Category( 391 scheme='http://schemas.google.com/g/2005#kind', 392 term='http://schemas.google.com/webmasters/tools/2007#sitemap-regular'), 393 sitemap_type=webmastertools.SitemapType(text=sitemap_type)) 394 response = self.Post( 395 sitemap_entry, 396 uri % {'site_id': urllib.quote_plus(site_uri)}, 397 url_params=url_params, 398 escape_params=escape_params, converter=converter) 399 if not converter and isinstance(response, atom.Entry): 400 return webmastertools.SitemapsEntryFromString(response.ToString()) 401 return response 402 403 def AddMobileSitemap(self, site_uri, sitemap_uri, 404 sitemap_mobile_markup_language='XHTML', uri=SITEMAPS_FEED_TEMPLATE, 405 url_params=None, escape_params=True, converter=None): 406 """Adds a mobile sitemap to a site. 407 408 Args: 409 site_uri: str URI of which site to add sitemap for. 410 sitemap_uri: str URI of sitemap to add to a site. 411 sitemap_mobile_markup_language: str Format of added sitemap. Valid types: 412 XHTML, WML, or cHTML. 413 uri: str (optional) URI template to add a sitemap. 414 Default SITEMAP_FEED_TEMPLATE. 415 url_params: dict (optional) Additional URL parameters to be included 416 in the insertion request. 417 escape_params: boolean (optional) If true, the url_parameters will be 418 escaped before they are included in the request. 419 converter: func (optional) Function which is executed on the server's 420 response before it is returned. Usually this is a function like 421 SitemapsEntryFromString which will parse the response and turn it into 422 an object. 423 424 Returns: 425 If converter is defined, the results of running converter on the server's 426 response. Otherwise, it will be a SitemapsEntry object. 427 """ 428 # FIXME 429 sitemap_entry = webmastertools.SitemapsEntry( 430 atom_id=atom.Id(text=sitemap_uri), 431 category=atom.Category( 432 scheme='http://schemas.google.com/g/2005#kind', 433 term='http://schemas.google.com/webmasters/tools/2007#sitemap-mobile'), 434 sitemap_mobile_markup_language=\ 435 webmastertools.SitemapMobileMarkupLanguage( 436 text=sitemap_mobile_markup_language)) 437 print sitemap_entry 438 response = self.Post( 439 sitemap_entry, 440 uri % {'site_id': urllib.quote_plus(site_uri)}, 441 url_params=url_params, 442 escape_params=escape_params, converter=converter) 443 if not converter and isinstance(response, atom.Entry): 444 return webmastertools.SitemapsEntryFromString(response.ToString()) 445 return response 446 447 def AddNewsSitemap(self, site_uri, sitemap_uri, 448 sitemap_news_publication_label, uri=SITEMAPS_FEED_TEMPLATE, 449 url_params=None, escape_params=True, converter=None): 450 """Adds a news sitemap to a site. 451 452 Args: 453 site_uri: str URI of which site to add sitemap for. 454 sitemap_uri: str URI of sitemap to add to a site. 455 sitemap_news_publication_label: str, list of str Publication Labels for 456 sitemap. 457 uri: str (optional) URI template to add a sitemap. 458 Default SITEMAP_FEED_TEMPLATE. 459 url_params: dict (optional) Additional URL parameters to be included 460 in the insertion request. 461 escape_params: boolean (optional) If true, the url_parameters will be 462 escaped before they are included in the request. 463 converter: func (optional) Function which is executed on the server's 464 response before it is returned. Usually this is a function like 465 SitemapsEntryFromString which will parse the response and turn it into 466 an object. 467 468 Returns: 469 If converter is defined, the results of running converter on the server's 470 response. Otherwise, it will be a SitemapsEntry object. 471 """ 472 473 sitemap_entry = webmastertools.SitemapsEntry( 474 atom_id=atom.Id(text=sitemap_uri), 475 category=atom.Category( 476 scheme='http://schemas.google.com/g/2005#kind', 477 term='http://schemas.google.com/webmasters/tools/2007#sitemap-news'), 478 sitemap_news_publication_label=[], 479 ) 480 if isinstance(sitemap_news_publication_label, str): 481 sitemap_news_publication_label = [sitemap_news_publication_label] 482 for label in sitemap_news_publication_label: 483 sitemap_entry.sitemap_news_publication_label.append( 484 webmastertools.SitemapNewsPublicationLabel(text=label)) 485 print sitemap_entry 486 response = self.Post( 487 sitemap_entry, 488 uri % {'site_id': urllib.quote_plus(site_uri)}, 489 url_params=url_params, 490 escape_params=escape_params, converter=converter) 491 if not converter and isinstance(response, atom.Entry): 492 return webmastertools.SitemapsEntryFromString(response.ToString()) 493 return response 494 495 def DeleteSitemap(self, site_uri, sitemap_uri, uri=SITEMAP_TEMPLATE, 496 url_params=None, escape_params=True): 497 """Removes a sitemap from a site. 498 499 Args: 500 site_uri: str URI of which site to remove a sitemap from. 501 sitemap_uri: str URI of sitemap to remove from a site. 502 uri: str (optional) A URI template to send DELETE request. 503 Default SITEMAP_TEMPLATE. 504 url_params: dict (optional) Additional URL parameters to be included 505 in the insertion request. 506 escape_params: boolean (optional) If true, the url_parameters will be 507 escaped before they are included in the request. 508 509 Returns: 510 True if the delete succeeded. 511 """ 512 513 return self.Delete( 514 uri % {'site_id': urllib.quote_plus(site_uri), 515 'sitemap_id': urllib.quote_plus(sitemap_uri)}, 516 url_params=url_params, escape_params=escape_params)