Skip to content

Instantly share code, notes, and snippets.

@alxgrk
alxgrk / app.js
Created January 17, 2024 23:03
AbortController in Express.js
const express = require("express");
const app = express();
console.log("initializing")
app.get("/foo", (req, res) => {
const abort = new AbortController()
console.log("entered /foo")
@alxgrk
alxgrk / sql-to-csv.js
Last active February 10, 2023 14:11
A good enough MySQL-dump-to-CSV script.
#!/usr/bin/env node
const Transform = require("stream").Transform;
function getArgValue(argName, defaultValue) {
const argIndex = process.argv.findIndex(arg => arg.indexOf(argName) !== -1);
const argValue = argIndex >= 0 && process.argv.length >= argIndex + 1
? process.argv[argIndex]
: defaultValue;
return argValue ? (/--[\w-]+=/.test(argValue) ? argValue.split("=")[1] : process.argv[argIndex + 1]) : argValue;
}
@alxgrk
alxgrk / kotlin-sealed-classes.kts
Last active September 24, 2020 06:41
Code for the post on dev.to:
#!/usr/bin/env kscript
//DEPS org.jetbrains.kotlin:kotlin-reflect:1.3.72,com.fasterxml.jackson.module:jackson-module-kotlin:2.11.2,com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.11.2
import Config_sealed_classes.Configuration.Greeting.SayHello
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
tweetEditor = openTweetEditor();
tweetEditor.addTextContent("...");
r = Random(0, 1);
if (r <= 0.2) {
tweetEditor.attachImage();
} else if (r <= 0.25) {
tweetEditor.attachVideo();
} else if (r <= 0.3) {
tweetEditor.attachGif();
searchResult = searchFor(searchTerm);
finished = false;
do
r = Random(0, 1);
if (r <= 0.7) {
searchResult.getHit(0).follow();
finished = true;
} else if (r <= 0.9) {
// there are different categories of results: people, tweets, photos, ..
@alxgrk
alxgrk / start-bot.sh
Last active May 7, 2020 14:53
Telegram Quiz Bot for Bash
#!/bin/bash
############################################################################################################
# #
# To run this script on startup (tested on Raspbian), do the following: #
# #
# 1. add /lib/systemd/system/quiz-bot-telegram.service file using `sudo` #
# ``` #
# Description=Quiz-Bot for Telegram #
# After=multi-user.target #
@alxgrk
alxgrk / ContributorSession.java
Last active June 3, 2020 09:30
Twitter-User Contributor-Algorithm
actAsContributor(previousSession) {
r = Random(0, 1)
currentSession = null
if (r <= 0.7 && previousSessions != Sessions.ScrollTimeline) {
currentSession = Sessions.ScrollTimeline
scrollTimelineSession()
} else if (r <= 0.9 && previousSessions != Sessions.Posting) {
currentSession = Sessions.Posting
@alxgrk
alxgrk / ScrollTimeline.java
Last active May 24, 2020 13:05
Twitter-User Scroll-Timeline-Algorithm
while( timeOfUseLessThan(30minutes) && endOfTimelineNotReached() ) {
currentTweet = scrollOverTweets(Random(1, 10))
// simulate reading current tweet
waitRandomBetween(1second, 10seconds)
// click depending on kind of tweet
// not always clicking, since seeing the preview might be enough
if (currentTweet.contains(Images) && Random(0, 1) <= 0.5 ) {