Skip to content

Instantly share code, notes, and snippets.

View alexanderscott's full-sized avatar

Alex Ehrnschwender alexanderscott

View GitHub Profile
@alexanderscott
alexanderscott / spotify_get_playlist_track_uris.py
Created May 23, 2021 06:20
Get Spotify track URIs from playlist URI
# Spotify's desktop UI no longer allows you to copy to clipboard all track URIs for a playlist
# Script taken from https://github.com/hbashton/spotify-ripper/issues/61
# Requirements: python v2, spotipy (can be install from pip)
# Usage: First go to Spotify developer page and create a new app for client_id and client_secret
# export SPOTIPY_CLIENT_ID=<your_client_id>
# export SPOTIPY_CLIENT_SECRET=<your_client_secret>
# python spotify_get_playlist_track_uris.py <playlist-uri>
from spotipy.oauth2 import SpotifyClientCredentials
@alexanderscott
alexanderscott / my.cnf
Created October 16, 2015 04:46
MySQL config for lower memory usage (for 2gb droplet)
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
@alexanderscott
alexanderscott / MySqlPool.scala
Created October 14, 2015 09:43
MySql connection pooling in Scala
// Taken largely from https://gist.github.com/tsuna/2245176
import java.sql._
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
import java.util.concurrent.atomic.{AtomicLong, AtomicInteger}
import java.util.concurrent.TimeUnit.MILLISECONDS
import scala.collection.mutable.ArrayBuffer
@alexanderscott
alexanderscott / RoboGrow.ino
Last active December 29, 2015 14:47
Arduino gardening sensors & watering code
#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTTYPE DHT11
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
/* Enable/Disable Features */
bool enableDhtSensor0 = true;
bool enableDhtSensor1 = false;
@alexanderscott
alexanderscott / iGrow.ino
Last active August 29, 2015 14:16
Arduino gardening sensors & watering code
/* Imports & Definitions */
#include <LiquidCrystal.h>
#include "DHT.h"
#include <MemoryFree.h>
#include <SerialReceiver.h>
#define DHTTYPE DHT11
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
@alexanderscott
alexanderscott / ZSUM.lua
Created January 26, 2015 08:53
Redis Lua script to return the sum of all scores in a ZSET
-- @desc: Return the sum of all scores in a sorted set
-- @usage: redis-cli eval "$(cat ZSUM.lua)" N <zset1> <zset2> ... <zsetN>
-- @return: integer sum
local function ZSUM(keys)
local sum = 0
local function sumKeyScores(key)
@alexanderscott
alexanderscott / ZSTDDEV.lua
Created January 26, 2015 08:52
Redis Lua script to return the standard deviation of ZSET scores
-- @desc Returns the std deviation of a ZSET key
-- @usage redis-cli EVAL "$(cat ZSTDDEV.lua)" 1 <zsetKey>
-- @return string representation of float value
local function ZSTDDEV(key)
local mean, sum, size, sumsqrs = 0, 0, 0, 0
local memberScores = redis.call("ZRANGE", key, 0, -1, "WITHSCORES")
local scores = {}
@alexanderscott
alexanderscott / ZSPREAD.lua
Created January 26, 2015 08:51
Redis Lua script to spread members between two ZSETs so they are even size
-- @desc: Spread members between two ZSETs so they are even size
-- @usage: redis-cli eval "$(cat ZSPREAD.lua)" 2 <zset1> <zset2>
-- @return: list of spread members
local function round(num)
if num >= 0 then return math.floor(num+.5)
else return math.ceil(num-.5) end
end
local function randomFromTo(from, to)
@alexanderscott
alexanderscott / ZSPLIT.lua
Created January 26, 2015 08:51
Redis Lua script to split a ZSET in two (round-robin)
-- @desc: Split a ZSET in two via round-robin
-- @usage: redis-cli eval "$(cat ZSPLIT)" 2 <originalZset> <newZset>
-- @return: list of moved members
local function ZSPLIT(zset1, zset2)
local zset1size = redis.call("ZCARD", zset1)
if zset1size == 0 then return {} end
local members = redis.call("ZREVRANGEBYSCORE", zset1, "+INF", "-INF", "WITHSCORES")
@alexanderscott
alexanderscott / ZMEAN.lua
Created January 26, 2015 08:50
Redis Lua script to return the mean of a ZSET
-- @desc: Return the mean of a ZSET
-- @usage: redis-cli eval "$(cat ZMEAN.lua)" 1 <zsetKey>
-- @return: string representation of float
local function ZMEAN(key)
local keySum = 0
local membersWithScores = redis.call("ZREVRANGE", key, 0, -1, "WITHSCORES")