diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..9349a0e --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "id": "8492734d-e7ba-4732-b1bf-596956811d9b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting python-dotenv\n", + " Downloading python_dotenv-0.20.0-py3-none-any.whl (17 kB)\n", + "Installing collected packages: python-dotenv\n", + "Successfully installed python-dotenv-0.20.0\n", + "\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", + "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "2075b6f7-9c53-454b-a6af-818f223f24b4", + "metadata": {}, + "outputs": [], + "source": [ + "'''\n", + "# template .env file\n", + "token='ghp_'\n", + "gist_id='abc...123'\n", + "filename='.txt'\n", + "'''\n", + "\n", + "# %pip install python-dotenv\n", + "\n", + "from dotenv import load_dotenv\n", + "import requests\n", + "import json\n", + "import os\n", + "\n", + "\n", + "### write current machine ip address to gist file\n", + "\n", + "# read env vars\n", + "# S/O https://stackoverflow.com/a/61029741\n", + "load_dotenv()\n", + "token = os.getenv('token')\n", + "gist_id = os.getenv('gist_id')\n", + "filename = os.getenv('filename')\n", + "\n", + "# fetch external ip address\n", + "# S/O https://stackoverflow.com/a/36205547\n", + "ip_addr = requests.get('https://api.ipify.org').content.decode('utf8')\n", + "\n", + "# request gist changes\n", + "# S/O https://stackoverflow.com/a/65761251\n", + "headers = {'Authorization': f'token {token}'}\n", + "request_url = f'https://api.github.com/gists/{gist_id}' \n", + "request_data = json.dumps({'files':{filename:{\"content\":ip_addr}}})\n", + "\n", + "r = requests.patch(\n", + " url=request_url,\n", + " data=request_data,\n", + " headers=headers)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "fcda799c-3feb-4591-9582-f6187994fe36", + "metadata": {}, + "outputs": [], + "source": [ + "### fetch gist file content\n", + "\n", + "headers = {'Authorization': f'token {token}'}\n", + "request_url = f'https://api.github.com/gists/{gist_id}' \n", + "request_data = json.dumps({'files':{filename:{\"content\":ip_addr}}})\n", + "r = requests.get(\n", + " url=request_url,\n", + " data=request_data,\n", + " headers=headers)\n", + "gist_content = r.json()['files'][filename.strip()]['content']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "07a9cbfe-8ab1-42d0-9810-cf2b68c97b5d", + "metadata": {}, + "outputs": [], + "source": [ + "gist_content" + ] + } + ], + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}