parse.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. # (The MIT License)
  3. #
  4. # Copyright (c) 2014 Kura
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the 'Software'), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. from datetime import datetime
  24. import xml.etree.ElementTree
  25. import requests
  26. from .exceptions import HTTPError
  27. def _get(pypi_server):
  28. """
  29. Query the PyPI RSS feed and return a list
  30. of XML items.
  31. """
  32. response = requests.get(pypi_server)
  33. if response.status_code >= 300:
  34. raise HTTPError(status_code=response.status_code,
  35. reason=response.reason)
  36. if hasattr(response.content, 'decode'):
  37. tree = xml.etree.ElementTree.fromstring(response.content.decode())
  38. else:
  39. tree = xml.etree.ElementTree.fromstring(response.content)
  40. channel = tree.find('channel')
  41. return channel.findall('item')
  42. def newest_packages(
  43. pypi_server="https://pypi.python.org/pypi?%3Aaction=packages_rss"):
  44. """
  45. Constructs a request to the PyPI server and returns a list of
  46. :class:`yarg.parse.Package`.
  47. :param pypi_server: (option) URL to the PyPI server.
  48. >>> import yarg
  49. >>> yarg.newest_packages()
  50. [<Package yarg>, <Package gray>, <Package ragy>]
  51. """
  52. items = _get(pypi_server)
  53. i = []
  54. for item in items:
  55. i_dict = {'name': item[0].text.split()[0],
  56. 'url': item[1].text,
  57. 'description': item[3].text,
  58. 'date': item[4].text}
  59. i.append(Package(i_dict))
  60. return i
  61. def latest_updated_packages(
  62. pypi_server="https://pypi.python.org/pypi?%3Aaction=rss"):
  63. """
  64. Constructs a request to the PyPI server and returns a list of
  65. :class:`yarg.parse.Package`.
  66. :param pypi_server: (option) URL to the PyPI server.
  67. >>> import yarg
  68. >>> yarg.latest_updated_packages()
  69. [<Package yarg>, <Package gray>, <Package ragy>]
  70. """
  71. items = _get(pypi_server)
  72. i = []
  73. for item in items:
  74. name, version = item[0].text.split()
  75. i_dict = {'name': name,
  76. 'version': version,
  77. 'url': item[1].text,
  78. 'description': item[2].text,
  79. 'date': item[3].text}
  80. i.append(Package(i_dict))
  81. return i
  82. class Package(object):
  83. """
  84. A PyPI package generated from the RSS feed information.
  85. :param pypi_dict: A dictionary retrieved from the PyPI server.
  86. """
  87. def __init__(self, pypi_dict):
  88. self._content = pypi_dict
  89. def __repr__(self):
  90. return "<Package {0}>".format(self.name)
  91. @property
  92. def name(self):
  93. """
  94. >>> package = yarg.newest_packages()[0]
  95. >>> package.name
  96. u'yarg'
  97. >>> package = yarg.latest_updated_packages()[0]
  98. >>> package.name
  99. u'yarg'
  100. """
  101. return self._content['name']
  102. @property
  103. def version(self):
  104. """
  105. >>> package = yarg.newest_packages()[0]
  106. >>> package.name
  107. u'yarg'
  108. >>> package = yarg.latest_updated_packages()[0]
  109. >>> package.name
  110. u'yarg'
  111. """
  112. if 'version' not in self._content:
  113. return None
  114. return self._content['version']
  115. @property
  116. def url(self):
  117. """
  118. This is only available for :meth:`yarg.latest_updated_packages`, for
  119. :meth:`yarg.newest_packages` will return `None`
  120. >>> package = yarg.latest_updated_packages()[0]
  121. >>> package.url
  122. u'http://pypi.python.org/pypi/yarg'
  123. """
  124. return self._content['url']
  125. @property
  126. def date(self):
  127. """
  128. >>> package = yarg.newest_packages()[0]
  129. >>> package.date
  130. datetime.datetime(2014, 8, 9, 8, 40, 20)
  131. >>> package = yarg.latest_updated_packages()[0]
  132. >>> package.date
  133. datetime.datetime(2014, 8, 9, 8, 40, 20)
  134. """
  135. return datetime.strptime(self._content['date'],
  136. "%d %b %Y %H:%M:%S %Z")
  137. @property
  138. def description(self):
  139. """
  140. >>> package = yarg.newest_packages()[0]
  141. >>> package.description
  142. u'Some random summary stuff'
  143. >>> package = yarg.latest_updated_packages()[0]
  144. >>> package.description
  145. u'Some random summary stuff'
  146. """
  147. return self._content['description']