Skip to content

Instantly share code, notes, and snippets.

@ggorlen
ggorlen / stream_openai_api_no_library.py
Last active February 23, 2025 18:51
Streaming OpenAI API responses in Python without a library
import json
import requests
api_key = ""
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
@ggorlen
ggorlen / webpage-to-markdown.js
Created May 20, 2024 22:59
scrape any webpage to markdown
const cheerio = require("cheerio"); // ^1.0.0-rc.12
const { JSDOM } = require("jsdom"); // ^24.0.0
const puppeteer = require("puppeteer"); // ^22.7.1
const TurndownService = require("turndown"); // ^7.1.2
const { Readability } = require("@mozilla/readability"); // ^0.5.0
const urlToMarkdown = async (page, url) => {
await page.goto(url, { waitUntil: "networkidle2" });
const doc = new JSDOM(await page.content(), { url });
@ggorlen
ggorlen / openai-api-fetch.js
Last active June 26, 2024 23:56
OpenAI API request with fetch
const apiKey = "YOUR_OPENAI_API_KEY";
const endpoint = "https://api.openai.com/v1/chat/completions";
const fetchCompletion = async (messages) => {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
@ggorlen
ggorlen / til.md
Last active January 11, 2026 17:32
Today I learned

Today I Learned

Nested .git repos aren't committed

After downloading a bunch of glitch projects as .git repos, I thought I'd backed them up by committing them all and pushing to github, but only a bunch of empty folders were committed.

Datadog *:id pattern

For codebases with Datadog logs logging with fooBarEntityId keys, rather than typing @fooBarEntityId:b6356b50-7bac-4474-bca9-765336dff804 to search, use *:b6356b50-7bac-4474-bca9-765336dff804.

@ggorlen
ggorlen / scrape-sfpl-hours.js
Created January 5, 2024 20:49
Scrape SFPL hours
// copy and paste url into browser and run code in console
// probably use puppeteer rather than fetch if you want to automate this
// (pagination doesn't seem to work with requests, maybe user agent? dunno why)
var url =
"https://sfpl.org/locations/#!/filters?sort_by=weight&sort_order=ASC&items_per_page=50";
var hours = [...document.querySelectorAll(".location--teaser--inner")]
.map(e => ({
name: e.querySelector(".location__title").textContent.trim(),
hours: [...e.querySelectorAll(".office-hours__item")].map(
e => e.textContent.replace(/\s+/g, " ").trim()
@ggorlen
ggorlen / angularjs-hello-world.html
Created September 17, 2023 19:47
AngularJS hello world
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="dark light" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.js"></script>
</head>
<body>
<div ng-app="exampleApp">
@ggorlen
ggorlen / llm-flags.json
Last active June 21, 2023 19:41
All my LLM flags on Stack Overflow through May 2023
[
{
"title": "How to configure sanity preview to work on live?",
"href": "https://stackoverflow.com/questions/76196916/how-to-configure-sanity-preview-to-work-on-live/76267279#76267279",
"userHref": "https://stackoverflow.com/users/12207505/mathias",
"user": "Mathias",
"comment": "ChatGPT likely",
"score": 0,
"posted": "2023-05-16 21:53:19Z",
"flagged": "2023-05-23 23:29:05Z",
@ggorlen
ggorlen / gaming.md
Last active March 5, 2026 17:53
gaming
@ggorlen
ggorlen / r-gotchas.md
Last active April 18, 2023 18:40
R gotchas

R gotchas

Weird vectorized string appends

Wrong:

result <- c()
result <- c(result, "foo")
result <- c(result, paste0(rep("X", 3), "z")) # => [1] "foo" "Xz"  "Xz"  "Xz" 
print(result) # =&gt; [1] "foo\nXz\nXz\nXz"