From cf37853c6cfe682eeebe5987ae2fd87e0c9c335d Mon Sep 17 00:00:00 2001 From: LiLeon Date: Tue, 21 Mar 2023 15:33:40 -0600 Subject: [PATCH] [Liliana_Leon]_Lab_Api_Scavenger ls --- challenge-1.py | 11 +- your-code/main.ipynb | 670 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 680 insertions(+), 1 deletion(-) create mode 100644 your-code/main.ipynb diff --git a/challenge-1.py b/challenge-1.py index df4b3cf..8d0a49b 100644 --- a/challenge-1.py +++ b/challenge-1.py @@ -1 +1,10 @@ -# enter your code below \ No newline at end of file +# enter your code below +import configparser +import time +import requests + +config = configparser.ConfigParser() +config.read('Ironhack/Config/config2.cfg') + +token = config['GITHUB']['token'] +headers = {'Authorization': 'token ' + token} \ No newline at end of file diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..d0f5f81 --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,670 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "972fb82d-5892-404a-95c7-6bb4c7aedbea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# enter your code below\n", + "import configparser\n", + "import time\n", + "import requests\n", + "\n", + "config = configparser.ConfigParser()\n", + "config.read('Ironhack/Config/config2.cfg')\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "fa6b1e15-87e5-4ba1-8153-2ffb0b816a14", + "metadata": {}, + "outputs": [], + "source": [ + "token = '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'\n", + "headers = {'Authorization': 'token ' + token}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "74175d70-f927-4715-8e4b-e0b15ed9e061", + "metadata": {}, + "outputs": [], + "source": [ + "url_inicial = 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents'" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "ea7948c3-98bb-4271-afcf-98ea3cef2d8d", + "metadata": {}, + "outputs": [], + "source": [ + "def validar_intentos_restantes(respuesta):\n", + " print('Hora reset',time.ctime(int(respuesta.headers['X-RateLimit-Reset'])))\n", + " print('Intentos restantes',respuesta.headers['X-RateLimit-Remaining'])\n", + " print('Intentos usados',respuesta.headers['X-RateLimit-Used'])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "352e97a7-f34d-4b96-a0f0-0a18674128d8", + "metadata": {}, + "outputs": [], + "source": [ + "def consulta(endpoint, validar_restantes = False, headers = headers):\n", + " response = requests.get(endpoint, headers = headers)\n", + " print(response.status_code)\n", + " if validar_restantes:\n", + " validar_intentos_restantes(response)\n", + " return response.json()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c7c22017-ce62-4211-a6bb-af113ca38ad4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n" + ] + } + ], + "source": [ + "result = consulta(url_inicial)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "614d9629-338c-4f9f-b73c-71c951ca71aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': '.gitignore',\n", + " 'path': '.gitignore',\n", + " 'sha': 'e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf',\n", + " 'size': 10,\n", + " 'url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/.gitignore?ref=master',\n", + " 'html_url': 'https://github.com/ironhack-datalabs/scavenger/blob/master/.gitignore',\n", + " 'git_url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf',\n", + " 'download_url': 'https://raw.githubusercontent.com/ironhack-datalabs/scavenger/master/.gitignore',\n", + " 'type': 'file',\n", + " '_links': {'self': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/.gitignore?ref=master',\n", + " 'git': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf',\n", + " 'html': 'https://github.com/ironhack-datalabs/scavenger/blob/master/.gitignore'}}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4c29be47-21a5-47fe-b9fa-59402b0fade6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'15024'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "path_ejemplo = result[1]['path']\n", + "path_ejemplo" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a48041a7-0445-46e3-a42b-f1c145fa301a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n" + ] + } + ], + "source": [ + "result_files = consulta(f'{url_inicial}/{path_ejemplo}')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "23844130-d089-4f23-9d4c-d4fea2f316e3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'name': '.0006.scavengerhunt',\n", + " 'path': '15024/.0006.scavengerhunt',\n", + " 'sha': '1c9064284a24b3486015eafdb391b141c27ada2b',\n", + " 'size': 3,\n", + " 'url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/.0006.scavengerhunt?ref=master',\n", + " 'html_url': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/.0006.scavengerhunt',\n", + " 'git_url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/1c9064284a24b3486015eafdb391b141c27ada2b',\n", + " 'download_url': 'https://raw.githubusercontent.com/ironhack-datalabs/scavenger/master/15024/.0006.scavengerhunt',\n", + " 'type': 'file',\n", + " '_links': {'self': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/.0006.scavengerhunt?ref=master',\n", + " 'git': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/1c9064284a24b3486015eafdb391b141c27ada2b',\n", + " 'html': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/.0006.scavengerhunt'}},\n", + " {'name': '40',\n", + " 'path': '15024/40',\n", + " 'sha': 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'size': 0,\n", + " 'url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/40?ref=master',\n", + " 'html_url': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/40',\n", + " 'git_url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'download_url': 'https://raw.githubusercontent.com/ironhack-datalabs/scavenger/master/15024/40',\n", + " 'type': 'file',\n", + " '_links': {'self': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/40?ref=master',\n", + " 'git': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'html': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/40'}},\n", + " {'name': '74',\n", + " 'path': '15024/74',\n", + " 'sha': 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'size': 0,\n", + " 'url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/74?ref=master',\n", + " 'html_url': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/74',\n", + " 'git_url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'download_url': 'https://raw.githubusercontent.com/ironhack-datalabs/scavenger/master/15024/74',\n", + " 'type': 'file',\n", + " '_links': {'self': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/74?ref=master',\n", + " 'git': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'html': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/74'}},\n", + " {'name': '99',\n", + " 'path': '15024/99',\n", + " 'sha': 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'size': 0,\n", + " 'url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/99?ref=master',\n", + " 'html_url': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/99',\n", + " 'git_url': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'download_url': 'https://raw.githubusercontent.com/ironhack-datalabs/scavenger/master/15024/99',\n", + " 'type': 'file',\n", + " '_links': {'self': 'https://api.github.com/repos/ironhack-datalabs/scavenger/contents/15024/99?ref=master',\n", + " 'git': 'https://api.github.com/repos/ironhack-datalabs/scavenger/git/blobs/e69de29bb2d1d6434b8b29ae775ad8c2e48c5391',\n", + " 'html': 'https://github.com/ironhack-datalabs/scavenger/blob/master/15024/99'}}]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result_files" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "769812e5-962e-479b-a79d-267ce325d8c3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'15024/.0006.scavengerhunt'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result_files[0]['path']" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "25f0c6f3-96aa-44f2-be8b-5220c2173481", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['15024/.0006.scavengerhunt']" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[element['path'] for element in result_files if element['path'].endswith('hunt')]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "46be10f7-ee09-4c3f-8782-1b0821de0861", + "metadata": {}, + "outputs": [], + "source": [ + "total_archivos = []" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "194e316c-2a5d-4daa-9f2c-98bc966f2120", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n" + ] + } + ], + "source": [ + "for directory in result[1:]:\n", + " path_ejemplo = directory['path']\n", + " respuesta_consulta = consulta(f'{url_inicial}/{path_ejemplo}')\n", + " archivos_utiles = [element['path'] for element in respuesta_consulta if element['path'].endswith('hunt')]\n", + " total_archivos += archivos_utiles" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e63c94d4-e56e-405b-837b-f614c82839e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(total_archivos)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "0b5a785b-2fcf-4b5c-b147-fdf84e71e7fe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['15024/.0006.scavengerhunt',\n", + " '15534/.0008.scavengerhunt',\n", + " '15534/.0012.scavengerhunt',\n", + " '17020/.0007.scavengerhunt',\n", + " '30351/.0021.scavengerhunt',\n", + " '40303/.0022.scavengerhunt',\n", + " '44639/.0005.scavengerhunt',\n", + " '45525/.0018.scavengerhunt',\n", + " '47222/.0016.scavengerhunt',\n", + " '47222/.0024.scavengerhunt',\n", + " '47830/.0010.scavengerhunt',\n", + " '49418/.0014.scavengerhunt',\n", + " '50896/.0011.scavengerhunt',\n", + " '55417/.0023.scavengerhunt',\n", + " '55685/.0020.scavengerhunt',\n", + " '60224/.0003.scavengerhunt',\n", + " '68848/.0004.scavengerhunt',\n", + " '70751/.0019.scavengerhunt',\n", + " '70985/.0017.scavengerhunt',\n", + " '88596/.0002.scavengerhunt',\n", + " '89338/.0013.scavengerhunt',\n", + " '91701/.0015.scavengerhunt',\n", + " '97881/.0009.scavengerhunt',\n", + " '98750/.0001.scavengerhunt']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total_archivos" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "85fdf8a6-d5a4-4d36-9268-fbdabc7e88bf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0006'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'15024/.0006.scavengerhunt'.split('.')[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "f94c97fe-c698-42a6-9495-53794d69427b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['98750/.0001.scavengerhunt',\n", + " '88596/.0002.scavengerhunt',\n", + " '60224/.0003.scavengerhunt',\n", + " '68848/.0004.scavengerhunt',\n", + " '44639/.0005.scavengerhunt',\n", + " '15024/.0006.scavengerhunt',\n", + " '17020/.0007.scavengerhunt',\n", + " '15534/.0008.scavengerhunt',\n", + " '97881/.0009.scavengerhunt',\n", + " '47830/.0010.scavengerhunt',\n", + " '50896/.0011.scavengerhunt',\n", + " '15534/.0012.scavengerhunt',\n", + " '89338/.0013.scavengerhunt',\n", + " '49418/.0014.scavengerhunt',\n", + " '91701/.0015.scavengerhunt',\n", + " '47222/.0016.scavengerhunt',\n", + " '70985/.0017.scavengerhunt',\n", + " '45525/.0018.scavengerhunt',\n", + " '70751/.0019.scavengerhunt',\n", + " '55685/.0020.scavengerhunt',\n", + " '30351/.0021.scavengerhunt',\n", + " '40303/.0022.scavengerhunt',\n", + " '55417/.0023.scavengerhunt',\n", + " '47222/.0024.scavengerhunt']" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_ordenada = sorted(total_archivos, key = lambda x: x.split('.')[1])\n", + "lista_ordenada" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "779b935d-93f1-465d-b7e1-be33828dd72d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'98750/.0001.scavengerhunt'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "path_ejemplo_file = lista_ordenada[0]\n", + "path_ejemplo_file" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "d1834e6e-90c8-40a5-adbd-b72a42359faf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "Hora reset Tue Mar 21 14:08:31 2023\n", + "Intentos restantes 4972\n", + "Intentos usados 28\n" + ] + } + ], + "source": [ + "result_file_content = consulta(f'{url_inicial}/{path_ejemplo_file}', True)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "21bf41b1-94a1-4950-bbd1-58b432c36f53", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'SW4K\\n'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str(result_file_content['content'])" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "662200a0-1206-42ce-87f7-c0cde3332917", + "metadata": {}, + "outputs": [], + "source": [ + "import base64" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "aa0668f4-d4d2-4626-b3a7-b51fa8862b52", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'In\\n'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "coded_string = result_file_content['content']\n", + "base64.b64decode(coded_string).decode('utf8')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "948896ba-b614-4955-ba89-f70a4f261fe5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'98750/.0001.scavengerhunt'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista_ordenada[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "3952cf01-d760-40d2-ad6f-64aa34c4a56c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n", + "200\n" + ] + } + ], + "source": [ + "contenidos = ''\n", + "for file in lista_ordenada:\n", + " result_file_content = consulta(f'{url_inicial}/{file}')\n", + " coded_string = result_file_content['content']\n", + " decoded_string = base64.b64decode(coded_string).decode('utf8')\n", + " contenidos += decoded_string" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "513af5dd-9812-49e7-9a7f-06896c84e194", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In data science, 80 percent of time spent is preparing data, 20 percent of time is spent complaining about the need to prepare data. \n" + ] + } + ], + "source": [ + "print(contenidos.replace('\\n',' '))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}