diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index 3938344..40d73b8 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -1,6 +1,17 @@
version: 2
updates:
- - package-ecosystem: "gomod"
+ - package-ecosystem: "cargo"
directory: "/"
schedule:
- interval: "daily"
+ interval: "weekly"
+ ignore:
+ - dependency-name: "*"
+ update-types: [ "version-update:semver-patch" ]
+
+ - package-ecosystem: "cargo"
+ directory: "/crunchy-cli-core"
+ schedule:
+ interval: "weekly"
+ ignore:
+ - dependency-name: "*"
+ update-types: [ "version-update:semver-patch" ]
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
deleted file mode 100644
index b2409a0..0000000
--- a/.github/workflows/ci.yml
+++ /dev/null
@@ -1,20 +0,0 @@
-name: CI
-
-on: [ push, pull_request ]
-
-jobs:
- test:
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v3
-
- - name: Set up Go
- uses: actions/setup-go@v3
- with:
- go-version: 1.18
-
- - name: Build
- run: go build -v .
-
- - name: Test
- run: go test -v .
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml
deleted file mode 100644
index e0f5470..0000000
--- a/.github/workflows/codeql-analysis.yml
+++ /dev/null
@@ -1,67 +0,0 @@
-# For most projects, this workflow file will not need changing; you simply need
-# to commit it to your repository.
-#
-# You may wish to alter this file to override the set of languages analyzed,
-# or to provide custom queries or build logic.
-#
-# ******** NOTE ********
-# We have attempted to detect the languages in your repository. Please check
-# the `language` matrix defined below to confirm you have the correct set of
-# supported CodeQL languages.
-#
-name: "CodeQL"
-
-on:
- push:
- pull_request:
- schedule:
- - cron: '40 3 * * 2'
-
-jobs:
- analyze:
- name: Analyze
- runs-on: ubuntu-latest
- permissions:
- actions: read
- contents: read
- security-events: write
-
- strategy:
- fail-fast: false
- matrix:
- language: [ 'go' ]
- # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
- # Learn more about CodeQL language support at https://git.io/codeql-language-support
-
- steps:
- - name: Checkout repository
- uses: actions/checkout@v3
-
- # Initializes the CodeQL tools for scanning.
- - name: Initialize CodeQL
- uses: github/codeql-action/init@v2
- with:
- languages: ${{ matrix.language }}
- # If you wish to specify custom queries, you can do so here or in a config file.
- # By default, queries listed here will override any specified in a config file.
- # Prefix the list here with "+" to use these queries and those in the config file.
- # queries: ./path/to/local/query, your-org/your-repo/queries@main
-
- # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
- # If this step fails, then you should remove it and run the build manually (see below)
- - name: Autobuild
- uses: github/codeql-action/autobuild@v2
-
- # ℹ️ Command-line programs to run using the OS shell.
- # 📚 https://git.io/JvXDl
-
- # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
- # and modify them (or add more) to build your code if your project
- # uses a compiled language
-
- #- run: |
- # make bootstrap
- # make release
-
- - name: Perform CodeQL Analysis
- uses: github/codeql-action/analyze@v2
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..05b2d65
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,1645 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aes"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "433cfd6710c9986c576a25ca913c39d66a6474107b406f34f91d4a8923395241"
+dependencies = [
+ "cfg-if",
+ "cipher",
+ "cpufeatures",
+]
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "android_system_properties"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "anyhow"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
+
+[[package]]
+name = "async-channel"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e14485364214912d3b19cc3435dde4df66065127f05fa0d75c712f36f12c2f28"
+dependencies = [
+ "concurrent-queue",
+ "event-listener",
+ "futures-core",
+]
+
+[[package]]
+name = "async-trait"
+version = "0.1.58"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e805d94e6b5001b651426cf4cd446b1ab5f319d27bab5c644f61de0a804360c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "block-padding"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0a90ec2df9600c28a01c56c4784c9207a96d2451833aeceb8cc97e4c9548bb78"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "bumpalo"
+version = "3.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
+
+[[package]]
+name = "bytes"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c"
+
+[[package]]
+name = "cache-padded"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c"
+
+[[package]]
+name = "castaway"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2698f953def977c68f935bb0dfa959375ad4638570e969e2f1e9f433cbf1af6"
+
+[[package]]
+name = "cbc"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6"
+dependencies = [
+ "cipher",
+]
+
+[[package]]
+name = "cc"
+version = "1.0.77"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "chrono"
+version = "0.4.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
+dependencies = [
+ "iana-time-zone",
+ "js-sys",
+ "num-integer",
+ "num-traits",
+ "serde",
+ "time",
+ "wasm-bindgen",
+ "winapi",
+]
+
+[[package]]
+name = "cipher"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1873270f8f7942c191139cb8a40fd228da6c3fd2fc376d7e92d47aa14aeb59e"
+dependencies = [
+ "crypto-common",
+ "inout",
+]
+
+[[package]]
+name = "clap"
+version = "4.0.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2148adefda54e14492fb9bddcc600b4344c5d1a3123bd666dcb939c6f0e0e57e"
+dependencies = [
+ "atty",
+ "bitflags",
+ "clap_derive",
+ "clap_lex",
+ "once_cell",
+ "strsim",
+ "termcolor",
+]
+
+[[package]]
+name = "clap_complete"
+version = "4.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96b0fba905b035a30d25c1b585bf1171690712fbb0ad3ac47214963aa4acc36c"
+dependencies = [
+ "clap",
+]
+
+[[package]]
+name = "clap_derive"
+version = "4.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "clap_lex"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
+dependencies = [
+ "os_str_bytes",
+]
+
+[[package]]
+name = "clap_mangen"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa149477df7854a1497db0def32b8a65bf98f72a14d04ac75b01938285d83420"
+dependencies = [
+ "clap",
+ "roff",
+]
+
+[[package]]
+name = "codespan-reporting"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
+dependencies = [
+ "termcolor",
+ "unicode-width",
+]
+
+[[package]]
+name = "concurrent-queue"
+version = "1.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af4780a44ab5696ea9e28294517f1fffb421a83a25af521333c838635509db9c"
+dependencies = [
+ "cache-padded",
+]
+
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
+
+[[package]]
+name = "cpufeatures"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "crunchy-cli"
+version = "0.1.0"
+dependencies = [
+ "chrono",
+ "clap",
+ "clap_complete",
+ "clap_mangen",
+ "crunchy-cli-core",
+ "tokio",
+]
+
+[[package]]
+name = "crunchy-cli-core"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "async-trait",
+ "chrono",
+ "clap",
+ "crunchyroll-rs",
+ "ctrlc",
+ "dirs",
+ "isahc",
+ "log",
+ "num_cpus",
+ "regex",
+ "signal-hook",
+ "sys-locale",
+ "tempfile",
+ "terminal_size",
+ "tokio",
+]
+
+[[package]]
+name = "crunchyroll-rs"
+version = "0.1.0"
+source = "git+https://github.com/crunchy-labs/crunchyroll-rs#7aedfc6c9a91a42ef46639ba9e99adba63cd0dda"
+dependencies = [
+ "aes",
+ "cbc",
+ "chrono",
+ "crunchyroll-rs-internal",
+ "http",
+ "isahc",
+ "m3u8-rs",
+ "regex",
+ "serde",
+ "serde_json",
+ "serde_urlencoded",
+ "smart-default",
+ "tokio",
+]
+
+[[package]]
+name = "crunchyroll-rs-internal"
+version = "0.1.0"
+source = "git+https://github.com/crunchy-labs/crunchyroll-rs#7aedfc6c9a91a42ef46639ba9e99adba63cd0dda"
+dependencies = [
+ "darling",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "crypto-common"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
+dependencies = [
+ "generic-array",
+ "typenum",
+]
+
+[[package]]
+name = "ctrlc"
+version = "3.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d91974fbbe88ec1df0c24a4f00f99583667a7e2e6272b2b92d294d81e462173"
+dependencies = [
+ "nix",
+ "winapi",
+]
+
+[[package]]
+name = "curl"
+version = "0.4.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "509bd11746c7ac09ebd19f0b17782eae80aadee26237658a6b4808afb5c11a22"
+dependencies = [
+ "curl-sys",
+ "libc",
+ "openssl-probe",
+ "openssl-sys",
+ "schannel",
+ "socket2",
+ "winapi",
+]
+
+[[package]]
+name = "curl-sys"
+version = "0.4.59+curl-7.86.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6cfce34829f448b08f55b7db6d0009e23e2e86a34e8c2b366269bf5799b4a407"
+dependencies = [
+ "cc",
+ "libc",
+ "libnghttp2-sys",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+ "winapi",
+]
+
+[[package]]
+name = "cxx"
+version = "1.0.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453"
+dependencies = [
+ "cc",
+ "cxxbridge-flags",
+ "cxxbridge-macro",
+ "link-cplusplus",
+]
+
+[[package]]
+name = "cxx-build"
+version = "1.0.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0"
+dependencies = [
+ "cc",
+ "codespan-reporting",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "scratch",
+ "syn",
+]
+
+[[package]]
+name = "cxxbridge-flags"
+version = "1.0.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71"
+
+[[package]]
+name = "cxxbridge-macro"
+version = "1.0.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "darling"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b0dd3cd20dc6b5a876612a6e5accfe7f3dd883db6d07acfbf14c128f61550dfa"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a784d2ccaf7c98501746bf0be29b2022ba41fd62a2e622af997a03e9f972859f"
+dependencies = [
+ "fnv",
+ "ident_case",
+ "proc-macro2",
+ "quote",
+ "strsim",
+ "syn",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7618812407e9402654622dd402b0a89dff9ba93badd6540781526117b92aab7e"
+dependencies = [
+ "darling_core",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "dirs"
+version = "4.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
+dependencies = [
+ "dirs-sys",
+]
+
+[[package]]
+name = "dirs-sys"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
+dependencies = [
+ "libc",
+ "redox_users",
+ "winapi",
+]
+
+[[package]]
+name = "encoding_rs"
+version = "0.8.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "errno"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
+dependencies = [
+ "errno-dragonfly",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "errno-dragonfly"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
+dependencies = [
+ "cc",
+ "libc",
+]
+
+[[package]]
+name = "event-listener"
+version = "2.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
+
+[[package]]
+name = "fastrand"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499"
+dependencies = [
+ "instant",
+]
+
+[[package]]
+name = "fnv"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+
+[[package]]
+name = "form_urlencoded"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8"
+dependencies = [
+ "percent-encoding",
+]
+
+[[package]]
+name = "futures-core"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac"
+
+[[package]]
+name = "futures-io"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb"
+
+[[package]]
+name = "futures-lite"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48"
+dependencies = [
+ "fastrand",
+ "futures-core",
+ "futures-io",
+ "memchr",
+ "parking",
+ "pin-project-lite",
+ "waker-fn",
+]
+
+[[package]]
+name = "generic-array"
+version = "0.14.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi 0.11.0+wasi-snapshot-preview1",
+]
+
+[[package]]
+name = "heck"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "http"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399"
+dependencies = [
+ "bytes",
+ "fnv",
+ "itoa",
+]
+
+[[package]]
+name = "httpdate"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
+
+[[package]]
+name = "iana-time-zone"
+version = "0.1.53"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765"
+dependencies = [
+ "android_system_properties",
+ "core-foundation-sys",
+ "iana-time-zone-haiku",
+ "js-sys",
+ "wasm-bindgen",
+ "winapi",
+]
+
+[[package]]
+name = "iana-time-zone-haiku"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
+dependencies = [
+ "cxx",
+ "cxx-build",
+]
+
+[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
+[[package]]
+name = "idna"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"
+dependencies = [
+ "unicode-bidi",
+ "unicode-normalization",
+]
+
+[[package]]
+name = "inout"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
+dependencies = [
+ "block-padding",
+ "generic-array",
+]
+
+[[package]]
+name = "instant"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "io-lifetimes"
+version = "0.7.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59ce5ef949d49ee85593fc4d3f3f95ad61657076395cbbce23e2121fc5542074"
+
+[[package]]
+name = "isahc"
+version = "1.7.0"
+source = "git+https://github.com/sagebind/isahc?rev=34f158ef#34f158ef9f87b2387bed2c81936916a29c1eaad1"
+dependencies = [
+ "async-channel",
+ "castaway",
+ "crossbeam-utils",
+ "curl",
+ "curl-sys",
+ "encoding_rs",
+ "event-listener",
+ "futures-lite",
+ "http",
+ "httpdate",
+ "log",
+ "mime",
+ "once_cell",
+ "polling",
+ "slab",
+ "sluice",
+ "tracing",
+ "tracing-futures",
+ "url",
+ "waker-fn",
+]
+
+[[package]]
+name = "itoa"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
+
+[[package]]
+name = "js-sys"
+version = "0.3.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
+dependencies = [
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.137"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
+
+[[package]]
+name = "libnghttp2-sys"
+version = "0.1.7+1.45.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57ed28aba195b38d5ff02b9170cbff627e336a20925e43b4945390401c5dc93f"
+dependencies = [
+ "cc",
+ "libc",
+]
+
+[[package]]
+name = "libz-sys"
+version = "1.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "link-cplusplus"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.0.46"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d"
+
+[[package]]
+name = "log"
+version = "0.4.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "m3u8-rs"
+version = "5.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "15d091887fd4a920417805283b7a838d0dcda68e8d632cd305a4439ee776d1ce"
+dependencies = [
+ "chrono",
+ "nom",
+]
+
+[[package]]
+name = "memchr"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
+
+[[package]]
+name = "mime"
+version = "0.3.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
+
+[[package]]
+name = "minimal-lexical"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
+
+[[package]]
+name = "nix"
+version = "0.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e322c04a9e3440c327fca7b6c8a63e6890a32fa2ad689db972425f07e0d22abb"
+dependencies = [
+ "autocfg",
+ "bitflags",
+ "cfg-if",
+ "libc",
+]
+
+[[package]]
+name = "nom"
+version = "7.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
+dependencies = [
+ "memchr",
+ "minimal-lexical",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "num_cpus"
+version = "1.14.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5"
+dependencies = [
+ "hermit-abi",
+ "libc",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.16.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
+
+[[package]]
+name = "openssl-probe"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
+
+[[package]]
+name = "openssl-src"
+version = "111.24.0+1.1.1s"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3498f259dab01178c6228c6b00dcef0ed2a2d5e20d648c017861227773ea4abd"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.78"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07d5c8cb6e57b3a3612064d7b18b117912b4ce70955c2504d4b741c9e244b132"
+dependencies = [
+ "autocfg",
+ "cc",
+ "libc",
+ "openssl-src",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "os_str_bytes"
+version = "6.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
+
+[[package]]
+name = "parking"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
+
+[[package]]
+name = "percent-encoding"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
+
+[[package]]
+name = "pin-project"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc"
+dependencies = [
+ "pin-project-internal",
+]
+
+[[package]]
+name = "pin-project-internal"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "pin-project-lite"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
+
+[[package]]
+name = "pkg-config"
+version = "0.3.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
+
+[[package]]
+name = "polling"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ab4609a838d88b73d8238967b60dd115cc08d38e2bbaf51ee1e4b695f89122e2"
+dependencies = [
+ "autocfg",
+ "cfg-if",
+ "libc",
+ "log",
+ "wepoll-ffi",
+ "winapi",
+]
+
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "redox_syscall"
+version = "0.2.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
+dependencies = [
+ "bitflags",
+]
+
+[[package]]
+name = "redox_users"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
+dependencies = [
+ "getrandom",
+ "redox_syscall",
+ "thiserror",
+]
+
+[[package]]
+name = "regex"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
+
+[[package]]
+name = "remove_dir_all"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "roff"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316"
+
+[[package]]
+name = "rustix"
+version = "0.35.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "727a1a6d65f786ec22df8a81ca3121107f235970dc1705ed681d3e6e8b9cd5f9"
+dependencies = [
+ "bitflags",
+ "errno",
+ "io-lifetimes",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys 0.42.0",
+]
+
+[[package]]
+name = "ryu"
+version = "1.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
+
+[[package]]
+name = "schannel"
+version = "0.1.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2"
+dependencies = [
+ "lazy_static",
+ "windows-sys 0.36.1",
+]
+
+[[package]]
+name = "scratch"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898"
+
+[[package]]
+name = "serde"
+version = "1.0.147"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.147"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.89"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
+[[package]]
+name = "serde_urlencoded"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
+dependencies = [
+ "form_urlencoded",
+ "itoa",
+ "ryu",
+ "serde",
+]
+
+[[package]]
+name = "signal-hook"
+version = "0.3.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d"
+dependencies = [
+ "libc",
+ "signal-hook-registry",
+]
+
+[[package]]
+name = "signal-hook-registry"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "slab"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "sluice"
+version = "0.5.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d7400c0eff44aa2fcb5e31a5f24ba9716ed90138769e4977a2ba6014ae63eb5"
+dependencies = [
+ "async-channel",
+ "futures-core",
+ "futures-io",
+]
+
+[[package]]
+name = "smart-default"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "socket2"
+version = "0.4.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "strsim"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
+[[package]]
+name = "syn"
+version = "1.0.103"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "sys-locale"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3358acbb4acd4146138b9bda219e904a6bb5aaaa237f8eed06f4d6bc1580ecee"
+dependencies = [
+ "js-sys",
+ "libc",
+ "wasm-bindgen",
+ "web-sys",
+ "winapi",
+]
+
+[[package]]
+name = "tempfile"
+version = "3.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
+dependencies = [
+ "cfg-if",
+ "fastrand",
+ "libc",
+ "redox_syscall",
+ "remove_dir_all",
+ "winapi",
+]
+
+[[package]]
+name = "termcolor"
+version = "1.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "terminal_size"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "40ca90c434fd12083d1a6bdcbe9f92a14f96c8a1ba600ba451734ac334521f7a"
+dependencies = [
+ "rustix",
+ "windows-sys 0.42.0",
+]
+
+[[package]]
+name = "thiserror"
+version = "1.0.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "time"
+version = "0.1.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
+dependencies = [
+ "libc",
+ "wasi 0.10.0+wasi-snapshot-preview1",
+ "winapi",
+]
+
+[[package]]
+name = "tinyvec"
+version = "1.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
+dependencies = [
+ "tinyvec_macros",
+]
+
+[[package]]
+name = "tinyvec_macros"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
+
+[[package]]
+name = "tokio"
+version = "1.22.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3"
+dependencies = [
+ "autocfg",
+ "num_cpus",
+ "pin-project-lite",
+ "tokio-macros",
+]
+
+[[package]]
+name = "tokio-macros"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tracing"
+version = "0.1.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
+dependencies = [
+ "cfg-if",
+ "log",
+ "pin-project-lite",
+ "tracing-attributes",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-attributes"
+version = "0.1.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "tracing-core"
+version = "0.1.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
+dependencies = [
+ "once_cell",
+]
+
+[[package]]
+name = "tracing-futures"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2"
+dependencies = [
+ "pin-project",
+ "tracing",
+]
+
+[[package]]
+name = "typenum"
+version = "1.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
+
+[[package]]
+name = "unicode-bidi"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
+
+[[package]]
+name = "unicode-normalization"
+version = "0.1.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
+dependencies = [
+ "tinyvec",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
+
+[[package]]
+name = "url"
+version = "2.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"
+dependencies = [
+ "form_urlencoded",
+ "idna",
+ "percent-encoding",
+]
+
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+
+[[package]]
+name = "waker-fn"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
+
+[[package]]
+name = "wasi"
+version = "0.10.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
+dependencies = [
+ "cfg-if",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
+dependencies = [
+ "bumpalo",
+ "log",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
+
+[[package]]
+name = "web-sys"
+version = "0.3.60"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
+dependencies = [
+ "js-sys",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "wepoll-ffi"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "windows-sys"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2"
+dependencies = [
+ "windows_aarch64_msvc 0.36.1",
+ "windows_i686_gnu 0.36.1",
+ "windows_i686_msvc 0.36.1",
+ "windows_x86_64_gnu 0.36.1",
+ "windows_x86_64_msvc 0.36.1",
+]
+
+[[package]]
+name = "windows-sys"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc 0.42.0",
+ "windows_i686_gnu 0.42.0",
+ "windows_i686_msvc 0.42.0",
+ "windows_x86_64_gnu 0.42.0",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc 0.42.0",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.36.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.42.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..c99a2a9
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,33 @@
+[package]
+name = "crunchy-cli"
+version = "0.1.0"
+edition = "2021"
+
+[features]
+default = ["static-curl"]
+
+# Embed a static curl library into the binary instead of just linking it.
+static-curl = ["crunchy-cli-core/static-curl"]
+# Embed a static openssl library into the binary instead of just linking it. If you want to compile this project against
+# musl and have openssl issues, this might solve these issues.
+static-ssl = ["crunchy-cli-core/static-ssl"]
+
+[dependencies]
+tokio = { version = "1.22", features = ["macros", "rt-multi-thread", "time"], default-features = false }
+
+crunchy-cli-core = { path = "./crunchy-cli-core" }
+
+[build-dependencies]
+chrono = "0.4"
+clap = { version = "4.0", features = ["string"] }
+clap_complete = "4.0"
+clap_mangen = "0.2"
+
+# The static-* features must be used here since build dependency features cannot be manipulated from the features
+# specified in this Cargo.toml [features].
+crunchy-cli-core = { path = "./crunchy-cli-core", features = ["static-curl", "static-ssl"] }
+
+[profile.release]
+strip = true
+opt-level = "z"
+lto = true
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 966872b..0000000
--- a/Makefile
+++ /dev/null
@@ -1,31 +0,0 @@
-VERSION=development
-BINARY_NAME=crunchy
-VERSION_BINARY_NAME=$(BINARY_NAME)-v$(VERSION)
-
-DESTDIR=
-PREFIX=/usr
-
-build:
- go build -ldflags "-X 'github.com/crunchy-labs/crunchy-cli/utils.Version=$(VERSION)'" -o $(BINARY_NAME) .
-
-clean:
- rm -f $(BINARY_NAME) $(VERSION_BINARY_NAME)_*
-
-install:
- install -Dm755 $(BINARY_NAME) $(DESTDIR)$(PREFIX)/bin/crunchy-cli
- ln -sf ./crunchy-cli $(DESTDIR)$(PREFIX)/bin/crunchy
- install -Dm644 crunchy-cli.1 $(DESTDIR)$(PREFIX)/share/man/man1/crunchy-cli.1
- install -Dm644 LICENSE $(DESTDIR)$(PREFIX)/share/licenses/crunchy-cli/LICENSE
-
-uninstall:
- rm -f $(DESTDIR)$(PREFIX)/bin/crunchy-cli
- rm -f $(DESTDIR)$(PREFIX)/bin/crunchy
- rm -f $(DESTDIR)$(PREFIX)/share/man/man1/crunchy-cli.1
- rm -f $(DESTDIR)$(PREFIX)/share/licenses/crunchy-cli/LICENSE
-
-release:
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X 'github.com/crunchy-labs/crunchy-cli/utils.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_linux .
- CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-X 'github.com/crunchy-labs/crunchy-cli/utils.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_windows.exe .
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X 'github.com/crunchy-labs/crunchy-cli/utils.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_darwin .
-
- strip $(VERSION_BINARY_NAME)_linux
diff --git a/README.md b/README.md
index cbe6a37..2229739 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# crunchy-cli
-A [Go](https://golang.org) written cli client for [crunchyroll](https://www.crunchyroll.com). To use it, you need a crunchyroll premium account for full access & features.
+A [Rust](https://www.rust-lang.org/) written cli client for [Crunchyroll](https://www.crunchyroll.com).
@@ -21,178 +21,202 @@ A [Go](https://golang.org) written cli client for [crunchyroll](https://www.crun
- CLI 🖥️
+ Usage 🖥️
•
Disclaimer ☝️
•
License ⚖
-_This repo was former known as **crunchyroll-go** (which still exists but now contains only the library part) but got split up into two separate repositories to provide more flexibility._
-
-> This tool relies on the [crunchyroll-go](https://github.com/crunchy-labs/crunchyroll-go) library to communicate with crunchyroll.
-> The library enters maintenance mode (only small fixes, no new features) with version v3 in favor of rewriting it completely in Rust.
-> **crunchy-cli** follows it (with version v2.3.0) and won't have major updates until the Rust rewrite of the library reaches a good usable state.
-
-# 🖥️ CLI
+> We are in no way affiliated with, maintained, authorized, sponsored, or officially associated with Crunchyroll LLC or any of its subsidiaries or affiliates.
+> The official Crunchyroll website can be found at https://crunchyroll.com/.
## ✨ Features
-- Download single videos and entire series from [crunchyroll](https://www.crunchyroll.com).
-- Archive episode or seasons in an `.mkv` file with multiple subtitles and audios and compress them to gzip or zip files.
+- Download single videos and entire series from [Crunchyroll](https://www.crunchyroll.com).
+- Archive episode or seasons in an `.mkv` file with multiple subtitles and audios.
- Specify a range which episodes to download from an anime.
## 💾 Get the executable
-- 📥 Download the latest binaries [here](https://github.com/crunchy-labs/crunchy-cli/releases/latest) or get it from below:
- - [Linux (x64)](https://smartrelease.bytedream.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_linux)
- - [Windows (x64)](https://smartrelease.bytedream.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_windows.exe)
- - [MacOS (x64)](https://smartrelease.bytedream.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_darwin)
-- If you use Arch btw. or any other Linux distro which is based on Arch Linux, you can download the package via the [AUR](https://aur.archlinux.org/packages/crunchyroll-go/):
- ```shell
- $ yay -S crunchy-cli
- ```
-- On Windows [scoop](https://scoop.sh/) can be used to install it (added by [@AdmnJ](https://github.com/AdmnJ)):
- ```shell
- $ scoop bucket add extras # <- in case you haven't added the extra repository already
- $ scoop install crunchyroll-go
- ```
-- 🛠 Build it yourself. Must be done if your target platform is not covered by the [provided binaries](https://github.com/crunchy-labs/crunchy-cli/releases/latest) (like Raspberry Pi or M1 Mac):
- - use `make` (requires `go` to be installed):
- ```shell
- $ git clone https://github.com/crunchy-labs/crunchy-cli
- $ cd crunchy-cli
- $ make
- $ sudo make install # <- only if you want to install it on your system
- ```
- - use `go`:
- ```shell
- $ git clone https://github.com/crunchy-labs/crunchy-cli
- $ cd crunchy-cli
- $ go build -o crunchy .
- ```
+### 📥 Download the latest binaries
-## 📝 Examples
+Checkout the [releases](https://github.com/crunchy-labs/crunchy-cli/releases) tab and get the binary from the newest release.
-_Before reading_: Because of the huge functionality not all cases can be covered in the README. Make sure to check the [wiki](https://github.com/crunchy-labs/crunchy-cli/wiki/Cli), further usages and options are described there.
+### 🛠 Build it yourself
+
+Since we do not support every platform and architecture you may have to build the project yourself.
+This requires [git](https://git-scm.com/) and [Cargo](https://doc.rust-lang.org/cargo).
+```shell
+$ git clone https://github.com/crunchy-labs/crunchy-cli
+$ cd crunchy-cli
+$ cargo build --release
+```
+After the binary has built successfully it is available in `target/release`.
+
+## 🖥️ Usage
+
+> All shown command are just examples
+
+Every command requires you to be logged in with an account.
+It doesn't matter if this account is premium or not, both works (but as free user you do not have access to premium content).
+You can pass your account via credentials (username & password) or refresh token.
+
+- Refresh Token
+ - To get the token you have to log in at [crunchyroll.com](https://www.crunchyroll.com/) and extract the `etp_rt` cookie.
+ The easiest way to get it is via a browser extension with lets you view your cookies, like [Cookie-Editor](https://cookie-editor.cgagnier.ca/) ([Firefox Store](https://addons.mozilla.org/en-US/firefox/addon/cookie-editor/); [Chrome Store](https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm)).
+ If installed, search the `etp_rt` entry and extract the value.
+ - ```shell
+ $ crunchy --etp-rt "abcd1234-zyxw-9876-98zy-a1b2c3d4e5f6"
+ ```
+- Credentials
+ - Credentials must be provided as one single expression.
+ Username and password must be separated by a `:`.
+ - ```shell
+ $ crunchy --credentials "user:password"
+ ```
### Login
-Before you can do something, you have to log in first.
-
-This can be performed via crunchyroll account email and password.
+If you do not want to provide your credentials every time you execute a command, they can be stored permanently on disk.
+This can be done with the `login` subcommand.
```shell
-$ crunchy login user@example.com password
+$ crunchy --etp-rt "abcd1234-zyxw-9876-98zy-a1b2c3d4e5f6" login
```
-or via refresh token / `etp_rt` cookie
-
-```shell
-$ crunchy login --refresh-token 7578ce50-5712-3gef-b97e-01332d6b588c
-```
+Once set, you do not need to provide `--etp-rt` / `--credentials` anymore when using the cli.
### Download
-By default, the cli tries to download the episode with your system language as audio. If no streams with your system language are available, the video will be downloaded with japanese audio and hardsubbed subtitles in your system language.
-**If your system language is not supported, an error message will be displayed and en-US (american english) will be chosen as language.**
+**Supported urls**
+- Single episode
+ ```shell
+ $ crunchy download https://www.crunchyroll.com/watch/GRDQPM1ZY/alone-and-lonesome
+ ```
+- Episode range
+
+ If you want only specific episodes / seasons of an anime you can easily provide the series url along with a _filter_.
+ The filter has to be attached to the url. See the [wiki](https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter) for more information
+ ```shell
+ $ crunchy download https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx[E1]
+ ```
+- Series
+ ```shell
+ $ crunchy download https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+
+**Options**
+- Audio language
-```shell
-$ crunchy download https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
-```
+ Which audio the episode(s) should be can be set via the `-a` / `--audio` flag.
+ This only works if the url points to a series since episode urls are language specific.
+ ```shell
+ $ crunchy download -a de-DE https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is your system language. If not supported by Crunchyroll, `en-US` (American English) is the default.
-With `-r best` the video(s) will have the best available resolution (mostly 1920x1080 / Full HD).
+- Subtitle language
-```shell
-$ crunchy download -r best https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
-```
+ Besides the audio, it's also possible to specify which language the subtitles should have with the `-s` / `--subtitle` flag.
+ The subtitle will be hardsubbed (burned into the video) and thus, can't be turned off or on.
+ ```shell
+ $ crunchy download -s de-DE https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is no subtitle.
-The file is by default saved as a `.ts` (mpeg transport stream) file.
-`.ts` files may can't be played or are looking very weird (it depends on the video player you are using). With the `-o` flag, you can change the name (and file ending) of the output file. So if you want to save it as, for example, `mp4`
-file, just name it `whatever.mp4`.
-**You need [ffmpeg](https://ffmpeg.org) to store the video in other file formats.**
+- Output filename
-```shell
-$ crunchy download -o "daaaaaaaaaaaaaaaarling.ts" https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
-```
+ You can specify the name of the output file with the `-o` / `--output` flag.
+ If you want to use any other file format than [`.ts`](https://en.wikipedia.org/wiki/MPEG_transport_stream) you need [ffmpeg](https://ffmpeg.org/).
+ ```shell
+ $ crunchy download -o "ditf.ts" https://www.crunchyroll.com/watch/GRDQPM1ZY/alone-and-lonesome
+ ```
+ Default is `{title}.ts`.
-With the `--audio` flag you can specify which audio the video should have and with `--subtitle` which subtitle it should have. Type `crunchy help download` to see all available locales.
+- Resolution
-```shell
-$ crunchy download --audio ja-JP --subtitle de-DE https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
-```
-
-##### Flags
-
-The following flags can be (optional) passed to modify the [download](#download) process.
-
-| Short | Extended | Description |
-|-------|----------------|--------------------------------------------------------------------------------|
-| `-a` | `--audio` | Forces audio of the video(s). |
-| `-s` | `--subtitle` | Forces subtitle of the video(s). |
-| `-d` | `--directory` | Directory to download the video(s) to. |
-| `-o` | `--output` | Name of the output file. |
-| `-r` | `--resolution` | The resolution of the video(s). `best` for best resolution, `worst` for worst. |
-| `-g` | `--goroutines` | Sets how many parallel segment downloads should be used. |
+ The resolution for videos can be set via the `-r` / `--resolution` flag.
+ ```shell
+ $ crunchy download -r worst https://www.crunchyroll.com/watch/GRDQPM1ZY/alone-and-lonesome
+ ```
+ Default is `best`.
### Archive
-Archive works just like [download](#download). It downloads the given videos as `.mkv` files and stores all (soft) subtitles in it. Default audio locales are japanese and your system language (if available) but you can set more or less with
-the `--language` flag.
+**Supported urls**
+- Series
-Archive a file
+ Only series urls are supported since single episode urls are (audio) language locked.
+ ```shell
+ $ crunchy archive https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+
+**Options**
+- Audio languages
-```shell
-$ crunchy archive https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
-```
+ Which audios the episode(s) should be can be set via the `-a` / `--audio` flag.
+ ```shell
+ $ crunchy archive -a ja-JP -a de-DE https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Can be used multiple times.
+ Default is your system language (if not supported by Crunchyroll, `en-US` (American English) is the default) + `ja-JP` (Japanese).
-Downloads the first two episode of Darling in the FranXX and stores it compressed in a file.
+- Subtitle languages
-```shell
-$ crunchy archive -c "ditf.tar.gz" https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
-```
+ Besides the audio, it's also possible to specify which languages the subtitles should have with the `-s` / `--subtitle` flag.
+ ```shell
+ $ crunchy archive -s de-DE https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is all subtitles.
-##### Flags
+- Output filename
-The following flags can be (optional) passed to modify the [archive](#archive) process.
+ You can specify the name of the output file with the `-o` / `--output` flag.
+ The only supported file / container format is [`.mkv`](https://en.wikipedia.org/wiki/Matroska) since it stores / can store multiple audio, video and subtitle streams.
+ ```shell
+ $ crunchy archive -o "{title}.mkv" https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is `{title}.mkv`.
-| Short | Extended | Description |
-|-------|----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `-l` | `--language` | Audio locale which should be downloaded. Can be used multiple times. |
-| `-d` | `--directory` | Directory to download the video(s) to. |
-| `-o` | `--output` | Name of the output file. |
-| `-m` | `--merge` | Sets the behavior of the stream merging. Valid behaviors are 'auto', 'audio', 'video'. See the [wiki](https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#archive) for more information. |
-| `-c` | `--compress` | If is set, all output will be compresses into an archive. This flag sets the name of the compressed output file and the file ending specifies the compression algorithm (gzip, tar, zip are supported). |
-| `-r` | `--resolution` | The resolution of the video(s). `best` for best resolution, `worst` for worst. |
-| `-g` | `--goroutines` | Sets how many parallel segment downloads should be used. |
+- Resolution
+ The resolution for videos can be set via the `-r` / `--resolution` flag.
+ ```shell
+ $ crunchy archive -r worst https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is `best`.
-### Info
+- Merge behavior
-The `info` displays some information about the account which is used for the cli.
+ Because of local restrictions (or other reasons) some episodes with different languages does not have the same length (e.g. when some scenes were cut out).
+ The ideal state, when multiple audios & subtitles used, would be if only one _video_ has to be stored and all other languages can be stored as audio-only.
+ But, as said, this is not always the case.
+ With the `-m` / `--merge` flag you can set what you want to do if some video lengths differ.
+ Valid options are `audio` - store one video and all other languages as audio only; `video` - store the video + audio for every language; `auto` - detect if videos differ in length: if so, behave like `video` else like `audio`.
+ Subtitles will always match to the first / primary audio and video.
+ ```shell
+ $ crunchy archive -m audio https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is `auto`.
-```shell
-$ crunchy info
-```
+- Default subtitle
-### Update
+ `--default_subtitle` set which subtitle language should be set as default / auto appear when starting the downloaded video(s).
+ ```shell
+ $ crunchy archive --default_subtitle en-US https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
+ Default is none.
-If you want to update your local version of `crunchy-cli`, this command makes this easier.
-It checks if a new version is available and if so, updates itself.
+- No subtitle optimizations
-```shell
-$ crunchy update
-```
-
-### Global flags
-
-These flags you can use across every sub-command:
-
-| Flag | Description |
-|------|------------------------------------------------------|
-| `-q` | Disables all output. |
-| `-v` | Shows additional debug output. |
-| `-p` | Use a proxy to hide your ip / redirect your traffic. |
+ Subtitles, as Crunchyroll delivers them, look weird in some video players (#66).
+ This can be fixed by adding a specific entry to the subtitles.
+ But since this entry is only a de-factor standard and not represented in the official specification of the subtitle format ([`.ass`](https://en.wikipedia.org/wiki/SubStation_Alpha)) it could cause issues with some video players (but no issue got reported so far, so it's relatively safe to use).
+ `--no_subtitle_optimizations` can disable these optimizations.
+ ```shell
+ $ crunchy archive --no_subtitle_optimizations https://www.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx
+ ```
# ☝️ Disclaimer
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..cc6a0cc
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,112 @@
+use clap::{Command, CommandFactory};
+use clap_complete::shells;
+use std::path::{Path, PathBuf};
+
+// this build file generates completions for various shells as well as manual pages
+
+fn main() -> std::io::Result<()> {
+ // do not generate anything when building non release
+ if cfg!(debug_assertions) {
+ return Ok(());
+ }
+
+ // note that we're using an anti-pattern here / violate the rust conventions. build script are
+ // not supposed to write outside of 'OUT_DIR'. to have the generated files in the build "root"
+ // (the same directory where the output binary lives) is much simpler than in 'OUT_DIR' since
+ // its nested in sub directories and is difficult to find (at least more difficult than in the
+ // build root)
+ let unconventional_out_dir =
+ std::path::PathBuf::from(std::env::var_os("OUT_DIR").ok_or(std::io::ErrorKind::NotFound)?)
+ .parent()
+ .unwrap()
+ .parent()
+ .unwrap()
+ .parent()
+ .unwrap()
+ .to_path_buf();
+
+ let completions_dir = exist_or_create_dir(unconventional_out_dir.join("completions"))?;
+ let manpage_dir = exist_or_create_dir(unconventional_out_dir.join("manpages"))?;
+
+ generate_completions(completions_dir)?;
+ generate_manpages(manpage_dir)?;
+
+ Ok(())
+}
+
+fn exist_or_create_dir(path: PathBuf) -> std::io::Result {
+ if !path.exists() {
+ std::fs::create_dir(path.clone())?
+ }
+ Ok(path)
+}
+
+fn generate_completions(out_dir: PathBuf) -> std::io::Result<()> {
+ let mut command: Command = crunchy_cli_core::Cli::command();
+
+ clap_complete::generate_to(
+ shells::Bash,
+ &mut command.clone(),
+ "crunchy-cli",
+ out_dir.clone(),
+ )?;
+ clap_complete::generate_to(
+ shells::Elvish,
+ &mut command.clone(),
+ "crunchy-cli",
+ out_dir.clone(),
+ )?;
+ println!(
+ "{}",
+ clap_complete::generate_to(
+ shells::Fish,
+ &mut command.clone(),
+ "crunchy-cli",
+ out_dir.clone(),
+ )?
+ .to_string_lossy()
+ );
+ clap_complete::generate_to(
+ shells::PowerShell,
+ &mut command.clone(),
+ "crunchy-cli",
+ out_dir.clone(),
+ )?;
+ clap_complete::generate_to(shells::Zsh, &mut command, "crunchy-cli", out_dir)?;
+
+ Ok(())
+}
+
+fn generate_manpages(out_dir: PathBuf) -> std::io::Result<()> {
+ fn generate_command_manpage(
+ mut command: Command,
+ base_path: &Path,
+ sub_name: &str,
+ ) -> std::io::Result<()> {
+ let (file_name, title) = if sub_name.is_empty() {
+ command = command.name("crunchy-cli");
+ ("crunchy-cli.1".to_string(), "crunchy-cli".to_string())
+ } else {
+ command = command.name(format!("crunchy-cli {}", sub_name));
+ (
+ format!("crunchy-cli-{}.1", sub_name),
+ format!("crunchy-cli-{}", sub_name),
+ )
+ };
+
+ let mut command_buf = vec![];
+ let man = clap_mangen::Man::new(command)
+ .title(title)
+ .date(chrono::Utc::now().format("%b %d, %Y").to_string());
+ man.render(&mut command_buf)?;
+
+ std::fs::write(base_path.join(file_name), command_buf)
+ }
+
+ generate_command_manpage(crunchy_cli_core::Cli::command(), &out_dir, "")?;
+ generate_command_manpage(crunchy_cli_core::Archive::command(), &out_dir, "archive")?;
+ generate_command_manpage(crunchy_cli_core::Download::command(), &out_dir, "download")?;
+ generate_command_manpage(crunchy_cli_core::Login::command(), &out_dir, "login")?;
+
+ Ok(())
+}
diff --git a/cli/commands/archive/archive.go b/cli/commands/archive/archive.go
deleted file mode 100644
index 744635f..0000000
--- a/cli/commands/archive/archive.go
+++ /dev/null
@@ -1,818 +0,0 @@
-package archive
-
-import (
- "bufio"
- "bytes"
- "context"
- "fmt"
- "io"
- "math"
- "os"
- "os/exec"
- "os/signal"
- "path/filepath"
- "regexp"
- "runtime"
- "sort"
- "strconv"
- "strings"
-
- "github.com/crunchy-labs/crunchy-cli/cli/commands"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "github.com/crunchy-labs/crunchyroll-go/v3"
- crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
- "github.com/grafov/m3u8"
- "github.com/spf13/cobra"
-)
-
-var (
- archiveLanguagesFlag []string
- archiveSubLanguagesFlag []string
-
- archiveDirectoryFlag string
- archiveOutputFlag string
- archiveTempDirFlag string
-
- archiveMergeFlag string
-
- archiveCompressFlag string
-
- archiveResolutionFlag string
-
- archiveGoroutinesFlag int
-)
-
-var Cmd = &cobra.Command{
- Use: "archive",
- Short: "Stores the given videos with all subtitles and multiple audios in a .mkv file",
- Args: cobra.MinimumNArgs(1),
-
- PreRunE: func(cmd *cobra.Command, args []string) error {
- utils.Log.Debug("Validating arguments")
-
- if !utils.HasFFmpeg() {
- return fmt.Errorf("ffmpeg is needed to run this command correctly")
- }
- utils.Log.Debug("FFmpeg detected")
-
- if filepath.Ext(archiveOutputFlag) != ".mkv" {
- return fmt.Errorf("currently only matroska / .mkv files are supported")
- }
-
- for _, locale := range archiveLanguagesFlag {
- if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(locale)) {
- // if locale is 'all', match all known locales
- if locale == "all" {
- archiveLanguagesFlag = utils.LocalesAsStrings()
- break
- }
- return fmt.Errorf("%s is not a valid locale. Choose from: %s", locale, strings.Join(utils.LocalesAsStrings(), ", "))
- }
- }
- utils.Log.Debug("Using following audio locales: %s", strings.Join(archiveLanguagesFlag, ", "))
-
- for _, locale := range archiveSubLanguagesFlag {
- if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(locale)) {
- // if locale is 'all', match all known locales
- if locale == "all" {
- archiveSubLanguagesFlag = utils.LocalesAsStrings()
- break
- }
- return fmt.Errorf("%s is not a valid locale for Subtitels. Choose from: %s", locale, strings.Join(utils.LocalesAsStrings(), ", "))
- }
- }
- utils.Log.Debug("Using following subtitels locales: %s", strings.Join(archiveSubLanguagesFlag, ", "))
-
- var found bool
- for _, mode := range []string{"auto", "audio", "video"} {
- if archiveMergeFlag == mode {
- utils.Log.Debug("Using %s merge behavior", archiveMergeFlag)
- found = true
- break
- }
- }
- if !found {
- return fmt.Errorf("'%s' is no valid merge flag. Use 'auto', 'audio' or 'video'", archiveMergeFlag)
- }
-
- if archiveCompressFlag != "" {
- found = false
- for _, algo := range []string{".tar", ".tar.gz", ".tgz", ".zip"} {
- if strings.HasSuffix(archiveCompressFlag, algo) {
- utils.Log.Debug("Using %s compression", algo)
- found = true
- break
- }
- }
- if !found {
- return fmt.Errorf("'%s' is no valid compress algorithm. Valid algorithms / file endings are '.tar', '.tar.gz', '.zip'",
- archiveCompressFlag)
- }
- }
-
- switch archiveResolutionFlag {
- case "1080p", "720p", "480p", "360p":
- intRes, _ := strconv.ParseFloat(strings.TrimSuffix(archiveResolutionFlag, "p"), 84)
- archiveResolutionFlag = fmt.Sprintf("%.0fx%s", math.Ceil(intRes*(float64(16)/float64(9))), strings.TrimSuffix(archiveResolutionFlag, "p"))
- case "240p":
- // 240p would round up to 427x240 if used in the case statement above, so it has to be handled separately
- archiveResolutionFlag = "428x240"
- case "1920x1080", "1280x720", "640x480", "480x360", "428x240", "best", "worst":
- default:
- return fmt.Errorf("'%s' is not a valid resolution", archiveResolutionFlag)
- }
- utils.Log.Debug("Using resolution '%s'", archiveResolutionFlag)
-
- return nil
- },
- RunE: func(cmd *cobra.Command, args []string) error {
- if err := commands.LoadCrunchy(); err != nil {
- return err
- }
-
- return archive(args)
- },
-}
-
-func init() {
- Cmd.Flags().StringSliceVarP(&archiveLanguagesFlag,
- "language",
- "l",
- []string{string(utils.SystemLocale(false)), string(crunchyroll.JP)},
- "Audio locale which should be downloaded. Can be used multiple times")
-
- Cmd.Flags().StringSliceVarP(&archiveSubLanguagesFlag,
- "sublang",
- "s",
- utils.LocalesAsStrings(),
- "Subtitles langs which should be downloaded. Can be used multiple times")
-
- cwd, _ := os.Getwd()
-
- Cmd.Flags().StringVarP(&archiveDirectoryFlag,
- "directory",
- "d",
- cwd,
- "The directory to store the files into")
-
- Cmd.Flags().StringVarP(&archiveOutputFlag,
- "output",
- "o",
- "{title}.mkv",
- "Name of the output file. If you use the following things in the name, the will get replaced:\n"+
- "\t{title} » Title of the video\n"+
- "\t{series_name} » Name of the series\n"+
- "\t{season_name} » Name of the season\n"+
- "\t{season_number} » Number of the season\n"+
- "\t{episode_number} » Number of the episode\n"+
- "\t{resolution} » Resolution of the video\n"+
- "\t{fps} » Frame Rate of the video\n"+
- "\t{audio} » Audio locale of the video\n"+
- "\t{subtitle} » Subtitle locale of the video")
-
- Cmd.Flags().StringVar(&archiveTempDirFlag,
- "temp",
- os.TempDir(),
- "Directory to store temporary files in")
-
- Cmd.Flags().StringVarP(&archiveMergeFlag,
- "merge",
- "m",
- "auto",
- "Sets the behavior of the stream merging. Valid behaviors are 'auto', 'audio', 'video'")
-
- Cmd.Flags().StringVarP(&archiveCompressFlag,
- "compress",
- "c",
- "",
- "If is set, all output will be compresses into an archive (every url generates a new one). "+
- "This flag sets the name of the compressed output file. The file ending specifies the compression algorithm. "+
- "The following algorithms are supported: gzip, tar, zip")
-
- Cmd.Flags().StringVarP(&archiveResolutionFlag,
- "resolution",
- "r",
- "best",
- "The video resolution. Can either be specified via the pixels, the abbreviation for pixels, or 'common-use' words\n"+
- "\tAvailable pixels: 1920x1080, 1280x720, 640x480, 480x360, 428x240\n"+
- "\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
- "\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
-
- Cmd.Flags().IntVarP(&archiveGoroutinesFlag,
- "goroutines",
- "g",
- runtime.NumCPU(),
- "Number of parallel segment downloads")
-}
-
-func archive(urls []string) error {
- for i, url := range urls {
- utils.Log.SetProcess("Parsing url %d", i+1)
- episodes, err := archiveExtractEpisodes(url)
- if err != nil {
- utils.Log.StopProcess("Failed to parse url %d", i+1)
- if utils.Crunchy.Config.Premium {
- utils.Log.Debug("If the error says no episodes could be found but the passed url is correct and a crunchyroll classic url, " +
- "try the corresponding crunchyroll beta url instead and try again. See https://github.com/crunchy-labs/crunchy-cli/issues/22 for more information")
- }
- return err
- }
- utils.Log.StopProcess("Parsed url %d", i+1)
-
- var compressFile *os.File
- var c Compress
-
- if archiveCompressFlag != "" {
- compressFile, err = os.Create(utils.GenerateFilename(archiveCompressFlag, ""))
- if err != nil {
- return fmt.Errorf("failed to create archive file: %v", err)
- }
- if strings.HasSuffix(archiveCompressFlag, ".tar") {
- c = NewTarCompress(compressFile)
- } else if strings.HasSuffix(archiveCompressFlag, ".tar.gz") || strings.HasSuffix(archiveCompressFlag, ".tgz") {
- c = NewGzipCompress(compressFile)
- } else if strings.HasSuffix(archiveCompressFlag, ".zip") {
- c = NewZipCompress(compressFile)
- }
- }
-
- for _, season := range episodes {
- utils.Log.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
-
- for j, info := range season {
- utils.Log.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
- j+1,
- info.Title,
- info.Resolution,
- info.FPS,
- info.SeasonNumber,
- info.EpisodeNumber)
- }
- }
- utils.Log.Empty()
-
- for j, season := range episodes {
- for k, info := range season {
- var filename string
- var writeCloser io.WriteCloser
- if c != nil {
- filename = info.FormatString(archiveOutputFlag)
- writeCloser, err = c.NewFile(info)
- if err != nil {
- return fmt.Errorf("failed to pre generate new archive file: %v", err)
- }
- } else {
- dir := info.FormatString(archiveDirectoryFlag)
- if _, err = os.Stat(dir); os.IsNotExist(err) {
- if err = os.MkdirAll(dir, 0777); err != nil {
- return fmt.Errorf("error while creating directory: %v", err)
- }
- }
- filename = utils.GenerateFilename(info.FormatString(archiveOutputFlag), dir)
- writeCloser, err = os.Create(filename)
- if err != nil {
- return fmt.Errorf("failed to create new file: %v", err)
- }
- }
-
- if err = archiveInfo(info, writeCloser, filename); err != nil {
- writeCloser.Close()
- if f, ok := writeCloser.(*os.File); ok {
- os.Remove(f.Name())
- } else {
- c.Close()
- compressFile.Close()
- os.RemoveAll(compressFile.Name())
- }
- return err
- }
- writeCloser.Close()
-
- if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
- utils.Log.Empty()
- }
- }
- }
- if c != nil {
- c.Close()
- }
- if compressFile != nil {
- compressFile.Close()
- }
- }
- return nil
-}
-
-func archiveInfo(info utils.FormatInformation, writeCloser io.WriteCloser, filename string) error {
- utils.Log.Debug("Entering season %d, episode %d with %d additional formats", info.SeasonNumber, info.EpisodeNumber, len(info.AdditionalFormats))
-
- dp, err := createArchiveProgress(info)
- if err != nil {
- return fmt.Errorf("error while setting up downloader: %v", err)
- }
- defer func() {
- if dp.Total != dp.Current {
- fmt.Println()
- }
- }()
-
- rootFile, err := os.CreateTemp("", fmt.Sprintf("%s_*.ts", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))))
- if err != nil {
- return fmt.Errorf("failed to create temp file: %v", err)
- }
- defer os.Remove(rootFile.Name())
- defer rootFile.Close()
-
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- downloader := crunchyroll.NewDownloader(ctx, rootFile, archiveGoroutinesFlag, func(segment *m3u8.MediaSegment, current, total int, file *os.File) error {
- // check if the context was cancelled.
- // must be done in to not print any progress messages if ctrl+c was pressed
- if ctx.Err() != nil {
- return nil
- }
-
- if utils.Log.IsDev() {
- dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
- } else {
- dp.Update()
- }
-
- if current == total {
- dp.UpdateMessage("Merging segments", false)
- }
- return nil
- })
- tmp, _ := os.MkdirTemp(archiveTempDirFlag, "crunchy_")
- downloader.TempDir = tmp
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt)
- go func() {
- select {
- case <-sig:
- signal.Stop(sig)
- utils.Log.Err("Exiting... (may take a few seconds)")
- utils.Log.Err("To force exit press ctrl+c (again)")
- cancel()
- // os.Exit(1) is not called since an immediate exit after the cancel function does not let
- // the download process enough time to stop gratefully. A result of this is that the temporary
- // directory where the segments are downloaded to will not be deleted
- case <-ctx.Done():
- // this is just here to end the goroutine and prevent it from running forever without a reason
- }
- }()
- utils.Log.Debug("Set up signal catcher")
-
- var additionalDownloaderOpts []string
- var mergeMessage string
- switch archiveMergeFlag {
- case "auto":
- additionalDownloaderOpts = []string{"-vn"}
- for _, format := range info.AdditionalFormats {
- if format.Video.Bandwidth != info.Format.Video.Bandwidth {
- // revoke the changed FFmpegOpts above
- additionalDownloaderOpts = []string{}
- break
- }
- }
- if len(additionalDownloaderOpts) > 0 {
- mergeMessage = "merging audio for additional formats"
- } else {
- mergeMessage = "merging video for additional formats"
- }
- case "audio":
- additionalDownloaderOpts = []string{"-vn"}
- mergeMessage = "merging audio for additional formats"
- case "video":
- mergeMessage = "merging video for additional formats"
- }
-
- utils.Log.Info("Downloading episode `%s` to `%s` (%s)", info.Title, filepath.Base(filename), mergeMessage)
- utils.Log.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
- utils.Log.Info("\tAudio: %s", info.Audio)
- utils.Log.Info("\tSubtitle: %s", info.Subtitle)
- utils.Log.Info("\tResolution: %spx", info.Resolution)
- utils.Log.Info("\tFPS: %.2f", info.FPS)
-
- var videoFiles, audioFiles, subtitleFiles []string
- defer func() {
- for _, f := range append(append(videoFiles, audioFiles...), subtitleFiles...) {
- os.RemoveAll(f)
- }
- }()
-
- var f []string
- if f, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.Format); err != nil {
- if err != ctx.Err() {
- return fmt.Errorf("error while downloading: %v", err)
- }
- return err
- }
- videoFiles = append(videoFiles, f[0])
-
- if len(additionalDownloaderOpts) == 0 {
- var videos []string
- downloader.FFmpegOpts = additionalDownloaderOpts
- if videos, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.AdditionalFormats...); err != nil {
- return fmt.Errorf("error while downloading additional videos: %v", err)
- }
- downloader.FFmpegOpts = []string{}
- videoFiles = append(videoFiles, videos...)
- } else {
- var audios []string
- if audios, err = archiveDownloadVideos(downloader, filepath.Base(filename), false, info.AdditionalFormats...); err != nil {
- return fmt.Errorf("error while downloading additional videos: %v", err)
- }
- audioFiles = append(audioFiles, audios...)
- }
-
- sort.Sort(crunchyUtils.SubtitlesByLocale(info.Format.Subtitles))
-
- sortSubtitles, _ := strconv.ParseBool(os.Getenv("SORT_SUBTITLES"))
- if sortSubtitles && len(archiveLanguagesFlag) > 0 {
- // this sort the subtitle locales after the languages which were specified
- // with the `archiveLanguagesFlag` flag
- for _, language := range archiveLanguagesFlag {
- for i, subtitle := range info.Format.Subtitles {
- if subtitle.Locale == crunchyroll.LOCALE(language) {
- info.Format.Subtitles = append([]*crunchyroll.Subtitle{subtitle}, append(info.Format.Subtitles[:i], info.Format.Subtitles[i+1:]...)...)
- break
- }
- }
- }
- }
-
- var subtitles []string
- if subtitles, err = archiveDownloadSubtitles(filepath.Base(filename), info.Format.Subtitles...); err != nil {
- return fmt.Errorf("error while downloading subtitles: %v", err)
- }
- subtitleFiles = append(subtitleFiles, subtitles...)
-
- if err = archiveFFmpeg(ctx, writeCloser, videoFiles, audioFiles, subtitleFiles); err != nil {
- return fmt.Errorf("failed to merge files: %v", err)
- }
-
- dp.UpdateMessage("Download finished", false)
-
- signal.Stop(sig)
- utils.Log.Debug("Stopped signal catcher")
-
- utils.Log.Empty()
-
- return nil
-}
-
-func createArchiveProgress(info utils.FormatInformation) (*commands.DownloadProgress, error) {
- var progressCount int
- if err := info.Format.InitVideo(); err != nil {
- return nil, fmt.Errorf("error while initializing a video: %v", err)
- }
- // + number of segments a video has +1 is for merging
- progressCount += int(info.Format.Video.Chunklist.Count()) + 1
- for _, f := range info.AdditionalFormats {
- if f == info.Format {
- continue
- }
-
- if err := f.InitVideo(); err != nil {
- return nil, err
- }
- // + number of segments a video has +1 is for merging
- progressCount += int(f.Video.Chunklist.Count()) + 1
- }
-
- dp := &commands.DownloadProgress{
- Prefix: utils.Log.(*commands.Logger).InfoLog.Prefix(),
- Message: "Downloading video",
- // number of segments a video +1 is for the success message
- Total: progressCount + 1,
- Dev: utils.Log.IsDev(),
- Quiet: utils.Log.(*commands.Logger).IsQuiet(),
- }
- if utils.Log.IsDev() {
- dp.Prefix = utils.Log.(*commands.Logger).DebugLog.Prefix()
- }
-
- return dp, nil
-}
-
-func archiveDownloadVideos(downloader crunchyroll.Downloader, filename string, video bool, formats ...*crunchyroll.Format) ([]string, error) {
- var files []string
-
- for _, format := range formats {
- var name string
- if video {
- name = fmt.Sprintf("%s_%s_video_*.ts", filename, format.AudioLocale)
- } else {
- name = fmt.Sprintf("%s_%s_audio_*.aac", filename, format.AudioLocale)
- }
-
- f, err := os.CreateTemp("", name)
- if err != nil {
- return nil, err
- }
- files = append(files, f.Name())
-
- downloader.Writer = f
- if err = format.Download(downloader); err != nil {
- f.Close()
- for _, file := range files {
- os.Remove(file)
- }
- return nil, err
- }
- f.Close()
-
- utils.Log.Debug("Downloaded '%s' video", format.AudioLocale)
- }
-
- return files, nil
-}
-
-func archiveDownloadSubtitles(filename string, subtitles ...*crunchyroll.Subtitle) ([]string, error) {
- var files []string
-
- for _, subtitle := range subtitles {
- if !utils.ElementInSlice(string(subtitle.Locale), archiveSubLanguagesFlag) {
- continue
- }
-
- f, err := os.CreateTemp("", fmt.Sprintf("%s_%s_subtitle_*.ass", filename, subtitle.Locale))
- if err != nil {
- return nil, err
- }
- files = append(files, f.Name())
-
- if err := subtitle.Save(f); err != nil {
- f.Close()
- for _, file := range files {
- os.Remove(file)
- }
- return nil, err
- }
- f.Close()
-
- utils.Log.Debug("Downloaded '%s' subtitles", subtitle.Locale)
- }
-
- return files, nil
-}
-
-func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, subtitleFiles []string) error {
- var input, maps, metadata []string
- re := regexp.MustCompile(`(?m)_([a-z]{2}-([A-Z]{2}|[0-9]{3}))_(video|audio|subtitle)`)
- // https://github.com/crunchy-labs/crunchy-cli/issues/32
- videoLength32Fix := regexp.MustCompile(`Duration:\s?(\d+):(\d+):(\d+).(\d+),`)
-
- videoLength := [4]int{0, 0, 0, 0}
-
- for i, video := range videoFiles {
- input = append(input, "-i", video)
- maps = append(maps, "-map", strconv.Itoa(i))
- locale := crunchyroll.LOCALE(re.FindStringSubmatch(video)[1])
- metadata = append(metadata, fmt.Sprintf("-metadata:s:v:%d", i), fmt.Sprintf("language=%s", locale))
- metadata = append(metadata, fmt.Sprintf("-metadata:s:v:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
- metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("language=%s", locale))
- metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
-
- var errBuf bytes.Buffer
- cmd := exec.CommandContext(ctx, "ffmpeg", "-i", video)
- cmd.Stderr = &errBuf
- cmd.Run()
-
- matches := videoLength32Fix.FindStringSubmatch(errBuf.String())
- hours, _ := strconv.Atoi(matches[1])
- minutes, _ := strconv.Atoi(matches[2])
- seconds, _ := strconv.Atoi(matches[3])
- millis, _ := strconv.Atoi(matches[4])
-
- if hours > videoLength[0] {
- videoLength = [4]int{hours, minutes, seconds, millis}
- } else if hours == videoLength[0] && minutes > videoLength[1] {
- videoLength = [4]int{hours, minutes, seconds, millis}
- } else if hours == videoLength[0] && minutes == videoLength[1] && seconds > videoLength[2] {
- videoLength = [4]int{hours, minutes, seconds, millis}
- } else if hours == videoLength[0] && minutes == videoLength[1] && seconds == videoLength[2] && millis > videoLength[3] {
- videoLength = [4]int{hours, minutes, seconds, millis}
- }
- }
-
- for i, audio := range audioFiles {
- input = append(input, "-i", audio)
- maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles))+":1")
- locale := crunchyroll.LOCALE(re.FindStringSubmatch(audio)[1])
- metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i+len(videoFiles)), fmt.Sprintf("language=%s", locale))
- metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i+len(videoFiles)), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
- }
-
- for i, subtitle := range subtitleFiles {
- input = append(input, "-i", subtitle)
- maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles)+len(audioFiles)))
- locale := crunchyroll.LOCALE(re.FindStringSubmatch(subtitle)[1])
- metadata = append(metadata, fmt.Sprintf("-metadata:s:s:%d", i), fmt.Sprintf("language=%s", locale))
- metadata = append(metadata, fmt.Sprintf("-metadata:s:s:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
- }
-
- commandOptions := []string{"-y"}
- commandOptions = append(commandOptions, input...)
- commandOptions = append(commandOptions, maps...)
- commandOptions = append(commandOptions, metadata...)
- // we have to create a temporary file here because it must be seekable
- // for ffmpeg.
- // ffmpeg could write to dst too, but this would require to re-encode
- // the audio which results in much higher time and resource consumption
- // (0-1 second with the temp file, ~20 seconds with re-encoding on my system)
- file, err := os.CreateTemp("", "")
- if err != nil {
- return err
- }
- file.Close()
- defer os.Remove(file.Name())
-
- commandOptions = append(commandOptions, "-disposition:s:0", "0", "-c", "copy", "-f", "matroska", file.Name())
-
- // just a little nicer debug output to copy and paste the ffmpeg for debug reasons
- if utils.Log.IsDev() {
- var debugOptions []string
-
- for _, option := range commandOptions {
- if strings.HasPrefix(option, "title=") {
- debugOptions = append(debugOptions, "title=\""+strings.TrimPrefix(option, "title=")+"\"")
- } else if strings.HasPrefix(option, "language=") {
- debugOptions = append(debugOptions, "language=\""+strings.TrimPrefix(option, "language=")+"\"")
- } else if strings.Contains(option, " ") {
- debugOptions = append(debugOptions, "\""+option+"\"")
- } else {
- debugOptions = append(debugOptions, option)
- }
- }
- utils.Log.Debug("FFmpeg merge command: ffmpeg %s", strings.Join(debugOptions, " "))
- }
-
- var errBuf bytes.Buffer
- cmd := exec.CommandContext(ctx, "ffmpeg", commandOptions...)
- cmd.Stderr = &errBuf
- if err = cmd.Run(); err != nil {
- return fmt.Errorf(errBuf.String())
- }
-
- file, err = os.Open(file.Name())
- if err != nil {
- return err
- }
- defer file.Close()
-
- errBuf.Reset()
- cmd = exec.CommandContext(ctx, "ffmpeg", "-i", file.Name())
- cmd.Stderr = &errBuf
- cmd.Run()
-
- matches := videoLength32Fix.FindStringSubmatch(errBuf.String())
- hours, _ := strconv.Atoi(matches[1])
- minutes, _ := strconv.Atoi(matches[2])
- seconds, _ := strconv.Atoi(matches[3])
- millis, _ := strconv.Atoi(matches[4])
-
- var reencode bool
- if hours > videoLength[0] {
- reencode = true
- } else if hours == videoLength[0] && minutes > videoLength[1] {
- reencode = true
- } else if hours == videoLength[0] && minutes == videoLength[1] && seconds > videoLength[2] {
- reencode = true
- } else if hours == videoLength[0] && minutes == videoLength[1] && seconds == videoLength[2] && millis > videoLength[3] {
- reencode = true
- }
-
- // very dirty solution to https://github.com/crunchy-labs/crunchy-cli/issues/32.
- // this might get triggered when not needed but there is currently no easy way to
- // bypass this unwanted triggering
- if reencode {
- utils.Log.Debug("Re-encode to short video length")
-
- file.Close()
-
- tmpFile, _ := os.CreateTemp("", filepath.Base(file.Name())+"-32_fix")
- tmpFile.Close()
-
- errBuf.Reset()
- cmd = exec.CommandContext(ctx, "ffmpeg",
- "-y",
- "-i", file.Name(),
- "-map", "0",
- "-c", "copy",
- "-disposition:s:0", "0",
- "-t", fmt.Sprintf("%02d:%02d:%02d.%d", videoLength[0], videoLength[1], videoLength[2], videoLength[3]),
- "-f", "matroska",
- tmpFile.Name())
- cmd.Stderr = &errBuf
- if err = cmd.Run(); err != nil {
- return fmt.Errorf(errBuf.String())
- }
-
- os.Remove(file.Name())
- os.Rename(tmpFile.Name(), file.Name())
-
- file, err = os.Open(file.Name())
- if err != nil {
- return err
- }
- defer file.Close()
- }
-
- _, err = bufio.NewWriter(dst).ReadFrom(file)
- return err
-}
-
-func archiveExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
- var hasJapanese bool
- languagesAsLocale := []crunchyroll.LOCALE{crunchyroll.JP}
- for _, language := range archiveLanguagesFlag {
- locale := crunchyroll.LOCALE(language)
- if locale == crunchyroll.JP {
- hasJapanese = true
- } else {
- languagesAsLocale = append(languagesAsLocale, locale)
- }
- }
-
- if _, ok := crunchyroll.ParseBetaEpisodeURL(url); ok {
- return nil, fmt.Errorf("archiving episodes by url is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
- } else if _, _, _, _, ok := crunchyroll.ParseEpisodeURL(url); ok {
- return nil, fmt.Errorf("archiving episodes by url is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
- }
-
- episodes, err := utils.ExtractEpisodes(url, languagesAsLocale...)
- if err != nil {
- return nil, err
- }
-
- if !hasJapanese && len(episodes[1:]) == 0 {
- return nil, fmt.Errorf("no episodes found")
- }
-
- for i, eps := range episodes {
- if len(eps) == 0 {
- utils.Log.SetProcess("%s has no matching episodes", languagesAsLocale[i])
- } else if len(episodes[0]) > len(eps) {
- utils.Log.SetProcess("%s has %d less episodes than existing in japanese (%d)", languagesAsLocale[i], len(episodes[0])-len(eps), len(episodes[0]))
- }
- }
-
- if !hasJapanese {
- episodes = episodes[1:]
- }
-
- eps := make(map[int]map[int]*utils.FormatInformation)
- for _, lang := range episodes {
- for _, season := range crunchyUtils.SortEpisodesBySeason(lang) {
- if _, ok := eps[season[0].SeasonNumber]; !ok {
- eps[season[0].SeasonNumber] = map[int]*utils.FormatInformation{}
- }
- for _, episode := range season {
- format, err := episode.GetFormat(archiveResolutionFlag, "", false)
- if err != nil {
- return nil, fmt.Errorf("error while receiving format for %s: %v", episode.Title, err)
- }
-
- if _, ok := eps[episode.SeasonNumber][episode.EpisodeNumber]; !ok {
- eps[episode.SeasonNumber][episode.EpisodeNumber] = &utils.FormatInformation{
- Format: format,
- AdditionalFormats: make([]*crunchyroll.Format, 0),
-
- Title: episode.Title,
- SeriesName: episode.SeriesTitle,
- SeasonName: episode.SeasonTitle,
- SeasonNumber: episode.SeasonNumber,
- EpisodeNumber: episode.EpisodeNumber,
- Resolution: format.Video.Resolution,
- FPS: format.Video.FrameRate,
- Audio: format.AudioLocale,
- }
- } else {
- eps[episode.SeasonNumber][episode.EpisodeNumber].AdditionalFormats = append(eps[episode.SeasonNumber][episode.EpisodeNumber].AdditionalFormats, format)
- }
- }
- }
- }
-
- var infoFormat [][]utils.FormatInformation
- for _, e := range eps {
- var tmpFormatInfo []utils.FormatInformation
-
- var keys []int
- for episodeNumber := range e {
- keys = append(keys, episodeNumber)
- }
- sort.Ints(keys)
-
- for _, key := range keys {
- tmpFormatInfo = append(tmpFormatInfo, *e[key])
- }
-
- infoFormat = append(infoFormat, tmpFormatInfo)
- }
-
- return infoFormat, nil
-}
diff --git a/cli/commands/archive/compress.go b/cli/commands/archive/compress.go
deleted file mode 100644
index e0b9ad4..0000000
--- a/cli/commands/archive/compress.go
+++ /dev/null
@@ -1,136 +0,0 @@
-package archive
-
-import (
- "archive/tar"
- "archive/zip"
- "bytes"
- "compress/gzip"
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "io"
- "os"
- "path/filepath"
- "sync"
- "time"
-)
-
-type Compress interface {
- io.Closer
-
- NewFile(information utils.FormatInformation) (io.WriteCloser, error)
-}
-
-func NewGzipCompress(file *os.File) *TarCompress {
- gw := gzip.NewWriter(file)
- return &TarCompress{
- parent: gw,
- dst: tar.NewWriter(gw),
- }
-}
-
-func NewTarCompress(file *os.File) *TarCompress {
- return &TarCompress{
- dst: tar.NewWriter(file),
- }
-}
-
-type TarCompress struct {
- Compress
-
- wg sync.WaitGroup
-
- parent *gzip.Writer
- dst *tar.Writer
-}
-
-func (tc *TarCompress) Close() error {
- // we have to wait here in case the actual content isn't copied completely into the
- // writer yet
- tc.wg.Wait()
-
- var err, err2 error
- if tc.parent != nil {
- err2 = tc.parent.Close()
- }
- err = tc.dst.Close()
-
- if err != nil && err2 != nil {
- // best way to show double errors at once that I've found
- return fmt.Errorf("%v\n%v", err, err2)
- } else if err == nil && err2 != nil {
- err = err2
- }
-
- return err
-}
-
-func (tc *TarCompress) NewFile(information utils.FormatInformation) (io.WriteCloser, error) {
- rp, wp := io.Pipe()
- go func() {
- tc.wg.Add(1)
- defer tc.wg.Done()
- var buf bytes.Buffer
- io.Copy(&buf, rp)
-
- header := &tar.Header{
- Name: filepath.Join(fmt.Sprintf("S%2d", information.SeasonNumber), information.Title),
- ModTime: time.Now(),
- Mode: 0644,
- Typeflag: tar.TypeReg,
- // fun fact: I did not set the size for quiet some time because I thought that it isn't
- // required. well because of this I debugged this part for multiple hours because without
- // proper size information only a tiny amount gets copied into the tar (or zip) writer.
- // this is also the reason why the file content is completely copied into a buffer before
- // writing it to the writer. I could bypass this and save some memory but this requires
- // some rewriting and im nearly at the (planned) finish for version 2 so nah in the future
- // maybe
- Size: int64(buf.Len()),
- }
- tc.dst.WriteHeader(header)
- io.Copy(tc.dst, &buf)
- }()
- return wp, nil
-}
-
-func NewZipCompress(file *os.File) *ZipCompress {
- return &ZipCompress{
- dst: zip.NewWriter(file),
- }
-}
-
-type ZipCompress struct {
- Compress
-
- wg sync.WaitGroup
-
- dst *zip.Writer
-}
-
-func (zc *ZipCompress) Close() error {
- zc.wg.Wait()
- return zc.dst.Close()
-}
-
-func (zc *ZipCompress) NewFile(information utils.FormatInformation) (io.WriteCloser, error) {
- rp, wp := io.Pipe()
- go func() {
- zc.wg.Add(1)
- defer zc.wg.Done()
-
- var buf bytes.Buffer
- io.Copy(&buf, rp)
-
- header := &zip.FileHeader{
- Name: filepath.Join(fmt.Sprintf("S%2d", information.SeasonNumber), information.Title),
- Modified: time.Now(),
- Method: zip.Deflate,
- UncompressedSize64: uint64(buf.Len()),
- }
- header.SetMode(0644)
-
- hw, _ := zc.dst.CreateHeader(header)
- io.Copy(hw, &buf)
- }()
-
- return wp, nil
-}
diff --git a/cli/commands/download/download.go b/cli/commands/download/download.go
deleted file mode 100644
index 39273d0..0000000
--- a/cli/commands/download/download.go
+++ /dev/null
@@ -1,368 +0,0 @@
-package download
-
-import (
- "context"
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/cli/commands"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "github.com/crunchy-labs/crunchyroll-go/v3"
- crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
- "github.com/grafov/m3u8"
- "github.com/spf13/cobra"
- "math"
- "os"
- "os/signal"
- "path/filepath"
- "runtime"
- "sort"
- "strconv"
- "strings"
-)
-
-var (
- downloadAudioFlag string
- downloadSubtitleFlag string
-
- downloadDirectoryFlag string
- downloadOutputFlag string
- downloadTempDirFlag string
-
- downloadResolutionFlag string
-
- downloadGoroutinesFlag int
-)
-
-var Cmd = &cobra.Command{
- Use: "download",
- Short: "Download a video",
- Args: cobra.MinimumNArgs(1),
-
- PreRunE: func(cmd *cobra.Command, args []string) error {
- utils.Log.Debug("Validating arguments")
-
- if filepath.Ext(downloadOutputFlag) != ".ts" {
- if !utils.HasFFmpeg() {
- return fmt.Errorf("the file ending for the output file (%s) is not `.ts`. "+
- "Install ffmpeg (https://ffmpeg.org/download.html) to use other media file endings (e.g. `.mp4`)", downloadOutputFlag)
- } else {
- utils.Log.Debug("Custom file ending '%s' (ffmpeg is installed)", filepath.Ext(downloadOutputFlag))
- }
- }
-
- if downloadAudioFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadAudioFlag)) {
- return fmt.Errorf("%s is not a valid audio locale. Choose from: %s", downloadAudioFlag, strings.Join(utils.LocalesAsStrings(), ", "))
- } else if downloadSubtitleFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadSubtitleFlag)) {
- return fmt.Errorf("%s is not a valid subtitle locale. Choose from: %s", downloadSubtitleFlag, strings.Join(utils.LocalesAsStrings(), ", "))
- }
- utils.Log.Debug("Locales: audio: %s / subtitle: %s", downloadAudioFlag, downloadSubtitleFlag)
-
- switch downloadResolutionFlag {
- case "1080p", "720p", "480p", "360p":
- intRes, _ := strconv.ParseFloat(strings.TrimSuffix(downloadResolutionFlag, "p"), 84)
- downloadResolutionFlag = fmt.Sprintf("%.0fx%s", math.Ceil(intRes*(float64(16)/float64(9))), strings.TrimSuffix(downloadResolutionFlag, "p"))
- case "240p":
- // 240p would round up to 427x240 if used in the case statement above, so it has to be handled separately
- downloadResolutionFlag = "428x240"
- case "1920x1080", "1280x720", "640x480", "480x360", "428x240", "best", "worst":
- default:
- return fmt.Errorf("'%s' is not a valid resolution", downloadResolutionFlag)
- }
- utils.Log.Debug("Using resolution '%s'", downloadResolutionFlag)
-
- return nil
- },
- RunE: func(cmd *cobra.Command, args []string) error {
- if err := commands.LoadCrunchy(); err != nil {
- return err
- }
-
- return download(args)
- },
-}
-
-func init() {
- Cmd.Flags().StringVarP(&downloadAudioFlag, "audio",
- "a",
- "",
- "The locale of the audio. Available locales: "+strings.Join(utils.LocalesAsStrings(), ", "))
- Cmd.Flags().StringVarP(&downloadSubtitleFlag,
- "subtitle",
- "s",
- "",
- "The locale of the subtitle. Available locales: "+strings.Join(utils.LocalesAsStrings(), ", "))
-
- cwd, _ := os.Getwd()
- Cmd.Flags().StringVarP(&downloadDirectoryFlag,
- "directory",
- "d",
- cwd,
- "The directory to download the file(s) into")
- Cmd.Flags().StringVarP(&downloadOutputFlag,
- "output",
- "o",
- "{title}.ts",
- "Name of the output file. "+
- "If you use the following things in the name, the will get replaced:\n"+
- "\t{title} » Title of the video\n"+
- "\t{series_name} » Name of the series\n"+
- "\t{season_name} » Name of the season\n"+
- "\t{season_number} » Number of the season\n"+
- "\t{episode_number} » Number of the episode\n"+
- "\t{resolution} » Resolution of the video\n"+
- "\t{fps} » Frame Rate of the video\n"+
- "\t{audio} » Audio locale of the video\n"+
- "\t{subtitle} » Subtitle locale of the video")
- Cmd.Flags().StringVar(&downloadTempDirFlag,
- "temp",
- os.TempDir(),
- "Directory to store temporary files in")
-
- Cmd.Flags().StringVarP(&downloadResolutionFlag,
- "resolution",
- "r",
- "best",
- "The video resolution. Can either be specified via the pixels, the abbreviation for pixels, or 'common-use' words\n"+
- "\tAvailable pixels: 1920x1080, 1280x720, 640x480, 480x360, 428x240\n"+
- "\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
- "\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
-
- Cmd.Flags().IntVarP(&downloadGoroutinesFlag,
- "goroutines",
- "g",
- runtime.NumCPU(),
- "Sets how many parallel segment downloads should be used")
-}
-
-func download(urls []string) error {
- for i, url := range urls {
- utils.Log.SetProcess("Parsing url %d", i+1)
- episodes, err := downloadExtractEpisodes(url)
- if err != nil {
- utils.Log.StopProcess("Failed to parse url %d", i+1)
- if utils.Crunchy.Config.Premium {
- utils.Log.Debug("If the error says no episodes could be found but the passed url is correct and a crunchyroll classic url, " +
- "try the corresponding crunchyroll beta url instead and try again. See https://github.com/crunchy-labs/crunchy-cli/issues/22 for more information")
- }
- return err
- }
- utils.Log.StopProcess("Parsed url %d", i+1)
-
- for _, season := range episodes {
- utils.Log.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
-
- for j, info := range season {
- utils.Log.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
- j+1,
- info.Title,
- info.Resolution,
- info.FPS,
- info.SeasonNumber,
- info.EpisodeNumber)
- }
- }
- utils.Log.Empty()
-
- for j, season := range episodes {
- for k, info := range season {
- dir := info.FormatString(downloadDirectoryFlag)
- if _, err = os.Stat(dir); os.IsNotExist(err) {
- if err = os.MkdirAll(dir, 0777); err != nil {
- return fmt.Errorf("error while creating directory: %v", err)
- }
- }
- file, err := os.Create(utils.GenerateFilename(info.FormatString(downloadOutputFlag), dir))
- if err != nil {
- return fmt.Errorf("failed to create output file: %v", err)
- }
-
- if err = downloadInfo(info, file); err != nil {
- file.Close()
- os.Remove(file.Name())
- return err
- }
- file.Close()
-
- if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
- utils.Log.Empty()
- }
- }
- }
- }
- return nil
-}
-
-func downloadInfo(info utils.FormatInformation, file *os.File) error {
- utils.Log.Debug("Entering season %d, episode %d", info.SeasonNumber, info.EpisodeNumber)
-
- if err := info.Format.InitVideo(); err != nil {
- return fmt.Errorf("error while initializing the video: %v", err)
- }
-
- dp := &commands.DownloadProgress{
- Prefix: utils.Log.(*commands.Logger).InfoLog.Prefix(),
- Message: "Downloading video",
- // number of segments a video has +2 is for merging and the success message
- Total: int(info.Format.Video.Chunklist.Count()) + 2,
- Dev: utils.Log.IsDev(),
- Quiet: utils.Log.(*commands.Logger).IsQuiet(),
- }
- if utils.Log.IsDev() {
- dp.Prefix = utils.Log.(*commands.Logger).DebugLog.Prefix()
- }
- defer func() {
- if dp.Total != dp.Current {
- fmt.Println()
- }
- }()
-
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- downloader := crunchyroll.NewDownloader(ctx, file, downloadGoroutinesFlag, func(segment *m3u8.MediaSegment, current, total int, file *os.File) error {
- // check if the context was cancelled.
- // must be done in to not print any progress messages if ctrl+c was pressed
- if ctx.Err() != nil {
- return nil
- }
-
- if utils.Log.IsDev() {
- dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
- } else {
- dp.Update()
- }
-
- if current == total {
- dp.UpdateMessage("Merging segments", false)
- }
- return nil
- })
- tmp, _ := os.MkdirTemp(downloadTempDirFlag, "crunchy_")
- downloader.TempDir = tmp
- if utils.HasFFmpeg() {
- downloader.FFmpegOpts = make([]string, 0)
- }
-
- sig := make(chan os.Signal, 1)
- signal.Notify(sig, os.Interrupt)
- go func() {
- select {
- case <-sig:
- signal.Stop(sig)
- utils.Log.Err("Exiting... (may take a few seconds)")
- utils.Log.Err("To force exit press ctrl+c (again)")
- cancel()
- // os.Exit(1) is not called because an immediate exit after the cancel function does not let
- // the download process enough time to stop gratefully. A result of this is that the temporary
- // directory where the segments are downloaded to will not be deleted
- case <-ctx.Done():
- // this is just here to end the goroutine and prevent it from running forever without a reason
- }
- }()
- utils.Log.Debug("Set up signal catcher")
-
- utils.Log.Info("Downloading episode `%s` to `%s`", info.Title, filepath.Base(file.Name()))
- utils.Log.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
- utils.Log.Info("\tAudio: %s", info.Audio)
- utils.Log.Info("\tSubtitle: %s", info.Subtitle)
- utils.Log.Info("\tResolution: %spx", info.Resolution)
- utils.Log.Info("\tFPS: %.2f", info.FPS)
- if err := info.Format.Download(downloader); err != nil {
- return fmt.Errorf("error while downloading: %v", err)
- }
-
- dp.UpdateMessage("Download finished", false)
-
- signal.Stop(sig)
- utils.Log.Debug("Stopped signal catcher")
-
- utils.Log.Empty()
-
- return nil
-}
-
-func downloadExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
- var episodes [][]*crunchyroll.Episode
- var final []*crunchyroll.Episode
-
- if downloadAudioFlag != "" {
- if _, ok := crunchyroll.ParseBetaEpisodeURL(url); ok {
- return nil, fmt.Errorf("downloading episodes by url and specifying a language is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
- } else if _, _, _, _, ok := crunchyroll.ParseEpisodeURL(url); ok {
- return nil, fmt.Errorf("downloading episodes by url and specifying a language is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
- }
-
- var err error
- episodes, err = utils.ExtractEpisodes(url, crunchyroll.JP, crunchyroll.LOCALE(downloadAudioFlag))
- if err != nil {
- return nil, err
- }
- japanese := episodes[0]
- custom := episodes[1]
-
- sort.Sort(crunchyUtils.EpisodesByNumber(japanese))
- sort.Sort(crunchyUtils.EpisodesByNumber(custom))
-
- var errMessages []string
-
- if len(japanese) == 0 || len(japanese) == len(custom) {
- final = custom
- } else {
- for _, jp := range japanese {
- before := len(final)
- for _, episode := range custom {
- if jp.SeasonNumber == episode.SeasonNumber && jp.EpisodeNumber == episode.EpisodeNumber {
- final = append(final, episode)
- }
- }
- if before == len(final) {
- errMessages = append(errMessages, fmt.Sprintf("%s has no %s audio, using %s as fallback", jp.Title, crunchyroll.LOCALE(downloadAudioFlag), crunchyroll.JP))
- final = append(final, jp)
- }
- }
- }
-
- if len(errMessages) > 10 {
- for _, msg := range errMessages[:10] {
- utils.Log.SetProcess(msg)
- }
- utils.Log.SetProcess("... and %d more", len(errMessages)-10)
- } else {
- for _, msg := range errMessages {
- utils.Log.SetProcess(msg)
- }
- }
- } else {
- var err error
- episodes, err = utils.ExtractEpisodes(url)
- if err != nil {
- return nil, err
- } else if len(episodes) == 0 {
- return nil, fmt.Errorf("cannot find any episode")
- }
- final = episodes[0]
- }
-
- var infoFormat [][]utils.FormatInformation
- for _, season := range crunchyUtils.SortEpisodesBySeason(final) {
- tmpFormatInformation := make([]utils.FormatInformation, 0)
- for _, episode := range season {
- format, err := episode.GetFormat(downloadResolutionFlag, crunchyroll.LOCALE(downloadSubtitleFlag), true)
- if err != nil {
- return nil, fmt.Errorf("error while receiving format for %s: %v", episode.Title, err)
- }
- tmpFormatInformation = append(tmpFormatInformation, utils.FormatInformation{
- Format: format,
-
- Title: episode.Title,
- SeriesName: episode.SeriesTitle,
- SeasonName: episode.SeasonTitle,
- SeasonNumber: episode.SeasonNumber,
- EpisodeNumber: episode.EpisodeNumber,
- Resolution: format.Video.Resolution,
- FPS: format.Video.FrameRate,
- Audio: format.AudioLocale,
- })
- }
- infoFormat = append(infoFormat, tmpFormatInformation)
- }
- return infoFormat, nil
-}
diff --git a/cli/commands/info/info.go b/cli/commands/info/info.go
deleted file mode 100644
index 650d8c7..0000000
--- a/cli/commands/info/info.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package info
-
-import (
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/cli/commands"
- "github.com/crunchy-labs/crunchy-cli/utils"
- crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
- "github.com/spf13/cobra"
-)
-
-var Cmd = &cobra.Command{
- Use: "info",
- Short: "Shows information about the logged in user",
- Args: cobra.MinimumNArgs(0),
-
- RunE: func(cmd *cobra.Command, args []string) error {
- if err := commands.LoadCrunchy(); err != nil {
- return err
- }
-
- return info()
- },
-}
-
-func info() error {
- account, err := utils.Crunchy.Account()
- if err != nil {
- return err
- }
-
- fmt.Println("Username: ", account.Username)
- fmt.Println("Email: ", account.Email)
- fmt.Println("Premium: ", utils.Crunchy.Config.Premium)
- fmt.Println("Interface language: ", crunchyUtils.LocaleLanguage(account.PreferredCommunicationLanguage))
- fmt.Println("Subtitle language: ", crunchyUtils.LocaleLanguage(account.PreferredContentSubtitleLanguage))
- fmt.Println("Created: ", account.Created)
- fmt.Println("Account ID: ", account.AccountID)
-
- return nil
-}
diff --git a/cli/commands/logger.go b/cli/commands/logger.go
deleted file mode 100644
index 89a4717..0000000
--- a/cli/commands/logger.go
+++ /dev/null
@@ -1,196 +0,0 @@
-package commands
-
-import (
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "io"
- "log"
- "os"
- "runtime"
- "strings"
- "sync"
- "time"
-)
-
-var prefix, progressDown, progressDownFinish string
-
-func initPrefixBecauseWindowsSucksBallsHard() {
- // dear windows user, please change to a good OS, linux in the best case.
- // MICROSHIT DOES NOT GET IT DONE TO SHOW THE SYMBOLS IN THE ELSE CLAUSE
- // CORRECTLY. NOT IN THE CMD NOR POWERSHELL. WHY TF, IT IS ONE OF THE MOST
- // PROFITABLE COMPANIES ON THIS PLANET AND CANNOT SHOW A PROPER UTF-8 SYMBOL
- // IN THEIR OWN PRODUCT WHICH GETS USED MILLION TIMES A DAY
- if runtime.GOOS == "windows" {
- prefix = "=>"
- progressDown = "|"
- progressDownFinish = "->"
- } else {
- prefix = "➞"
- progressDown = "↓"
- progressDownFinish = "↳"
- }
-}
-
-type progress struct {
- message string
- stop bool
-}
-
-func NewLogger(debug, info, err bool) *Logger {
- initPrefixBecauseWindowsSucksBallsHard()
-
- debugLog, infoLog, errLog := log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0)
-
- if debug {
- debugLog.SetOutput(os.Stdout)
- }
- if info {
- infoLog.SetOutput(os.Stdout)
- }
- if err {
- errLog.SetOutput(os.Stderr)
- }
-
- if debug {
- debugLog = log.New(debugLog.Writer(), "[debug] ", 0)
- infoLog = log.New(infoLog.Writer(), "[info] ", 0)
- errLog = log.New(errLog.Writer(), "[err] ", 0)
- }
-
- return &Logger{
- DebugLog: debugLog,
- InfoLog: infoLog,
- ErrLog: errLog,
-
- devView: debug,
- }
-}
-
-type Logger struct {
- utils.Logger
-
- DebugLog *log.Logger
- InfoLog *log.Logger
- ErrLog *log.Logger
-
- devView bool
-
- progress chan progress
- done chan interface{}
- lock sync.Mutex
-}
-
-func (l *Logger) IsDev() bool {
- return l.devView
-}
-
-func (l *Logger) IsQuiet() bool {
- return l.DebugLog.Writer() == io.Discard && l.InfoLog.Writer() == io.Discard && l.ErrLog.Writer() == io.Discard
-}
-
-func (l *Logger) Debug(format string, v ...interface{}) {
- l.DebugLog.Printf(format, v...)
-}
-
-func (l *Logger) Info(format string, v ...interface{}) {
- l.InfoLog.Printf(format, v...)
-}
-
-func (l *Logger) Warn(format string, v ...interface{}) {
- l.Err(format, v...)
-}
-
-func (l *Logger) Err(format string, v ...interface{}) {
- l.ErrLog.Printf(format, v...)
-}
-
-func (l *Logger) Empty() {
- if !l.devView && l.InfoLog.Writer() != io.Discard {
- fmt.Println("")
- }
-}
-
-func (l *Logger) SetProcess(format string, v ...interface{}) {
- if l.InfoLog.Writer() == io.Discard {
- return
- } else if l.devView {
- l.Debug(format, v...)
- return
- }
-
- initialMessage := fmt.Sprintf(format, v...)
-
- p := progress{
- message: initialMessage,
- }
-
- l.lock.Lock()
- if l.done != nil {
- l.progress <- p
- return
- } else {
- l.progress = make(chan progress, 1)
- l.progress <- p
- l.done = make(chan interface{})
- }
-
- go func() {
- states := []string{"-", "\\", "|", "/"}
-
- var count int
-
- for i := 0; ; i++ {
- select {
- case p := <-l.progress:
- if p.stop {
- fmt.Printf("\r" + strings.Repeat(" ", len(prefix)+len(initialMessage)))
- if count > 1 {
- fmt.Printf("\r%s %s\n", progressDownFinish, p.message)
- } else {
- fmt.Printf("\r%s %s\n", prefix, p.message)
- }
-
- if l.done != nil {
- l.done <- nil
- }
- l.progress = nil
-
- l.lock.Unlock()
- return
- } else {
- if count > 0 {
- fmt.Printf("\r%s %s\n", progressDown, p.message)
- }
- l.progress = make(chan progress, 1)
-
- count++
-
- fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
- l.lock.Unlock()
- }
- default:
- if i%10 == 0 {
- fmt.Printf("\r%s %s", states[i/10%4], initialMessage)
- }
- time.Sleep(35 * time.Millisecond)
- }
- }
- }()
-}
-
-func (l *Logger) StopProcess(format string, v ...interface{}) {
- if l.InfoLog.Writer() == io.Discard {
- return
- } else if l.devView {
- l.Debug(format, v...)
- return
- }
-
- l.lock.Lock()
- l.progress <- progress{
- message: fmt.Sprintf(format, v...),
- stop: true,
- }
- <-l.done
- l.done = nil
-}
diff --git a/cli/commands/login/login.go b/cli/commands/login/login.go
deleted file mode 100644
index 9fce181..0000000
--- a/cli/commands/login/login.go
+++ /dev/null
@@ -1,159 +0,0 @@
-package login
-
-import (
- "bytes"
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/cli/commands"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "github.com/crunchy-labs/crunchyroll-go/v3"
- "github.com/spf13/cobra"
- "os"
-)
-
-var (
- loginPersistentFlag bool
- loginEncryptFlag bool
-
- loginSessionIDFlag bool
- loginRefreshTokenFlag bool
-)
-
-var Cmd = &cobra.Command{
- Use: "login",
- Short: "Login to crunchyroll",
- Args: cobra.RangeArgs(1, 2),
-
- RunE: func(cmd *cobra.Command, args []string) error {
- if loginSessionIDFlag {
- return loginSessionID(args[0])
- } else if loginRefreshTokenFlag {
- return loginRefreshToken(args[0])
- } else {
- return loginCredentials(args[0], args[1])
- }
- },
-}
-
-func init() {
- Cmd.Flags().BoolVar(&loginPersistentFlag,
- "persistent",
- false,
- "If the given credential should be stored persistent")
- Cmd.Flags().BoolVar(&loginEncryptFlag,
- "encrypt",
- false,
- "Encrypt the given credentials (won't do anything if --session-id is given or --persistent is not given)")
-
- Cmd.Flags().BoolVar(&loginSessionIDFlag,
- "session-id",
- false,
- "Use a session id to login instead of username and password")
- Cmd.Flags().BoolVar(&loginRefreshTokenFlag,
- "refresh-token",
- false,
- "Use a refresh token to login instead of username and password. Can be obtained by copying the `etp-rt` cookie from beta.crunchyroll.com")
-
- Cmd.MarkFlagsMutuallyExclusive("session-id", "refresh-token")
-}
-
-func loginCredentials(user, password string) error {
- utils.Log.Debug("Logging in via credentials")
- c, err := crunchyroll.LoginWithCredentials(user, password, utils.SystemLocale(false), utils.Client)
- if err != nil {
- return err
- }
-
- if loginPersistentFlag {
- var passwd []byte
- if loginEncryptFlag {
- for {
- fmt.Print("Enter password: ")
- passwd, err = commands.ReadLineSilent()
- if err != nil {
- return err
- }
- fmt.Println()
-
- fmt.Print("Enter password again: ")
- repasswd, err := commands.ReadLineSilent()
- if err != nil {
- return err
- }
- fmt.Println()
-
- if bytes.Equal(passwd, repasswd) {
- break
- }
- fmt.Println("Passwords does not match, try again")
- }
- }
- if err = utils.SaveCredentialsPersistent(user, password, passwd); err != nil {
- return err
- }
- if !loginEncryptFlag {
- utils.Log.Warn("The login information will be stored permanently UNENCRYPTED on your drive. " +
- "To encrypt it, use the `--encrypt` flag")
- }
- }
- if err = utils.SaveSession(c); err != nil {
- return err
- }
-
- if !loginPersistentFlag {
- utils.Log.Info("Due to security reasons, you have to login again on the next reboot")
- }
-
- return nil
-}
-
-func loginSessionID(sessionID string) error {
- utils.Log.Debug("Logging in via session id")
- utils.Log.Warn("Logging in with session id is deprecated and not very reliable. Consider choosing another option (if it fails)")
- var c *crunchyroll.Crunchyroll
- var err error
- if c, err = crunchyroll.LoginWithSessionID(sessionID, utils.SystemLocale(false), utils.Client); err != nil {
- return err
- }
-
- if loginPersistentFlag {
- if err = utils.SaveSessionPersistent(c); err != nil {
- return err
- }
- utils.Log.Warn("The login information will be stored permanently UNENCRYPTED on your drive")
- }
- if err = utils.SaveSession(c); err != nil {
- return err
- }
-
- if !loginPersistentFlag {
- utils.Log.Info("Due to security reasons, you have to login again on the next reboot")
- }
-
- return nil
-}
-
-func loginRefreshToken(refreshToken string) error {
- utils.Log.Debug("Logging in via refresh token")
- var c *crunchyroll.Crunchyroll
- var err error
- if c, err = crunchyroll.LoginWithRefreshToken(refreshToken, utils.SystemLocale(false), utils.Client); err != nil {
- utils.Log.Err(err.Error())
- os.Exit(1)
- }
-
- if loginPersistentFlag {
- if err = utils.SaveSessionPersistent(c); err != nil {
- return err
- }
- utils.Log.Warn("The login information will be stored permanently UNENCRYPTED on your drive")
- }
- if err = utils.SaveSession(c); err != nil {
- return err
- }
-
- if !loginPersistentFlag {
- utils.Log.Info("Due to security reasons, you have to login again on the next reboot")
- }
-
- return nil
-}
diff --git a/cli/commands/unix.go b/cli/commands/unix.go
deleted file mode 100644
index ec69180..0000000
--- a/cli/commands/unix.go
+++ /dev/null
@@ -1,48 +0,0 @@
-//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
-
-package commands
-
-import (
- "bufio"
- "os"
- "os/exec"
- "syscall"
-)
-
-// https://github.com/bgentry/speakeasy/blob/master/speakeasy_unix.go
-var stty string
-
-func init() {
- var err error
- if stty, err = exec.LookPath("stty"); err != nil {
- panic(err)
- }
-}
-
-func ReadLineSilent() ([]byte, error) {
- pid, err := setEcho(false)
- if err != nil {
- return nil, err
- }
- defer setEcho(true)
-
- syscall.Wait4(pid, nil, 0, nil)
-
- l, _, err := bufio.NewReader(os.Stdin).ReadLine()
- return l, err
-}
-
-func setEcho(on bool) (pid int, err error) {
- fds := []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()}
-
- if on {
- pid, err = syscall.ForkExec(stty, []string{"stty", "echo"}, &syscall.ProcAttr{Files: fds})
- } else {
- pid, err = syscall.ForkExec(stty, []string{"stty", "-echo"}, &syscall.ProcAttr{Files: fds})
- }
-
- if err != nil {
- return 0, err
- }
- return
-}
diff --git a/cli/commands/update/update.go b/cli/commands/update/update.go
deleted file mode 100644
index 6608f26..0000000
--- a/cli/commands/update/update.go
+++ /dev/null
@@ -1,151 +0,0 @@
-package update
-
-import (
- "encoding/json"
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "github.com/spf13/cobra"
- "io"
- "os"
- "os/exec"
- "path"
- "runtime"
- "strings"
-)
-
-var (
- updateInstallFlag bool
-)
-
-var Cmd = &cobra.Command{
- Use: "update",
- Short: "Check if updates are available",
- Args: cobra.MaximumNArgs(0),
-
- RunE: func(cmd *cobra.Command, args []string) error {
- return update()
- },
-}
-
-func init() {
- Cmd.Flags().BoolVarP(&updateInstallFlag,
- "install",
- "i",
- false,
- "If set and a new version is available, the new version gets installed")
-}
-
-func update() error {
- var release map[string]interface{}
-
- resp, err := utils.Client.Get("https://api.github.com/repos/crunchy-labs/crunchy-cli/releases/latest")
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- if err = json.NewDecoder(resp.Body).Decode(&release); err != nil {
- return err
- }
- releaseVersion := strings.TrimPrefix(release["tag_name"].(string), "v")
-
- if utils.Version == "development" {
- utils.Log.Info("Development version, update service not available")
- return nil
- }
-
- latestRelease := strings.SplitN(releaseVersion, ".", 4)
- if len(latestRelease) != 3 {
- return fmt.Errorf("latest tag name (%s) is not parsable", releaseVersion)
- }
-
- internalVersion := strings.SplitN(utils.Version, ".", 4)
- if len(internalVersion) != 3 {
- return fmt.Errorf("internal version (%s) is not parsable", utils.Version)
- }
-
- utils.Log.Info("Installed version is %s", utils.Version)
-
- var hasUpdate bool
- for i := 0; i < 3; i++ {
- if latestRelease[i] < internalVersion[i] {
- utils.Log.Info("Local version is newer than version in latest release (%s)", releaseVersion)
- return nil
- } else if latestRelease[i] > internalVersion[i] {
- hasUpdate = true
- }
- }
-
- if !hasUpdate {
- utils.Log.Info("Version is up-to-date")
- return nil
- }
-
- utils.Log.Info("A new version is available (%s): https://github.com/crunchy-labs/crunchy-cli/releases/tag/v%s", releaseVersion, releaseVersion)
-
- if updateInstallFlag {
- if runtime.GOARCH != "amd64" {
- return fmt.Errorf("invalid architecture found (%s), only amd64 is currently supported for automatic updating. "+
- "You have to update manually (https://github.com/crunchy-labs/crunchy-cli)", runtime.GOARCH)
- }
-
- var downloadFile string
- switch runtime.GOOS {
- case "linux":
- pacmanCommand := exec.Command("pacman -Q crunchy-cli")
- if pacmanCommand.Run() == nil && pacmanCommand.ProcessState.Success() {
- utils.Log.Info("crunchy-cli was probably installed via an Arch Linux AUR helper (like yay). Updating via this AUR helper is recommended")
- return nil
- }
- downloadFile = fmt.Sprintf("crunchy-v%s_linux", releaseVersion)
- case "darwin":
- downloadFile = fmt.Sprintf("crunchy-v%s_darwin", releaseVersion)
- case "windows":
- downloadFile = fmt.Sprintf("crunchy-v%s_windows.exe", releaseVersion)
- default:
- return fmt.Errorf("invalid operation system found (%s), only linux, windows and darwin / macos are currently supported. "+
- "You have to update manually (https://github.com/crunchy-labs/crunchy-cli", runtime.GOOS)
- }
-
- executePath := os.Args[0]
- var perms os.FileInfo
- // check if the path is relative, absolute or non (if so, the executable must be in PATH)
- if strings.HasPrefix(executePath, "."+string(os.PathSeparator)) || path.IsAbs(executePath) {
- if perms, err = os.Stat(os.Args[0]); err != nil {
- return err
- }
- } else {
- executePath, err = exec.LookPath(os.Args[0])
- if err != nil {
- return err
- }
- if perms, err = os.Stat(executePath); err != nil {
- return err
- }
- }
-
- utils.Log.SetProcess("Updating executable %s", executePath)
-
- if err = os.Remove(executePath); err != nil {
- return err
- }
- executeFile, err := os.OpenFile(executePath, os.O_CREATE|os.O_WRONLY, perms.Mode())
- if err != nil {
- return err
- }
- defer executeFile.Close()
-
- resp, err := utils.Client.Get(fmt.Sprintf("https://github.com/crunchy-labs/crunchy-cli/releases/download/v%s/%s", releaseVersion, downloadFile))
- if err != nil {
- return err
- }
- defer resp.Body.Close()
-
- if _, err = io.Copy(executeFile, resp.Body); err != nil {
- return err
- }
-
- utils.Log.StopProcess("Updated executable %s", executePath)
- }
-
- return nil
-}
diff --git a/cli/commands/utils.go b/cli/commands/utils.go
deleted file mode 100644
index 53e51f2..0000000
--- a/cli/commands/utils.go
+++ /dev/null
@@ -1,125 +0,0 @@
-package commands
-
-import (
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "os"
- "os/exec"
- "runtime"
- "strconv"
- "strings"
- "sync"
-)
-
-type DownloadProgress struct {
- Prefix string
- Message string
-
- Total int
- Current int
-
- Dev bool
- Quiet bool
-
- lock sync.Mutex
-}
-
-func (dp *DownloadProgress) Update() {
- dp.update("", false)
-}
-
-func (dp *DownloadProgress) UpdateMessage(msg string, permanent bool) {
- dp.update(msg, permanent)
-}
-
-func (dp *DownloadProgress) update(msg string, permanent bool) {
- if dp.Quiet {
- return
- }
-
- if dp.Current >= dp.Total {
- return
- }
-
- dp.lock.Lock()
- defer dp.lock.Unlock()
- dp.Current++
-
- if msg == "" {
- msg = dp.Message
- }
- if permanent {
- dp.Message = msg
- }
-
- if dp.Dev {
- fmt.Printf("%s%s\n", dp.Prefix, msg)
- return
- }
-
- percentage := float32(dp.Current) / float32(dp.Total) * 100
-
- pre := fmt.Sprintf("%s%s [", dp.Prefix, msg)
- post := fmt.Sprintf("]%4d%% %8d/%d", int(percentage), dp.Current, dp.Total)
-
- // I don't really know why +2 is needed here but without it the Printf below would not print to the line end
- progressWidth := terminalWidth() - len(pre) - len(post) + 2
- repeatCount := int(percentage / float32(100) * float32(progressWidth))
- // it can be lower than zero when the terminal is very tiny
- if repeatCount < 0 {
- repeatCount = 0
- }
- progressPercentage := strings.Repeat("=", repeatCount)
- if dp.Current != dp.Total {
- progressPercentage += ">"
- }
-
- fmt.Printf("\r%s%-"+fmt.Sprint(progressWidth)+"s%s", pre, progressPercentage, post)
-}
-
-func terminalWidth() int {
- if runtime.GOOS != "windows" {
- cmd := exec.Command("stty", "size")
- cmd.Stdin = os.Stdin
- res, err := cmd.Output()
- if err != nil {
- return 60
- }
- // on alpine linux the command `stty size` does not respond the terminal size
- // but something like "stty: standard input". this may also apply to other systems
- splitOutput := strings.SplitN(strings.ReplaceAll(string(res), "\n", ""), " ", 2)
- if len(splitOutput) == 1 {
- return 60
- }
- width, err := strconv.Atoi(splitOutput[1])
- if err != nil {
- return 60
- }
- return width
- }
- return 60
-}
-
-func LoadCrunchy() error {
- var encryptionKey []byte
-
- if utils.IsTempSession() {
- encryptionKey = nil
- } else {
- if encrypted, err := utils.IsSavedSessionEncrypted(); err != nil {
- if os.IsNotExist(err) {
- return fmt.Errorf("to use this command, login first. Type `%s login -h` to get help", os.Args[0])
- }
- return err
- } else if encrypted {
- encryptionKey, err = ReadLineSilent()
- if err != nil {
- return fmt.Errorf("failed to read password")
- }
- }
- }
-
- var err error
- utils.Crunchy, err = utils.LoadSession(encryptionKey)
- return err
-}
diff --git a/cli/commands/windows.go b/cli/commands/windows.go
deleted file mode 100644
index a9bce74..0000000
--- a/cli/commands/windows.go
+++ /dev/null
@@ -1,41 +0,0 @@
-//go:build windows
-
-package commands
-
-import (
- "bufio"
- "os"
- "syscall"
-)
-
-// https://github.com/bgentry/speakeasy/blob/master/speakeasy_windows.go
-func ReadLineSilent() ([]byte, error) {
- var oldMode uint32
-
- if err := syscall.GetConsoleMode(syscall.Stdin, &oldMode); err != nil {
- return nil, err
- }
-
- newMode := oldMode &^ 0x0004
-
- err := setConsoleMode(syscall.Stdin, newMode)
- defer setConsoleMode(syscall.Stdin, oldMode)
-
- if err != nil {
- return nil, err
- }
-
- l, _, err := bufio.NewReader(os.Stdin).ReadLine()
- if err != nil {
- return nil, err
- }
- return l, err
-}
-
-func setConsoleMode(console syscall.Handle, mode uint32) error {
- dll := syscall.MustLoadDLL("kernel32")
- proc := dll.MustFindProc("SetConsoleMode")
- _, _, err := proc.Call(uintptr(console), uintptr(mode))
-
- return err
-}
diff --git a/cli/root.go b/cli/root.go
deleted file mode 100644
index b589a03..0000000
--- a/cli/root.go
+++ /dev/null
@@ -1,85 +0,0 @@
-package cli
-
-import (
- "context"
- "fmt"
- "github.com/crunchy-labs/crunchy-cli/cli/commands"
- "github.com/crunchy-labs/crunchy-cli/cli/commands/archive"
- "github.com/crunchy-labs/crunchy-cli/cli/commands/download"
- "github.com/crunchy-labs/crunchy-cli/cli/commands/info"
- "github.com/crunchy-labs/crunchy-cli/cli/commands/login"
- "github.com/crunchy-labs/crunchy-cli/cli/commands/update"
- "github.com/crunchy-labs/crunchy-cli/utils"
- "github.com/spf13/cobra"
- "os"
- "runtime/debug"
- "strings"
-)
-
-var (
- quietFlag bool
- verboseFlag bool
-
- proxyFlag string
-
- useragentFlag string
-)
-
-var RootCmd = &cobra.Command{
- Use: "crunchy-cli",
- Version: utils.Version,
- Short: "Download crunchyroll videos with ease. See the wiki for details about the cli and library: https://github.com/crunchy-labs/crunchy-cli/wiki",
-
- SilenceErrors: true,
- SilenceUsage: true,
-
- PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
- if verboseFlag {
- utils.Log = commands.NewLogger(true, true, true)
- } else if quietFlag {
- utils.Log = commands.NewLogger(false, false, false)
- }
-
- utils.Log.Debug("Executing `%s` command with %d arg(s)", cmd.Name(), len(args))
-
- utils.Client, err = utils.CreateOrDefaultClient(proxyFlag, useragentFlag)
- return
- },
-}
-
-func init() {
- RootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "Disable all output")
- RootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Adds debug messages to the normal output")
-
- RootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
-
- RootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchy-cli/%s", utils.Version), "Useragent to do all request with")
-
- RootCmd.AddCommand(archive.Cmd)
- RootCmd.AddCommand(download.Cmd)
- RootCmd.AddCommand(info.Cmd)
- RootCmd.AddCommand(login.Cmd)
- RootCmd.AddCommand(update.Cmd)
-
- utils.Log = commands.NewLogger(false, true, true)
-}
-
-func Execute() {
- RootCmd.CompletionOptions.HiddenDefaultCmd = true
- defer func() {
- if r := recover(); r != nil {
- if utils.Log.IsDev() {
- utils.Log.Err("%v: %s", r, debug.Stack())
- } else {
- utils.Log.Err("Unexpected error: %v", r)
- }
- os.Exit(1)
- }
- }()
- if err := RootCmd.Execute(); err != nil {
- if !strings.HasSuffix(err.Error(), context.Canceled.Error()) {
- utils.Log.Err("An error occurred: %v", err)
- }
- os.Exit(1)
- }
-}
diff --git a/crunchy-cli-core/Cargo.toml b/crunchy-cli-core/Cargo.toml
new file mode 100644
index 0000000..893b278
--- /dev/null
+++ b/crunchy-cli-core/Cargo.toml
@@ -0,0 +1,29 @@
+[package]
+name = "crunchy-cli-core"
+version = "0.1.0"
+edition = "2021"
+
+[features]
+# Embed a static curl library into the binary instead of just linking it.
+static-curl = ["crunchyroll-rs/static-curl"]
+# Embed a static openssl library into the binary instead of just linking it. If you want to compile this project against
+# musl and have openssl issues, this might solve these issues.
+static-ssl = ["crunchyroll-rs/static-ssl"]
+
+[dependencies]
+anyhow = "1.0"
+async-trait = "0.1"
+clap = { version = "4.0", features = ["derive"] }
+chrono = "0.4"
+crunchyroll-rs = { git = "https://github.com/crunchy-labs/crunchyroll-rs", default-features = false, features = ["stream", "parse"] }
+ctrlc = "3.2"
+dirs = "4.0"
+isahc = { git = "https://github.com/sagebind/isahc", rev = "34f158ef" }
+log = { version = "0.4", features = ["std"] }
+num_cpus = "1.13"
+regex = "1.6"
+signal-hook = "0.3"
+tempfile = "3.3"
+terminal_size = "0.2"
+tokio = { version = "1.21", features = ["macros", "rt-multi-thread", "time"] }
+sys-locale = "0.2"
diff --git a/crunchy-cli-core/src/cli/archive.rs b/crunchy-cli-core/src/cli/archive.rs
new file mode 100644
index 0000000..21ae13b
--- /dev/null
+++ b/crunchy-cli-core/src/cli/archive.rs
@@ -0,0 +1,567 @@
+use crate::cli::log::tab_info;
+use crate::cli::utils::{download_segments, find_resolution};
+use crate::utils::context::Context;
+use crate::utils::format::{format_string, Format};
+use crate::utils::log::progress;
+use crate::utils::os::{free_file, tempfile};
+use crate::utils::parse::{parse_url, UrlFilter};
+use crate::utils::sort::{sort_formats_after_seasons, sort_seasons_after_number};
+use crate::Execute;
+use anyhow::{bail, Result};
+use crunchyroll_rs::media::{Resolution, StreamSubtitle};
+use crunchyroll_rs::{Locale, Media, MediaCollection, Series};
+use log::{debug, error, info};
+use regex::Regex;
+use std::collections::BTreeMap;
+use std::io::Write;
+use std::path::PathBuf;
+use std::process::{Command, Stdio};
+use tempfile::TempPath;
+
+#[derive(Clone, Debug)]
+pub enum MergeBehavior {
+ Auto,
+ Audio,
+ Video,
+}
+
+fn parse_merge_behavior(s: &str) -> Result {
+ Ok(match s.to_lowercase().as_str() {
+ "auto" => MergeBehavior::Auto,
+ "audio" => MergeBehavior::Audio,
+ "video" => MergeBehavior::Video,
+ _ => return Err(format!("'{}' is not a valid merge behavior", s)),
+ })
+}
+
+#[derive(Debug, clap::Parser)]
+#[clap(about = "Archive a video")]
+#[command(arg_required_else_help(true))]
+#[command()]
+pub struct Archive {
+ #[arg(help = format!("Audio languages. Can be used multiple times. \
+ Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(long_help = format!("Audio languages. Can be used multiple times. \
+ Available languages are:\n{}", Locale::all().into_iter().map(|l| format!("{:<6} → {}", l.to_string(), l.to_human_readable())).collect::>().join("\n ")))]
+ #[arg(short, long, default_values_t = vec![crate::utils::locale::system_locale(), Locale::ja_JP])]
+ audio: Vec,
+ #[arg(help = format!("Subtitle languages. Can be used multiple times. \
+ Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(long_help = format!("Subtitle languages. Can be used multiple times. \
+ Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(short, long, default_values_t = Locale::all())]
+ subtitle: Vec,
+
+ #[arg(help = "Name of the output file")]
+ #[arg(long_help = "Name of the output file.\
+ If you use one of the following pattern they will get replaced:\n \
+ {title} → Title of the video\n \
+ {series_name} → Name of the series\n \
+ {season_name} → Name of the season\n \
+ {audio} → Audio language of the video\n \
+ {resolution} → Resolution of the video\n \
+ {season_number} → Number of the season\n \
+ {episode_number} → Number of the episode\n \
+ {series_id} → ID of the series\n \
+ {season_id} → ID of the season\n \
+ {episode_id} → ID of the episode")]
+ #[arg(short, long, default_value = "{title}.mkv")]
+ output: String,
+
+ #[arg(help = "Video resolution")]
+ #[arg(long_help = "The video resolution.\
+ Can either be specified via the pixels (e.g. 1920x1080), the abbreviation for pixels (e.g. 1080p) or 'common-use' words (e.g. best). \
+ Specifying the exact pixels is not recommended, use one of the other options instead. \
+ Crunchyroll let you choose the quality with pixel abbreviation on their clients, so you might be already familiar with the available options. \
+ The available common-use words are 'best' (choose the best resolution available) and 'worst' (worst resolution available)")]
+ #[arg(short, long, default_value = "best")]
+ #[arg(value_parser = crate::utils::clap::clap_parse_resolution)]
+ resolution: Resolution,
+
+ #[arg(
+ help = "Sets the behavior of the stream merging. Valid behaviors are 'auto', 'audio' and 'video'"
+ )]
+ #[arg(
+ long_help = "Because of local restrictions (or other reasons) some episodes with different languages does not have the same length (e.g. when some scenes were cut out). \
+ With this flag you can set the behavior when handling multiple language.
+ Valid options are 'audio' (stores one video and all other languages as audio only), 'video' (stores the video + audio for every language) and 'auto' (detects if videos differ in length: if so, behave like 'video' else like 'audio')"
+ )]
+ #[arg(short, long, default_value = "auto")]
+ #[arg(value_parser = parse_merge_behavior)]
+ merge: MergeBehavior,
+
+ #[arg(
+ help = "Set which subtitle language should be set as default / auto shown when starting a video"
+ )]
+ #[arg(long)]
+ default_subtitle: Option,
+ #[arg(help = "Disable subtitle optimizations")]
+ #[arg(
+ long_help = "By default, Crunchyroll delivers subtitles in a format which may cause issues in some video players. \
+ These issues are fixed internally by setting a flag which is not part of the official specification of the subtitle format. \
+ If you do not want this fixes or they cause more trouble than they solve (for you), it can be disabled with this flag"
+ )]
+ #[arg(long)]
+ no_subtitle_optimizations: bool,
+
+ #[arg(help = "Crunchyroll series url(s)")]
+ urls: Vec,
+}
+
+#[async_trait::async_trait(?Send)]
+impl Execute for Archive {
+ async fn execute(self, ctx: Context) -> Result<()> {
+ let mut parsed_urls = vec![];
+
+ for (i, url) in self.urls.iter().enumerate() {
+ let _progress_handler = progress!("Parsing url {}", i + 1);
+ match parse_url(&ctx.crunchy, url.clone(), true).await {
+ Ok((media_collection, url_filter)) => {
+ parsed_urls.push((media_collection, url_filter));
+ info!("Parsed url {}", i + 1)
+ }
+ Err(e) => bail!("url {} could not be parsed: {}", url, e),
+ }
+ }
+
+ for (i, (media_collection, url_filter)) in parsed_urls.into_iter().enumerate() {
+ let archive_formats = match media_collection {
+ MediaCollection::Series(series) => {
+ let _progress_handler = progress!("Fetching series details");
+ formats_from_series(&self, series, &url_filter).await?
+ }
+ MediaCollection::Season(_) => bail!("Archiving a season is not supported"),
+ MediaCollection::Episode(episode) => bail!("Archiving a episode is not supported. Use url filtering instead to specify the episode (https://www.crunchyroll.com/series/{}/{}[S{}E{}])", episode.metadata.series_id, episode.metadata.series_slug_title, episode.metadata.season_number, episode.metadata.episode_number),
+ MediaCollection::MovieListing(_) => bail!("Archiving a movie listing is not supported"),
+ MediaCollection::Movie(_) => bail!("Archiving a movie is not supported")
+ };
+
+ if archive_formats.is_empty() {
+ info!("Skipping url {} (no matching episodes found)", i + 1);
+ continue;
+ }
+ info!("Loaded series information for url {}", i + 1);
+
+ if log::max_level() == log::Level::Debug {
+ let seasons = sort_formats_after_seasons(
+ archive_formats
+ .clone()
+ .into_iter()
+ .map(|(a, _)| a.get(0).unwrap().clone())
+ .collect(),
+ );
+ debug!("Series has {} seasons", seasons.len());
+ for (i, season) in seasons.into_iter().enumerate() {
+ info!("Season {} ({})", i + 1, season.get(0).unwrap().season_title);
+ for format in season {
+ info!(
+ "{}: {}px, {:.02} FPS (S{:02}E{:02})",
+ format.title,
+ format.stream.resolution,
+ format.stream.fps,
+ format.season_number,
+ format.number,
+ )
+ }
+ }
+ } else {
+ for season in sort_formats_after_seasons(
+ archive_formats
+ .clone()
+ .into_iter()
+ .map(|(a, _)| a.get(0).unwrap().clone())
+ .collect(),
+ ) {
+ let first = season.get(0).unwrap();
+ info!(
+ "{} Season {} ({})",
+ first.series_name, first.season_number, first.season_title
+ );
+
+ for (i, format) in season.into_iter().enumerate() {
+ tab_info!(
+ "{}. {} » {}px, {:.2} FPS (S{:02}E{:02})",
+ i + 1,
+ format.title,
+ format.stream.resolution,
+ format.stream.fps,
+ format.season_number,
+ format.number
+ )
+ }
+ }
+ }
+
+ for (formats, subtitles) in archive_formats {
+ let (primary, additionally) = formats.split_first().unwrap();
+
+ let mut path = PathBuf::from(&self.output);
+ path = free_file(
+ path.with_file_name(format_string(
+ if let Some(fname) = path.file_name() {
+ fname.to_str().unwrap()
+ } else {
+ "{title}.mkv"
+ }
+ .to_string(),
+ primary,
+ )),
+ )
+ .0;
+
+ info!(
+ "Downloading {} to '{}'",
+ primary.title,
+ path.to_str().unwrap()
+ );
+ tab_info!(
+ "Episode: S{:02}E{:02}",
+ primary.season_number,
+ primary.number
+ );
+ tab_info!(
+ "Audio: {} (primary), {}",
+ primary.audio,
+ additionally
+ .iter()
+ .map(|a| a.audio.to_string())
+ .collect::>()
+ .join(", ")
+ );
+ tab_info!(
+ "Subtitle: {}",
+ subtitles
+ .iter()
+ .map(|s| {
+ if let Some(default) = &self.default_subtitle {
+ if default == &s.locale {
+ return format!("{} (primary)", default);
+ }
+ }
+ s.locale.to_string()
+ })
+ .collect::>()
+ .join(", ")
+ );
+ tab_info!("Resolution: {}", primary.stream.resolution);
+ tab_info!("FPS: {:.2}", primary.stream.fps);
+
+ let mut video_paths = vec![];
+ let mut audio_paths = vec![];
+ let mut subtitle_paths = vec![];
+
+ video_paths.push((download_video(&ctx, primary, false).await?, primary));
+ for additional in additionally {
+ let only_audio = match self.merge {
+ MergeBehavior::Auto => additionally
+ .iter()
+ .all(|a| a.stream.bandwidth == primary.stream.bandwidth),
+ MergeBehavior::Audio => true,
+ MergeBehavior::Video => false,
+ };
+ let path = download_video(&ctx, additional, only_audio).await?;
+ if only_audio {
+ audio_paths.push((path, additional))
+ } else {
+ video_paths.push((path, additional))
+ }
+ }
+
+ for subtitle in subtitles {
+ subtitle_paths
+ .push((download_subtitle(&self, subtitle.clone()).await?, subtitle))
+ }
+
+ generate_mkv(&self, path, video_paths, audio_paths, subtitle_paths)?
+ }
+ }
+
+ Ok(())
+ }
+}
+
+async fn formats_from_series(
+ archive: &Archive,
+ series: Media,
+ url_filter: &UrlFilter,
+) -> Result, Vec)>> {
+ let mut seasons = series.seasons().await?;
+
+ // filter any season out which does not contain the specified audio languages
+ for season in sort_seasons_after_number(seasons.clone()) {
+ // get all locales which are specified but not present in the current iterated season and
+ // print an error saying this
+ let not_present_audio = archive
+ .audio
+ .clone()
+ .into_iter()
+ .filter(|l| !season.iter().any(|s| &s.metadata.audio_locale == l))
+ .collect::>();
+ for not_present in not_present_audio {
+ error!(
+ "Season {} of series {} is not available with {} audio",
+ season.first().unwrap().metadata.season_number,
+ series.title,
+ not_present
+ )
+ }
+
+ // remove all seasons with the wrong audio for the current iterated season number
+ seasons.retain(|s| {
+ s.metadata.season_number != season.first().unwrap().metadata.season_number
+ || archive.audio.contains(&s.metadata.audio_locale)
+ })
+ }
+
+ #[allow(clippy::type_complexity)]
+ let mut result: BTreeMap, Vec)>> =
+ BTreeMap::new();
+ for season in series.seasons().await? {
+ if !url_filter.is_season_valid(season.metadata.season_number)
+ || !archive.audio.contains(&season.metadata.audio_locale)
+ {
+ continue;
+ }
+
+ for episode in season.episodes().await? {
+ if !url_filter.is_episode_valid(
+ episode.metadata.episode_number,
+ episode.metadata.season_number,
+ ) {
+ continue;
+ }
+
+ let streams = episode.streams().await?;
+ let streaming_data = streams.streaming_data(None).await?;
+ let Some(stream) = find_resolution(streaming_data, &archive.resolution) else {
+ bail!(
+ "Resolution ({}x{}) is not available for episode {} ({}) of season {} ({}) of {}",
+ archive.resolution.width,
+ archive.resolution.height,
+ episode.metadata.episode_number,
+ episode.title,
+ episode.metadata.season_number,
+ episode.metadata.season_title,
+ episode.metadata.series_title
+ )
+ };
+
+ let (ref mut formats, _) = result
+ .entry(season.metadata.season_number)
+ .or_insert_with(BTreeMap::new)
+ .entry(episode.metadata.episode_number)
+ .or_insert_with(|| {
+ let subtitles: Vec = archive
+ .subtitle
+ .iter()
+ .filter_map(|l| streams.subtitles.get(l).cloned())
+ .collect();
+ (vec![], subtitles)
+ });
+ formats.push(Format::new_from_episode(episode, stream));
+ }
+ }
+
+ Ok(result.into_values().flat_map(|v| v.into_values()).collect())
+}
+
+async fn download_video(ctx: &Context, format: &Format, only_audio: bool) -> Result {
+ let tempfile = if only_audio {
+ tempfile(".aac")?
+ } else {
+ tempfile(".ts")?
+ };
+ let (_, path) = tempfile.into_parts();
+
+ let ffmpeg = Command::new("ffmpeg")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .arg("-y")
+ .args(["-f", "mpegts", "-i", "pipe:"])
+ .args(if only_audio { vec!["-vn"] } else { vec![] })
+ .arg(path.to_str().unwrap())
+ .spawn()?;
+
+ download_segments(
+ ctx,
+ &mut ffmpeg.stdin.unwrap(),
+ Some(format!("Download {}", format.audio)),
+ format.stream.segments().await?,
+ )
+ .await?;
+
+ Ok(path)
+}
+
+async fn download_subtitle(archive: &Archive, subtitle: StreamSubtitle) -> Result {
+ let tempfile = tempfile(".ass")?;
+ let (mut file, path) = tempfile.into_parts();
+
+ let mut buf = vec![];
+ subtitle.write_to(&mut buf).await?;
+ if !archive.no_subtitle_optimizations {
+ buf = fix_subtitle(buf)
+ }
+
+ file.write_all(buf.as_slice())?;
+
+ Ok(path)
+}
+
+/// Add `ScaledBorderAndShadows: yes` to subtitles; without it they look very messy on some video
+/// players. See [crunchy-labs/crunchy-cli#66](https://github.com/crunchy-labs/crunchy-cli/issues/66)
+/// for more information.
+fn fix_subtitle(raw: Vec) -> Vec {
+ let mut script_info = false;
+ let mut new = String::new();
+
+ for line in String::from_utf8_lossy(raw.as_slice()).split('\n') {
+ if line.trim().starts_with('[') && script_info {
+ new.push_str("ScaledBorderAndShadows: yes\n");
+ script_info = false
+ } else if line.trim() == "[Script Info]" {
+ script_info = true
+ }
+ new.push_str(line);
+ new.push('\n')
+ }
+
+ new.into_bytes()
+}
+
+fn generate_mkv(
+ archive: &Archive,
+ target: PathBuf,
+ video_paths: Vec<(TempPath, &Format)>,
+ audio_paths: Vec<(TempPath, &Format)>,
+ subtitle_paths: Vec<(TempPath, StreamSubtitle)>,
+) -> Result<()> {
+ let mut input = vec![];
+ let mut maps = vec![];
+ let mut metadata = vec![];
+
+ let mut video_length = (0, 0, 0, 0);
+
+ for (i, (video_path, format)) in video_paths.iter().enumerate() {
+ input.extend(["-i".to_string(), video_path.to_string_lossy().to_string()]);
+ maps.extend(["-map".to_string(), i.to_string()]);
+ metadata.extend([
+ format!("-metadata:s:v:{}", i),
+ format!("language={}", format.audio),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:v:{}", i),
+ format!("title={}", format.audio.to_human_readable()),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:a:{}", i),
+ format!("language={}", format.audio),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:a:{}", i),
+ format!("title={}", format.audio.to_human_readable()),
+ ]);
+
+ let vid_len = get_video_length(video_path.to_path_buf())?;
+ if vid_len > video_length {
+ video_length = vid_len
+ }
+ }
+ for (i, (audio_path, format)) in audio_paths.iter().enumerate() {
+ input.extend(["-i".to_string(), audio_path.to_string_lossy().to_string()]);
+ maps.extend(["-map".to_string(), (i + video_paths.len()).to_string()]);
+ metadata.extend([
+ format!("-metadata:s:a:{}", i + video_paths.len()),
+ format!("language={}", format.audio),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:a:{}", i + video_paths.len()),
+ format!("title={}", format.audio.to_human_readable()),
+ ]);
+ }
+ for (i, (subtitle_path, subtitle)) in subtitle_paths.iter().enumerate() {
+ input.extend([
+ "-i".to_string(),
+ subtitle_path.to_string_lossy().to_string(),
+ ]);
+ maps.extend([
+ "-map".to_string(),
+ (i + video_paths.len() + audio_paths.len()).to_string(),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:s:{}", i),
+ format!("language={}", subtitle.locale),
+ ]);
+ metadata.extend([
+ format!("-metadata:s:s:{}", i),
+ format!("title={}", subtitle.locale.to_human_readable()),
+ ]);
+ }
+
+ let mut command_args = vec!["-y".to_string()];
+ command_args.extend(input);
+ command_args.extend(maps);
+ command_args.extend(metadata);
+
+ // set default subtitle
+ if let Some(default_subtitle) = &archive.default_subtitle {
+ // if `--default_subtitle ` is given set the default subtitle to the given locale
+ if let Some(position) = subtitle_paths
+ .into_iter()
+ .position(|s| &s.1.locale == default_subtitle)
+ {
+ command_args.push(format!("-disposition:s:{}", position))
+ } else {
+ command_args.extend(["-disposition:s:0".to_string(), "0".to_string()])
+ }
+ } else {
+ command_args.extend(["-disposition:s:0".to_string(), "0".to_string()])
+ }
+
+ command_args.extend([
+ "-c".to_string(),
+ "copy".to_string(),
+ "-f".to_string(),
+ "matroska".to_string(),
+ target.to_string_lossy().to_string(),
+ ]);
+
+ debug!("ffmpeg {}", command_args.join(" "));
+
+ let ffmpeg = Command::new("ffmpeg")
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .args(command_args)
+ .output()?;
+ if !ffmpeg.status.success() {
+ bail!("{}", String::from_utf8_lossy(ffmpeg.stderr.as_slice()))
+ }
+
+ Ok(())
+}
+
+/// Get the length of a video. This is required because sometimes subtitles have an unnecessary entry
+/// long after the actual video ends with artificially extends the video length on some video players.
+/// To prevent this, the video length must be hard set with ffmpeg. See
+/// [crunchy-labs/crunchy-cli#32](https://github.com/crunchy-labs/crunchy-cli/issues/32) for more
+/// information.
+fn get_video_length(path: PathBuf) -> Result<(u32, u32, u32, u32)> {
+ let video_length = Regex::new(r"Duration:\s?(\d+):(\d+):(\d+).(\d+),")?;
+
+ let ffmpeg = Command::new("ffmpeg")
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .arg("-y")
+ .args(["-i", path.to_str().unwrap()])
+ .output()?;
+ let ffmpeg_output = String::from_utf8(ffmpeg.stderr)?;
+ let caps = video_length.captures(ffmpeg_output.as_str()).unwrap();
+
+ Ok((
+ caps[1].parse()?,
+ caps[2].parse()?,
+ caps[3].parse()?,
+ caps[4].parse()?,
+ ))
+}
diff --git a/crunchy-cli-core/src/cli/download.rs b/crunchy-cli-core/src/cli/download.rs
new file mode 100644
index 0000000..7f9e3ec
--- /dev/null
+++ b/crunchy-cli-core/src/cli/download.rs
@@ -0,0 +1,452 @@
+use crate::cli::log::tab_info;
+use crate::cli::utils::{download_segments, find_resolution};
+use crate::utils::context::Context;
+use crate::utils::format::{format_string, Format};
+use crate::utils::log::progress;
+use crate::utils::os::{free_file, has_ffmpeg};
+use crate::utils::parse::{parse_url, UrlFilter};
+use crate::utils::sort::{sort_formats_after_seasons, sort_seasons_after_number};
+use crate::Execute;
+use anyhow::{bail, Result};
+use crunchyroll_rs::media::{Resolution, VariantSegment};
+use crunchyroll_rs::{
+ Episode, Locale, Media, MediaCollection, Movie, MovieListing, Season, Series,
+};
+use log::{debug, error, info};
+use std::fs::File;
+use std::path::{Path, PathBuf};
+use std::process::{Command, Stdio};
+
+#[derive(Debug, clap::Parser)]
+#[clap(about = "Download a video")]
+#[command(arg_required_else_help(true))]
+pub struct Download {
+ #[arg(help = format!("Audio language. Can only be used if the provided url(s) point to a series. \
+ Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(long_help = format!("Audio language. Can only be used if the provided url(s) point to a series. \
+ Available languages are:\n{}", Locale::all().into_iter().map(|l| format!("{:<6} → {}", l.to_string(), l.to_human_readable())).collect::>().join("\n ")))]
+ #[arg(short, long, default_value_t = crate::utils::locale::system_locale())]
+ audio: Locale,
+ #[arg(help = format!("Subtitle language. Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(long_help = format!("Subtitle language. If set, the subtitle will be burned into the video and cannot be disabled. \
+ Available languages are: {}", Locale::all().into_iter().map(|l| l.to_string()).collect::>().join(", ")))]
+ #[arg(short, long)]
+ subtitle: Option,
+
+ #[arg(help = "Name of the output file")]
+ #[arg(long_help = "Name of the output file.\
+ If you use one of the following pattern they will get replaced:\n \
+ {title} → Title of the video\n \
+ {series_name} → Name of the series\n \
+ {season_name} → Name of the season\n \
+ {audio} → Audio language of the video\n \
+ {resolution} → Resolution of the video\n \
+ {season_number} → Number of the season\n \
+ {episode_number} → Number of the episode\n \
+ {series_id} → ID of the series\n \
+ {season_id} → ID of the season\n \
+ {episode_id} → ID of the episode")]
+ #[arg(short, long, default_value = "{title}.ts")]
+ output: String,
+
+ #[arg(help = "Video resolution")]
+ #[arg(long_help = "The video resolution.\
+ Can either be specified via the pixels (e.g. 1920x1080), the abbreviation for pixels (e.g. 1080p) or 'common-use' words (e.g. best). \
+ Specifying the exact pixels is not recommended, use one of the other options instead. \
+ Crunchyroll let you choose the quality with pixel abbreviation on their clients, so you might be already familiar with the available options. \
+ The available common-use words are 'best' (choose the best resolution available) and 'worst' (worst resolution available)")]
+ #[arg(short, long, default_value = "best")]
+ #[arg(value_parser = crate::utils::clap::clap_parse_resolution)]
+ resolution: Resolution,
+
+ #[arg(help = "Url(s) to Crunchyroll episodes or series")]
+ urls: Vec,
+}
+
+#[async_trait::async_trait(?Send)]
+impl Execute for Download {
+ async fn execute(self, ctx: Context) -> Result<()> {
+ let mut parsed_urls = vec![];
+
+ for (i, url) in self.urls.iter().enumerate() {
+ let _progress_handler = progress!("Parsing url {}", i + 1);
+ match parse_url(&ctx.crunchy, url.clone(), true).await {
+ Ok((media_collection, url_filter)) => {
+ parsed_urls.push((media_collection, url_filter));
+ info!("Parsed url {}", i + 1)
+ }
+ Err(e) => bail!("url {} could not be parsed: {}", url, e),
+ }
+ }
+
+ for (i, (media_collection, url_filter)) in parsed_urls.into_iter().enumerate() {
+ let _progress_handler = progress!("Fetching series details");
+ let formats = match media_collection {
+ MediaCollection::Series(series) => {
+ debug!("Url {} is series ({})", i + 1, series.title);
+ formats_from_series(&self, series, &url_filter).await?
+ }
+ MediaCollection::Season(season) => {
+ debug!(
+ "Url {} is season {} ({})",
+ i + 1,
+ season.metadata.season_number,
+ season.title
+ );
+ formats_from_season(&self, season, &url_filter).await?
+ }
+ MediaCollection::Episode(episode) => {
+ debug!(
+ "Url {} is episode {} ({}) of season {} ({}) of {}",
+ i + 1,
+ episode.metadata.episode_number,
+ episode.title,
+ episode.metadata.season_number,
+ episode.metadata.season_title,
+ episode.metadata.series_title
+ );
+ format_from_episode(&self, episode, &url_filter, false)
+ .await?
+ .map(|fmt| vec![fmt])
+ }
+ MediaCollection::MovieListing(movie_listing) => {
+ debug!("Url {} is movie listing ({})", i + 1, movie_listing.title);
+ format_from_movie_listing(&self, movie_listing, &url_filter).await?
+ }
+ MediaCollection::Movie(movie) => {
+ debug!("Url {} is movie ({})", i + 1, movie.title);
+ format_from_movie(&self, movie, &url_filter)
+ .await?
+ .map(|fmt| vec![fmt])
+ }
+ };
+
+ let Some(formats) = formats else {
+ info!("Skipping url {} (no matching episodes found)", i + 1);
+ continue;
+ };
+ info!("Loaded series information for url {}", i + 1);
+ drop(_progress_handler);
+
+ if log::max_level() == log::Level::Debug {
+ let seasons = sort_formats_after_seasons(formats.clone());
+ debug!("Series has {} seasons", seasons.len());
+ for (i, season) in seasons.into_iter().enumerate() {
+ info!("Season {} ({})", i + 1, season.get(0).unwrap().season_title);
+ for format in season {
+ info!(
+ "{}: {}px, {:.02} FPS (S{:02}E{:02})",
+ format.title,
+ format.stream.resolution,
+ format.stream.fps,
+ format.season_number,
+ format.number,
+ )
+ }
+ }
+ } else {
+ for season in sort_formats_after_seasons(formats.clone()) {
+ let first = season.get(0).unwrap();
+ info!(
+ "{} Season {} ({})",
+ first.series_name, first.season_number, first.season_title
+ );
+
+ for (i, format) in season.into_iter().enumerate() {
+ tab_info!(
+ "{}. {} » {}px, {:.2} FPS (S{:02}E{:02})",
+ i + 1,
+ format.title,
+ format.stream.resolution,
+ format.stream.fps,
+ format.season_number,
+ format.number
+ )
+ }
+ }
+ }
+
+ for format in formats {
+ let mut path = PathBuf::from(&self.output);
+ path = free_file(
+ path.with_file_name(format_string(
+ if let Some(fname) = path.file_name() {
+ fname.to_str().unwrap()
+ } else {
+ "{title}.ts"
+ }
+ .to_string(),
+ &format,
+ )),
+ )
+ .0;
+
+ let use_ffmpeg = if let Some(extension) = path.extension() {
+ if extension != "ts" {
+ if !has_ffmpeg() {
+ bail!(
+ "File ending is not `.ts`, ffmpeg is required to convert the video"
+ )
+ }
+ true
+ } else {
+ false
+ }
+ } else {
+ false
+ };
+
+ info!(
+ "Downloading {} to '{}'",
+ format.title,
+ path.file_name().unwrap().to_str().unwrap()
+ );
+ tab_info!("Episode: S{:02}E{:02}", format.season_number, format.number);
+ tab_info!("Audio: {}", format.audio);
+ tab_info!(
+ "Subtitles: {}",
+ self.subtitle
+ .clone()
+ .map_or("None".to_string(), |l| l.to_string())
+ );
+ tab_info!("Resolution: {}", format.stream.resolution);
+ tab_info!("FPS: {:.2}", format.stream.fps);
+
+ let segments = format.stream.segments().await?;
+
+ if use_ffmpeg {
+ download_ffmpeg(&ctx, segments, path.as_path()).await?;
+ } else if path.to_str().unwrap() == "-" {
+ let mut stdout = std::io::stdout().lock();
+ download_segments(&ctx, &mut stdout, None, segments).await?;
+ } else {
+ let mut file = File::options().create(true).write(true).open(&path)?;
+ download_segments(&ctx, &mut file, None, segments).await?
+ }
+ }
+ }
+
+ Ok(())
+ }
+}
+
+async fn download_ffmpeg(
+ ctx: &Context,
+ segments: Vec,
+ target: &Path,
+) -> Result<()> {
+ let ffmpeg = Command::new("ffmpeg")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .arg("-y")
+ .args(["-f", "mpegts", "-i", "pipe:"])
+ .args(["-safe", "0"])
+ .args(["-c", "copy"])
+ .arg(target.to_str().unwrap())
+ .spawn()?;
+
+ download_segments(ctx, &mut ffmpeg.stdin.unwrap(), None, segments).await?;
+
+ Ok(())
+}
+
+async fn formats_from_series(
+ download: &Download,
+ series: Media,
+ url_filter: &UrlFilter,
+) -> Result