Skip to content

Instantly share code, notes, and snippets.

@ggorlen
ggorlen / openai-api-fetch.php
Created July 12, 2024 14:19
OpenAI API request with PHP
<?php
function openAICompletion(array $messages): string {
$apiKey = 'OpenAI API key here';
$url = 'https://api.openai.com/v1/chat/completions';
$payload = [
'messages' => $messages,
'model' => 'gpt-4o',
'temperature' => 0
];
$options = [
@ggorlen
ggorlen / browser-automation-reprex.md
Last active July 5, 2024 22:38
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 August 31, 2024 04:34
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
Last active September 7, 2024 15:16
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 September 13, 2024 22:22
Today I learned

Random tidbits

Playwright VSCode .env isn't detected

Even with a normal Playwright + dotenv setup that works on the command line, running Playwright in VSCode seems to pull .env values from settings.json's "playwright.env" object rather than .env. The normal .env variables are undefined.

git: accidentally merging a large feature branch and reverting it can cause trouble later

If you accidentally merge a large, long-running feature branch, then revert it, big problems can arise when trying to reconcile the branches later. Probably better to reset hard before the merge and force push to undo it. Also, disable pushing to develop, even for admins.

@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()