2020-07-09 22:20:38 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2020-07-09 23:15:08 +00:00
|
|
|
username = os.getenv("GITHUB_USERNAME")
|
|
|
|
password = os.getenv("GITHUB_PASSWORD")
|
|
|
|
API_URL = "https://api.github.com"
|
2020-07-09 22:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_response(page: int) -> dict:
|
|
|
|
res = requests.get(
|
2020-07-09 23:15:08 +00:00
|
|
|
f"{API_URL}/search/code",
|
2020-07-09 22:20:38 +00:00
|
|
|
auth=(username, password),
|
2020-07-09 23:15:08 +00:00
|
|
|
params={"q": "fastapi language:Python", "per_page": 100, "page": page},
|
2020-07-09 22:20:38 +00:00
|
|
|
)
|
|
|
|
return res
|
|
|
|
|
2020-07-09 23:15:08 +00:00
|
|
|
|
2020-07-09 22:20:38 +00:00
|
|
|
def get_next_link(link_header: str) -> str:
|
2020-07-09 23:15:08 +00:00
|
|
|
return getattr(
|
|
|
|
{
|
|
|
|
rel: link
|
|
|
|
for (link, rel) in re.findall(r'<(http.*?)>; rel="(.*?)"', link_header)
|
|
|
|
},
|
|
|
|
"next",
|
|
|
|
None,
|
|
|
|
)
|
2020-07-09 22:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
filename = "links.txt"
|
2020-07-09 23:15:08 +00:00
|
|
|
file1 = open(filename, "a") # append mode
|
2020-07-09 22:20:38 +00:00
|
|
|
has_next = True
|
|
|
|
page = 1
|
|
|
|
while has_next:
|
|
|
|
sleep(1)
|
|
|
|
res = get_response(page)
|
|
|
|
res_json = res.json()
|
2020-07-09 23:15:08 +00:00
|
|
|
if "items" in res_json:
|
|
|
|
for item in res_json["items"]:
|
2020-07-09 22:20:38 +00:00
|
|
|
file1.write(f"{item['repository']['html_url']}\n")
|
|
|
|
print(f"Page: {page}")
|
|
|
|
print(res.headers)
|
|
|
|
# print(json.dumps(res_json, indent=4, sort_keys=True))
|
|
|
|
# print(res.headers.get('X-RateLimit-Reset', 0))
|
2020-07-09 23:15:08 +00:00
|
|
|
if int(
|
|
|
|
res.headers.get("X-RateLimit-Remaining", 0)
|
|
|
|
) == 0 or "422" in res.headers.get("Status", "422"):
|
2020-07-09 22:20:38 +00:00
|
|
|
has_next = False
|
|
|
|
page += 1
|
|
|
|
|
|
|
|
file1.close()
|