From a9142c9f5ccdfab285f71f48f20573fe7bf8ddff Mon Sep 17 00:00:00 2001
From: EuAndreh <eu@euandre.org>
Date: Thu, 24 Dec 2020 05:48:44 -0300
Subject: Add torrent.html page with WebTorrent

---
 .gitmodules           |  3 +++
 _layouts/podcast.html |  9 ++++++---
 torrent.html          | 23 +++++++++++++++++++++++
 vendor/webtorrent     |  1 +
 4 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 torrent.html
 create mode 160000 vendor/webtorrent

diff --git a/.gitmodules b/.gitmodules
index 6d301d9..f169aba 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,6 @@
 [submodule "vendor/reveal.js"]
 	path = vendor/reveal.js
 	url = https://github.com/hakimel/reveal.js
+[submodule "vendor/webtorrent"]
+	path = vendor/webtorrent
+	url = https://github.com/webtorrent/webtorrent
diff --git a/_layouts/podcast.html b/_layouts/podcast.html
index 982fbc6..197943f 100644
--- a/_layouts/podcast.html
+++ b/_layouts/podcast.html
@@ -8,9 +8,12 @@ layout: post
 </audio>
 
 <div class="torrent-link">
-  <a href="/resources/podcasts/{{ page.date | date: "%Y-%m-%d" }}-{{ page.slug }}.ogg.torrent">
-    torrent
-  </a>
+  {% capture torrent-file %}{{ site.url }}/resources/podcasts/{{ page.date | date: "%Y-%m-%d" }}-{{ page.slug }}.ogg.torrent{% endcapture %}
+  <a href="{{ torrent-file }}">torrent</a>
+  {% if site.torrent %}
+    |
+    <a href="/torrent.html?torrent={{ torrent-file | uri_escape }}">online webtorrent</a>
+  {% endif %}
 </div>
 
 {{ content }}
diff --git a/torrent.html b/torrent.html
new file mode 100644
index 0000000..7be57d0
--- /dev/null
+++ b/torrent.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1" />
+    <link rel="stylesheet" href="/styles.css" />
+    <title>WebTorrent player - EuAndreh</title>
+    <script src="/vendor/webtorrent/webtorrent.min.js"></script>
+  </head>
+  <body>
+    <div id="output"></div>
+    <div id="torrentLink"></div>
+    <script>
+      const params = new URLSearchParams(window.location.search);
+      const torrentID = params.get("torrent"); // "https://webtorrent.io/torrents/sintel.torrent";
+      document.querySelector("#torrentLink").innerHTML = `Downloading <a href=${torrentID}>${torrentID}</a>`;
+
+      const client = new WebTorrent();
+      client.add(torrentID, torrent =>
+        torrent.files.forEach(file => file.appendTo("#output")));
+    </script>
+  </body>
+</html>
diff --git a/vendor/webtorrent b/vendor/webtorrent
new file mode 160000
index 0000000..2d78a30
--- /dev/null
+++ b/vendor/webtorrent
@@ -0,0 +1 @@
+Subproject commit 2d78a30a72072b859336f8d9340398d6bb2169e0
-- 
cgit v1.2.3