From d8a16eeb1a4a4ce59ef84d71f54f73a0076d447a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandr=20Zhuravl=D1=91v?= Date: Fri, 13 Nov 2020 13:38:18 +0300 Subject: [PATCH] Add torrent-viewer script (#644) [skip ci] --- examples/README.md | 11 +++++++++++ examples/torrent-viewer.clj | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 examples/torrent-viewer.clj diff --git a/examples/README.md b/examples/README.md index 046a7e1b..bf0219c7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -334,3 +334,14 @@ $ examples/image_viewer.clj ``` See [image_viewer.clj](image_viewer.clj). + +### Torrent viewer + +Shows the content of a torrent file. Note that pieces' content is hidden. + +Example usage: +``` shell +$ examples/torrent-viewer.clj file.torrent +``` + +See [torrent-viewer.clj](torrent-viewer.clj). diff --git a/examples/torrent-viewer.clj b/examples/torrent-viewer.clj new file mode 100644 index 00000000..fb394066 --- /dev/null +++ b/examples/torrent-viewer.clj @@ -0,0 +1,19 @@ +#!/usr/bin/env bb + +(require '[clojure.java.io :as io]) +(require '[bencode.core :refer [read-bencode]]) +(require '[clojure.walk :refer [prewalk]]) +(require '[clojure.pprint :refer [pprint]]) +(import 'java.io.PushbackInputStream) + +(defn bytes->strings [coll] + (prewalk #(if (bytes? %) (String. % "UTF-8") %) coll)) + +(defn read-torrent [src] + (with-open [in (io/input-stream (io/file src))] + (-> in PushbackInputStream. read-bencode bytes->strings))) + +(when-let [src (first *command-line-args*)] + (-> (read-torrent src) + (assoc-in ["info" "pieces"] "...") ; binary data + pprint))