vladimyr's notebooks

  • JSON Endpoint Example 1 - /vladimyr/json-endpoint-example-1
    Last edited 4 months ago
    Send a request to a URL like https://tonicdev.io/tonic/json-endpoint-example-1/branches/master?url=http://google.com, get back the title of the page in the url query parameter.
  • .profile - /vladimyr/my.profile
    Last edited 4 months ago
    Something to test keyoxide verification: [Verifying my OpenPGP key: openpgp4fpr:...]
  • MetaWeather - /vladimyr/metaweather
    Last edited 4 months ago
    /** * https://www.metaweather.com/api/#location * * USAGE: * https://badgen.net/runkit/vladimyr/metaweather/44418/state * https://badgen.net/runkit/vladimyr/metaweather/44418/temperature * https://badgen.net/runkit/vladimyr/metaweather/44418/temperature/f * https://badgen.net/runkit/vladimyr/metaweather/44418/wind * https://badgen.net/runkit/vladimyr/metaweather/44418/wind/mph * https://badgen.net/runkit/vladimyr/metaweather/44418/humidity */ const axios = require('axios') const { send } = require('micro') const client = axios.create({ baseURL: 'https://www.metaweather.com/api/' }) exports.endpoint = async function (req, res) { const args = req.url.split('/').filter(Boolean) if (args.length < 2) { return send(res, 400, { subject: 'metaweather', status: 'malformed args', color: 'grey' }) } try { send(res, 200, await getWeather(...args)) } catch (err) { send(res, err.response ? err.response.status : 400, { subject: 'metaweather', status: 'invalid', color: 'grey' }) } } async function getWeather(woeid, topic, ...args) { const { title, consolidated_weather: info } = await client.get(`/location/${woeid}`).then(res => res.data) switch (topic) { case 'state': return { subject: `${title} (state)`, status: info[0].weather_state_name, color: 'green' } case 'temp': case 'temperature': { const subject = `${title} (temp)` const color = 'green' const temp = info[0].the_temp const unit = args[0] if (unit === 'f') { const value = Math.round(temp * 9/5) + 32 const status = `${value}°F` return { subject, status, color } } const value = Math.round(temp) const status = `${value}°C` return { subject, status, color } } case 'wind': { const subject = `${title} (wind)` const color = 'green' const directions = { N: '↓', NNE: '↓', NE: '↙', ENE: '←', E: '←', ESE: '←', SE: '↖', SSE: '↑', S: '↑', SSW: '↑', SW: '↗', WSW: '→', W: '→', WNW: '→', NW: '↘', NNW: '↓' } const direction = directions[info[0].wind_direction_compass] const speed = info[0].wind_speed const unit = args[0] if (unit === 'mph') { const value = Math.round(speed) const status = `${direction}${value}mph` return { subject, status, color } } const value = Math.round(speed * 1.609) const status = `${direction}${value}km/h` return { subject, status, color } } case 'humidity': return { subject: `${title} (humidity)`, status: info[0].humidity, color: 'green' } } return { subject: 'metaweather', status: 'unknown', color: 'grey' } }
  • jsonfeed2atom - /vladimyr/jsonfeed2atom
    Last edited 2 years ago
    const axios = require('axios'); const jsonfeed2atom = require('jsonfeed-to-atom'); const { send } = require('micro'); exports.endpoint = async function (req, res) { send(res, 200, req.url); };
  • Total stars count (Codeberg + Github) - /vladimyr/total-stars
    Last edited 3 years ago
    /** * https://codeberg.org/api/v1/repos/teddit/teddit * https://api.github.com/repos/teddit-net/teddit * * USAGE: * https://badgen.net/runkit/vladimyr/total-stars/codeberg;owner=teddit;repo=teddit/github;owner=teddit-net;repo=teddit * */ const ghGot = require('gh-got'); const got = require('got'); const { millify } = require('millify'); const { send } = require('micro'); const cbGot = got.extend({ prefixUrl: 'https://codeberg.org/api/v1', responseType: 'json' }); exports.endpoint = async function (req, res) { const groups = req.url.split('/').filter(Boolean); if (groups.length < 2) { return send(res, 400, { subject: 'total stars', status: 'malformed args', color: 'grey' }); } const stars = await Promise.all(groups.slice(0, 2).map(args => { const [host, ...params] = args.split(';'); const { owner, repo } = Object.fromEntries(params.map(it => it.split('='))); return fetchStars(host, owner, repo); })); const totalStars = stars.reduce((acc ,val) => acc + val); return send(res, 200, { subject: `total stars`, status: millify(totalStars), color: 'blue' }); } async function fetchStars(host, owner, repo) { switch (host) { case 'gh': case 'github': { const { body: data } = await ghGot(`repos/${owner}/${repo}`); return data.stargazers_count; } case 'cb': case 'codeberg': { const { body: data } = await cbGot(`repos/${owner}/${repo}`); return data.stars_count; } } }
  • matrix params - /vladimyr/matrix-params
    Last edited 3 years ago
    const matrixParser = require('matrix-parser/lib/parsers/default-matrix-parser'); const url = 'https://image-service.test/;width=480;height=320/;quality=0.9;blur=true' const { pathname } = new URL(url); matrixParser(pathname)
  • string methods vs regex - /vladimyr/string-methods-vs-regex
    Last edited 3 years ago
    const { Benchmark } = require("benchmark"); const suite = new Benchmark.Suite(); // from query const context = 'dockercloud'; // from gh api const status = { context: 'ci/dockercloud' }; suite.add("string methods", function() { status.context.toLowerCase().includes(context.toLowerCase()); }); suite.add("regex", function () { const pattern = new RegExp(context, 'i'); pattern.test(status.context); }); suite.on("cycle", function(event) { console.log(String(event.target)); }); suite.on("complete", function() { console.log("Fastest is " + this.filter("fastest").map("name")); }) suite.run();