A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

241 lines
8.7 KiB

  1. """
  2. :synopsis: Main crawler module, to oversee all site-specific crawlers.
  3. Contains all website/framework-specific Class crawlers.
  4. """
  5. import logging
  6. import time
  7. import threading
  8. import requests
  9. from . import indexer
  10. class GitHubCrawler(threading.Thread):
  11. """
  12. Crawler that retrieves links to all of GitHub's public repositories.
  13. GitHubCrawler is a threaded singleton that queries GitHub's API for urls
  14. to its public repositories, which it inserts into a :class:`Queue.Queue`
  15. shared with :class:`indexer.GitIndexer`.
  16. :ivar clone_queue: (:class:`Queue.Queue`) Contains :class:`GitRepository`
  17. with repository metadata retrieved by :class:`GitHubCrawler`, and other Git
  18. crawlers, to be processed by :class:`indexer.GitIndexer`.
  19. :ivar _logger: (:class:`logging.Logger`) A class-specific logger object.
  20. """
  21. AUTHENTICATION = {
  22. "client_id" : "436cb884ae09be7f2a4e",
  23. "client_secret" : "8deeefbc2439409c5b7a092fd086772fe8b1f24e"
  24. }
  25. def __init__(self, clone_queue, run_event):
  26. """
  27. Create an instance of the singleton `GitHubCrawler`.
  28. :param clone_queue: see :attr:`self.clone_queue`
  29. :type clone_queue: see :attr:`self.clone_queue`
  30. """
  31. self.clone_queue = clone_queue
  32. self.run_event = run_event
  33. self._logger = logging.getLogger("%s.%s" %
  34. (__name__, self.__class__.__name__))
  35. self._logger.info("Starting.")
  36. super(GitHubCrawler, self).__init__(name=self.__class__.__name__)
  37. def run(self):
  38. """
  39. Query the GitHub API for data about every public repository.
  40. Pull all of GitHub's repositories by making calls to its API in a loop,
  41. accessing a subsequent page of results via the "next" URL returned in an
  42. API response header. Uses Severyn Kozak's (sevko) authentication
  43. credentials. For every new repository, a :class:`GitRepository` is
  44. inserted into :attr:`self.clone_queue`.
  45. """
  46. next_api_url = "https://api.github.com/repositories"
  47. api_request_interval = 5e3 / 60 ** 2
  48. while next_api_url and self.run_event.is_set():
  49. start_time = time.time()
  50. try:
  51. resp = requests.get(next_api_url, params=self.AUTHENTICATION)
  52. except requests.ConnectionError:
  53. self._logger.exception("API %s call failed:" % next_api_url)
  54. time.sleep(0.5)
  55. continue
  56. queue_percent_full = (float(self.clone_queue.qsize()) /
  57. self.clone_queue.maxsize) * 100
  58. self._logger.info("API call made. Queue size: %d/%d, %d%%." %
  59. ((self.clone_queue.qsize(), self.clone_queue.maxsize,
  60. queue_percent_full)))
  61. repo_names = [repo["full_name"] for repo in resp.json()]
  62. repo_stars = self._get_repositories_stars(repo_names)
  63. for repo in resp.json():
  64. while self.clone_queue.full():
  65. time.sleep(1)
  66. self.clone_queue.put(indexer.GitRepository(
  67. repo["html_url"], repo["full_name"].replace("/", ""),
  68. "GitHub", repo_stars[repo["full_name"]]))
  69. if int(resp.headers["x-ratelimit-remaining"]) == 0:
  70. time.sleep(int(resp.headers["x-ratelimit-reset"]) -
  71. time.time())
  72. next_api_url = resp.headers["link"].split(">")[0][1:]
  73. sleep_time = api_request_interval - (time.time() - start_time)
  74. if sleep_time > 0:
  75. time.sleep(sleep_time)
  76. def _get_repositories_stars(self, repo_names):
  77. """
  78. Return the number of stargazers for several repositories.
  79. Queries the GitHub API for the number of stargazers for any given
  80. repositories, and blocks if the query limit is exceeded.
  81. :param repo_names: An array of repository names, in
  82. `username/repository_name` format.
  83. :type repo_names: str
  84. :return: A dictionary with repository name keys, and corresponding
  85. stargazer count values.
  86. Example dictionary:
  87. .. code-block:: python
  88. {
  89. "user/repository" : 100
  90. }
  91. :rtype: dictionary
  92. """
  93. API_URL = "https://api.github.com/search/repositories"
  94. REPOS_PER_QUERY = 25
  95. repo_stars = {}
  96. for names in [repo_names[ind:ind + REPOS_PER_QUERY] for ind in
  97. xrange(0, len(repo_names), REPOS_PER_QUERY)]:
  98. query_url = "%s?q=%s" % (API_URL,
  99. "+".join("repo:%s" % name for name in names))
  100. params = self.AUTHENTICATION
  101. resp = requests.get(query_url,
  102. params=params,
  103. headers={
  104. "Accept" : "application/vnd.github.preview"
  105. })
  106. if int(resp.headers["x-ratelimit-remaining"]) == 0:
  107. sleep_time = int(resp.headers["x-ratelimit-reset"]) - \
  108. time.time() + 1
  109. if sleep_time > 0:
  110. logging.info("API quota exceeded. Sleep time: %d." %
  111. sleep_time)
  112. time.sleep(sleep_time)
  113. for repo in resp.json()["items"]:
  114. rank = float(repo["stargazers_count"]) / 1000
  115. repo_stars[repo["full_name"]] = rank if rank < 1.0 else 1.0
  116. for name in repo_names:
  117. if name not in repo_stars:
  118. repo_stars[name] = 0.5
  119. return repo_stars
  120. class BitbucketCrawler(threading.Thread):
  121. """
  122. Crawler that retrieves links to all of Bitbucket's public repositories.
  123. BitbucketCrawler is a threaded singleton that queries Bitbucket's API for
  124. urls to its public repositories, and inserts them as
  125. :class:`indexer.GitRepository` into a :class:`Queue.Queue` shared with
  126. :class:`indexer.GitIndexer`.
  127. :ivar clone_queue: (:class:`Queue.Queue`) The shared queue to insert
  128. :class:`indexer.GitRepository` repository urls into.
  129. :ivar _logger: (:class:`logging.Logger`) A class-specific logger object.
  130. """
  131. def __init__(self, clone_queue, run_event):
  132. """
  133. Create an instance of the singleton `BitbucketCrawler`.
  134. :param clone_queue: see :attr:`self.clone_queue`
  135. :type clone_queue: see :attr:`self.clone_queue`
  136. """
  137. self.clone_queue = clone_queue
  138. self.run_event = run_event
  139. self._logger = logging.getLogger("%s.%s" %
  140. (__name__, self.__class__.__name__))
  141. self._logger.info("Starting.")
  142. super(BitbucketCrawler, self).__init__(name=self.__class__.__name__)
  143. def run(self):
  144. """
  145. Query the Bitbucket API for data about every public repository.
  146. Query the Bitbucket API's "/repositories" endpoint and read its
  147. paginated responses in a loop; any "git" repositories have their
  148. clone-urls and names inserted into a :class:`indexer.GitRepository` in
  149. :attr:`self.clone_queue`.
  150. """
  151. next_api_url = "https://api.bitbucket.org/2.0/repositories"
  152. while self.run_event.is_set():
  153. try:
  154. response = requests.get(next_api_url).json()
  155. except requests.ConnectionError:
  156. self._logger.exception("API %s call failed:", next_api_url)
  157. time.sleep(0.5)
  158. continue
  159. queue_percent_full = (float(self.clone_queue.qsize()) /
  160. self.clone_queue.maxsize) * 100
  161. self._logger.info("API call made. Queue size: %d/%d, %d%%." %
  162. ((self.clone_queue.qsize(), self.clone_queue.maxsize,
  163. queue_percent_full)))
  164. for repo in response["values"]:
  165. if repo["scm"] == "git":
  166. while self.clone_queue.full():
  167. time.sleep(1)
  168. clone_links = repo["links"]["clone"]
  169. clone_url = (clone_links[0]["href"] if
  170. clone_links[0]["name"] == "https" else
  171. clone_links[1]["href"])
  172. try:
  173. watchers = requests.get(
  174. repo["links"]["watchers"]["href"])
  175. rank = len(watchers.json()["values"]) / 100
  176. except requests.ConnectionError:
  177. err = "API %s call failed:" % next_api_url
  178. self._logger.exception(err)
  179. time.sleep(0.5)
  180. continue
  181. self.clone_queue.put(indexer.GitRepository(
  182. clone_url, repo["full_name"], "Bitbucket"),
  183. rank if rank < 1.0 else 1.0)
  184. next_api_url = response["next"]
  185. time.sleep(0.2)