Unverified Commit dcb636ad authored by Noé Lopez's avatar Noé Lopez
Browse files

Refactor into functions

parent 8b4a0464
...@@ -2,43 +2,52 @@ import requests ...@@ -2,43 +2,52 @@ import requests
import json import json
import pandas as pd import pandas as pd
# Repository details def fetch_issues(owner, repo):
owner = "EbookFoundation" '''Return a list of issues from OWNER/REPO.
repo = "free-programming-books" '''
# GitHub API endpoint
# GitHub API endpoint url = f"https://api.github.com/repos/{owner}/{repo}/issues"
url = f"https://api.github.com/repos/{owner}/{repo}/issues" headers = {
"Accept": "application/vnd.github+json",
headers = { # "Authorization": "Bearer YOUR_GITHUB_TOKEN" # optional
"Accept": "application/vnd.github+json", }
# "Authorization": "Bearer YOUR_GITHUB_TOKEN" # optional # Fetch issues
} response = requests.get(url, headers=headers)
if response.status_code == 200:
# Fetch issues return response.json()
response = requests.get(url, headers=headers) else:
raise f"Failed to fetch issues from {owner}/{repo}: status {response.status_code}"
if response.status_code == 200:
issues = response.json() def serialize_issue(issue):
'''Serializes an issue object from the Github API to a dictionary
# Extract key fields from each issue of key fields.
'''
return {
"id": issue.get("id"),
"number": issue.get("number"),
"title": issue.get("title"),
"state": issue.get("state"),
"created_at": issue.get("created_at"),
"updated_at": issue.get("updated_at"),
"user_login": issue["user"]["login"] if "user" in issue and issue["user"] else None,
"comments": issue.get("comments"),
"html_url": issue.get("html_url")
}
def main():
# Repository details
owner = "EbookFoundation"
repo = "free-programming-books"
issues = fetch_issues(owner, repo)
data = [] data = []
for issue in issues: for issue in issues:
data.append({ data.append(serialize_issue(issue))
"id": issue.get("id"),
"number": issue.get("number"),
"title": issue.get("title"),
"state": issue.get("state"),
"created_at": issue.get("created_at"),
"updated_at": issue.get("updated_at"),
"user_login": issue["user"]["login"] if "user" in issue and issue["user"] else None,
"comments": issue.get("comments"),
"html_url": issue.get("html_url")
})
# Convert to DataFrame # Convert to DataFrame
df = pd.DataFrame(data) df = pd.DataFrame(data)
print(df.head()) print(df.head())
df.to_json("github_data.json") df.to_json("github_data.json")
else: if __name__=="__main__":
print(f"Failed to fetch issues: {response.status_code}") main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment