-
- Callable as
gifc
from cli viapip install gifc
as a script.(To be released in a week)
- Callable as
-
- Ability to just append to a gist.
-
Add support to update a file in a gist with the contents of a local file
-
Get gist by id (
gifc get -id ffg564g4hfh..
) -
Search gists based on keywords (display answer based on completeness of match) [
gifc search curry
]-- Search by title (
gifc search -t <title>
) - Search in description contents (
gifc search -d <description contents>
) - Search in gist contents (
gifc search -c <body content>
)
- Search by title (
-
- Use urllib instead of requests so that only standard libraries are used
- GETting the json data
import urllib.request url = 'https://jsonplaceholder.typicode.com/posts' r = urllib.request.urlopen(url).read() data = json.loads(r.decode('utf-8'))
- POSTing the resource
import json import urllib.request import urllib.parse d = {"spam": 1, "eggs": 2, "bacon": 0} #data = urllib.parse.urlencode(d) data = json.dumps(d) data = data.encode() req = urllib.request.Request("http://localhost:5000/random", data) req.add_header('Content-Type', 'application/json') req.add_header('Authorization', 12345) with urllib.request.urlopen(req) as f: print(f.read().decode('utf-8'))
- PATCHing the resource
req = urllib.request.Request("http://localhost:5000/random", data, method='PATCH') #req.get_method = lambda: 'PATCH' with urllib.request.urlopen(req) as f: print(f.read().decode('utf-8'))
- DELETEing the resource
req = urllib.request.Request("http://localhost:5000/random", data, method='DELETE') #req.get_method = lambda: 'DELETE' with urllib.request.urlopen(req) as f: print(f.read().decode('utf-8'))
-
- Improve the arguements.
-
- Make it more intuitive to use.
-
- Add Demo gif
-
Make it callable from cli itself i.e remove any
config.yml
files-
Environment Variables
$ export DUMMY="dum dum"
$ python3
>>> import os
>>> "DUMMY" in os.environ
True
>>> os.environ['DUMMY']
'dum dum'- Shell, shell and its processes, local, systemwide environment var setup
-
-
- Seeing the contents of the file you want to append to or edit.
POC
- Seeing the contents of the file you want to append to or edit.
import os
import requests
import tempfile
import subprocess
f = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
n = f.name
config = {'USER_ID': 'xxx', 'TOKEN': 'yyy'}
r = requests.get('https://api.github.com/gists/3a081894e8ea725dceb0f13db5a1f0c9')
content = r.json()['files']['tips.md']['content']
#print(content)
f.write(content)
f.close()
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
f.close()
os.remove(n)
-
- Creating gists from the editor
POC
- Creating gists from the editor
#!/usr/bin/env python3
import os
import tempfile
import subprocess
import argparse
import requests
import yaml
parser = argparse.ArgumentParser(description='Github gists from command line')
#Create gists
parser.add_argument('-c','--create', help='Full file name')
parser.add_argument('-d','--describe', help='Explain, elucidate or expound the gist')
parser.add_argument('-p','--public', help='Make a public gist', default=False)
#parser.add_argument('-m','--message', help='Gist contents as string')
#parser.add_argument('-i','--input', help='Input file name')
args = vars(parser.parse_args())
def get_config_details(key):
try:
with open('gist_config.yml', 'r') as f:
config = yaml.safe_load(f)
except:
print('Configuration file not found')
exit()
else:
if config.get(key):
return config[key]
else:
if key == 'TOKEN':
print('User token not found. Please add your token to gist_config.yaml')
exit()
elif key == 'USER_ID':
print('User ID not found. Please add your github user_id to gist_config.yml')
exit()
print('Configured...')
header = {"Authorization": f"Bearer {get_config_details('TOKEN')}"}
#./create.py -c "gist_file_name.md" -d "Some description for gist" -p True
f = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
f_name = f.name
f.close()
subprocess.run(['nano', f_name])
with open(f_name) as f:
contents = f.read()
payload = {'description': args['describe'], 'public': args['public'], 'files':{args['create']: {'content': contents}}}
r = requests.post(f"https://api.github.com/gists", headers=header, json=payload)
if r.status_code == 201:
print(r.status_code)
print(r.json()['id'])
print('Gist successfully created')
else:
print('Creating gist failed')
-
- Get method doesn't work for private gists. May have to use OAuth for that.
-
- Requests raises exceptions inherited from RequestException that you are not catching. So commands like
r = request.post(....)
can fail on bad internet connections or because other random issues. Catch them like -ORexcept requests.exceptions.RequestException as e: # This is the correct syntax print e
except requests.exceptions.Timeout: # Maybe set up for a retry, or continue in a retry loop except requests.exceptions.TooManyRedirects: # Tell the user their URL was bad and try a different one except requests.exceptions.RequestException as e: # catastrophic error. bail. print e
- Requests raises exceptions inherited from RequestException that you are not catching. So commands like
- Handle FileNotFoundError in creating of gists via file parameter.
- Instead of using
__file__
as infoo_config = open(os.path.join(os.path.dirname(__file__),'foo.conf').read()
orcwd = os.path.dirname(os.path.abspath(__file__))
, usepkg_resources
instead. Help1, Help2 - Displaying gists in cli
- Update is still hardcoded with
nano
. Can we open it in system default editor? - If no file arguement and description is given in update then open the first file in that gist in interactive mode for editing ?
- Add support for creating multiple file gists
- Add support to update multiple files in a gist