Skip to content

Instantly share code, notes, and snippets.

@johnholdun
johnholdun / benchmark.js
Created September 27, 2023 00:38
Benchmarking idempotent `CREATE IF NOT EXISTS` sqlite statements
import fs from 'fs'
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
const filename = './database.db'
if (fs.existsSync(filename)) {
fs.unlinkSync(filename)
}
@johnholdun
johnholdun / sinatra-rails.rb
Last active December 18, 2022 06:59
Using ActionController inside a Sinatra App
require 'rubygems'
require 'sinatra/base'
# ActionPack is a gem that is part of Rails. It includes ActionController and ActionView
require 'action_pack'
# Rack::Test is not used in this file but ActionController will yell if we don't require it
require 'rack/test'
# This is part of the ActionPack gem
@johnholdun
johnholdun / scrape-instagram-feed.js
Created December 15, 2021 20:50
log in to instagram in your browser, then run this in the console. use at your own risk! works as of 2021-12-15 but is likely to break!
;(async () => {
const PATH = 'https://www.instagram.com/graphql/query/'
const QUERY_HASHES = {
list: '8c2a529969ee035a5063f2fc8602a0fd',
post: '7d4d42b121a214d23bd43206e5142c8c'
}
const get = (query, args) => {
const queryParams = { query_hash: QUERY_HASHES[query], variables: JSON.stringify(args) }
@johnholdun
johnholdun / literal-club-feed.rb
Created August 20, 2021 17:31
Get a list of your updates to literal.club. There's no guarantee this will continue to work over time!
require 'date'
require 'json'
require 'net/http'
require 'uri'
# This should be your username
USERNAME = 'johnholdun'.freeze
QUERY = <<-QUERY.freeze
query getBooks($username: String) {
@johnholdun
johnholdun / scarcity.rb
Created March 10, 2021 23:32
Track Zach Gage's Scarcity. Run this every day or you risk losing it forever.
require 'date'
require 'digest'
require 'net/http'
ledger = (DATA || '').each_line.map { |l| l.strip.split(' ') }.to_h
def url_for(token)
"http://stfj.net/scarcity/#{token}"
end
@johnholdun
johnholdun / h.js
Created January 13, 2021 16:31
who needs jsx?
function h(tag, props = {}, children = null) {
if (arguments.length === 2 && props && (typeof props !== 'object' || Array.isArray(props))) {
children = props
props = {}
}
const inner = children ? [].concat(children).join('') : ''
if (!tag) {
return inner
@johnholdun
johnholdun / imgur-bookmarklet.js
Created April 9, 2011 22:43
Bookmarklet for uploading the image currently loaded in your browser to imgur
// that is, navigate to the direct URL for an image hosted on the world wide web, then >>>INVOKE<<<
// i wanted the bookmarklet to redirect to the new direct image URL but that was out of scope!
javascript:void(window.location="http://api.imgur.com/2/upload.json?url="+encodeURIComponent(window.location.href));
@johnholdun
johnholdun / create_campaign.rb
Created May 11, 2019 03:20
Creates a Mailchimp campaign based on a URL. Useful if you tend to deliver your most recent blog post or whatever but aren't satisfied with the RSS campaign feature.
require 'rest-client'
require 'json'
require 'yaml'
require 'opengraph_parser'
require 'open-uri'
# Generate a new Mailchimp campaign from a page one your website. I used to use
# this with a purpose-built template in my static site generator that
# automatiacally set up my most recent post as an email, just the way I liked
# it. You'll probably need to make some changes to suit your needs but you're
@johnholdun
johnholdun / upload.rb
Created May 11, 2019 03:13
Sync your locally-generated, git-tracked static website to S3
require 'rubygems'
require 'active_support/all'
require 'date'
require 'aws-sdk'
require 'pry'
# If you have a static website that you generate locally, track with git, and
# push to S3, you can use this script to incrementally push new files to your
# bucket. It worked for me when johnholdun.com was hosted with Amazon but use it
# at your own risk!
@johnholdun
johnholdun / mastodon-parser.rb
Created April 19, 2017 16:20
Turn a Mastodon permalink URL into a one-line plain text status
require 'nokogiri'
require 'open-uri'
require 'upmark'
require 'cgi'
require 'uri'
class TootReader
ACTIVITY_NAMESPACE = 'http://activitystrea.ms/spec/1.0/'.freeze
COMMENT_TYPE = 'http://activitystrea.ms/schema/1.0/comment'.freeze