Published
Edited
Apr 14, 2019
1 fork
Importers
8 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
corsBypasserUrl= "https://cors-anywhere.herokuapp.com/"
Insert cell
function load(url,type){
return new Promise((resolve,reject)=>{
var promisedData = getData(url,type);
promisedData.then(function(result){
resolve(result);
})
.catch(function(e){
resolve(getData(corsBypasserUrl+url,type))
});
})
}
Insert cell
async function requireFromGithub(jsFileUrl,prop){
const response = await fetch(jsFileUrl);
const blob = await response.blob();
return require(URL.createObjectURL(blob)).catch(() => {return window[prop]});
}
Insert cell
Insert cell
function scrape(url,type){
return new Promise((resolve,reject)=>{
var textPromise = load(url,'text');
textPromise.then(text=>{
var div = document.createElement('div');
div.innerHTML=`<div>${text}</div>`
// div.innerHTML = text;
if(type=="dom"||type=="object"){
resolve(div.querySelector('div'));
}
resolve(div);
})
})
}
Insert cell
function getData(url,type){
var convertedUrl = convert(url,type);
if(type){
convertedUrl.type=type;
}
if(convertedUrl.isHttp){
return getData(corsBypasserUrl+url,type);
}
if(convertedUrl.isSvg){
return loadSvg(url,type);
}
if(convertedUrl.isSpreadSheet){
return loadGoogleSpreadsheet(url,type);
}
if(convertedUrl.isExcel){
return loadExcellFile(url,type);
}
if(convertedUrl.isJsonLike){
return d3.json(url);
}
if(!!d3[convertedUrl.type]){
return d3[convertedUrl.type](url);
}
if(d3[convertedUrl.extension]){
return d3[convertedUrl.extension](url);
}
return d3.json(url)
}
Insert cell
function loadSvg(url){
function stripXmlPreamble( str ) {
return str .replace( /^<\?xml version='1.0'\?>/, '' ) .replace(/^\n/,'')
}
const textPromise = fetch(url).then( response => response.text() );
return new Promise((res,rej)=>{
debugger;
textPromise.then(text=>{
res( svg`${stripXmlPreamble( text )}` )
})
})
}
Insert cell
function loadGoogleSpreadsheet(url,isNotSimpleSheet){
return new Promise((resolve,reject)=>{
Tabletop.init(
{
key: url,
callback: function(data){
resolve(data);
},
simpleSheet: isNotSimpleSheet?false:true })
})
}
Insert cell
function loadExcellFile (url){
return new Promise((resolve,reject)=>{
var req = new window.XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";

req.onload = function(e) {
var data = new Uint8Array(req.response);
var workbook = XLSX.read(data, {type:"array"});
resolve(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]))
}
req.send();
})
}
Insert cell
Insert cell
XLSX = require('https://unpkg.com/xlsx@0.12.9/dist/xlsx.full.min.js')
.then(d=>{
return window.XLSX;
})
Insert cell
d3 = require("d3-fetch")
Insert cell
Tabletop = require('tabletop')
Insert cell
Insert cell
function convert (url,type){
return {
endsWith:endsWith(url,type),
url:url,
isSpreadSheet:isSpreadsheet(url,type),
isExcel:isExcel(url,type),
isJsonLike:isJsonLike(url,type),
isHttp:isHttp(url,type),
extension:extension(url,type)
}
}
Insert cell
function endsWith(url){
var matched = url.match(/\.([a-zA-Z]*)$/g)
if(!matched) return
return matched[0];
}
Insert cell
function isSpreadsheet(url){
var matched = url.match(/docs\.google\.com\/spreadsheets\//g)
if(!matched) return false
return !!matched[0];
}
Insert cell
excellExtensions = ['xlsx','xml','.xlsx','.xml']
Insert cell
function isExcel(url,type){
var fileExtension = endsWith(url);
if(!fileExtension) return false;
var files=excellExtensions;
return files.includes(fileExtension)||files.includes(type)
}
Insert cell
jsonExtensions = [".json",".topojson",".geojson"]
Insert cell
function isJsonLike(url){
var fileExtension = endsWith(url);
if(!fileExtension) return false;
var files=jsonExtensions;
return files.includes(fileExtension)
}
Insert cell
function isHttp(url){
var matched = url.match(/^http:/g)
if(!matched) return false
return !!matched[0];
}
Insert cell
function extension(url){
var matched = url.match(/([a-zA-Z]*)$/g)
if(!matched) return
return matched[0];
}
Insert cell
Insert cell
urls = [
"https://gist.githubusercontent.com/vorth/3363aceb1dad0f521bcf9fd92b0e148c/raw/cd7812b655f4516b5cefe6f6adba838cb8a6b534/vZome-lifelike-ball.svg",
"https://docs.google.com/spreadsheets/d/1GT9RMEV___rCtJvpdcThMQ62pKMrejx1BHsMO_Mrna0/edit#gid=0",
"https://raw.githubusercontent.com/bumbeishvili/data-house/master/geo/mosaxleoba.xml",
"https://raw.githubusercontent.com/bumbeishvili/geojson-georgian-regions/master/geo_regions.topojson",
"https://raw.githubusercontent.com/bumbeishvili/geojson-georgian-regions/master/geo_regions.geojson",
"https://gist.githubusercontent.com/mbostock/4063570/raw/11847750012dfe5351ee1eb290d2a254a67051d0/flare.csv",
"https://raw.githubusercontent.com/bumbeishvili/resume.json/master/resume.json",
"http://www.nbg.ge/rss.php",
]
Insert cell
Insert cell
converted= JSON.stringify(urls.map(convert),null,' ')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more