Skip to content

Instantly share code, notes, and snippets.

@citrus-lemon
Created December 3, 2016 10:14
Show Gist options
  • Save citrus-lemon/0c83582d65880452f9da9e0ad5cb55d9 to your computer and use it in GitHub Desktop.
Save citrus-lemon/0c83582d65880452f9da9e0ad5cb55d9 to your computer and use it in GitHub Desktop.
github.py
from requests import request
from pprint import pprint as p
import re, json
"GitHub API Python"
import configparser
config = configparser.ConfigParser()
config.read('config.txt')
API = config['github']['api']
# common methods
def doc(fun):
print(fun.__doc__)
def url(l):
p = re.compile('^https:\/\/api\.github\.com\/', re.IGNORECASE)
q = re.compile('^\/?')
if p.match(l):
return l
else:
return q.sub('https://api.github.com/', l)
def req(location = '', type = 'GET', api = True, data = {}, params = {}):
headers = {'Authorization': 'token ' + API} if api else {}
r = request(type, url(location), headers = headers, data = json.dumps(data), params = params)
try:
return r.json()
except:
return r.text
print('GitHub API...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment