File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import requests
2+
from operator import itemgetter
3+
4+
# Meke an API call and store the respose
5+
api_url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
6+
r = requests.get(api_url)
7+
print('Status code: ', r.status_code)
8+
9+
# Process information about each submission
10+
submission_ids = r.json()
11+
submission_dicts = []
12+
13+
for submission_id in submission_ids[:30]:
14+
# Make a seperate API call to for each submission
15+
url = (
16+
f'https://hacker-news.firebaseio.com/v0/item/{str(submission_id)}.json')
17+
submission_r = requests.get(url)
18+
response_dict = submission_r.json()
19+
20+
submission_dict = {
21+
'title': response_dict['title'],
22+
'link': f'http://news.ycombinator.com/item?id={str(submission_id)}',
23+
'comments': response_dict.get('descendants', 0), # return 0 in Null
24+
}
25+
26+
submission_dicts.append(submission_dict)
27+
28+
submission_dicts = sorted(
29+
submission_dicts, key=itemgetter('comments'), reverse=True)
30+
31+
for submission_dict in submission_dicts:
32+
print('\nTitle:', submission_dict['title'])
33+
print('Discussion link:', submission_dict['link'])
34+
print('Comments:', submission_dict['comments'])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import requests
2+
import pygal
3+
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
4+
5+
# Make an API request and store response
6+
url = 'https://api..com/search/repositories?q=language:javascript&sort=stars'
7+
r = requests.get(url)
8+
print('API Request Status: ', r.status_code)
9+
10+
# Store API response in a variable
11+
response_dict = r.json()
12+
print('Total repositories: ', response_dict['total_count'])
13+
14+
# Explore information about the repositories
15+
repo_dicts = response_dict['items']
16+
17+
names, plot_dicts = [], []
18+
for repo_dict in repo_dicts:
19+
names.append(repo_dict['name'])
20+
21+
plot_dict = {
22+
'value': repo_dict['stargazers_count'],
23+
'label': repo_dict['description'],
24+
'xlink': repo_dict['html_url'],
25+
}
26+
27+
plot_dicts.append(plot_dict)
28+
29+
# Visualize with pygal bar chart
30+
my_style = LS('#733380', base_style=LCS)
31+
my_style.title_font_size = 24
32+
my_style.label_font_size = 14
33+
my_style.major_label_font_size = 18
34+
35+
my_config = pygal.Config()
36+
my_config.value_formatter = lambda y: f'{y:,.0f}' # format (123,456)
37+
my_config.x_label_rotation = 45
38+
my_config.show_legend = False
39+
my_config.tooltip_fancy_mode = False
40+
my_config.truncate_label = 15
41+
my_config.show_y_guides = False
42+
my_config.width = 1000
43+
44+
chart = pygal.Bar(my_config, style=my_style)
45+
chart.title = 'Most-Starred JavaScript Projects on '
46+
chart.x_labels = names
47+
48+
chart.add('', plot_dicts)
49+
chart.render_to_file('./working_with_apis/images/svg/javascript_repos.svg')
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
# Visualize with pygal bar chart
3030
my_style = LS('#336699', base_style=LCS)
3131
my_style.title_font_size = 24
32-
my_style.title_font_color = '#1bcc1b'
3332
my_style.label_font_size = 14
3433
my_style.major_label_font_size = 18
3534

3635
my_config = pygal.Config()
36+
my_config.value_formatter = lambda y: f'{y:,.0f}' # format (123,456)
3737
my_config.x_label_rotation = 45
3838
my_config.show_legend = False
3939
my_config.tooltip_fancy_mode = False # Hides title from tooltip on hover

0 commit comments

Comments
 (0)