|
8 | 8 | RL = RandomLanguage()
|
9 | 9 | print(f'Creating a {RL.language_title} chart...')
|
10 | 10 |
|
11 |
| -# Make an API request and store response |
12 |
| -url = f'https://api..com/search/repositories?q=language:{RL.language}&sort=stars' |
13 |
| -r = requests.get(url) |
14 |
| -print('API Request Status: ', r.status_code) |
15 |
| - |
16 |
| -# Store API response in a variable |
17 |
| -response_dict = r.json() |
18 |
| -print('Total repositories: ', response_dict['total_count']) |
19 |
| - |
20 |
| -# Explore information about the repositories |
21 |
| -repo_dicts = response_dict['items'] |
22 |
| - |
23 |
| -names, plot_dicts = [], [] |
24 |
| -for repo_dict in repo_dicts: |
25 |
| -names.append(repo_dict['name']) |
26 |
| - |
27 |
| -plot_dict = { |
28 |
| -'value': repo_dict['stargazers_count'], |
29 |
| -'label': repo_dict['description'] or "", |
30 |
| -'xlink': repo_dict['html_url'], |
31 |
| -} |
32 |
| - |
33 |
| -plot_dicts.append(plot_dict) |
34 |
| - |
35 |
| -# Visualize with pygal bar chart |
36 |
| -my_style = LS(RL.language_color, base_style=LCS) |
37 |
| -my_style.title_font_size = 24 |
38 |
| -my_style.label_font_size = 14 |
39 |
| -my_style.major_label_font_size = 18 |
40 |
| - |
41 |
| -my_config = pygal.Config() |
42 |
| -my_config.value_formatter = lambda y: f'{y:,.0f}' # format (123,456) |
43 |
| -my_config.x_label_rotation = 45 |
44 |
| -my_config.show_legend = False |
45 |
| -my_config.tooltip_fancy_mode = False |
46 |
| -my_config.truncate_label = 15 |
47 |
| -my_config.show_y_guides = False |
48 |
| -my_config.width = 1000 |
49 |
| - |
50 |
| -chart = pygal.Bar(my_config, style=my_style) |
51 |
| -chart.title = f'Most-Starred {RL.language_title} Projects on ' |
52 |
| -chart.x_labels = names |
53 |
| - |
54 |
| -chart.add('', plot_dicts) |
55 |
| -chart.render_to_file( |
56 |
| -f'./working_with_apis/images/svg/{RL.language}_repos.svg') |
57 |
| -print(f'Created a {RL.language_title} chart.') |
| 11 | + |
| 12 | +def get_response(): |
| 13 | +""" Make an API request and return response """ |
| 14 | +url = f'https://api..com/search/repositories?q=language:{RL.language}&sort=stars' |
| 15 | +r = requests.get(url) |
| 16 | +print('API Request Status: ', r.status_code) |
| 17 | +return r |
| 18 | + |
| 19 | + |
| 20 | +def get_repo_dicts(response): |
| 21 | +""" Store and return language repository. """ |
| 22 | +response_dict = r.json() |
| 23 | +print('Total repositories: ', response_dict['total_count']) |
| 24 | +repo_dicts = response_dict['items'] |
| 25 | +return repo_dicts |
| 26 | + |
| 27 | + |
| 28 | +def get_names_plot_dicts(repo_dicts): |
| 29 | +""" Process the response dicts, returns dicts and names for plotting. """ |
| 30 | +names, plot_dicts = [], [] |
| 31 | +for repo_dict in repo_dicts: |
| 32 | +names.append(repo_dict['name']) |
| 33 | + |
| 34 | +plot_dict = { |
| 35 | +'value': repo_dict['stargazers_count'], |
| 36 | +'label': repo_dict['description'] or "", |
| 37 | +'xlink': repo_dict['html_url'], |
| 38 | +} |
| 39 | + |
| 40 | +plot_dicts.append(plot_dict) |
| 41 | +return names, plot_dicts |
| 42 | + |
| 43 | + |
| 44 | +def vizualise_data(names, plot_dicts): |
| 45 | +# Visualize with pygal bar chart |
| 46 | +my_style = LS(RL.language_color, base_style=LCS) |
| 47 | +my_style.title_font_size = 24 |
| 48 | +my_style.label_font_size = 14 |
| 49 | +my_style.major_label_font_size = 18 |
| 50 | + |
| 51 | +my_config = pygal.Config() |
| 52 | +my_config.value_formatter = lambda y: f'{y:,.0f}' # format (123,456) |
| 53 | +my_config.x_label_rotation = 45 |
| 54 | +my_config.show_legend = False |
| 55 | +my_config.tooltip_fancy_mode = False |
| 56 | +my_config.truncate_label = 15 |
| 57 | +my_config.show_y_guides = False |
| 58 | +my_config.width = 1000 |
| 59 | + |
| 60 | +chart = pygal.Bar(my_config, style=my_style) |
| 61 | +chart.title = f'Most-Starred {RL.language_title} Projects on ' |
| 62 | +chart.x_labels = names |
| 63 | + |
| 64 | +chart.add('', plot_dicts) |
| 65 | +chart.render_to_file( |
| 66 | +f'./working_with_apis/images/svg/{RL.language}_repos.svg') |
| 67 | +print(f'Created a {RL.language_title} chart.') |
| 68 | + |
| 69 | + |
| 70 | +r = get_response() |
| 71 | +repo_dicts = get_repo_dicts(r) |
| 72 | +names, plot_dicts = get_names_plot_dicts(repo_dicts) |
| 73 | +vizualise_data(names, plot_dicts) |
0 commit comments