Skip to content

Instantly share code, notes, and snippets.

@samn
samn / download-favs-from-json.rb
Created January 16, 2017 18:28
Archive your vine favorites
# This script uses the output from fetch-favs-json.rb
# (json files with data about posts you've liked) to download
# images and videos for these posts. It also constructs JSON objects
# formatted to be used in your archive downloaded from vine.
# This data will be written to posts.json
# Look for window.VINE_DATA in index.html from your vine archive and
# replace the value for the "posts" key with the data in posts.json
# You'll then be able to browse your favorites in a convenient web interface.
# Set this to false to only generate posts.json

Keybase proof

I hereby claim:

  • I am samn on github.
  • I am samn (https://keybase.io/samn) on keybase.
  • I have a public key ASCxXfhjXadKKe-hABsbXWeL-2pIT9cqWPyzCYyj3hVXhQo

To claim this, I am signing this object:

@samn
samn / async_wait.clj
Created October 16, 2013 21:45
This shows a technique for waiting for an asynchronous operation (here persistence) to occur without using locks. This is useful if we normally want this operation to occur asynchronously (in a future thread pool) but would like to wait for its completion at exceptional times. For example, it may be fine to periodically persist data asynchronous…
(defn wait-for-persistence
"Dereferences and waits on the value of persistence-lock-atom,
if the value is dereferenceble & returns the dereferenced value.
Otherwise returns nil."
[persistence-lock-atom]
(when (instance? clojure.lang.IDeref @persistence-lock-atom)
;; This could deref with a timeout by using `deref` instead of `@`
@@persistence-lock-atom))
(defn persist!
@samn
samn / async_fetch.clj
Last active December 25, 2015 17:39
This gist describes a technique for asynchronously fetching values from an external resource. This fetch may be expensive so we don't want to block the calling thread. Using a hash-set and watcher decouples the fetch of the value from the request for the value without callbacks. Looking at the difference of the before and after set values in the…
(def fetch-set (ref (hash-set)))
(def resource-map (ref {}))
(defn fetch-resource
[resource-key]
;; perform some expensive operation that returns a value
)
(defn request-fetch
"Requests an asynchronous fetch of some resource."
@samn
samn / media_dog.clj
Last active October 16, 2019 22:11
Using the Twitter API with Clojure
(ns media-dog
(:require [twitter.oauth :as oauth]
[twitter.callbacks.handlers :as handlers]
[twitter.api.streaming :as streaming]
[cheshire.core :as json])
(:import (twitter.callbacks.protocols AsyncStreamingCallback)))
(def creds (oauth/make-oauth-creds
; consumer key
"CDdirssorFxBYmWWQmM1xw"
@samn
samn / lock_less_monster.clj
Last active April 4, 2022 01:45
The Lock Less Monster
#_(
Let's say you have a threadpool doing work that requires access to some shared resource.
And this resource needs to be refreshed at times. E.g. an OAuth bearer token that can expire or be revoked.
If this resource were expensive to refresh, or subject to rate limiting (OAuth tokens are often both)
then it's desirable to refresh as little as possible.
It's undesirable, however, to mix complicated synchronization code for updating the resource in
with the business logic consuming it.
Enter the lock less monster, a lock free method for coordinating updates to a shared reference.