Skip to content

Instantly share code, notes, and snippets.

@rplevy
Last active December 11, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rplevy/4661769 to your computer and use it in GitHub Desktop.
Save rplevy/4661769 to your computer and use it in GitHub Desktop.
A non-browser-based imitation of http://vinepeek.com (play random videos from http://vine.co) in 50 lines of Clojure in the REPL. Another way in which this is different from vinepeek.com is that it doesn't repeat videos that have already been viewed (unless you restart the process). Make sure to set video-player to a video-player application tha…
(require '[cemerick.pomegranate :refer [add-dependencies]])
(add-dependencies
:coordinates '[[clj-http "0.6.3"]
[swiss-arrows "0.5.1"]]
:repositories {"clojars" "http://clojars.org/repo"})
(require '[clj-http.client :as client]
'[swiss-arrows.core :refer [-<>]]
'[clojure.string :as str]
'[clojure.java.shell :refer [sh]])
(def search-term "")
(def video-player "/usr/bin/mplayer")
(defn vine-video-url [vine-url]
(try (-<> (:body (client/get vine-url))
(str/split #"\n")
(filter (partial re-find #"source src") <>)
first
(str/replace #"^.*(http[^\"]+)\".*$" "$1"))
(catch Exception e nil)))
(defn vine-urls []
(try (->> (client/get "http://search.twitter.com/search.json"
{:query-params
{"q" (format "vine.co %s" (or search-term ""))
"rpp" "10"
"include_entities" "true"
"result_type" "mixed"}
:as :json})
:body
:results
(take 10)
(map (comp first
(partial filter #(re-find #"vine\.co" %))
(partial map :expanded_url)
:urls
:entities)))
(catch Exception e nil)))
(def play-video
(memoize
(fn [url]
(with-open [w (clojure.java.io/output-stream "tmp.mp4")]
(.write w (:body (client/get url {:as :byte-array}))))
(sh video-player "tmp.mp4"))))
(loop [] (doseq [vid-url (keep vine-video-url (vine-urls))]
(play-video vid-url))
(recur))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment