Created
December 3, 2016 10:14
-
-
Save citrus-lemon/0c83582d65880452f9da9e0ad5cb55d9 to your computer and use it in GitHub Desktop.
github.py
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
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