Skip to content

Commit

Permalink
17
Browse files Browse the repository at this point in the history
  • Loading branch information
StepaniaH committed Aug 4, 2024
1 parent 88a273f commit c9d5c22
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 151617-Project02-Data-Visualization/17_try/1701-Other-Languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import requests
import plotly.express as px

# Make an API call and check the response.
url = "https://api.github.com/search/repositories"
# url += "?q=language:python+sort:stars+stars:>10000"
url += "?q=language:javascript+sort:stars+stars:>10000"

headers = {
"Accept": "application/vnd.github.v3+json"
}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Process overall results.
response_dict = r.json()
print(f"Complete results: {not response_dict['incomplete_results']}")

# Process repository information.
repo_dicts = response_dict["items"]
repo_links, stars, hover_texts = [], [], []
for repo_dict in repo_dicts:
# Turn repo name into a link.
repo_name = repo_dict["name"]
repo_url = repo_dict["html_url"]
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)

stars.append(repo_dict["stargazers_count"])

# Build hover texts.
owner = repo_dict["owner"]["login"]
description = repo_dict["description"]
hover_texts.append(f"{owner}<br />{description}")

# Make visualization.
title = "Most-Starred JavaScript Projects on GitHub"
labels = {'x': 'Repository', 'y': 'Stars'}
fig = px.bar(
x = repo_links,
y = stars,
title = title,
labels = labels,
hover_name = hover_texts
)

fig.update_layout(
title_font_size = 28,
xaxis_title_font_size = 20,
yaxis_title_font_size = 20,
)

fig.update_traces(
marker_color = "SteelBlue",
marker_opacity = 0.6,
)

fig.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from operator import itemgetter

import requests
import plotly.express as px

# Make an API call and check the response.
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
r = requests.get(url)
print(f"Status code: {r.status_code}")

# Process the information about each submission.
submissiong_ids = r.json()
submissions_dicts = []
for submission_id in submissiong_ids[:20]:
# Make a new API call for each submission.
url = f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
r = requests.get(url)
# print(f"id: {submission_id}\tstatus: {r.status_code}")
response_dict = r.json()

# Build a dictionary for each article.
try:
submission_dict = {
"title": response_dict["title"],
"hn_link": f"https://news.ycombinator.com/item?id={submission_id}",
"comments": response_dict["descendants"],
}
except KeyError:
continue
else:
submissions_dicts.append(submission_dict)

submission_dicts = sorted(submissions_dicts, key=itemgetter("comments"), reverse=True)

article_links, comment_counts, hover_texts = [], [], []
for submission_dict in submission_dicts:
title = submission_dict["title"][:30]
discussion_link = submission_dict["hn_link"]
article_link = f'<a href="{discussion_link}"">{title}</a>'
comment_count = submission_dict["comments"]

article_links.append(article_link)
comment_counts.append(comment_count)
hover_texts.append(submission_dict["title"])

title = "Most Commented Hacker News Articles"
labels = {
'x': 'Article',
'y': 'Comments count',
}

fig = px.bar(
x = article_links,
y = comment_counts,
title = title,
labels = labels,
hover_name = hover_texts,
)

fig.update_layout(
title_font_size = 20,
xaxis_title_font_size = 15,
yaxis_title_font_size = 15,
)

fig.update_traces(
marker_color = 'SteelBlue',
marker_opacity = 0.8,
)

fig.show()

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests

import pytest

# Make an API call and check the response.
url = "https://api.github.com/search/repositories"
url += "?q=language:python+sort:stars+stars:>10000"

headers = {
"Accept": "application/vnd.github.v3+json"
}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Convert the response object to a dictionary.
response_dict = r.json()

print(f"Total repositories: {response_dict['total_count']}")
print(f"Complete results: {not response_dict['incomplete_results']}")

# Explore information about the repositories.
repo_dicts = response_dict["items"]
print(f"Repositories returned: {len(repo_dicts)}")

# Examine the first repository.
repo_dict = repo_dicts[0]

print("\nSelected information about first repository:")
for repo_dict in repo_dicts:
print(f"Name: {repo_dict['name']}")
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count' ]}")
print(f"Repository: {repo_dict['html_url']}")
print(f"Created: {repo_dict['created_at']}")
print(f"Updated: {repo_dict['updated_at']}")
print(f"Description: {repo_dict['description']}")

def test_status_code():
url = "https://api.github.com/search/repositories"
url += "?q=language:python+sort:stars+stars:>10000"

headers = {
"Accept": "application/vnd.github.v3+json"
}
r = requests.get(url, headers=headers)
assert r.status_code == 200


13 changes: 13 additions & 0 deletions 151617-Project02-Data-Visualization/hn_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import requests
import json


# Make an API call to Hacker News and store the response.
url = "https://hacker-news.firebaseio.com/v0/item/31353677.json"
r = requests.get(url)
print(f"Status code: {r.status_code}")

# Explore the structure of the data.
response_dict = r.json()
response_string = json.dumps(response_dict, indent=4)
print(response_string)
33 changes: 33 additions & 0 deletions 151617-Project02-Data-Visualization/hn_submissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from operator import itemgetter

import requests

# Make an API call and check the response.
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
r = requests.get(url)
print(f"Status code: {r.status_code}")

# Process the information about each submission.
submissiong_ids = r.json()
submissions_dicts = []
for submission_id in submissiong_ids[:5]:
# Make a new API call for each submission.
url = f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
r = requests.get(url)
print(f"id: {submission_id}\tstatus: {r.status_code}")
response_dict = r.json()

# Build a dictionary for each article.
submission_dict = {
"title": response_dict["title"],
"hn_link": f"https://news.ycombinator.com/item?id={submission_id}",
"comments": response_dict["descendants"],
}
submissions_dicts.append(submission_dict)

submission_dicts = sorted(submissions_dicts, key=itemgetter("comments"), reverse=True)

for submission_dict in submission_dicts:
print(f"\nTitle: {submission_dict['title']}")
print(f"Discussion link: {submission_dict['hn_link']}")
print(f"Comments: {submission_dict['comments']}")
34 changes: 34 additions & 0 deletions 151617-Project02-Data-Visualization/python_repos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests

# Make an API call and check the response.
url = "https://api.github.com/search/repositories"
url += "?q=language:python+sort:stars+stars:>10000"

headers = {
"Accept": "application/vnd.github.v3+json"
}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Convert the response object to a dictionary.
response_dict = r.json()

print(f"Total repositories: {response_dict['total_count']}")
print(f"Complete results: {not response_dict['incomplete_results']}")

# Explore information about the repositories.
repo_dicts = response_dict["items"]
print(f"Repositories returned: {len(repo_dicts)}")

# Examine the first repository.
repo_dict = repo_dicts[0]

print("\nSelected information about first repository:")
for repo_dict in repo_dicts:
print(f"Name: {repo_dict['name']}")
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count' ]}")
print(f"Repository: {repo_dict['html_url']}")
print(f"Created: {repo_dict['created_at']}")
print(f"Updated: {repo_dict['updated_at']}")
print(f"Description: {repo_dict['description']}")
57 changes: 57 additions & 0 deletions 151617-Project02-Data-Visualization/python_repos_visual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import requests
import plotly.express as px

# Make an API call and check the response.
url = "https://api.github.com/search/repositories"
url += "?q=language:python+sort:stars+stars:>10000"

headers = {
"Accept": "application/vnd.github.v3+json"
}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# Process overall results.
response_dict = r.json()
print(f"Complete results: {not response_dict['incomplete_results']}")

# Process repository information.
repo_dicts = response_dict["items"]
repo_links, stars, hover_texts = [], [], []
for repo_dict in repo_dicts:
# Turn repo name into a link.
repo_name = repo_dict["name"]
repo_url = repo_dict["html_url"]
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)

stars.append(repo_dict["stargazers_count"])

# Build hover texts.
owner = repo_dict["owner"]["login"]
description = repo_dict["description"]
hover_texts.append(f"{owner}<br />{description}")

# Make visualization.
title = "Most-Starred Python Projects on GitHub"
labels = {'x': 'Repository', 'y': 'Stars'}
fig = px.bar(
x = repo_links,
y = stars,
title = title,
labels = labels,
hover_name = hover_texts
)

fig.update_layout(
title_font_size = 28,
xaxis_title_font_size = 20,
yaxis_title_font_size = 20,
)

fig.update_traces(
marker_color = "SteelBlue",
marker_opacity = 0.6,
)

fig.show()

0 comments on commit c9d5c22

Please sign in to comment.