Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active October 6, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/e3f521b730dd07411b59 to your computer and use it in GitHub Desktop.
Save renoirb/e3f521b730dd07411b59 to your computer and use it in GitHub Desktop.
BrowserCompat API CLI
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'])
@renoirb
Copy link
Author

renoirb commented Oct 30, 2015

Terminal output sample

python read.py web-css-position
== position ==
 - Chrome for Desktop (1.0): yes
 - Firefox for Android (1.0): yes
 - Firefox for Desktop (1.0): yes
 - Internet Explorer for Desktop (4.0): yes
 - Opera for Desktop (4.0): yes
 - Safari for Desktop (1.0): yes
 - Safari for iOS (7.0): yes

@renoirb
Copy link
Author

renoirb commented Nov 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment