Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: e247db2e42
Fetching contributors…

Cannot retrieve contributors at this time

93 lines (69 sloc) 2.28 KB
from flask import Flask, jsonify, abort
import os
import requests
import json
import re
repo_url = "{}/{}"
branch_url = "{}/{}/branches"
tree_url = "{}/{}/git/trees/{}"
blob_url = "{}/{}/git/blobs/{}"
'name': 'MIT license (MIT)',
'text': open("licenses/mit.txt").read(),
'name': 'ISC License (ISC)',
'text': open("licenses/isc.txt").read(),
def get_branch(repo, branches):
for branch in branches:
if branch['name'] == repo.get('master_branch', 'master'):
return branch
raise KeyError("No branch found")
def find_license_file(tree):
for blob in tree['tree']:
if blob['path'].startswith("LICENSE"):
return blob
raise KeyError("No LICENSE file found")
def license_type(blob):
blob = re.sub(r"\s", "", blob)
print blob
for license in LICENSES:
print re.sub(r"\s", "", license['text'])
if re.sub(r"\s", "", license['text']) in blob:
return license['name']
return "Unknown License"
def get(*args, **kwargs):
resp = requests.get(*args, **kwargs)
data = json.loads(resp.text)
if not resp.ok:
raise ValueError("Github API Error: {}".format(data['message']))
return data
def get_license(user, repo_name):
repo = get(repo_url.format(user, repo_name))
branches = get(repo['url'] + "/branches")
branch = get_branch(repo, branches)
tree = get(tree_url.format(user, repo_name, branch['commit']['sha']))
license = find_license_file(tree)
resp = requests.get(license['url'],
headers={'Accept': 'application/vnd.github.v3.raw'})
return license_type(resp.text)
app = Flask(__name__)
def index_with_url(user, repo):
license_type = get_license(user, repo)
except (KeyError, ValueError) as e:
resp = jsonify(message=unicode(e))
resp.status_code = 400
return resp
return jsonify({
'user': user,
'repo': repo,
'license': license_type,
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))'', port=port, debug=True)
Jump to Line
Something went wrong with that request. Please try again.