From f3e601af4f343b1c3fa50ef52fb21043879470bd Mon Sep 17 00:00:00 2001 From: Eugene Afonin Date: Wed, 8 Feb 2023 00:47:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=B71=20=D0=9C=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D1=8B=20=D1=81=D0=B1=D0=BE=D1=80=D0=B0=20=D0=B8=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=B8=D0=B7=20=D1=81=D0=B5=D1=82=D0=B8=20?= =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D1=80=D0=BD=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw1/task-1.py | 29 ++++++++++++++++++++++++ hw1/task-2.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 hw1/task-1.py create mode 100644 hw1/task-2.py diff --git a/hw1/task-1.py b/hw1/task-1.py new file mode 100644 index 0000000..35258e9 --- /dev/null +++ b/hw1/task-1.py @@ -0,0 +1,29 @@ +""" +1. Посмотреть документацию к API GitHub, разобраться как вывести список репозиториев для конкретного пользователя, +сохранить JSON-вывод в файле *.json. +""" + +import requests +import json + +def get_json(url: str) -> dict: + """ + Получение json по url + :param url: Ссылка + :return: + """ + response = requests.get(url) + if response.status_code != 200: + raise requests.ConnectionError + return response.json() + +USERNAME = 'eadevlab' +url = 'https://api.github.com/users/%s/repos' % USERNAME +try: + repos_info = get_json(url) + repo_names = [_['name'] for _ in repos_info ] + with open('./repos.json', 'w') as f: + json.dump(repo_names, f) + print('Список репозиториев:', *repo_names, sep='\n') +except requests.ConnectionError: + print('Возникла ошибка') \ No newline at end of file diff --git a/hw1/task-2.py b/hw1/task-2.py new file mode 100644 index 0000000..a536cce --- /dev/null +++ b/hw1/task-2.py @@ -0,0 +1,63 @@ +""" +2. Изучить список открытых API (https://www.programmableweb.com/category/all/apis). +Найти среди них любое, требующее авторизацию (любого типа). Выполнить запросы к нему, пройдя авторизацию. +Ответ сервера записать в файл. +""" +from urllib.parse import urlencode +import requests +import json + +class ApiClient: + base_url = 'https://api.thecatapi.com/v1/' + def __init__(self, api_key): + """ + Конструктор + :param api_key: Ключ доступа к апи + """ + self.api_key = api_key + + def search(self,page:int=1,limit:int=10) -> dict: + """ + Постраничный поиск фото + https://api.thecatapi.com/v1/images/search?limit=&page + :param page: + :param limit: + :return: + """ + return self.__send('images/search', { + 'limit': limit, + 'page': page + }) + + def __send(self, method, params): + """ + Отправка API запроса + :param method: + :param params: + :return: + """ + url = self.__build(method, params) + response = requests.get(url) + if response.status_code != 200: + raise requests.ConnectionError + return response.json() + + def __build(self, method, params): + """ + Построение ссылки + :param method: + :param params: + :return: + """ + params['api_key'] = self.api_key + return self.base_url+method+'?'+urlencode(params) + + +API_KEY = 'live_oNsVsaL8TAcRbmZzJvaWrPK0KJUlaVSF1Gu90EQ4Ldj1sJPirxXrFx8VhpaGwhaz' +client = ApiClient(API_KEY) +try: + with open('./out.json','w') as f: + json.dump(client.search(), f) + print('Готово') +except requests.ConnectionError: + print('Ошибка!') \ No newline at end of file