diff --git a/scripts/update_swarm/hosts.py b/scripts/update_swarm/hosts.py new file mode 100644 index 0000000..45378c1 --- /dev/null +++ b/scripts/update_swarm/hosts.py @@ -0,0 +1,30 @@ +from __future__ import annotations +from urllib.parse import urlparse + +def classify_url(url: str) -> tuple[str, str]: + p = urlparse(url or "") + host = (p.netloc or "").lower().removeprefix("www.") + path = p.path or "" + if host in ("github.com", "raw.githubusercontent.com", "objects.githubusercontent.com"): + if host != "github.com" or "/releases/download/" in path: + return "github", "ungated" + return "github", "gated" + if host.endswith("archive.org"): + return "archive", "ungated" + if host.endswith("mediafire.com"): + return "mediafire", "ungated" + if host.endswith("mega.nz") or host.endswith("mega.co.nz"): + return "mega", "ungated" if p.fragment else "gated" + if host.endswith("itch.io"): + return "itch", "gated" + if host in ("drive.google.com", "docs.google.com"): + return "drive", "gated" + if host in ("discord.com", "discord.gg"): + return "discord", "gated" + if host.endswith("ko-fi.com"): + return "kofi", "gated" + if host.endswith("hackdex.app"): + return "hackdex", "gated" + if host.endswith("pokecommunity.com"): + return "pokecommunity", "gated" + return "other", "gated" diff --git a/tests/update_swarm/test_hosts.py b/tests/update_swarm/test_hosts.py new file mode 100644 index 0000000..f401fc5 --- /dev/null +++ b/tests/update_swarm/test_hosts.py @@ -0,0 +1,24 @@ +import os, sys, unittest +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) +sys.path.insert(0, os.path.join(ROOT, "scripts")) +from update_swarm.hosts import classify_url + +class TestClassify(unittest.TestCase): + def test_ungated(self): + self.assertEqual(classify_url("https://github.com/a/b/releases/download/v1/x.gba"), ("github", "ungated")) + self.assertEqual(classify_url("https://archive.org/download/id/file.gba"), ("archive", "ungated")) + self.assertEqual(classify_url("https://www.mediafire.com/file/abc/x.zip/file"), ("mediafire", "ungated")) + self.assertEqual(classify_url("https://mega.nz/file/abc#keyfragment"), ("mega", "ungated")) + + def test_gated(self): + self.assertEqual(classify_url("https://mega.nz/file/abc"), ("mega", "gated")) + self.assertEqual(classify_url("https://foo.itch.io/bar"), ("itch", "gated")) + self.assertEqual(classify_url("https://drive.google.com/file/d/x/view"), ("drive", "gated")) + self.assertEqual(classify_url("https://discord.gg/abc"), ("discord", "gated")) + self.assertEqual(classify_url("https://ko-fi.com/s/abc"), ("kofi", "gated")) + self.assertEqual(classify_url("https://hackdex.app/hack/x"), ("hackdex", "gated")) + self.assertEqual(classify_url("https://www.pokecommunity.com/threads/x.1/"), ("pokecommunity", "gated")) + self.assertEqual(classify_url("https://mystery.example/file.zip"), ("other", "gated")) + +if __name__ == "__main__": + unittest.main()