Last active
April 15, 2018 05:39
-
-
Save IamTails/2ad11ab4488abdf8f5b9b2ec88f00e58 to your computer and use it in GitHub Desktop.
Create gist example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3.6 | |
| __author__ = "Tails" | |
| __email__ = "Tails@nekos.life" | |
| import asyncio | |
| import inspect | |
| import aiohttp | |
| async def create_gist(description, filename, content, public, token, session): | |
| api_uri = "https://api.github.com/gists" | |
| headers = { | |
| "Authorization": f"token {token}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "description": description, | |
| "public": public, | |
| "files": { | |
| filename: { | |
| "content": content | |
| } | |
| } | |
| } | |
| try: | |
| r = await session.post(api_uri, headers=headers, json=payload) | |
| if r.status >= 200: | |
| data = await r.json() | |
| if 'message' in data: | |
| # some error like Bad credentials | |
| return data | |
| elif 'html_url' in data: | |
| # gist link | |
| return data['html_url'] | |
| else: | |
| return | |
| else: | |
| return | |
| except aiohttp.client_exceptions: | |
| return | |
| if __name__ == '__main__': | |
| loop = asyncio.get_event_loop() | |
| client_session = aiohttp.ClientSession(loop=loop) | |
| github_token = "" # your github token https://github.com/settings/tokens | |
| gist_filename = "create_gist.py" | |
| gist_description = "Create gist example" | |
| gist_content = inspect.getsource(create_gist) | |
| public_gist = True | |
| print(loop.run_until_complete( | |
| create_gist(gist_description, gist_filename, gist_content, public_gist, github_token, client_session) | |
| )) | |
| client_session.close() | |
| loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment