Created
June 4, 2019 14:13
-
-
Save emres/6d34f1a7982d8fcaf6138029de4bcecb to your computer and use it in GitHub Desktop.
Get as many Zen koans as possible from GitHub API using Python
This file contains hidden or 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
import time | |
import random | |
import requests | |
from requests.exceptions import HTTPError | |
koans = [] | |
num_same_koan_found = 0 | |
max_num_same_koan_found = 30 | |
zen_url = 'https://api.github.com/zen' | |
random.seed() | |
while True: | |
if num_same_koan_found > max_num_same_koan_found: | |
break | |
time.sleep(random.randint(1, 5) / 10) | |
try: | |
response = requests.get(zen_url) | |
if "rate limit exceeded" in response.text: | |
print("Sorry, but API rate limit exceeded for anonymous GitHub API access!") | |
print() | |
break | |
if response.status_code == 200: | |
if response.text not in koans: | |
koans.append(response.text) | |
else: | |
num_same_koan_found += 1 | |
except HTTPError as http_err: | |
print(f'HTTP error occurred: {http_err}') | |
except Exception as err: | |
print(f'Other error occurred: {err}') | |
for koan in koans: | |
print(koan) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment