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

Refactor into functions

parent 8b4a0464
......@@ -2,43 +2,52 @@ import requests
import json
import pandas as pd
# Repository details
owner = "EbookFoundation"
repo = "free-programming-books"
# GitHub API endpoint
url = f"https://api.github.com/repos/{owner}/{repo}/issues"
headers = {
"Accept": "application/vnd.github+json",
# "Authorization": "Bearer YOUR_GITHUB_TOKEN" # optional
}
# Fetch issues
response = requests.get(url, headers=headers)
if response.status_code == 200:
issues = response.json()
# Extract key fields from each issue
def fetch_issues(owner, repo):
'''Return a list of issues from OWNER/REPO.
'''
# GitHub API endpoint
url = f"https://api.github.com/repos/{owner}/{repo}/issues"
headers = {
"Accept": "application/vnd.github+json",
# "Authorization": "Bearer YOUR_GITHUB_TOKEN" # optional
}
# Fetch issues
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise f"Failed to fetch issues from {owner}/{repo}: status {response.status_code}"
def serialize_issue(issue):
'''Serializes an issue object from the Github API to a dictionary
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 = []
for issue in issues:
data.append({
"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")
})
data.append(serialize_issue(issue))
# Convert to DataFrame
df = pd.DataFrame(data)
print(df.head())
df.to_json("github_data.json")
else:
print(f"Failed to fetch issues: {response.status_code}")
if __name__=="__main__":
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