|
60 | 60 |
|
61 | 61 |
|
62 | 62 |
|
63 | | -# Implementing the task |
| 63 | +# Implementing the task 1 |
64 | 64 | # Finding information about numbers from web resource numbersapi.com
|
65 | 65 | # Reading numbers from the file and writing them into the list
|
66 | 66 | n = []
|
|
105 | 105 | # Writing content in the new file
|
106 | 106 | with open('test_numbers_results.txt', 'w') as w:
|
107 | 107 | w.write(content)
|
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +# Implementing task 2 |
| 113 | +# Obtaining information about art workers from the web resource artsy.net |
| 114 | +# As inputs there will be id of each art worker |
| 115 | +# We need to get names of each worker and date of birth |
| 116 | +# As output - show the names of workers sorted by dates of birth |
| 117 | +# If for some of them the date is similar - sort these ones by alphabetic order |
| 118 | + |
| 119 | +# Getting a token from web resource artsy.net with api |
| 120 | +client_id = 'c106a77ee4dda462f463' |
| 121 | +client_secret = '3aa6adfa54057aa5c4341152482a97ee' |
| 122 | + |
| 123 | +# Creating a request |
| 124 | +request = requests.post("https://api.artsy.net/api/tokens/xapp_token", |
| 125 | + data={ |
| 126 | + "client_id": client_id, |
| 127 | + "client_secret": client_secret |
| 128 | + }) |
| 129 | + |
| 130 | +# Getting the dictionary from json data with 'json' library |
| 131 | +data = json.loads(request.text) |
| 132 | + |
| 133 | +# Obtaining the token |
| 134 | +token = data["token"] |
| 135 | + |
| 136 | +# Creating header with our token |
| 137 | +headers = {"X-Xapp-Token": token} |
| 138 | + |
| 139 | +# Reading the id of art workers and writing the results into the list |
| 140 | +with open("test_dataset.txt") as f: |
| 141 | + results = [] |
| 142 | + for line in f: |
| 143 | + line = line.rstrip() # Deleting any symbols at the end of the line |
| 144 | + # Creating a template for the request |
| 145 | + template = 'https://api.artsy.net/api/artists/{}' |
| 146 | + # Creating a request with header |
| 147 | + respond = requests.get(template.format(line), headers=headers) |
| 148 | + # Getting the results in 'utf-8' coding |
| 149 | + respond.encoding = 'utf-8' |
| 150 | + # Getting the dictionary from json data with 'json' library |
| 151 | + data = json.loads(respond.text) |
| 152 | + # Adding all data in the list |
| 153 | + results.append(data) |
| 154 | + |
| 155 | +# Sorting results with method 'sorted' for list |
| 156 | +# As an argument for this method we use 'key' |
| 157 | +# As an value for this argument we use results of 'lambda' function |
| 158 | +# Results of 'lambda' function in our case are birthdays and then names |
| 159 | +# So, firstly method 'sorted' will sort by birthday |
| 160 | +# And if there is the same value of birthday it will sort by name |
| 161 | +sorted_results = sorted(results, key=lambda artist: (int(artist['birthday']), artist['name'])) |
| 162 | + |
| 163 | +# Showing the final results |
| 164 | +for item in sorted_results: |
| 165 | + print(item['name']) |
0 commit comments