Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / bookmarks.rb
Last active February 8, 2026 23:43
How to fetch a user's bookmarks with the X API as of February 2026
# Fetch X bookmarks and output them as CSV.
#
# On first run, opens a browser for OAuth 2.0 authorization and stores
# tokens in tokens-USERNAME.json. Subsequent runs reuse (and auto-refresh)
# those tokens, so no further login is needed. Multiple users are supported
# via separate token files.
#
# Usage:
# ruby bookmarks.rb [--username USER] [--limit N] [--per-call N]
#
@peterc
peterc / mp3-to-text
Created February 7, 2026 01:19
Transcribe MP3 files using Mistral's Voxtral API with speaker diarization
#!/usr/bin/env python3
import json
import os
import sys
from mistralai import Mistral
def format_time(seconds):
minutes = int(seconds) // 60
@peterc
peterc / transcribe
Created February 6, 2026 12:48
Transcribe audio files to plain text using OpenAI Whisper API. Compresses via ffmpeg (mono, 22kHz, 64kbps) and trims if over 25MB.
#!/usr/bin/env bash
set -euo pipefail
if [ -z "${OPENAI_API_KEY:-}" ]; then
echo "Error: OPENAI_API_KEY is not set" >&2
exit 1
fi
if [ $# -ne 1 ] || [ ! -f "$1" ]; then
echo "Usage: transcribe <audio-file>" >&2
@peterc
peterc / macos_app.rb
Created February 4, 2026 20:34
A native macOS app written in Ruby using FFI to call libobjc, etc.
#!/usr/bin/env ruby
# A macOS GUI app written in Ruby, calling libobjc directly via Fiddle.
# No gems. No Objective-C. Just Ruby and the runtime.
require "fiddle"
OBJC = Fiddle.dlopen("/usr/lib/libobjc.A.dylib")
APPKIT = Fiddle.dlopen("/System/Library/Frameworks/AppKit.framework/AppKit")
# Core runtime functions
@peterc
peterc / helloworld.c
Created February 4, 2026 20:26
A macOS app entirely in plain C
// A macOS GUI app written in pure C, using the Objective-C runtime directly.
// No Objective-C syntax anywhere. Just raw objc_msgSend and runtime calls.
// This is cursed. Enjoy.
// Compile with clang -Wall -Wextra -o hello_runtime hello_runtime.c \
// -framework Cocoa -lobjc
#include <objc/objc.h>
#include <objc/message.h>
#include <objc/runtime.h>
@peterc
peterc / image.rb
Created November 17, 2025 15:41
Produce images with gemini-2.5-flash-image (Nano Banana) with RubyLLM 1.9.1
# Just because the example in the docs doesn't seem to work outside of a Rails app
require 'ruby_llm'
RubyLLM.configure do |config|
config.gemini_api_key = ENV['GEMINI_API_KEY']
end
chat = RubyLLM.chat(model: "gemini-2.5-flash-image")
reply = chat.ask("Cyberpunk city street at night, raining, neon signs")
@peterc
peterc / openai.rb
Last active August 8, 2025 16:49
Calling GPT-5 from a plain Ruby script using the official OpenAI library
require "openai"
client = OpenAI::Client.new # assumes valid key in OPENAI_API_KEY
begin
resp = client.chat.completions.create(
model: :"gpt-5-nano",
reasoning_effort: :high,
verbosity: :low,
messages: [{ role: "user", content: "Tell me a joke" }]
@peterc
peterc / make_palette.py
Created August 2, 2025 16:25
Generate color palettes by generating AI images and extracting the colors
import sys
import requests
import replicate
from Pylette import extract_colors
# YOU NEED A REPLICATE API TOKEN IN 'REPLICATE_API_TOKEN'
prompt = sys.argv[1] if len(sys.argv) > 1 else "sandy beach"
output = replicate.run(
@peterc
peterc / openai-openrouter-multiple-models.rb
Last active December 10, 2025 10:52
How to call an OpenRouter LLM with the official Ruby OpenAI gem
require "openai"
client = OpenAI::Client.new(
api_key: ENV.fetch("OPENROUTER_API_KEY"),
base_url: "https://openrouter.ai/api/v1"
)
models = %w{
google/gemma-2b-it
anthropic/claude-sonnet-4
@peterc
peterc / useful.md
Last active July 7, 2025 09:20
Useful random stuff