Skip to content

Instantly share code, notes, and snippets.

@ggorlen
ggorlen / main.lua
Last active February 24, 2025 03:34
love2d-10print
local size = 20
local tiles = {}
function love.load()
local r, g, b = love.math.colorFromBytes(0, 130, 200)
love.graphics.setBackgroundColor(r, g, b)
love.window.setTitle("10 print")
height = love.graphics.getHeight()
width = love.graphics.getWidth()
math.randomseed(os.time())
@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 / 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 March 14, 2025 06:34
Today I learned

Today I Learned

TypeORM database operations in a setTimeout can cause random Jest tests to fail

Consider a Jest-tested function that does a TypeORM operation in a fire-and-forget timeout after the test ends:

setTimeout(async () => {
  await someEntity.reload();
}, 20_000);