Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# Search commands in history
set -e -u -o pipefail
if [ "$#" -lt 1 ]; then
echo "Usage: $0 wordToMatch anotherWordToMatch -skipIfWordMatches +wordToMatchCaseSensitive ..."
exit 1
fi
command="cat ~/.bash_history"
@tokland
tokland / google-timeline-download.sh
Last active March 7, 2024 12:38
Get KML of Google Maps Timeline for a date range
#!/bin/bash
set -e -u -o pipefail
# Usage: Export a cookies.txt file from a browser logged-in in timeline.google.com with some
# add-on/extension (i.e. Export cookies, Get cookies.txt). Now run the script for the
# desired period:
#
# $ bash google-timeline-download.sh cookies.txt 2022-01-01 2022-02-20 out.kml
#
# Dependencies: curl, perl-xml-twig.
@tokland
tokland / ar_arel_wrapper.rb
Last active March 5, 2024 06:22
Simple wrapper over arel
require 'active_record'
require 'arel'
# Ruby-like syntax in AR conditions using the underlying Arel layer (Rails >= 3.0).
#
# What you would usually write like this:
#
# User.where(["users.created_at > ? AND users.name LIKE ?", Date.yesterday, "Mary"])
#
# can now be written like this (note those parentheses required by the operators precedences):
@tokland
tokland / puppeteer-click-by-text.js
Last active August 11, 2023 03:48
Click link by text in Puppeteer
const puppeteer = require('puppeteer');
const escapeXpathString = str => {
const splitedQuotes = str.replace(/'/g, `', "'", '`);
return `concat('${splitedQuotes}', '')`;
};
const clickByText = async (page, text) => {
const escapedText = escapeXpathString(text);
const linkHandlers = await page.$x(`//a[contains(text(), ${escapedText})]`);
@tokland
tokland / action_dispatch_extensions.rb
Created September 20, 2011 13:52
How to add locale scope to i18n_routing
class ActionDispatch::Routing::Mapper
def localize_and_scope_for(locales, options = {}, &block)
scoped_locales = locales - Array(options[:skip_scope])
localized(locales) do
locale_regexp = Regexp.new(scoped_locales.join('|'))
scope("/:i18n_locale", :constraints => {:i18n_locale => locale_regexp}) do
yield
end
yield if options[:skip_scope]
#!/bin/bash
set -e -u -o pipefail
run() {
for version in "$@"; do
local url="https://play.dhis2.org/$version/api"
echo "# $version"
curl -sS -u 'admin:district' -L "$url/events.json?event=QsAhMiZtnl2&fields=*" | jq >events.json
@tokland
tokland / SelectedPick.ts
Last active June 26, 2022 21:21
SelectorPick<Model, Selector>: An exhanced version of Pick<T, Keys> to select nested properties from T
/* SelectedPick<T, Selector>
An extended version of Pick<T, Key> where, instead of a union of keys,
you pass an object with the properties to get from a type. */
type Extends<T1, T2> = [T1] extends [T2] ? true : false
type OmitNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>
export type Selector<Model> = {
[Key in keyof Model]?: boolean | NestedSelectorValue<Model[Key]>
@tokland
tokland / rac1-alacarta-userscript.js
Created April 2, 2022 16:01
Add forward/backward buttons to RAC1 A la Carta web player [tampermonkey] [greasemonkey] [userscripts]
// ==UserScript==
// @name rac1-alacarta
// @description Add back/forward buttons to audio player
// @namespace https://github.com/tokland
// @homepage https://github.com/cvzi/rollup-userscript-template
// @author tokland
// @match https://www.rac1.cat/a-la-carta*
// @icon https://www.google.com/s2/favicons?sz=64&domain=rac1.cat
// @run-at document-start
// @version 0.0.1
@tokland
tokland / escape-xpath-string.js
Last active March 31, 2022 23:07
Escape xpath string using concat
function escapeXpathString(str) {
const splitedQuotes = str.replace(/'/g, `', "'", '`);
return `concat('${splitedQuotes}', '')`;
}
@tokland
tokland / flask_cors_proxy.py
Created March 21, 2022 11:45
Flask CORS proxy redirector
import json
import requests
import urllib
from flask import Flask, Response, stream_with_context, request
app = Flask(__name__)
@app.route('/<path:url>', methods=["GET", "POST", "PUT", "DELETE"])
def proxy(url):