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

Use document writer to output issues in right format

parent b4c53206
Pipeline #12099 canceled with stages
import requests import requests
import json import json
import pandas as pd
from document import Document
from document_writer import DocumentWriter
def fetch_issues(owner, repo): def fetch_issues(owner, repo):
'''Return a list of issues from OWNER/REPO. '''Return a list of issues from OWNER/REPO.
...@@ -18,21 +20,26 @@ def fetch_issues(owner, repo): ...@@ -18,21 +20,26 @@ def fetch_issues(owner, repo):
else: else:
raise f"Failed to fetch issues from {owner}/{repo}: status {response.status_code}" raise f"Failed to fetch issues from {owner}/{repo}: status {response.status_code}"
def serialize_issue(issue): def serialize_issue(issue, owner, repo):
'''Serializes an issue object from the Github API to a dictionary '''Serializes an issue object from the Github API to a Document object.
of key fields.
''' '''
return { id = issue.get("id")
doc = Document()
doc.set_name(f"{owner}/{repo}/issues/{id}")
doc.set_title(issue.get("title"))
doc.set_attributes({
"id": issue.get("id"), "id": issue.get("id"),
"number": issue.get("number"), "number": issue.get("number"),
"title": issue.get("title"),
"state": issue.get("state"), "state": issue.get("state"),
"created_at": issue.get("created_at"), "creation_date": issue.get("created_at"),
"updated_at": issue.get("updated_at"), "updated_at": issue.get("updated_at"),
"user_login": issue["user"]["login"] if "user" in issue and issue["user"] else None, #"user_login": issue["user"]["login"] if "user" in issue and issue["user"] else None,
"comments": issue.get("comments"), "comments": issue.get("comments"),
"html_url": issue.get("html_url") "html_url": issue.get("html_url")
} })
doc.set_main_content(issue.get("body") or "")
return doc
def main(): def main():
# Repository details # Repository details
...@@ -40,14 +47,15 @@ def main(): ...@@ -40,14 +47,15 @@ def main():
repo = "free-programming-books" repo = "free-programming-books"
issues = fetch_issues(owner, repo) issues = fetch_issues(owner, repo)
data = [] documents = []
for issue in issues: for issue in issues:
data.append(serialize_issue(issue)) documents.append(serialize_issue(issue, owner, repo))
print(f"Collected {len(documents)} documents")
# Convert to DataFrame writer = DocumentWriter("output")
df = pd.DataFrame(data) for doc in documents:
print(df.head()) writer.write_document(doc)
df.to_json("github_data.json")
if __name__=="__main__": if __name__=="__main__":
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