-
Notifications
You must be signed in to change notification settings - Fork 0
/
Github Commit Time Machine
1 lines (1 loc) · 4.07 KB
/
Github Commit Time Machine
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyPYvDdKagAw9U1TaDjh6tS1"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# TIME MACHINE"],"metadata":{"id":"wy35CqQlAvDX"}},{"cell_type":"code","source":["import requests\n","import base64\n","import csv"],"metadata":{"id":"vbWBRazFAqrA"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Function to connect with github api"],"metadata":{"id":"QNe-5nYCcpgx"}},{"cell_type":"code","source":["def req(url):\n"," response = requests.get(url, headers={\n"," 'Authorization': 'Bearer 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',\n"," 'X-GitHub-Api-Version': '2022-11-28',\n"," 'Accept': 'application/vnd.github+json'\n"," }) \n"," return response.json()"],"metadata":{"id":"Q2iwyHyaOw5g"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Extract content from each commits"],"metadata":{"id":"7YtnxbljcVwm"}},{"cell_type":"code","source":["commits = req('https://api.github.com/repos/Kachiiee/Presh/commits')\n","\n","# Print the url to each commit messages\n","contents = []\n","count = 1\n","for commit in commits:\n"," print(f'processing commit {count}')\n"," timestamp = commit['commit']['author']['date']\n"," author = commit['commit']['author']['name']\n"," url = commit['commit']['tree']['url']\n"," # Get the blob from the commit tree\n"," print(f'extracting content from commit {count}')\n"," blob = req(url)\n"," # Get the blob url\n"," blob_url = blob['tree'][0]['url']\n"," # Get the blob info\n"," blob_info = req(blob_url)\n"," content = blob_info['content']\n"," # Save the information to a dictionary\n"," print(f'contents from commit {count} saved!')\n"," contents.append({'timestamp': timestamp, 'content': content, 'author': author})\n"," count += 1"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"zhKJMvLMB3y6","executionInfo":{"status":"ok","timestamp":1679844416085,"user_tz":-60,"elapsed":4896,"user":{"displayName":"Abdulbasit Abdullahi","userId":"18437898134401891312"}},"outputId":"c66d9af0-26d6-4441-fc94-f6ec670bb6c6"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["processing commit 1\n","extracting content from commit 1\n","contents from commit 1 saved!\n","processing commit 2\n","extracting content from commit 2\n","contents from commit 2 saved!\n","processing commit 3\n","extracting content from commit 3\n","contents from commit 3 saved!\n","processing commit 4\n","extracting content from commit 4\n","contents from commit 4 saved!\n","processing commit 5\n","extracting content from commit 5\n","contents from commit 5 saved!\n","processing commit 6\n","extracting content from commit 6\n","contents from commit 6 saved!\n"]}]},{"cell_type":"markdown","source":["Decode the content of the file at each commit and write to csv\n","\n","---\n","\n"],"metadata":{"id":"ut0OWAAdci96"}},{"cell_type":"code","source":["with open('timemachine.csv', 'w') as file:\n"," csv_writer = csv.writer(file, delimiter=\",\")\n"," print('creating a csv file...')\n"," csv_writer.writerow(['CONTENT', 'TIMESTAMP', 'AUTHOR'])\n"," i = 0\n"," for content in contents:\n"," base64_byte = base64.b64decode(content.get('content'))\n"," print('writing to a csv file..')\n"," csv_writer.writerow([base64_byte.decode('utf8'), content.get('timestamp'), content.get('author')])\n","print('Time machine saved successfully!')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"cMNzM_BbcECv","executionInfo":{"status":"ok","timestamp":1679844774313,"user_tz":-60,"elapsed":498,"user":{"displayName":"Abdulbasit Abdullahi","userId":"18437898134401891312"}},"outputId":"623534e5-1c4c-4ec3-c4ca-65e534a5f959"},"execution_count":77,"outputs":[{"output_type":"stream","name":"stdout","text":["creating a csv file...\n","writing to a csv file..\n","writing to a csv file..\n","writing to a csv file..\n","writing to a csv file..\n","writing to a csv file..\n","writing to a csv file..\n","Time machine saved successfully!\n"]}]}]}