/gdata/webmastertools/__init__.py

http://radioappz.googlecode.com/ · Python · 544 lines · 392 code · 125 blank · 27 comment · 33 complexity · ae11c3ae38d7f927ff0dca886b5ef190 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. """Contains extensions to Atom objects used with Google Webmaster Tools."""
  17. __author__ = 'livibetter (Yu-Jie Lin)'
  18. try:
  19. from xml.etree import cElementTree as ElementTree
  20. except ImportError:
  21. try:
  22. import cElementTree as ElementTree
  23. except ImportError:
  24. try:
  25. from xml.etree import ElementTree
  26. except ImportError:
  27. from elementtree import ElementTree
  28. import atom
  29. import gdata
  30. # XML namespaces which are often used in Google Webmaster Tools entities.
  31. GWEBMASTERTOOLS_NAMESPACE = 'http://schemas.google.com/webmasters/tools/2007'
  32. GWEBMASTERTOOLS_TEMPLATE = '{http://schemas.google.com/webmasters/tools/2007}%s'
  33. class Indexed(atom.AtomBase):
  34. _tag = 'indexed'
  35. _namespace = GWEBMASTERTOOLS_NAMESPACE
  36. def IndexedFromString(xml_string):
  37. return atom.CreateClassFromXMLString(Indexed, xml_string)
  38. class Crawled(atom.Date):
  39. _tag = 'crawled'
  40. _namespace = GWEBMASTERTOOLS_NAMESPACE
  41. def CrawledFromString(xml_string):
  42. return atom.CreateClassFromXMLString(Crawled, xml_string)
  43. class GeoLocation(atom.AtomBase):
  44. _tag = 'geolocation'
  45. _namespace = GWEBMASTERTOOLS_NAMESPACE
  46. def GeoLocationFromString(xml_string):
  47. return atom.CreateClassFromXMLString(GeoLocation, xml_string)
  48. class PreferredDomain(atom.AtomBase):
  49. _tag = 'preferred-domain'
  50. _namespace = GWEBMASTERTOOLS_NAMESPACE
  51. def PreferredDomainFromString(xml_string):
  52. return atom.CreateClassFromXMLString(PreferredDomain, xml_string)
  53. class CrawlRate(atom.AtomBase):
  54. _tag = 'crawl-rate'
  55. _namespace = GWEBMASTERTOOLS_NAMESPACE
  56. def CrawlRateFromString(xml_string):
  57. return atom.CreateClassFromXMLString(CrawlRate, xml_string)
  58. class EnhancedImageSearch(atom.AtomBase):
  59. _tag = 'enhanced-image-search'
  60. _namespace = GWEBMASTERTOOLS_NAMESPACE
  61. def EnhancedImageSearchFromString(xml_string):
  62. return atom.CreateClassFromXMLString(EnhancedImageSearch, xml_string)
  63. class Verified(atom.AtomBase):
  64. _tag = 'verified'
  65. _namespace = GWEBMASTERTOOLS_NAMESPACE
  66. def VerifiedFromString(xml_string):
  67. return atom.CreateClassFromXMLString(Verified, xml_string)
  68. class VerificationMethodMeta(atom.AtomBase):
  69. _tag = 'meta'
  70. _namespace = atom.ATOM_NAMESPACE
  71. _children = atom.AtomBase._children.copy()
  72. _attributes = atom.AtomBase._attributes.copy()
  73. _attributes['name'] = 'name'
  74. _attributes['content'] = 'content'
  75. def __init__(self, text=None, name=None, content=None,
  76. extension_elements=None, extension_attributes=None):
  77. self.text = text
  78. self.name = name
  79. self.content = content
  80. self.extension_elements = extension_elements or []
  81. self.extension_attributes = extension_attributes or {}
  82. def VerificationMethodMetaFromString(xml_string):
  83. return atom.CreateClassFromXMLString(VerificationMethodMeta, xml_string)
  84. class VerificationMethod(atom.AtomBase):
  85. _tag = 'verification-method'
  86. _namespace = GWEBMASTERTOOLS_NAMESPACE
  87. _children = atom.Text._children.copy()
  88. _attributes = atom.Text._attributes.copy()
  89. _children['{%s}meta' % atom.ATOM_NAMESPACE] = (
  90. 'meta', VerificationMethodMeta)
  91. _attributes['in-use'] = 'in_use'
  92. _attributes['type'] = 'type'
  93. def __init__(self, text=None, in_use=None, meta=None, type=None,
  94. extension_elements=None, extension_attributes=None):
  95. self.text = text
  96. self.in_use = in_use
  97. self.meta = meta
  98. self.type = type
  99. self.extension_elements = extension_elements or []
  100. self.extension_attributes = extension_attributes or {}
  101. def VerificationMethodFromString(xml_string):
  102. return atom.CreateClassFromXMLString(VerificationMethod, xml_string)
  103. class MarkupLanguage(atom.AtomBase):
  104. _tag = 'markup-language'
  105. _namespace = GWEBMASTERTOOLS_NAMESPACE
  106. def MarkupLanguageFromString(xml_string):
  107. return atom.CreateClassFromXMLString(MarkupLanguage, xml_string)
  108. class SitemapMobile(atom.AtomBase):
  109. _tag = 'sitemap-mobile'
  110. _namespace = GWEBMASTERTOOLS_NAMESPACE
  111. _children = atom.AtomBase._children.copy()
  112. _attributes = atom.AtomBase._attributes.copy()
  113. _children['{%s}markup-language' % GWEBMASTERTOOLS_NAMESPACE] = (
  114. 'markup_language', [MarkupLanguage])
  115. def __init__(self, markup_language=None,
  116. extension_elements=None, extension_attributes=None, text=None):
  117. self.markup_language = markup_language or []
  118. self.text = text
  119. self.extension_elements = extension_elements or []
  120. self.extension_attributes = extension_attributes or {}
  121. def SitemapMobileFromString(xml_string):
  122. return atom.CreateClassFromXMLString(SitemapMobile, xml_string)
  123. class SitemapMobileMarkupLanguage(atom.AtomBase):
  124. _tag = 'sitemap-mobile-markup-language'
  125. _namespace = GWEBMASTERTOOLS_NAMESPACE
  126. def SitemapMobileMarkupLanguageFromString(xml_string):
  127. return atom.CreateClassFromXMLString(SitemapMobileMarkupLanguage, xml_string)
  128. class PublicationLabel(atom.AtomBase):
  129. _tag = 'publication-label'
  130. _namespace = GWEBMASTERTOOLS_NAMESPACE
  131. def PublicationLabelFromString(xml_string):
  132. return atom.CreateClassFromXMLString(PublicationLabel, xml_string)
  133. class SitemapNews(atom.AtomBase):
  134. _tag = 'sitemap-news'
  135. _namespace = GWEBMASTERTOOLS_NAMESPACE
  136. _children = atom.AtomBase._children.copy()
  137. _attributes = atom.AtomBase._attributes.copy()
  138. _children['{%s}publication-label' % GWEBMASTERTOOLS_NAMESPACE] = (
  139. 'publication_label', [PublicationLabel])
  140. def __init__(self, publication_label=None,
  141. extension_elements=None, extension_attributes=None, text=None):
  142. self.publication_label = publication_label or []
  143. self.text = text
  144. self.extension_elements = extension_elements or []
  145. self.extension_attributes = extension_attributes or {}
  146. def SitemapNewsFromString(xml_string):
  147. return atom.CreateClassFromXMLString(SitemapNews, xml_string)
  148. class SitemapNewsPublicationLabel(atom.AtomBase):
  149. _tag = 'sitemap-news-publication-label'
  150. _namespace = GWEBMASTERTOOLS_NAMESPACE
  151. def SitemapNewsPublicationLabelFromString(xml_string):
  152. return atom.CreateClassFromXMLString(SitemapNewsPublicationLabel, xml_string)
  153. class SitemapLastDownloaded(atom.Date):
  154. _tag = 'sitemap-last-downloaded'
  155. _namespace = GWEBMASTERTOOLS_NAMESPACE
  156. def SitemapLastDownloadedFromString(xml_string):
  157. return atom.CreateClassFromXMLString(SitemapLastDownloaded, xml_string)
  158. class SitemapType(atom.AtomBase):
  159. _tag = 'sitemap-type'
  160. _namespace = GWEBMASTERTOOLS_NAMESPACE
  161. def SitemapTypeFromString(xml_string):
  162. return atom.CreateClassFromXMLString(SitemapType, xml_string)
  163. class SitemapStatus(atom.AtomBase):
  164. _tag = 'sitemap-status'
  165. _namespace = GWEBMASTERTOOLS_NAMESPACE
  166. def SitemapStatusFromString(xml_string):
  167. return atom.CreateClassFromXMLString(SitemapStatus, xml_string)
  168. class SitemapUrlCount(atom.AtomBase):
  169. _tag = 'sitemap-url-count'
  170. _namespace = GWEBMASTERTOOLS_NAMESPACE
  171. def SitemapUrlCountFromString(xml_string):
  172. return atom.CreateClassFromXMLString(SitemapUrlCount, xml_string)
  173. class LinkFinder(atom.LinkFinder):
  174. """An "interface" providing methods to find link elements
  175. SitesEntry elements often contain multiple links which differ in the rel
  176. attribute or content type. Often, developers are interested in a specific
  177. type of link so this class provides methods to find specific classes of links.
  178. This class is used as a mixin in SitesEntry.
  179. """
  180. def GetSelfLink(self):
  181. """Find the first link with rel set to 'self'
  182. Returns:
  183. An atom.Link or none if none of the links had rel equal to 'self'
  184. """
  185. for a_link in self.link:
  186. if a_link.rel == 'self':
  187. return a_link
  188. return None
  189. def GetEditLink(self):
  190. for a_link in self.link:
  191. if a_link.rel == 'edit':
  192. return a_link
  193. return None
  194. def GetPostLink(self):
  195. """Get a link containing the POST target URL.
  196. The POST target URL is used to insert new entries.
  197. Returns:
  198. A link object with a rel matching the POST type.
  199. """
  200. for a_link in self.link:
  201. if a_link.rel == 'http://schemas.google.com/g/2005#post':
  202. return a_link
  203. return None
  204. def GetFeedLink(self):
  205. for a_link in self.link:
  206. if a_link.rel == 'http://schemas.google.com/g/2005#feed':
  207. return a_link
  208. return None
  209. class SitesEntry(atom.Entry, LinkFinder):
  210. """A Google Webmaster Tools meta Entry flavor of an Atom Entry """
  211. _tag = atom.Entry._tag
  212. _namespace = atom.Entry._namespace
  213. _children = atom.Entry._children.copy()
  214. _attributes = atom.Entry._attributes.copy()
  215. _children['{%s}entryLink' % gdata.GDATA_NAMESPACE] = (
  216. 'entry_link', [gdata.EntryLink])
  217. _children['{%s}indexed' % GWEBMASTERTOOLS_NAMESPACE] = ('indexed', Indexed)
  218. _children['{%s}crawled' % GWEBMASTERTOOLS_NAMESPACE] = (
  219. 'crawled', Crawled)
  220. _children['{%s}geolocation' % GWEBMASTERTOOLS_NAMESPACE] = (
  221. 'geolocation', GeoLocation)
  222. _children['{%s}preferred-domain' % GWEBMASTERTOOLS_NAMESPACE] = (
  223. 'preferred_domain', PreferredDomain)
  224. _children['{%s}crawl-rate' % GWEBMASTERTOOLS_NAMESPACE] = (
  225. 'crawl_rate', CrawlRate)
  226. _children['{%s}enhanced-image-search' % GWEBMASTERTOOLS_NAMESPACE] = (
  227. 'enhanced_image_search', EnhancedImageSearch)
  228. _children['{%s}verified' % GWEBMASTERTOOLS_NAMESPACE] = (
  229. 'verified', Verified)
  230. _children['{%s}verification-method' % GWEBMASTERTOOLS_NAMESPACE] = (
  231. 'verification_method', [VerificationMethod])
  232. def __GetId(self):
  233. return self.__id
  234. # This method was created to strip the unwanted whitespace from the id's
  235. # text node.
  236. def __SetId(self, id):
  237. self.__id = id
  238. if id is not None and id.text is not None:
  239. self.__id.text = id.text.strip()
  240. id = property(__GetId, __SetId)
  241. def __init__(self, category=None, content=None,
  242. atom_id=None, link=None, title=None, updated=None,
  243. entry_link=None, indexed=None, crawled=None,
  244. geolocation=None, preferred_domain=None, crawl_rate=None,
  245. enhanced_image_search=None,
  246. verified=None, verification_method=None,
  247. extension_elements=None, extension_attributes=None, text=None):
  248. atom.Entry.__init__(self, category=category,
  249. content=content, atom_id=atom_id, link=link,
  250. title=title, updated=updated, text=text)
  251. self.entry_link = entry_link or []
  252. self.indexed = indexed
  253. self.crawled = crawled
  254. self.geolocation = geolocation
  255. self.preferred_domain = preferred_domain
  256. self.crawl_rate = crawl_rate
  257. self.enhanced_image_search = enhanced_image_search
  258. self.verified = verified
  259. self.verification_method = verification_method or []
  260. def SitesEntryFromString(xml_string):
  261. return atom.CreateClassFromXMLString(SitesEntry, xml_string)
  262. class SitesFeed(atom.Feed, LinkFinder):
  263. """A Google Webmaster Tools meta Sites feed flavor of an Atom Feed"""
  264. _tag = atom.Feed._tag
  265. _namespace = atom.Feed._namespace
  266. _children = atom.Feed._children.copy()
  267. _attributes = atom.Feed._attributes.copy()
  268. _children['{%s}startIndex' % gdata.OPENSEARCH_NAMESPACE] = (
  269. 'start_index', gdata.StartIndex)
  270. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [SitesEntry])
  271. del _children['{%s}generator' % atom.ATOM_NAMESPACE]
  272. del _children['{%s}author' % atom.ATOM_NAMESPACE]
  273. del _children['{%s}contributor' % atom.ATOM_NAMESPACE]
  274. del _children['{%s}logo' % atom.ATOM_NAMESPACE]
  275. del _children['{%s}icon' % atom.ATOM_NAMESPACE]
  276. del _children['{%s}rights' % atom.ATOM_NAMESPACE]
  277. del _children['{%s}subtitle' % atom.ATOM_NAMESPACE]
  278. def __GetId(self):
  279. return self.__id
  280. def __SetId(self, id):
  281. self.__id = id
  282. if id is not None and id.text is not None:
  283. self.__id.text = id.text.strip()
  284. id = property(__GetId, __SetId)
  285. def __init__(self, start_index=None, atom_id=None, title=None, entry=None,
  286. category=None, link=None, updated=None,
  287. extension_elements=None, extension_attributes=None, text=None):
  288. """Constructor for Source
  289. Args:
  290. category: list (optional) A list of Category instances
  291. id: Id (optional) The entry's Id element
  292. link: list (optional) A list of Link instances
  293. title: Title (optional) the entry's title element
  294. updated: Updated (optional) the entry's updated element
  295. entry: list (optional) A list of the Entry instances contained in the
  296. feed.
  297. text: String (optional) The text contents of the element. This is the
  298. contents of the Entry's XML text node.
  299. (Example: <foo>This is the text</foo>)
  300. extension_elements: list (optional) A list of ExtensionElement instances
  301. which are children of this element.
  302. extension_attributes: dict (optional) A dictionary of strings which are
  303. the values for additional XML attributes of this element.
  304. """
  305. self.start_index = start_index
  306. self.category = category or []
  307. self.id = atom_id
  308. self.link = link or []
  309. self.title = title
  310. self.updated = updated
  311. self.entry = entry or []
  312. self.text = text
  313. self.extension_elements = extension_elements or []
  314. self.extension_attributes = extension_attributes or {}
  315. def SitesFeedFromString(xml_string):
  316. return atom.CreateClassFromXMLString(SitesFeed, xml_string)
  317. class SitemapsEntry(atom.Entry, LinkFinder):
  318. """A Google Webmaster Tools meta Sitemaps Entry flavor of an Atom Entry """
  319. _tag = atom.Entry._tag
  320. _namespace = atom.Entry._namespace
  321. _children = atom.Entry._children.copy()
  322. _attributes = atom.Entry._attributes.copy()
  323. _children['{%s}sitemap-type' % GWEBMASTERTOOLS_NAMESPACE] = (
  324. 'sitemap_type', SitemapType)
  325. _children['{%s}sitemap-status' % GWEBMASTERTOOLS_NAMESPACE] = (
  326. 'sitemap_status', SitemapStatus)
  327. _children['{%s}sitemap-last-downloaded' % GWEBMASTERTOOLS_NAMESPACE] = (
  328. 'sitemap_last_downloaded', SitemapLastDownloaded)
  329. _children['{%s}sitemap-url-count' % GWEBMASTERTOOLS_NAMESPACE] = (
  330. 'sitemap_url_count', SitemapUrlCount)
  331. _children['{%s}sitemap-mobile-markup-language' % GWEBMASTERTOOLS_NAMESPACE] \
  332. = ('sitemap_mobile_markup_language', SitemapMobileMarkupLanguage)
  333. _children['{%s}sitemap-news-publication-label' % GWEBMASTERTOOLS_NAMESPACE] \
  334. = ('sitemap_news_publication_label', SitemapNewsPublicationLabel)
  335. def __GetId(self):
  336. return self.__id
  337. # This method was created to strip the unwanted whitespace from the id's
  338. # text node.
  339. def __SetId(self, id):
  340. self.__id = id
  341. if id is not None and id.text is not None:
  342. self.__id.text = id.text.strip()
  343. id = property(__GetId, __SetId)
  344. def __init__(self, category=None, content=None,
  345. atom_id=None, link=None, title=None, updated=None,
  346. sitemap_type=None, sitemap_status=None, sitemap_last_downloaded=None,
  347. sitemap_url_count=None, sitemap_mobile_markup_language=None,
  348. sitemap_news_publication_label=None,
  349. extension_elements=None, extension_attributes=None, text=None):
  350. atom.Entry.__init__(self, category=category,
  351. content=content, atom_id=atom_id, link=link,
  352. title=title, updated=updated, text=text)
  353. self.sitemap_type = sitemap_type
  354. self.sitemap_status = sitemap_status
  355. self.sitemap_last_downloaded = sitemap_last_downloaded
  356. self.sitemap_url_count = sitemap_url_count
  357. self.sitemap_mobile_markup_language = sitemap_mobile_markup_language
  358. self.sitemap_news_publication_label = sitemap_news_publication_label
  359. def SitemapsEntryFromString(xml_string):
  360. return atom.CreateClassFromXMLString(SitemapsEntry, xml_string)
  361. class SitemapsFeed(atom.Feed, LinkFinder):
  362. """A Google Webmaster Tools meta Sitemaps feed flavor of an Atom Feed"""
  363. _tag = atom.Feed._tag
  364. _namespace = atom.Feed._namespace
  365. _children = atom.Feed._children.copy()
  366. _attributes = atom.Feed._attributes.copy()
  367. _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [SitemapsEntry])
  368. _children['{%s}sitemap-mobile' % GWEBMASTERTOOLS_NAMESPACE] = (
  369. 'sitemap_mobile', SitemapMobile)
  370. _children['{%s}sitemap-news' % GWEBMASTERTOOLS_NAMESPACE] = (
  371. 'sitemap_news', SitemapNews)
  372. del _children['{%s}generator' % atom.ATOM_NAMESPACE]
  373. del _children['{%s}author' % atom.ATOM_NAMESPACE]
  374. del _children['{%s}contributor' % atom.ATOM_NAMESPACE]
  375. del _children['{%s}logo' % atom.ATOM_NAMESPACE]
  376. del _children['{%s}icon' % atom.ATOM_NAMESPACE]
  377. del _children['{%s}rights' % atom.ATOM_NAMESPACE]
  378. del _children['{%s}subtitle' % atom.ATOM_NAMESPACE]
  379. def __GetId(self):
  380. return self.__id
  381. def __SetId(self, id):
  382. self.__id = id
  383. if id is not None and id.text is not None:
  384. self.__id.text = id.text.strip()
  385. id = property(__GetId, __SetId)
  386. def __init__(self, category=None, content=None,
  387. atom_id=None, link=None, title=None, updated=None,
  388. entry=None, sitemap_mobile=None, sitemap_news=None,
  389. extension_elements=None, extension_attributes=None, text=None):
  390. self.category = category or []
  391. self.id = atom_id
  392. self.link = link or []
  393. self.title = title
  394. self.updated = updated
  395. self.entry = entry or []
  396. self.text = text
  397. self.sitemap_mobile = sitemap_mobile
  398. self.sitemap_news = sitemap_news
  399. self.extension_elements = extension_elements or []
  400. self.extension_attributes = extension_attributes or {}
  401. def SitemapsFeedFromString(xml_string):
  402. return atom.CreateClassFromXMLString(SitemapsFeed, xml_string)