Last active
April 29, 2022 20:36
-
-
Save code-review-doctor/52778aed13a8ce9482f76d77ad1723e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Client: | |
def get_popular_repositories(self, topic, count, per_page=100): | |
urls = [] | |
q = "topic:{topic}&order=desc&sort=stars" | |
url = f"https://api.github.com/search/repositories?q={q}" | |
first_resp = self.session.get( | |
url=url, | |
params={"per_page": per_page} | |
) | |
for resp in self.paginate(first_resp): | |
resp.raise_for_status() | |
urls += [i['clone_url'] for i in resp.json()['items']] | |
if len(urls) >= count: | |
break | |
return urls[:count] | |
def paginate(self, response): | |
while True: | |
yield response | |
if 'next' not in response.links: | |
break | |
response = self.session.get( | |
url=response.links['next']['url'], | |
headers=response.request.headers | |
) | |
# We then ran that code like | |
client = Client() | |
clone_urls = client.get_popular_repositories( | |
topic='python', count=666 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment