Skip to content

Instantly share code, notes, and snippets.

@ggorlen
ggorlen / browser-automation-reprex.md
Created July 2, 2024 00:15
Creating Reproducible Browser Automation Examples

Creating Reproducible Browser Automation Examples

Motivation

  • Without a minimal, reproducible example (reprex), in most cases, it's impossible to answer your question.
  • Without a reprex, the question isn't likely to provide value to future visitors with the same problem (the main purpose of Q&A sites like Stack Overflow).

Question Checklist

  1. Include your reprex code as text, not an image in the question itself (not an external link).
@ggorlen
ggorlen / food.py
Created July 1, 2024 17:11
Turtle Snake
from random import randint
from turtle import Turtle
class Food:
def __init__(self, move_dist):
self.move_dist = move_dist
self.t = t = Turtle()
t.shape("circle")
t.penup()
@ggorlen
ggorlen / codementor-payouts.html
Last active June 21, 2024 14:08
Codementor payouts visualizer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Codementor payout visualizer">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Codementor Payout Visualizer</title>
</head>
<body>
<div>
@ggorlen
ggorlen / guitar.md
Created June 16, 2024 18:07
guitar

coldharborstores - z e r o

x06650
x46650
4x6650
6x66xx
x766xx
@ggorlen
ggorlen / stream_openai_api_no_library.py
Last active June 26, 2024 23:56
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 June 25, 2024 01:35
Today I learned

Random tidbits

Vue nested click handlers bubble

Consider this code:

<div
  v-for="question in questions"
 @click="switchToQuestion(question)"
@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">