/gdata/webmastertools/service.py

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