Add torrent-viewer script (#644) [skip ci]

This commit is contained in:
Aleksandr Zhuravlёv 2020-11-13 13:38:18 +03:00 committed by GitHub
parent 3e66570596
commit d8a16eeb1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -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).

View file

@ -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))