Last active
October 6, 2016 15:23
-
-
Save renoirb/e3f521b730dd07411b59 to your computer and use it in GitHub Desktop.
BrowserCompat API CLI
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
import os | |
import requests | |
from sets import Set | |
from types import ListType | |
import sys | |
from string import Template | |
supportLine = Template(' - $b ($v): $s') | |
featureLine = Template('== $feature ==') | |
if len(sys.argv) >= 1: | |
SLUG = sys.argv[1] | |
else: | |
SLUG = 'web-css-position' | |
ENTRY_CONTEXT_ROOT = '/api/v1/features?slug=' | |
BC_HOST = os.environ.get('BC_HOST', 'https://browsercompat.herokuapp.com') | |
# http://jakeaustwick.me/extending-the-requests-response-class/ | |
# https://github.com/kennethreitz/grequests | |
# http://docs.python-requests.org/en/latest/ | |
class Resource: | |
''' | |
A Python representation of a JSON API response body | |
''' | |
types = [ | |
"supports.version", | |
"supports.feature", | |
"supports.history_current", | |
"supports.history", | |
"features.sections", | |
"features.supports", | |
"features.parent", | |
"features.children", | |
"features.history_current", | |
"features.history", | |
"versions.browser", | |
"versions.supports", | |
"versions.history", | |
"versions.history_current", | |
"browsers.history", | |
"browsers.history_current", | |
"browsers.versions", | |
] | |
parents = Set([]) | |
keys = [] | |
type = '' | |
links = {} | |
relationships = {} | |
data = {} | |
meta = {} | |
def __init__(self, url): | |
for t in self.types: | |
self.parents.add(t[:t.find('.')]) | |
#print url | |
r = requests.get(url) | |
if r.status_code == 200: | |
if r.headers['content-type'].find('json') != -1: | |
dto = r.json() | |
self.keys = dto.keys() | |
self.links = dto['links'] | |
del dto['links'] | |
self.data = dto | |
for key in dto.keys(): | |
if self.isDataTypeElement(key) == True: | |
self.type = key | |
if type(dto[key]) is ListType: | |
self.data = dto[key][0] | |
else: | |
self.data = dto[key] | |
def getLinks(self, keyword): | |
if keyword in self.data['links']: | |
links = self.data['links'][keyword] | |
linkSelector = self.type + '.' + keyword | |
href = self.linkify(linkSelector) | |
out = [] | |
if type(links) is ListType: | |
for l in links: | |
out.append(href + l) | |
else: | |
out.append(href + links) | |
return out | |
def isDataTypeElement(self, check): | |
if len(self.parents) == 0: | |
raise Exception('Did you initialize this object first?') | |
return check in self.parents | |
def linkify(self, selector): | |
return self.links[selector]['href'].replace('{'+selector+'}','') | |
feature = Resource(BC_HOST + ENTRY_CONTEXT_ROOT + SLUG) | |
supports = feature.getLinks('supports') | |
print featureLine.substitute(feature=feature.data['name']) | |
for s in supports: | |
support = Resource(s) | |
versions = support.getLinks('version') | |
version = Resource(versions[0]) | |
browsers = version.getLinks('browser') | |
browser = Resource(browsers[0]) | |
print supportLine.substitute(b=browser.data['name']['en'], v=version.data['version'], s=support.data['support']) |
Author
renoirb
commented
Nov 11, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment