mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Merge remote-tracking branch 'origin/next/partially-v3'
# Conflicts: # Makefile # README.md # cli/commands/archive/archive.go # cli/commands/download/download.go # cmd/crunchyroll-go/cmd/login.go # cmd/crunchyroll-go/cmd/root.go # cmd/crunchyroll-go/cmd/utils.go # cmd/crunchyroll-go/main.go # crunchy-cli.1 # crunchyroll.go # go.mod # go.sum # utils/locale.go # utils/sort.go
This commit is contained in:
commit
a907958a71
44 changed files with 2297 additions and 3140 deletions
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
|
|
@ -14,7 +14,7 @@ jobs:
|
||||||
go-version: 1.18
|
go-version: 1.18
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go build -v cmd/crunchyroll-go/main.go
|
run: go build -v .
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test -v cmd/crunchyroll-go/main.go
|
run: go test -v .
|
||||||
|
|
|
||||||
687
LICENSE
687
LICENSE
|
|
@ -1,61 +1,674 @@
|
||||||
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
Preamble
|
||||||
0. Additional Definitions.
|
|
||||||
|
|
||||||
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
1. Exception to Section 3 of the GNU GPL.
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
2. Conveying Modified Versions.
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
Finally, every program is threatened constantly by software patents.
|
||||||
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
3. Object Code Incorporating Material from Library Header Files.
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
0. Definitions.
|
||||||
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
||||||
|
|
||||||
4. Combined Works.
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
"recipients" may be individuals or organizations.
|
||||||
d) Do one of the following:
|
|
||||||
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
|
||||||
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
|
||||||
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
||||||
|
|
||||||
5. Combined Libraries.
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
To "propagate" a work means to do anything with it that, without
|
||||||
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
6. Revised Versions of the GNU Lesser General Public License.
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
1. Source Code.
|
||||||
|
|
||||||
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||||
|
|
|
||||||
24
Makefile
24
Makefile
|
|
@ -1,4 +1,4 @@
|
||||||
VERSION=2.2.2
|
VERSION=development
|
||||||
BINARY_NAME=crunchy
|
BINARY_NAME=crunchy
|
||||||
VERSION_BINARY_NAME=$(BINARY_NAME)-v$(VERSION)
|
VERSION_BINARY_NAME=$(BINARY_NAME)-v$(VERSION)
|
||||||
|
|
||||||
|
|
@ -6,26 +6,26 @@ DESTDIR=
|
||||||
PREFIX=/usr
|
PREFIX=/usr
|
||||||
|
|
||||||
build:
|
build:
|
||||||
go build -ldflags "-X 'github.com/crunchy-labs/crunchyroll-go/v2/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(BINARY_NAME) cmd/crunchyroll-go/main.go
|
go build -ldflags "-X 'github.com/crunchy-labs/crunchy-cli/utils.Version=$(VERSION)'" -o $(BINARY_NAME) .
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(BINARY_NAME) $(VERSION_BINARY_NAME)_*
|
rm -f $(BINARY_NAME) $(VERSION_BINARY_NAME)_*
|
||||||
|
|
||||||
install:
|
install:
|
||||||
install -Dm755 $(BINARY_NAME) $(DESTDIR)$(PREFIX)/bin/crunchyroll-go
|
install -Dm755 $(BINARY_NAME) $(DESTDIR)$(PREFIX)/bin/crunchy-cli
|
||||||
ln -sf ./crunchyroll-go $(DESTDIR)$(PREFIX)/bin/crunchy
|
ln -sf ./crunchy-cli $(DESTDIR)$(PREFIX)/bin/crunchy
|
||||||
install -Dm644 crunchyroll-go.1 $(DESTDIR)$(PREFIX)/share/man/man1/crunchyroll-go.1
|
install -Dm644 crunchy-cli.1 $(DESTDIR)$(PREFIX)/share/man/man1/crunchy-cli.1
|
||||||
install -Dm644 LICENSE $(DESTDIR)$(PREFIX)/share/licenses/crunchyroll-go/LICENSE
|
install -Dm644 LICENSE $(DESTDIR)$(PREFIX)/share/licenses/crunchy-cli/LICENSE
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f $(DESTDIR)$(PREFIX)/bin/crunchyroll-go
|
rm -f $(DESTDIR)$(PREFIX)/bin/crunchy-cli
|
||||||
rm -f $(DESTDIR)$(PREFIX)/bin/crunchy
|
rm -f $(DESTDIR)$(PREFIX)/bin/crunchy
|
||||||
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/crunchyroll-go.1
|
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/crunchy-cli.1
|
||||||
rm -f $(DESTDIR)$(PREFIX)/share/licenses/crunchyroll-go/LICENSE
|
rm -f $(DESTDIR)$(PREFIX)/share/licenses/crunchy-cli/LICENSE
|
||||||
|
|
||||||
release:
|
release:
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X 'github.com/crunchy-labs/crunchyroll-go/v2/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_linux cmd/crunchyroll-go/main.go
|
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/crunchyroll-go/v2/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_windows.exe cmd/crunchyroll-go/main.go
|
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/crunchyroll-go/v2/cmd/crunchyroll-go/cmd.Version=$(VERSION)'" -o $(VERSION_BINARY_NAME)_darwin cmd/crunchyroll-go/main.go
|
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
|
strip $(VERSION_BINARY_NAME)_linux
|
||||||
|
|
|
||||||
28
README.md
28
README.md
|
|
@ -1,6 +1,6 @@
|
||||||
# crunchy-cli
|
# 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 (api) access.
|
A [go](https://golang.org) written cli client for [crunchyroll](https://www.crunchyroll.com). To use it, you need a crunchyroll premium account to for full (api) access.
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/crunchy-labs/crunchy-cli">
|
<a href="https://github.com/crunchy-labs/crunchy-cli">
|
||||||
|
|
@ -18,11 +18,8 @@ A [go](https://golang.org) written cli client for [crunchyroll](https://www.crun
|
||||||
<a href="https://github.com/crunchy-labs/crunchy-cli/releases/latest">
|
<a href="https://github.com/crunchy-labs/crunchy-cli/releases/latest">
|
||||||
<img src="https://img.shields.io/github/v/release/crunchy-labs/crunchy-cli?style=flat-square" alt="Release">
|
<img src="https://img.shields.io/github/v/release/crunchy-labs/crunchy-cli?style=flat-square" alt="Release">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://discord.gg/PXGPGpQxgk">
|
<a href="https://discord.gg/gUWwekeNNg">
|
||||||
<img src="https://img.shields.io/discord/994882878125121596?label=discord&style=flat-square" alt="Discord">
|
<img src="https://img.shields.io/discord/915659846836162561?label=discord&style=flat-square" alt="Discord">
|
||||||
</a>
|
|
||||||
<a href="https://github.com/crunchy-labs/crunchy-cli/actions/workflows/ci.yml">
|
|
||||||
<img src="https://github.com/crunchy-labs/crunchy-cli/workflows/CI/badge.svg?style=flat" alt="CI">
|
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
@ -35,7 +32,11 @@ A [go](https://golang.org) written cli client for [crunchyroll](https://www.crun
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
_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 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.
|
||||||
See [#39](https://github.com/crunchy-labs/crunchy-cli/issues/39) for more information._
|
See #39 for more information._
|
||||||
|
|
||||||
|
> 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
|
# 🖥️ CLI
|
||||||
|
|
||||||
|
|
@ -48,18 +49,23 @@ See [#39](https://github.com/crunchy-labs/crunchy-cli/issues/39) for more inform
|
||||||
## 💾 Get the executable
|
## 💾 Get the executable
|
||||||
|
|
||||||
- 📥 Download the latest binaries [here](https://github.com/crunchy-labs/crunchy-cli/releases/latest) or get it from below:
|
- 📥 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)
|
- [Linux (x64)](https://smartrelease.crunchy-labs.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_linux)
|
||||||
- [Windows (x64)](https://smartrelease.bytedream.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_windows.exe)
|
- [Windows (x64)](https://smartrelease.crunchy-labs.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_windows.exe)
|
||||||
- [MacOS (x64)](https://smartrelease.bytedream.org/github/crunchy-labs/crunchy-cli/crunchy-{tag}_darwin)
|
- [MacOS (x64)](https://smartrelease.crunchy-labs.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/):
|
- 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
|
```shell
|
||||||
$ yay -S crunchyroll-go
|
$ yay -S crunchyroll-go
|
||||||
```
|
```
|
||||||
- On Windows [scoop](https://scoop.sh/) can be used to install it (added by [@AdmnJ](https://github.com/AdmnJ)):
|
- <del>
|
||||||
|
|
||||||
|
On Windows [scoop](https://scoop.sh/) can be used to install it (added by [@AdmnJ](https://github.com/AdmnJ)):
|
||||||
```shell
|
```shell
|
||||||
$ scoop bucket add extras # <- in case you haven't added the extra repository already
|
$ scoop bucket add extras # <- in case you haven't added the extra repository already
|
||||||
$ scoop install crunchyroll-go
|
$ scoop install crunchyroll-go
|
||||||
```
|
```
|
||||||
|
|
||||||
|
</del>
|
||||||
|
<i>Currently not working because the repo got renamed!</i>
|
||||||
- 🛠 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):
|
- 🛠 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):
|
- use `make` (requires `go` to be installed):
|
||||||
```shell
|
```shell
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,14 @@
|
||||||
package cmd
|
package archive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"archive/zip"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
"github.com/crunchy-labs/crunchy-cli/cli/commands"
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2/utils"
|
"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/grafov/m3u8"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -23,8 +22,6 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -32,6 +29,7 @@ var (
|
||||||
|
|
||||||
archiveDirectoryFlag string
|
archiveDirectoryFlag string
|
||||||
archiveOutputFlag string
|
archiveOutputFlag string
|
||||||
|
archiveTempDirFlag string
|
||||||
|
|
||||||
archiveMergeFlag string
|
archiveMergeFlag string
|
||||||
|
|
||||||
|
|
@ -42,39 +40,39 @@ var (
|
||||||
archiveGoroutinesFlag int
|
archiveGoroutinesFlag int
|
||||||
)
|
)
|
||||||
|
|
||||||
var archiveCmd = &cobra.Command{
|
var Cmd = &cobra.Command{
|
||||||
Use: "archive",
|
Use: "archive",
|
||||||
Short: "Stores the given videos with all subtitles and multiple audios in a .mkv file",
|
Short: "Stores the given videos with all subtitles and multiple audios in a .mkv file",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
out.Debug("Validating arguments")
|
utils.Log.Debug("Validating arguments")
|
||||||
|
|
||||||
if !hasFFmpeg() {
|
if !utils.HasFFmpeg() {
|
||||||
return fmt.Errorf("ffmpeg is needed to run this command correctly")
|
return fmt.Errorf("ffmpeg is needed to run this command correctly")
|
||||||
}
|
}
|
||||||
out.Debug("FFmpeg detected")
|
utils.Log.Debug("FFmpeg detected")
|
||||||
|
|
||||||
if filepath.Ext(archiveOutputFlag) != ".mkv" {
|
if filepath.Ext(archiveOutputFlag) != ".mkv" {
|
||||||
return fmt.Errorf("currently only matroska / .mkv files are supported")
|
return fmt.Errorf("currently only matroska / .mkv files are supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, locale := range archiveLanguagesFlag {
|
for _, locale := range archiveLanguagesFlag {
|
||||||
if !utils.ValidateLocale(crunchyroll.LOCALE(locale)) {
|
if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(locale)) {
|
||||||
// if locale is 'all', match all known locales
|
// if locale is 'all', match all known locales
|
||||||
if locale == "all" {
|
if locale == "all" {
|
||||||
archiveLanguagesFlag = allLocalesAsStrings()
|
archiveLanguagesFlag = utils.LocalesAsStrings()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return fmt.Errorf("%s is not a valid locale. Choose from: %s", locale, strings.Join(allLocalesAsStrings(), ", "))
|
return fmt.Errorf("%s is not a valid locale. Choose from: %s", locale, strings.Join(utils.LocalesAsStrings(), ", "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.Debug("Using following audio locales: %s", strings.Join(archiveLanguagesFlag, ", "))
|
utils.Log.Debug("Using following audio locales: %s", strings.Join(archiveLanguagesFlag, ", "))
|
||||||
|
|
||||||
var found bool
|
var found bool
|
||||||
for _, mode := range []string{"auto", "audio", "video"} {
|
for _, mode := range []string{"auto", "audio", "video"} {
|
||||||
if archiveMergeFlag == mode {
|
if archiveMergeFlag == mode {
|
||||||
out.Debug("Using %s merge behavior", archiveMergeFlag)
|
utils.Log.Debug("Using %s merge behavior", archiveMergeFlag)
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -87,7 +85,7 @@ var archiveCmd = &cobra.Command{
|
||||||
found = false
|
found = false
|
||||||
for _, algo := range []string{".tar", ".tar.gz", ".tgz", ".zip"} {
|
for _, algo := range []string{".tar", ".tar.gz", ".tgz", ".zip"} {
|
||||||
if strings.HasSuffix(archiveCompressFlag, algo) {
|
if strings.HasSuffix(archiveCompressFlag, algo) {
|
||||||
out.Debug("Using %s compression", algo)
|
utils.Log.Debug("Using %s compression", algo)
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -100,8 +98,8 @@ var archiveCmd = &cobra.Command{
|
||||||
|
|
||||||
switch archiveResolutionFlag {
|
switch archiveResolutionFlag {
|
||||||
case "1080p", "720p", "480p", "360p":
|
case "1080p", "720p", "480p", "360p":
|
||||||
intRes, _ := strconv.ParseFloat(strings.TrimSuffix(downloadResolutionFlag, "p"), 84)
|
intRes, _ := strconv.ParseFloat(strings.TrimSuffix(archiveResolutionFlag, "p"), 84)
|
||||||
archiveResolutionFlag = fmt.Sprintf("%.0fx%s", math.Ceil(intRes*(float64(16)/float64(9))), strings.TrimSuffix(downloadResolutionFlag, "p"))
|
archiveResolutionFlag = fmt.Sprintf("%.0fx%s", math.Ceil(intRes*(float64(16)/float64(9))), strings.TrimSuffix(archiveResolutionFlag, "p"))
|
||||||
case "240p":
|
case "240p":
|
||||||
// 240p would round up to 427x240 if used in the case statement above, so it has to be handled separately
|
// 240p would round up to 427x240 if used in the case statement above, so it has to be handled separately
|
||||||
archiveResolutionFlag = "428x240"
|
archiveResolutionFlag = "428x240"
|
||||||
|
|
@ -109,31 +107,33 @@ var archiveCmd = &cobra.Command{
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("'%s' is not a valid resolution", archiveResolutionFlag)
|
return fmt.Errorf("'%s' is not a valid resolution", archiveResolutionFlag)
|
||||||
}
|
}
|
||||||
out.Debug("Using resolution '%s'", archiveResolutionFlag)
|
utils.Log.Debug("Using resolution '%s'", archiveResolutionFlag)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
loadCrunchy()
|
if err := commands.LoadCrunchy(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return archive(args)
|
return archive(args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
archiveCmd.Flags().StringSliceVarP(&archiveLanguagesFlag,
|
Cmd.Flags().StringSliceVarP(&archiveLanguagesFlag,
|
||||||
"language",
|
"language",
|
||||||
"l",
|
"l",
|
||||||
[]string{string(systemLocale(false)), string(crunchyroll.JP)},
|
[]string{string(utils.SystemLocale(false)), string(crunchyroll.JP)},
|
||||||
"Audio locale which should be downloaded. Can be used multiple times")
|
"Audio locale which should be downloaded. Can be used multiple times")
|
||||||
|
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
archiveCmd.Flags().StringVarP(&archiveDirectoryFlag,
|
Cmd.Flags().StringVarP(&archiveDirectoryFlag,
|
||||||
"directory",
|
"directory",
|
||||||
"d",
|
"d",
|
||||||
cwd,
|
cwd,
|
||||||
"The directory to store the files into")
|
"The directory to store the files into")
|
||||||
archiveCmd.Flags().StringVarP(&archiveOutputFlag,
|
Cmd.Flags().StringVarP(&archiveOutputFlag,
|
||||||
"output",
|
"output",
|
||||||
"o",
|
"o",
|
||||||
"{title}.mkv",
|
"{title}.mkv",
|
||||||
|
|
@ -147,14 +147,18 @@ func init() {
|
||||||
"\t{fps} » Frame Rate of the video\n"+
|
"\t{fps} » Frame Rate of the video\n"+
|
||||||
"\t{audio} » Audio locale of the video\n"+
|
"\t{audio} » Audio locale of the video\n"+
|
||||||
"\t{subtitle} » Subtitle locale of the video")
|
"\t{subtitle} » Subtitle locale of the video")
|
||||||
|
Cmd.Flags().StringVar(&archiveTempDirFlag,
|
||||||
|
"temp",
|
||||||
|
os.TempDir(),
|
||||||
|
"Directory to store temporary files in")
|
||||||
|
|
||||||
archiveCmd.Flags().StringVarP(&archiveMergeFlag,
|
Cmd.Flags().StringVarP(&archiveMergeFlag,
|
||||||
"merge",
|
"merge",
|
||||||
"m",
|
"m",
|
||||||
"auto",
|
"auto",
|
||||||
"Sets the behavior of the stream merging. Valid behaviors are 'auto', 'audio', 'video'")
|
"Sets the behavior of the stream merging. Valid behaviors are 'auto', 'audio', 'video'")
|
||||||
|
|
||||||
archiveCmd.Flags().StringVarP(&archiveCompressFlag,
|
Cmd.Flags().StringVarP(&archiveCompressFlag,
|
||||||
"compress",
|
"compress",
|
||||||
"c",
|
"c",
|
||||||
"",
|
"",
|
||||||
|
|
@ -162,7 +166,7 @@ func init() {
|
||||||
"This flag sets the name of the compressed output file. The file ending specifies the compression algorithm. "+
|
"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")
|
"The following algorithms are supported: gzip, tar, zip")
|
||||||
|
|
||||||
archiveCmd.Flags().StringVarP(&archiveResolutionFlag,
|
Cmd.Flags().StringVarP(&archiveResolutionFlag,
|
||||||
"resolution",
|
"resolution",
|
||||||
"r",
|
"r",
|
||||||
"best",
|
"best",
|
||||||
|
|
@ -171,49 +175,49 @@ func init() {
|
||||||
"\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
|
"\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
|
||||||
"\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
|
"\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
|
||||||
|
|
||||||
archiveCmd.Flags().IntVarP(&archiveGoroutinesFlag,
|
Cmd.Flags().IntVarP(&archiveGoroutinesFlag,
|
||||||
"goroutines",
|
"goroutines",
|
||||||
"g",
|
"g",
|
||||||
runtime.NumCPU(),
|
runtime.NumCPU(),
|
||||||
"Number of parallel segment downloads")
|
"Number of parallel segment downloads")
|
||||||
|
|
||||||
rootCmd.AddCommand(archiveCmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func archive(urls []string) error {
|
func archive(urls []string) error {
|
||||||
for i, url := range urls {
|
for i, url := range urls {
|
||||||
out.SetProgress("Parsing url %d", i+1)
|
utils.Log.SetProcess("Parsing url %d", i+1)
|
||||||
episodes, err := archiveExtractEpisodes(url)
|
episodes, err := archiveExtractEpisodes(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out.StopProgress("Failed to parse url %d", i+1)
|
utils.Log.StopProcess("Failed to parse url %d", i+1)
|
||||||
out.Debug("If the error says no episodes could be found but the passed url is correct and a crunchyroll classic url, " +
|
if utils.Crunchy.Config.Premium {
|
||||||
"try the corresponding crunchyroll beta url instead and try again. See https://github.com/crunchy-labs/crunchyroll-go/issues/22 for more information")
|
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
|
return err
|
||||||
}
|
}
|
||||||
out.StopProgress("Parsed url %d", i+1)
|
utils.Log.StopProcess("Parsed url %d", i+1)
|
||||||
|
|
||||||
var compressFile *os.File
|
var compressFile *os.File
|
||||||
var c compress
|
var c Compress
|
||||||
|
|
||||||
if archiveCompressFlag != "" {
|
if archiveCompressFlag != "" {
|
||||||
compressFile, err = os.Create(generateFilename(archiveCompressFlag, ""))
|
compressFile, err = os.Create(utils.GenerateFilename(archiveCompressFlag, ""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create archive file: %v", err)
|
return fmt.Errorf("failed to create archive file: %v", err)
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(archiveCompressFlag, ".tar") {
|
if strings.HasSuffix(archiveCompressFlag, ".tar") {
|
||||||
c = newTarCompress(compressFile)
|
c = NewTarCompress(compressFile)
|
||||||
} else if strings.HasSuffix(archiveCompressFlag, ".tar.gz") || strings.HasSuffix(archiveCompressFlag, ".tgz") {
|
} else if strings.HasSuffix(archiveCompressFlag, ".tar.gz") || strings.HasSuffix(archiveCompressFlag, ".tgz") {
|
||||||
c = newGzipCompress(compressFile)
|
c = NewGzipCompress(compressFile)
|
||||||
} else if strings.HasSuffix(archiveCompressFlag, ".zip") {
|
} else if strings.HasSuffix(archiveCompressFlag, ".zip") {
|
||||||
c = newZipCompress(compressFile)
|
c = NewZipCompress(compressFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, season := range episodes {
|
for _, season := range episodes {
|
||||||
out.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
|
utils.Log.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
|
||||||
|
|
||||||
for j, info := range season {
|
for j, info := range season {
|
||||||
out.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
|
utils.Log.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
|
||||||
j+1,
|
j+1,
|
||||||
info.Title,
|
info.Title,
|
||||||
info.Resolution,
|
info.Resolution,
|
||||||
|
|
@ -222,26 +226,26 @@ func archive(urls []string) error {
|
||||||
info.EpisodeNumber)
|
info.EpisodeNumber)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
|
|
||||||
for j, season := range episodes {
|
for j, season := range episodes {
|
||||||
for k, info := range season {
|
for k, info := range season {
|
||||||
var filename string
|
var filename string
|
||||||
var writeCloser io.WriteCloser
|
var writeCloser io.WriteCloser
|
||||||
if c != nil {
|
if c != nil {
|
||||||
filename = info.Format(archiveOutputFlag)
|
filename = info.FormatString(archiveOutputFlag)
|
||||||
writeCloser, err = c.NewFile(info)
|
writeCloser, err = c.NewFile(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to pre generate new archive file: %v", err)
|
return fmt.Errorf("failed to pre generate new archive file: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dir := info.Format(archiveDirectoryFlag)
|
dir := info.FormatString(archiveDirectoryFlag)
|
||||||
if _, err = os.Stat(dir); os.IsNotExist(err) {
|
if _, err = os.Stat(dir); os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(dir, 0777); err != nil {
|
if err = os.MkdirAll(dir, 0777); err != nil {
|
||||||
return fmt.Errorf("error while creating directory: %v", err)
|
return fmt.Errorf("error while creating directory: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filename = generateFilename(info.Format(archiveOutputFlag), dir)
|
filename = utils.GenerateFilename(info.FormatString(archiveOutputFlag), dir)
|
||||||
writeCloser, err = os.Create(filename)
|
writeCloser, err = os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new file: %v", err)
|
return fmt.Errorf("failed to create new file: %v", err)
|
||||||
|
|
@ -262,7 +266,7 @@ func archive(urls []string) error {
|
||||||
writeCloser.Close()
|
writeCloser.Close()
|
||||||
|
|
||||||
if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
|
if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -276,8 +280,8 @@ func archive(urls []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename string) error {
|
func archiveInfo(info utils.FormatInformation, writeCloser io.WriteCloser, filename string) error {
|
||||||
out.Debug("Entering season %d, episode %d with %d additional formats", info.SeasonNumber, info.EpisodeNumber, len(info.additionalFormats))
|
utils.Log.Debug("Entering season %d, episode %d with %d additional formats", info.SeasonNumber, info.EpisodeNumber, len(info.AdditionalFormats))
|
||||||
|
|
||||||
dp, err := createArchiveProgress(info)
|
dp, err := createArchiveProgress(info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -305,7 +309,7 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if out.IsDev() {
|
if utils.Log.IsDev() {
|
||||||
dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
|
dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
|
||||||
} else {
|
} else {
|
||||||
dp.Update()
|
dp.Update()
|
||||||
|
|
@ -316,6 +320,8 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
tmp, _ := os.MkdirTemp(archiveTempDirFlag, "crunchy_")
|
||||||
|
downloader.TempDir = tmp
|
||||||
|
|
||||||
sig := make(chan os.Signal, 1)
|
sig := make(chan os.Signal, 1)
|
||||||
signal.Notify(sig, os.Interrupt)
|
signal.Notify(sig, os.Interrupt)
|
||||||
|
|
@ -323,8 +329,8 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
select {
|
select {
|
||||||
case <-sig:
|
case <-sig:
|
||||||
signal.Stop(sig)
|
signal.Stop(sig)
|
||||||
out.Exit("Exiting... (may take a few seconds)")
|
utils.Log.Err("Exiting... (may take a few seconds)")
|
||||||
out.Exit("To force exit press ctrl+c (again)")
|
utils.Log.Err("To force exit press ctrl+c (again)")
|
||||||
cancel()
|
cancel()
|
||||||
// os.Exit(1) is not called since an immediate exit after the cancel function does not let
|
// 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
|
// the download process enough time to stop gratefully. A result of this is that the temporary
|
||||||
|
|
@ -333,15 +339,15 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
// this is just here to end the goroutine and prevent it from running forever without a reason
|
// this is just here to end the goroutine and prevent it from running forever without a reason
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
out.Debug("Set up signal catcher")
|
utils.Log.Debug("Set up signal catcher")
|
||||||
|
|
||||||
var additionalDownloaderOpts []string
|
var additionalDownloaderOpts []string
|
||||||
var mergeMessage string
|
var mergeMessage string
|
||||||
switch archiveMergeFlag {
|
switch archiveMergeFlag {
|
||||||
case "auto":
|
case "auto":
|
||||||
additionalDownloaderOpts = []string{"-vn"}
|
additionalDownloaderOpts = []string{"-vn"}
|
||||||
for _, format := range info.additionalFormats {
|
for _, format := range info.AdditionalFormats {
|
||||||
if format.Video.Bandwidth != info.format.Video.Bandwidth {
|
if format.Video.Bandwidth != info.Format.Video.Bandwidth {
|
||||||
// revoke the changed FFmpegOpts above
|
// revoke the changed FFmpegOpts above
|
||||||
additionalDownloaderOpts = []string{}
|
additionalDownloaderOpts = []string{}
|
||||||
break
|
break
|
||||||
|
|
@ -359,12 +365,12 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
mergeMessage = "merging video for additional formats"
|
mergeMessage = "merging video for additional formats"
|
||||||
}
|
}
|
||||||
|
|
||||||
out.Info("Downloading episode `%s` to `%s` (%s)", info.Title, filepath.Base(filename), mergeMessage)
|
utils.Log.Info("Downloading episode `%s` to `%s` (%s)", info.Title, filepath.Base(filename), mergeMessage)
|
||||||
out.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
|
utils.Log.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
|
||||||
out.Info("\tAudio: %s", info.Audio)
|
utils.Log.Info("\tAudio: %s", info.Audio)
|
||||||
out.Info("\tSubtitle: %s", info.Subtitle)
|
utils.Log.Info("\tSubtitle: %s", info.Subtitle)
|
||||||
out.Info("\tResolution: %spx", info.Resolution)
|
utils.Log.Info("\tResolution: %spx", info.Resolution)
|
||||||
out.Info("\tFPS: %.2f", info.FPS)
|
utils.Log.Info("\tFPS: %.2f", info.FPS)
|
||||||
|
|
||||||
var videoFiles, audioFiles, subtitleFiles []string
|
var videoFiles, audioFiles, subtitleFiles []string
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -374,7 +380,7 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var f []string
|
var f []string
|
||||||
if f, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.format); err != nil {
|
if f, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.Format); err != nil {
|
||||||
if err != ctx.Err() {
|
if err != ctx.Err() {
|
||||||
return fmt.Errorf("error while downloading: %v", err)
|
return fmt.Errorf("error while downloading: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -385,29 +391,29 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
if len(additionalDownloaderOpts) == 0 {
|
if len(additionalDownloaderOpts) == 0 {
|
||||||
var videos []string
|
var videos []string
|
||||||
downloader.FFmpegOpts = additionalDownloaderOpts
|
downloader.FFmpegOpts = additionalDownloaderOpts
|
||||||
if videos, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.additionalFormats...); err != nil {
|
if videos, err = archiveDownloadVideos(downloader, filepath.Base(filename), true, info.AdditionalFormats...); err != nil {
|
||||||
return fmt.Errorf("error while downloading additional videos: %v", err)
|
return fmt.Errorf("error while downloading additional videos: %v", err)
|
||||||
}
|
}
|
||||||
downloader.FFmpegOpts = []string{}
|
downloader.FFmpegOpts = []string{}
|
||||||
videoFiles = append(videoFiles, videos...)
|
videoFiles = append(videoFiles, videos...)
|
||||||
} else {
|
} else {
|
||||||
var audios []string
|
var audios []string
|
||||||
if audios, err = archiveDownloadVideos(downloader, filepath.Base(filename), false, info.additionalFormats...); err != nil {
|
if audios, err = archiveDownloadVideos(downloader, filepath.Base(filename), false, info.AdditionalFormats...); err != nil {
|
||||||
return fmt.Errorf("error while downloading additional videos: %v", err)
|
return fmt.Errorf("error while downloading additional videos: %v", err)
|
||||||
}
|
}
|
||||||
audioFiles = append(audioFiles, audios...)
|
audioFiles = append(audioFiles, audios...)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(utils.SubtitlesByLocale(info.format.Subtitles))
|
sort.Sort(crunchyUtils.SubtitlesByLocale(info.Format.Subtitles))
|
||||||
|
|
||||||
sortSubtitles, _ := strconv.ParseBool(os.Getenv("SORT_SUBTITLES"))
|
sortSubtitles, _ := strconv.ParseBool(os.Getenv("SORT_SUBTITLES"))
|
||||||
if sortSubtitles && len(archiveLanguagesFlag) > 0 {
|
if sortSubtitles && len(archiveLanguagesFlag) > 0 {
|
||||||
// this sort the subtitle locales after the languages which were specified
|
// this sort the subtitle locales after the languages which were specified
|
||||||
// with the `archiveLanguagesFlag` flag
|
// with the `archiveLanguagesFlag` flag
|
||||||
for _, language := range archiveLanguagesFlag {
|
for _, language := range archiveLanguagesFlag {
|
||||||
for i, subtitle := range info.format.Subtitles {
|
for i, subtitle := range info.Format.Subtitles {
|
||||||
if subtitle.Locale == crunchyroll.LOCALE(language) {
|
if subtitle.Locale == crunchyroll.LOCALE(language) {
|
||||||
info.format.Subtitles = append([]*crunchyroll.Subtitle{subtitle}, append(info.format.Subtitles[:i], info.format.Subtitles[i+1:]...)...)
|
info.Format.Subtitles = append([]*crunchyroll.Subtitle{subtitle}, append(info.Format.Subtitles[:i], info.Format.Subtitles[i+1:]...)...)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -415,7 +421,7 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
}
|
}
|
||||||
|
|
||||||
var subtitles []string
|
var subtitles []string
|
||||||
if subtitles, err = archiveDownloadSubtitles(filepath.Base(filename), info.format.Subtitles...); err != nil {
|
if subtitles, err = archiveDownloadSubtitles(filepath.Base(filename), info.Format.Subtitles...); err != nil {
|
||||||
return fmt.Errorf("error while downloading subtitles: %v", err)
|
return fmt.Errorf("error while downloading subtitles: %v", err)
|
||||||
}
|
}
|
||||||
subtitleFiles = append(subtitleFiles, subtitles...)
|
subtitleFiles = append(subtitleFiles, subtitles...)
|
||||||
|
|
@ -427,22 +433,22 @@ func archiveInfo(info formatInformation, writeCloser io.WriteCloser, filename st
|
||||||
dp.UpdateMessage("Download finished", false)
|
dp.UpdateMessage("Download finished", false)
|
||||||
|
|
||||||
signal.Stop(sig)
|
signal.Stop(sig)
|
||||||
out.Debug("Stopped signal catcher")
|
utils.Log.Debug("Stopped signal catcher")
|
||||||
|
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createArchiveProgress(info formatInformation) (*downloadProgress, error) {
|
func createArchiveProgress(info utils.FormatInformation) (*commands.DownloadProgress, error) {
|
||||||
var progressCount int
|
var progressCount int
|
||||||
if err := info.format.InitVideo(); err != nil {
|
if err := info.Format.InitVideo(); err != nil {
|
||||||
return nil, fmt.Errorf("error while initializing a video: %v", err)
|
return nil, fmt.Errorf("error while initializing a video: %v", err)
|
||||||
}
|
}
|
||||||
// + number of segments a video has +1 is for merging
|
// + number of segments a video has +1 is for merging
|
||||||
progressCount += int(info.format.Video.Chunklist.Count()) + 1
|
progressCount += int(info.Format.Video.Chunklist.Count()) + 1
|
||||||
for _, f := range info.additionalFormats {
|
for _, f := range info.AdditionalFormats {
|
||||||
if f == info.format {
|
if f == info.Format {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -453,16 +459,16 @@ func createArchiveProgress(info formatInformation) (*downloadProgress, error) {
|
||||||
progressCount += int(f.Video.Chunklist.Count()) + 1
|
progressCount += int(f.Video.Chunklist.Count()) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
dp := &downloadProgress{
|
dp := &commands.DownloadProgress{
|
||||||
Prefix: out.InfoLog.Prefix(),
|
Prefix: utils.Log.(*commands.Logger).InfoLog.Prefix(),
|
||||||
Message: "Downloading video",
|
Message: "Downloading video",
|
||||||
// number of segments a video +1 is for the success message
|
// number of segments a video +1 is for the success message
|
||||||
Total: progressCount + 1,
|
Total: progressCount + 1,
|
||||||
Dev: out.IsDev(),
|
Dev: utils.Log.IsDev(),
|
||||||
Quiet: out.IsQuiet(),
|
Quiet: utils.Log.(*commands.Logger).IsQuiet(),
|
||||||
}
|
}
|
||||||
if out.IsDev() {
|
if utils.Log.IsDev() {
|
||||||
dp.Prefix = out.DebugLog.Prefix()
|
dp.Prefix = utils.Log.(*commands.Logger).DebugLog.Prefix()
|
||||||
}
|
}
|
||||||
|
|
||||||
return dp, nil
|
return dp, nil
|
||||||
|
|
@ -495,7 +501,7 @@ func archiveDownloadVideos(downloader crunchyroll.Downloader, filename string, v
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
out.Debug("Downloaded '%s' video", format.AudioLocale)
|
utils.Log.Debug("Downloaded '%s' video", format.AudioLocale)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files, nil
|
return files, nil
|
||||||
|
|
@ -520,7 +526,7 @@ func archiveDownloadSubtitles(filename string, subtitles ...*crunchyroll.Subtitl
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
out.Debug("Downloaded '%s' subtitles", subtitle.Locale)
|
utils.Log.Debug("Downloaded '%s' subtitles", subtitle.Locale)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files, nil
|
return files, nil
|
||||||
|
|
@ -535,9 +541,9 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
maps = append(maps, "-map", strconv.Itoa(i))
|
maps = append(maps, "-map", strconv.Itoa(i))
|
||||||
locale := crunchyroll.LOCALE(re.FindStringSubmatch(video)[1])
|
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("language=%s", locale))
|
||||||
metadata = append(metadata, fmt.Sprintf("-metadata:s:v:%d", i), fmt.Sprintf("title=%s", utils.LocaleLanguage(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("language=%s", locale))
|
||||||
metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("title=%s", utils.LocaleLanguage(locale)))
|
metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, audio := range audioFiles {
|
for i, audio := range audioFiles {
|
||||||
|
|
@ -545,7 +551,7 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles)))
|
maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles)))
|
||||||
locale := crunchyroll.LOCALE(re.FindStringSubmatch(audio)[1])
|
locale := crunchyroll.LOCALE(re.FindStringSubmatch(audio)[1])
|
||||||
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("language=%s", locale))
|
||||||
metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("title=%s", utils.LocaleLanguage(locale)))
|
metadata = append(metadata, fmt.Sprintf("-metadata:s:a:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, subtitle := range subtitleFiles {
|
for i, subtitle := range subtitleFiles {
|
||||||
|
|
@ -553,7 +559,7 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles)+len(audioFiles)))
|
maps = append(maps, "-map", strconv.Itoa(i+len(videoFiles)+len(audioFiles)))
|
||||||
locale := crunchyroll.LOCALE(re.FindStringSubmatch(subtitle)[1])
|
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("language=%s", locale))
|
||||||
metadata = append(metadata, fmt.Sprintf("-metadata:s:s:%d", i), fmt.Sprintf("title=%s", utils.LocaleLanguage(locale)))
|
metadata = append(metadata, fmt.Sprintf("-metadata:s:s:%d", i), fmt.Sprintf("title=%s", crunchyUtils.LocaleLanguage(locale)))
|
||||||
}
|
}
|
||||||
|
|
||||||
commandOptions := []string{"-y"}
|
commandOptions := []string{"-y"}
|
||||||
|
|
@ -575,7 +581,7 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
commandOptions = append(commandOptions, "-disposition:s:0", "0", "-c", "copy", "-f", "matroska", 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
|
// just a little nicer debug output to copy and paste the ffmpeg for debug reasons
|
||||||
if out.IsDev() {
|
if utils.Log.IsDev() {
|
||||||
var debugOptions []string
|
var debugOptions []string
|
||||||
|
|
||||||
for _, option := range commandOptions {
|
for _, option := range commandOptions {
|
||||||
|
|
@ -589,7 +595,7 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
debugOptions = append(debugOptions, option)
|
debugOptions = append(debugOptions, option)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.Debug("FFmpeg merge command: ffmpeg %s", strings.Join(debugOptions, " "))
|
utils.Log.Debug("FFmpeg merge command: ffmpeg %s", strings.Join(debugOptions, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
var errBuf bytes.Buffer
|
var errBuf bytes.Buffer
|
||||||
|
|
@ -609,7 +615,7 @@ func archiveFFmpeg(ctx context.Context, dst io.Writer, videoFiles, audioFiles, s
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
func archiveExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
|
||||||
var hasJapanese bool
|
var hasJapanese bool
|
||||||
languagesAsLocale := []crunchyroll.LOCALE{crunchyroll.JP}
|
languagesAsLocale := []crunchyroll.LOCALE{crunchyroll.JP}
|
||||||
for _, language := range archiveLanguagesFlag {
|
for _, language := range archiveLanguagesFlag {
|
||||||
|
|
@ -621,7 +627,13 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
episodes, err := extractEpisodes(url, languagesAsLocale...)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -632,9 +644,9 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
|
|
||||||
for i, eps := range episodes {
|
for i, eps := range episodes {
|
||||||
if len(eps) == 0 {
|
if len(eps) == 0 {
|
||||||
out.SetProgress("%s has no matching episodes", languagesAsLocale[i])
|
utils.Log.SetProcess("%s has no matching episodes", languagesAsLocale[i])
|
||||||
} else if len(episodes[0]) > len(eps) {
|
} else if len(episodes[0]) > len(eps) {
|
||||||
out.SetProgress("%s has %d less episodes than existing in japanese (%d)", languagesAsLocale[i], len(episodes[0])-len(eps), len(episodes[0]))
|
utils.Log.SetProcess("%s has %d less episodes than existing in japanese (%d)", languagesAsLocale[i], len(episodes[0])-len(eps), len(episodes[0]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -642,11 +654,11 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
episodes = episodes[1:]
|
episodes = episodes[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
eps := make(map[int]map[int]*formatInformation)
|
eps := make(map[int]map[int]*utils.FormatInformation)
|
||||||
for _, lang := range episodes {
|
for _, lang := range episodes {
|
||||||
for _, season := range utils.SortEpisodesBySeason(lang) {
|
for _, season := range crunchyUtils.SortEpisodesBySeason(lang) {
|
||||||
if _, ok := eps[season[0].SeasonNumber]; !ok {
|
if _, ok := eps[season[0].SeasonNumber]; !ok {
|
||||||
eps[season[0].SeasonNumber] = map[int]*formatInformation{}
|
eps[season[0].SeasonNumber] = map[int]*utils.FormatInformation{}
|
||||||
}
|
}
|
||||||
for _, episode := range season {
|
for _, episode := range season {
|
||||||
format, err := episode.GetFormat(archiveResolutionFlag, "", false)
|
format, err := episode.GetFormat(archiveResolutionFlag, "", false)
|
||||||
|
|
@ -655,9 +667,9 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := eps[episode.SeasonNumber][episode.EpisodeNumber]; !ok {
|
if _, ok := eps[episode.SeasonNumber][episode.EpisodeNumber]; !ok {
|
||||||
eps[episode.SeasonNumber][episode.EpisodeNumber] = &formatInformation{
|
eps[episode.SeasonNumber][episode.EpisodeNumber] = &utils.FormatInformation{
|
||||||
format: format,
|
Format: format,
|
||||||
additionalFormats: make([]*crunchyroll.Format, 0),
|
AdditionalFormats: make([]*crunchyroll.Format, 0),
|
||||||
|
|
||||||
Title: episode.Title,
|
Title: episode.Title,
|
||||||
SeriesName: episode.SeriesTitle,
|
SeriesName: episode.SeriesTitle,
|
||||||
|
|
@ -669,15 +681,15 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
Audio: format.AudioLocale,
|
Audio: format.AudioLocale,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eps[episode.SeasonNumber][episode.EpisodeNumber].additionalFormats = append(eps[episode.SeasonNumber][episode.EpisodeNumber].additionalFormats, format)
|
eps[episode.SeasonNumber][episode.EpisodeNumber].AdditionalFormats = append(eps[episode.SeasonNumber][episode.EpisodeNumber].AdditionalFormats, format)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var infoFormat [][]formatInformation
|
var infoFormat [][]utils.FormatInformation
|
||||||
for _, e := range eps {
|
for _, e := range eps {
|
||||||
var tmpFormatInfo []formatInformation
|
var tmpFormatInfo []utils.FormatInformation
|
||||||
|
|
||||||
var keys []int
|
var keys []int
|
||||||
for episodeNumber := range e {
|
for episodeNumber := range e {
|
||||||
|
|
@ -694,124 +706,3 @@ func archiveExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
|
|
||||||
return infoFormat, nil
|
return infoFormat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type compress interface {
|
|
||||||
io.Closer
|
|
||||||
|
|
||||||
NewFile(information 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 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 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
|
|
||||||
}
|
|
||||||
136
cli/commands/archive/compress.go
Normal file
136
cli/commands/archive/compress.go
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
package cmd
|
package download
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
"github.com/crunchy-labs/crunchy-cli/cli/commands"
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2/utils"
|
"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/grafov/m3u8"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"math"
|
"math"
|
||||||
|
|
@ -23,35 +25,36 @@ var (
|
||||||
|
|
||||||
downloadDirectoryFlag string
|
downloadDirectoryFlag string
|
||||||
downloadOutputFlag string
|
downloadOutputFlag string
|
||||||
|
downloadTempDirFlag string
|
||||||
|
|
||||||
downloadResolutionFlag string
|
downloadResolutionFlag string
|
||||||
|
|
||||||
downloadGoroutinesFlag int
|
downloadGoroutinesFlag int
|
||||||
)
|
)
|
||||||
|
|
||||||
var downloadCmd = &cobra.Command{
|
var Cmd = &cobra.Command{
|
||||||
Use: "download",
|
Use: "download",
|
||||||
Short: "Download a video",
|
Short: "Download a video",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
|
||||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
out.Debug("Validating arguments")
|
utils.Log.Debug("Validating arguments")
|
||||||
|
|
||||||
if filepath.Ext(downloadOutputFlag) != ".ts" {
|
if filepath.Ext(downloadOutputFlag) != ".ts" {
|
||||||
if !hasFFmpeg() {
|
if !utils.HasFFmpeg() {
|
||||||
return fmt.Errorf("the file ending for the output file (%s) is not `.ts`. "+
|
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)
|
"Install ffmpeg (https://ffmpeg.org/download.html) to use other media file endings (e.g. `.mp4`)", downloadOutputFlag)
|
||||||
} else {
|
} else {
|
||||||
out.Debug("Custom file ending '%s' (ffmpeg is installed)", filepath.Ext(downloadOutputFlag))
|
utils.Log.Debug("Custom file ending '%s' (ffmpeg is installed)", filepath.Ext(downloadOutputFlag))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !utils.ValidateLocale(crunchyroll.LOCALE(downloadAudioFlag)) {
|
if downloadAudioFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadAudioFlag)) {
|
||||||
return fmt.Errorf("%s is not a valid audio locale. Choose from: %s", downloadAudioFlag, strings.Join(allLocalesAsStrings(), ", "))
|
return fmt.Errorf("%s is not a valid audio locale. Choose from: %s", downloadAudioFlag, strings.Join(utils.LocalesAsStrings(), ", "))
|
||||||
} else if downloadSubtitleFlag != "" && !utils.ValidateLocale(crunchyroll.LOCALE(downloadSubtitleFlag)) {
|
} else if downloadSubtitleFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadSubtitleFlag)) {
|
||||||
return fmt.Errorf("%s is not a valid subtitle locale. Choose from: %s", downloadSubtitleFlag, strings.Join(allLocalesAsStrings(), ", "))
|
return fmt.Errorf("%s is not a valid subtitle locale. Choose from: %s", downloadSubtitleFlag, strings.Join(utils.LocalesAsStrings(), ", "))
|
||||||
}
|
}
|
||||||
out.Debug("Locales: audio: %s / subtitle: %s", downloadAudioFlag, downloadSubtitleFlag)
|
utils.Log.Debug("Locales: audio: %s / subtitle: %s", downloadAudioFlag, downloadSubtitleFlag)
|
||||||
|
|
||||||
switch downloadResolutionFlag {
|
switch downloadResolutionFlag {
|
||||||
case "1080p", "720p", "480p", "360p":
|
case "1080p", "720p", "480p", "360p":
|
||||||
|
|
@ -64,35 +67,37 @@ var downloadCmd = &cobra.Command{
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("'%s' is not a valid resolution", downloadResolutionFlag)
|
return fmt.Errorf("'%s' is not a valid resolution", downloadResolutionFlag)
|
||||||
}
|
}
|
||||||
out.Debug("Using resolution '%s'", downloadResolutionFlag)
|
utils.Log.Debug("Using resolution '%s'", downloadResolutionFlag)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
loadCrunchy()
|
if err := commands.LoadCrunchy(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return download(args)
|
return download(args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
downloadCmd.Flags().StringVarP(&downloadAudioFlag, "audio",
|
Cmd.Flags().StringVarP(&downloadAudioFlag, "audio",
|
||||||
"a",
|
"a",
|
||||||
string(systemLocale(false)),
|
"",
|
||||||
"The locale of the audio. Available locales: "+strings.Join(allLocalesAsStrings(), ", "))
|
"The locale of the audio. Available locales: "+strings.Join(utils.LocalesAsStrings(), ", "))
|
||||||
downloadCmd.Flags().StringVarP(&downloadSubtitleFlag,
|
Cmd.Flags().StringVarP(&downloadSubtitleFlag,
|
||||||
"subtitle",
|
"subtitle",
|
||||||
"s",
|
"s",
|
||||||
"",
|
"",
|
||||||
"The locale of the subtitle. Available locales: "+strings.Join(allLocalesAsStrings(), ", "))
|
"The locale of the subtitle. Available locales: "+strings.Join(utils.LocalesAsStrings(), ", "))
|
||||||
|
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
downloadCmd.Flags().StringVarP(&downloadDirectoryFlag,
|
Cmd.Flags().StringVarP(&downloadDirectoryFlag,
|
||||||
"directory",
|
"directory",
|
||||||
"d",
|
"d",
|
||||||
cwd,
|
cwd,
|
||||||
"The directory to download the file(s) into")
|
"The directory to download the file(s) into")
|
||||||
downloadCmd.Flags().StringVarP(&downloadOutputFlag,
|
Cmd.Flags().StringVarP(&downloadOutputFlag,
|
||||||
"output",
|
"output",
|
||||||
"o",
|
"o",
|
||||||
"{title}.ts",
|
"{title}.ts",
|
||||||
|
|
@ -107,8 +112,12 @@ func init() {
|
||||||
"\t{fps} » Frame Rate of the video\n"+
|
"\t{fps} » Frame Rate of the video\n"+
|
||||||
"\t{audio} » Audio locale of the video\n"+
|
"\t{audio} » Audio locale of the video\n"+
|
||||||
"\t{subtitle} » Subtitle locale of the video")
|
"\t{subtitle} » Subtitle locale of the video")
|
||||||
|
Cmd.Flags().StringVar(&downloadTempDirFlag,
|
||||||
|
"temp",
|
||||||
|
os.TempDir(),
|
||||||
|
"Directory to store temporary files in")
|
||||||
|
|
||||||
downloadCmd.Flags().StringVarP(&downloadResolutionFlag,
|
Cmd.Flags().StringVarP(&downloadResolutionFlag,
|
||||||
"resolution",
|
"resolution",
|
||||||
"r",
|
"r",
|
||||||
"best",
|
"best",
|
||||||
|
|
@ -117,32 +126,32 @@ func init() {
|
||||||
"\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
|
"\tAvailable abbreviations: 1080p, 720p, 480p, 360p, 240p\n"+
|
||||||
"\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
|
"\tAvailable common-use words: best (best available resolution), worst (worst available resolution)")
|
||||||
|
|
||||||
downloadCmd.Flags().IntVarP(&downloadGoroutinesFlag,
|
Cmd.Flags().IntVarP(&downloadGoroutinesFlag,
|
||||||
"goroutines",
|
"goroutines",
|
||||||
"g",
|
"g",
|
||||||
runtime.NumCPU(),
|
runtime.NumCPU(),
|
||||||
"Sets how many parallel segment downloads should be used")
|
"Sets how many parallel segment downloads should be used")
|
||||||
|
|
||||||
rootCmd.AddCommand(downloadCmd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func download(urls []string) error {
|
func download(urls []string) error {
|
||||||
for i, url := range urls {
|
for i, url := range urls {
|
||||||
out.SetProgress("Parsing url %d", i+1)
|
utils.Log.SetProcess("Parsing url %d", i+1)
|
||||||
episodes, err := downloadExtractEpisodes(url)
|
episodes, err := downloadExtractEpisodes(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out.StopProgress("Failed to parse url %d", i+1)
|
utils.Log.StopProcess("Failed to parse url %d", i+1)
|
||||||
out.Debug("If the error says no episodes could be found but the passed url is correct and a crunchyroll classic url, " +
|
if utils.Crunchy.Config.Premium {
|
||||||
"try the corresponding crunchyroll beta url instead and try again. See https://github.com/crunchy-labs/crunchyroll-go/issues/22 for more information")
|
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
|
return err
|
||||||
}
|
}
|
||||||
out.StopProgress("Parsed url %d", i+1)
|
utils.Log.StopProcess("Parsed url %d", i+1)
|
||||||
|
|
||||||
for _, season := range episodes {
|
for _, season := range episodes {
|
||||||
out.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
|
utils.Log.Info("%s Season %d", season[0].SeriesName, season[0].SeasonNumber)
|
||||||
|
|
||||||
for j, info := range season {
|
for j, info := range season {
|
||||||
out.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
|
utils.Log.Info("\t%d. %s » %spx, %.2f FPS (S%02dE%02d)",
|
||||||
j+1,
|
j+1,
|
||||||
info.Title,
|
info.Title,
|
||||||
info.Resolution,
|
info.Resolution,
|
||||||
|
|
@ -151,17 +160,17 @@ func download(urls []string) error {
|
||||||
info.EpisodeNumber)
|
info.EpisodeNumber)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
|
|
||||||
for j, season := range episodes {
|
for j, season := range episodes {
|
||||||
for k, info := range season {
|
for k, info := range season {
|
||||||
dir := info.Format(downloadDirectoryFlag)
|
dir := info.FormatString(downloadDirectoryFlag)
|
||||||
if _, err = os.Stat(dir); os.IsNotExist(err) {
|
if _, err = os.Stat(dir); os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(dir, 0777); err != nil {
|
if err = os.MkdirAll(dir, 0777); err != nil {
|
||||||
return fmt.Errorf("error while creating directory: %v", err)
|
return fmt.Errorf("error while creating directory: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file, err := os.Create(generateFilename(info.Format(downloadOutputFlag), dir))
|
file, err := os.Create(utils.GenerateFilename(info.FormatString(downloadOutputFlag), dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create output file: %v", err)
|
return fmt.Errorf("failed to create output file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -174,7 +183,7 @@ func download(urls []string) error {
|
||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
|
if i != len(urls)-1 || j != len(episodes)-1 || k != len(season)-1 {
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,23 +191,23 @@ func download(urls []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadInfo(info formatInformation, file *os.File) error {
|
func downloadInfo(info utils.FormatInformation, file *os.File) error {
|
||||||
out.Debug("Entering season %d, episode %d", info.SeasonNumber, info.EpisodeNumber)
|
utils.Log.Debug("Entering season %d, episode %d", info.SeasonNumber, info.EpisodeNumber)
|
||||||
|
|
||||||
if err := info.format.InitVideo(); err != nil {
|
if err := info.Format.InitVideo(); err != nil {
|
||||||
return fmt.Errorf("error while initializing the video: %v", err)
|
return fmt.Errorf("error while initializing the video: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dp := &downloadProgress{
|
dp := &commands.DownloadProgress{
|
||||||
Prefix: out.InfoLog.Prefix(),
|
Prefix: utils.Log.(*commands.Logger).InfoLog.Prefix(),
|
||||||
Message: "Downloading video",
|
Message: "Downloading video",
|
||||||
// number of segments a video has +2 is for merging and the success message
|
// number of segments a video has +2 is for merging and the success message
|
||||||
Total: int(info.format.Video.Chunklist.Count()) + 2,
|
Total: int(info.Format.Video.Chunklist.Count()) + 2,
|
||||||
Dev: out.IsDev(),
|
Dev: utils.Log.IsDev(),
|
||||||
Quiet: out.IsQuiet(),
|
Quiet: utils.Log.(*commands.Logger).IsQuiet(),
|
||||||
}
|
}
|
||||||
if out.IsDev() {
|
if utils.Log.IsDev() {
|
||||||
dp.Prefix = out.DebugLog.Prefix()
|
dp.Prefix = utils.Log.(*commands.Logger).DebugLog.Prefix()
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if dp.Total != dp.Current {
|
if dp.Total != dp.Current {
|
||||||
|
|
@ -215,7 +224,7 @@ func downloadInfo(info formatInformation, file *os.File) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if out.IsDev() {
|
if utils.Log.IsDev() {
|
||||||
dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
|
dp.UpdateMessage(fmt.Sprintf("Downloading %d/%d (%.2f%%) » %s", current, total, float32(current)/float32(total)*100, segment.URI), false)
|
||||||
} else {
|
} else {
|
||||||
dp.Update()
|
dp.Update()
|
||||||
|
|
@ -226,7 +235,9 @@ func downloadInfo(info formatInformation, file *os.File) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if hasFFmpeg() {
|
tmp, _ := os.MkdirTemp(downloadTempDirFlag, "crunchy_")
|
||||||
|
downloader.TempDir = tmp
|
||||||
|
if utils.HasFFmpeg() {
|
||||||
downloader.FFmpegOpts = make([]string, 0)
|
downloader.FFmpegOpts = make([]string, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,8 +247,8 @@ func downloadInfo(info formatInformation, file *os.File) error {
|
||||||
select {
|
select {
|
||||||
case <-sig:
|
case <-sig:
|
||||||
signal.Stop(sig)
|
signal.Stop(sig)
|
||||||
out.Exit("Exiting... (may take a few seconds)")
|
utils.Log.Err("Exiting... (may take a few seconds)")
|
||||||
out.Exit("To force exit press ctrl+c (again)")
|
utils.Log.Err("To force exit press ctrl+c (again)")
|
||||||
cancel()
|
cancel()
|
||||||
// os.Exit(1) is not called because an immediate exit after the cancel function does not let
|
// 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
|
// the download process enough time to stop gratefully. A result of this is that the temporary
|
||||||
|
|
@ -246,42 +257,52 @@ func downloadInfo(info formatInformation, file *os.File) error {
|
||||||
// this is just here to end the goroutine and prevent it from running forever without a reason
|
// this is just here to end the goroutine and prevent it from running forever without a reason
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
out.Debug("Set up signal catcher")
|
utils.Log.Debug("Set up signal catcher")
|
||||||
|
|
||||||
out.Info("Downloading episode `%s` to `%s`", info.Title, filepath.Base(file.Name()))
|
utils.Log.Info("Downloading episode `%s` to `%s`", info.Title, filepath.Base(file.Name()))
|
||||||
out.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
|
utils.Log.Info("\tEpisode: S%02dE%02d", info.SeasonNumber, info.EpisodeNumber)
|
||||||
out.Info("\tAudio: %s", info.Audio)
|
utils.Log.Info("\tAudio: %s", info.Audio)
|
||||||
out.Info("\tSubtitle: %s", info.Subtitle)
|
utils.Log.Info("\tSubtitle: %s", info.Subtitle)
|
||||||
out.Info("\tResolution: %spx", info.Resolution)
|
utils.Log.Info("\tResolution: %spx", info.Resolution)
|
||||||
out.Info("\tFPS: %.2f", info.FPS)
|
utils.Log.Info("\tFPS: %.2f", info.FPS)
|
||||||
if err := info.format.Download(downloader); err != nil {
|
if err := info.Format.Download(downloader); err != nil {
|
||||||
return fmt.Errorf("error while downloading: %v", err)
|
return fmt.Errorf("error while downloading: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dp.UpdateMessage("Download finished", false)
|
dp.UpdateMessage("Download finished", false)
|
||||||
|
|
||||||
signal.Stop(sig)
|
signal.Stop(sig)
|
||||||
out.Debug("Stopped signal catcher")
|
utils.Log.Debug("Stopped signal catcher")
|
||||||
|
|
||||||
out.Empty()
|
utils.Log.Empty()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadExtractEpisodes(url string) ([][]formatInformation, error) {
|
func downloadExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
|
||||||
episodes, err := extractEpisodes(url, crunchyroll.JP, crunchyroll.LOCALE(downloadAudioFlag))
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
japanese := episodes[0]
|
japanese := episodes[0]
|
||||||
custom := episodes[1]
|
custom := episodes[1]
|
||||||
|
|
||||||
sort.Sort(utils.EpisodesByNumber(japanese))
|
sort.Sort(crunchyUtils.EpisodesByNumber(japanese))
|
||||||
sort.Sort(utils.EpisodesByNumber(custom))
|
sort.Sort(crunchyUtils.EpisodesByNumber(custom))
|
||||||
|
|
||||||
var errMessages []string
|
var errMessages []string
|
||||||
|
|
||||||
var final []*crunchyroll.Episode
|
|
||||||
if len(japanese) == 0 || len(japanese) == len(custom) {
|
if len(japanese) == 0 || len(japanese) == len(custom) {
|
||||||
final = custom
|
final = custom
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -301,25 +322,35 @@ func downloadExtractEpisodes(url string) ([][]formatInformation, error) {
|
||||||
|
|
||||||
if len(errMessages) > 10 {
|
if len(errMessages) > 10 {
|
||||||
for _, msg := range errMessages[:10] {
|
for _, msg := range errMessages[:10] {
|
||||||
out.SetProgress(msg)
|
utils.Log.SetProcess(msg)
|
||||||
}
|
}
|
||||||
out.SetProgress("... and %d more", len(errMessages)-10)
|
utils.Log.SetProcess("... and %d more", len(errMessages)-10)
|
||||||
} else {
|
} else {
|
||||||
for _, msg := range errMessages {
|
for _, msg := range errMessages {
|
||||||
out.SetProgress(msg)
|
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 [][]formatInformation
|
var infoFormat [][]utils.FormatInformation
|
||||||
for _, season := range utils.SortEpisodesBySeason(final) {
|
for _, season := range crunchyUtils.SortEpisodesBySeason(final) {
|
||||||
tmpFormatInformation := make([]formatInformation, 0)
|
tmpFormatInformation := make([]utils.FormatInformation, 0)
|
||||||
for _, episode := range season {
|
for _, episode := range season {
|
||||||
format, err := episode.GetFormat(downloadResolutionFlag, crunchyroll.LOCALE(downloadSubtitleFlag), true)
|
format, err := episode.GetFormat(downloadResolutionFlag, crunchyroll.LOCALE(downloadSubtitleFlag), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error while receiving format for %s: %v", episode.Title, err)
|
return nil, fmt.Errorf("error while receiving format for %s: %v", episode.Title, err)
|
||||||
}
|
}
|
||||||
tmpFormatInformation = append(tmpFormatInformation, formatInformation{
|
tmpFormatInformation = append(tmpFormatInformation, utils.FormatInformation{
|
||||||
format: format,
|
Format: format,
|
||||||
|
|
||||||
Title: episode.Title,
|
Title: episode.Title,
|
||||||
SeriesName: episode.SeriesTitle,
|
SeriesName: episode.SeriesTitle,
|
||||||
40
cli/commands/info/info.go
Normal file
40
cli/commands/info/info.go
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
package cmd
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchy-cli/utils"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -35,19 +36,7 @@ type progress struct {
|
||||||
stop bool
|
stop bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type logger struct {
|
func NewLogger(debug, info, err bool) *Logger {
|
||||||
DebugLog *log.Logger
|
|
||||||
InfoLog *log.Logger
|
|
||||||
ErrLog *log.Logger
|
|
||||||
|
|
||||||
devView bool
|
|
||||||
|
|
||||||
progress chan progress
|
|
||||||
done chan interface{}
|
|
||||||
lock sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func newLogger(debug, info, err bool) *logger {
|
|
||||||
initPrefixBecauseWindowsSucksBallsHard()
|
initPrefixBecauseWindowsSucksBallsHard()
|
||||||
|
|
||||||
debugLog, infoLog, errLog := log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0)
|
debugLog, infoLog, errLog := log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0), log.New(io.Discard, prefix+" ", 0)
|
||||||
|
|
@ -68,7 +57,7 @@ func newLogger(debug, info, err bool) *logger {
|
||||||
errLog = log.New(errLog.Writer(), "[err] ", 0)
|
errLog = log.New(errLog.Writer(), "[err] ", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &logger{
|
return &Logger{
|
||||||
DebugLog: debugLog,
|
DebugLog: debugLog,
|
||||||
InfoLog: infoLog,
|
InfoLog: infoLog,
|
||||||
ErrLog: errLog,
|
ErrLog: errLog,
|
||||||
|
|
@ -77,38 +66,52 @@ func newLogger(debug, info, err bool) *logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) IsDev() bool {
|
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
|
return l.devView
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) IsQuiet() bool {
|
func (l *Logger) IsQuiet() bool {
|
||||||
return l.DebugLog.Writer() == io.Discard && l.InfoLog.Writer() == io.Discard && l.ErrLog.Writer() == io.Discard
|
return l.DebugLog.Writer() == io.Discard && l.InfoLog.Writer() == io.Discard && l.ErrLog.Writer() == io.Discard
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Debug(format string, v ...interface{}) {
|
func (l *Logger) Debug(format string, v ...interface{}) {
|
||||||
l.DebugLog.Printf(format, v...)
|
l.DebugLog.Printf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Info(format string, v ...interface{}) {
|
func (l *Logger) Info(format string, v ...interface{}) {
|
||||||
l.InfoLog.Printf(format, v...)
|
l.InfoLog.Printf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Err(format string, v ...interface{}) {
|
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...)
|
l.ErrLog.Printf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Exit(format string, v ...interface{}) {
|
func (l *Logger) Empty() {
|
||||||
fmt.Fprintln(l.ErrLog.Writer(), fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logger) Empty() {
|
|
||||||
if !l.devView && l.InfoLog.Writer() != io.Discard {
|
if !l.devView && l.InfoLog.Writer() != io.Discard {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) SetProgress(format string, v ...interface{}) {
|
func (l *Logger) SetProcess(format string, v ...interface{}) {
|
||||||
if out.InfoLog.Writer() == io.Discard {
|
if l.InfoLog.Writer() == io.Discard {
|
||||||
return
|
return
|
||||||
} else if l.devView {
|
} else if l.devView {
|
||||||
l.Debug(format, v...)
|
l.Debug(format, v...)
|
||||||
|
|
@ -175,8 +178,8 @@ func (l *logger) SetProgress(format string, v ...interface{}) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) StopProgress(format string, v ...interface{}) {
|
func (l *Logger) StopProcess(format string, v ...interface{}) {
|
||||||
if out.InfoLog.Writer() == io.Discard {
|
if l.InfoLog.Writer() == io.Discard {
|
||||||
return
|
return
|
||||||
} else if l.devView {
|
} else if l.devView {
|
||||||
l.Debug(format, v...)
|
l.Debug(format, v...)
|
||||||
159
cli/commands/login/login.go
Normal file
159
cli/commands/login/login.go
Normal file
|
|
@ -0,0 +1,159 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
48
cli/commands/unix.go
Normal file
48
cli/commands/unix.go
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
//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
|
||||||
|
}
|
||||||
135
cli/commands/update/update.go
Normal file
135
cli/commands/update/update.go
Normal file
|
|
@ -0,0 +1,135 @@
|
||||||
|
package update
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchy-cli/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"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":
|
||||||
|
yayCommand := exec.Command("pacman -Q crunchy-cli")
|
||||||
|
if yayCommand.Run() == nil && yayCommand.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)
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Log.SetProcess("Updating executable %s", os.Args[0])
|
||||||
|
|
||||||
|
perms, err := os.Stat(os.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
os.Remove(os.Args[0])
|
||||||
|
executeFile, err := os.OpenFile(os.Args[0], 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", os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
125
cli/commands/utils.go
Normal file
125
cli/commands/utils.go
Normal file
|
|
@ -0,0 +1,125 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
41
cli/commands/windows.go
Normal file
41
cli/commands/windows.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
//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
|
||||||
|
}
|
||||||
85
cli/root.go
Normal file
85
cli/root.go
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,102 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
loginPersistentFlag bool
|
|
||||||
|
|
||||||
loginSessionIDFlag bool
|
|
||||||
)
|
|
||||||
|
|
||||||
var loginCmd = &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 {
|
|
||||||
return loginCredentials(args[0], args[1])
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
loginCmd.Flags().BoolVar(&loginPersistentFlag,
|
|
||||||
"persistent",
|
|
||||||
false,
|
|
||||||
"If the given credential should be stored persistent")
|
|
||||||
|
|
||||||
loginCmd.Flags().BoolVar(&loginSessionIDFlag,
|
|
||||||
"session-id",
|
|
||||||
false,
|
|
||||||
"Use a session id to login instead of username and password")
|
|
||||||
|
|
||||||
rootCmd.AddCommand(loginCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func loginCredentials(user, password string) error {
|
|
||||||
out.Debug("Logging in via credentials")
|
|
||||||
c, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(false), client)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if loginPersistentFlag {
|
|
||||||
if configDir, err := os.UserConfigDir(); err != nil {
|
|
||||||
return fmt.Errorf("could not save credentials persistent: %w", err)
|
|
||||||
} else {
|
|
||||||
os.MkdirAll(filepath.Join(configDir, "crunchyroll-go"), 0755)
|
|
||||||
if err = os.WriteFile(filepath.Join(configDir, "crunchyroll-go", "crunchy"), []byte(fmt.Sprintf("%s\n%s", user, password)), 0600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", filepath.Join(configDir, "crunchyroll-go", "crunchy"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(c.SessionID), 0600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !loginPersistentFlag {
|
|
||||||
out.Info("Due to security reasons, you have to login again on the next reboot")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func loginSessionID(sessionID string) error {
|
|
||||||
out.Debug("Logging in via session id")
|
|
||||||
if _, err := crunchyroll.LoginWithSessionID(sessionID, systemLocale(false), client); err != nil {
|
|
||||||
out.Err(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
if loginPersistentFlag {
|
|
||||||
if configDir, err := os.UserConfigDir(); err != nil {
|
|
||||||
return fmt.Errorf("could not save credentials persistent: %w", err)
|
|
||||||
} else {
|
|
||||||
os.MkdirAll(filepath.Join(configDir, "crunchyroll-go"), 0755)
|
|
||||||
if err = os.WriteFile(filepath.Join(configDir, "crunchyroll-go", "crunchy"), []byte(sessionID), 0600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", filepath.Join(configDir, "crunchyroll-go", "crunchy"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(sessionID), 0600); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !loginPersistentFlag {
|
|
||||||
out.Info("Due to security reasons, you have to login again on the next reboot")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
@ -1,78 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"runtime/debug"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var Version = "development"
|
|
||||||
|
|
||||||
var (
|
|
||||||
client *http.Client
|
|
||||||
crunchy *crunchyroll.Crunchyroll
|
|
||||||
out = newLogger(false, true, true)
|
|
||||||
|
|
||||||
quietFlag bool
|
|
||||||
verboseFlag bool
|
|
||||||
|
|
||||||
proxyFlag string
|
|
||||||
|
|
||||||
useragentFlag string
|
|
||||||
)
|
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
|
||||||
Use: "crunchyroll-go",
|
|
||||||
Version: Version,
|
|
||||||
Short: "Download crunchyroll videos with ease. See the wiki for details about the cli and library: https://github.com/crunchy-labs/crunchyroll-go/wiki",
|
|
||||||
|
|
||||||
SilenceErrors: true,
|
|
||||||
SilenceUsage: true,
|
|
||||||
|
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
||||||
if verboseFlag {
|
|
||||||
out = newLogger(true, true, true)
|
|
||||||
} else if quietFlag {
|
|
||||||
out = newLogger(false, false, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
out.Debug("Executing `%s` command with %d arg(s)", cmd.Name(), len(args))
|
|
||||||
|
|
||||||
client, err = 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("crunchyroll-go/%s", Version), "Useragent to do all request with")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Execute() {
|
|
||||||
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
if out.IsDev() {
|
|
||||||
out.Err("%v: %s", r, debug.Stack())
|
|
||||||
} else {
|
|
||||||
out.Err("Unexpected error: %v", r)
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
|
||||||
if !strings.HasSuffix(err.Error(), context.Canceled.Error()) {
|
|
||||||
out.Exit("An error occurred: %v", err)
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,445 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2/utils"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
|
||||||
"regexp"
|
|
||||||
"runtime"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ahh i love windows :)))
|
|
||||||
invalidWindowsChars = []string{"\\", "<", ">", ":", "\"", "/", "|", "?", "*"}
|
|
||||||
invalidNotWindowsChars = []string{"/"}
|
|
||||||
)
|
|
||||||
|
|
||||||
var urlFilter = regexp.MustCompile(`(S(\d+))?(E(\d+))?((-)(S(\d+))?(E(\d+))?)?(,|$)`)
|
|
||||||
|
|
||||||
// systemLocale receives the system locale
|
|
||||||
// https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590
|
|
||||||
func systemLocale(verbose bool) crunchyroll.LOCALE {
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
if lang, ok := os.LookupEnv("LANG"); ok {
|
|
||||||
var l crunchyroll.LOCALE
|
|
||||||
if preSuffix := strings.Split(strings.Split(lang, ".")[0], "_"); len(preSuffix) == 1 {
|
|
||||||
l = crunchyroll.LOCALE(preSuffix[0])
|
|
||||||
} else {
|
|
||||||
prefix := strings.Split(lang, "_")[0]
|
|
||||||
l = crunchyroll.LOCALE(fmt.Sprintf("%s-%s", prefix, preSuffix[1]))
|
|
||||||
}
|
|
||||||
if !utils.ValidateLocale(l) {
|
|
||||||
if verbose {
|
|
||||||
out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
|
|
||||||
}
|
|
||||||
l = crunchyroll.US
|
|
||||||
}
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
|
|
||||||
if output, err := cmd.Output(); err == nil {
|
|
||||||
l := crunchyroll.LOCALE(strings.Trim(string(output), "\r\n"))
|
|
||||||
if !utils.ValidateLocale(l) {
|
|
||||||
if verbose {
|
|
||||||
out.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
|
|
||||||
}
|
|
||||||
l = crunchyroll.US
|
|
||||||
}
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if verbose {
|
|
||||||
out.Err("Failed to get locale, using %s", crunchyroll.US)
|
|
||||||
}
|
|
||||||
return crunchyroll.US
|
|
||||||
}
|
|
||||||
|
|
||||||
func allLocalesAsStrings() (locales []string) {
|
|
||||||
for _, locale := range utils.AllLocales {
|
|
||||||
locales = append(locales, string(locale))
|
|
||||||
}
|
|
||||||
sort.Strings(locales)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type headerRoundTripper struct {
|
|
||||||
http.RoundTripper
|
|
||||||
header map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rht headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
||||||
resp, err := rht.RoundTripper.RoundTrip(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for k, v := range rht.header {
|
|
||||||
resp.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func createOrDefaultClient(proxy, useragent string) (*http.Client, error) {
|
|
||||||
if proxy == "" {
|
|
||||||
return http.DefaultClient, nil
|
|
||||||
} else {
|
|
||||||
out.Info("Using custom proxy %s", proxy)
|
|
||||||
proxyURL, err := url.Parse(proxy)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var rt http.RoundTripper = &http.Transport{
|
|
||||||
DisableCompression: true,
|
|
||||||
Proxy: http.ProxyURL(proxyURL),
|
|
||||||
}
|
|
||||||
if useragent != "" {
|
|
||||||
rt = headerRoundTripper{
|
|
||||||
RoundTripper: rt,
|
|
||||||
header: map[string]string{"User-Agent": useragent},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{
|
|
||||||
Transport: rt,
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
}
|
|
||||||
return client, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func freeFileName(filename string) (string, bool) {
|
|
||||||
ext := filepath.Ext(filename)
|
|
||||||
base := strings.TrimSuffix(filename, ext)
|
|
||||||
// checks if a .tar stands before the "actual" file ending
|
|
||||||
if extraExt := filepath.Ext(base); extraExt == ".tar" {
|
|
||||||
ext = extraExt + ext
|
|
||||||
base = strings.TrimSuffix(base, extraExt)
|
|
||||||
}
|
|
||||||
j := 0
|
|
||||||
for ; ; j++ {
|
|
||||||
if _, stat := os.Stat(filename); stat != nil && !os.IsExist(stat) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
filename = fmt.Sprintf("%s (%d)%s", base, j+1, ext)
|
|
||||||
}
|
|
||||||
return filename, j != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadCrunchy() {
|
|
||||||
out.SetProgress("Logging in")
|
|
||||||
|
|
||||||
tmpFilePath := filepath.Join(os.TempDir(), ".crunchy")
|
|
||||||
if _, statErr := os.Stat(tmpFilePath); !os.IsNotExist(statErr) {
|
|
||||||
body, err := os.ReadFile(tmpFilePath)
|
|
||||||
if err != nil {
|
|
||||||
out.StopProgress("Failed to read login information: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if crunchy, err = crunchyroll.LoginWithSessionID(url.QueryEscape(string(body)), systemLocale(true), client); err != nil {
|
|
||||||
out.Debug("Failed to login with temp session id: %w", err)
|
|
||||||
} else {
|
|
||||||
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", body)
|
|
||||||
|
|
||||||
out.StopProgress("Logged in")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if configDir, err := os.UserConfigDir(); err == nil {
|
|
||||||
persistentFilePath := filepath.Join(configDir, "crunchyroll-go", "crunchy")
|
|
||||||
if _, statErr := os.Stat(persistentFilePath); statErr == nil {
|
|
||||||
body, err := os.ReadFile(persistentFilePath)
|
|
||||||
if err != nil {
|
|
||||||
out.StopProgress("Failed to read login information: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
split := strings.SplitN(string(body), "\n", 2)
|
|
||||||
if len(split) == 1 || split[1] == "" {
|
|
||||||
split[0] = url.QueryEscape(split[0])
|
|
||||||
if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(true), client); err != nil {
|
|
||||||
out.StopProgress(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0])
|
|
||||||
} else {
|
|
||||||
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(true), client); err != nil {
|
|
||||||
out.StopProgress(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", crunchy.SessionID)
|
|
||||||
// the session id is written to a temp file to reduce the amount of re-logging in.
|
|
||||||
// it seems like that crunchyroll has also a little cooldown if a user logs in too often in a short time
|
|
||||||
os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(crunchy.SessionID), 0600)
|
|
||||||
}
|
|
||||||
out.StopProgress("Logged in")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out.StopProgress("To use this command, login first. Type `%s login -h` to get help", os.Args[0])
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasFFmpeg() bool {
|
|
||||||
return exec.Command("ffmpeg", "-h").Run() == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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 generateFilename(name, directory string) string {
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
for _, char := range invalidNotWindowsChars {
|
|
||||||
name = strings.ReplaceAll(name, char, "")
|
|
||||||
}
|
|
||||||
out.Debug("Replaced invalid characters (not windows)")
|
|
||||||
} else {
|
|
||||||
for _, char := range invalidWindowsChars {
|
|
||||||
name = strings.ReplaceAll(name, char, "")
|
|
||||||
}
|
|
||||||
out.Debug("Replaced invalid characters (windows)")
|
|
||||||
}
|
|
||||||
|
|
||||||
filename, changed := freeFileName(filepath.Join(directory, name))
|
|
||||||
if changed {
|
|
||||||
out.Debug("File `%s` already exists, changing name to `%s`", filepath.Base(name), filepath.Base(filename))
|
|
||||||
}
|
|
||||||
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
|
|
||||||
func extractEpisodes(url string, locales ...crunchyroll.LOCALE) ([][]*crunchyroll.Episode, error) {
|
|
||||||
var matches [][]string
|
|
||||||
|
|
||||||
lastOpen := strings.LastIndex(url, "[")
|
|
||||||
if strings.HasSuffix(url, "]") && lastOpen != -1 && lastOpen < len(url) {
|
|
||||||
matches = urlFilter.FindAllStringSubmatch(url[lastOpen+1:len(url)-1], -1)
|
|
||||||
|
|
||||||
var all string
|
|
||||||
for _, match := range matches {
|
|
||||||
all += match[0]
|
|
||||||
}
|
|
||||||
if all != url[lastOpen+1:len(url)-1] {
|
|
||||||
return nil, fmt.Errorf("invalid episode filter")
|
|
||||||
}
|
|
||||||
url = url[:lastOpen]
|
|
||||||
}
|
|
||||||
|
|
||||||
final := make([][]*crunchyroll.Episode, len(locales))
|
|
||||||
episodes, err := crunchy.ExtractEpisodesFromUrl(url, locales...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get episodes: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(episodes) == 0 {
|
|
||||||
return nil, fmt.Errorf("no episodes found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches != nil {
|
|
||||||
for _, match := range matches {
|
|
||||||
fromSeason, fromEpisode, toSeason, toEpisode := -1, -1, -1, -1
|
|
||||||
if match[2] != "" {
|
|
||||||
fromSeason, _ = strconv.Atoi(match[2])
|
|
||||||
}
|
|
||||||
if match[4] != "" {
|
|
||||||
fromEpisode, _ = strconv.Atoi(match[4])
|
|
||||||
}
|
|
||||||
if match[8] != "" {
|
|
||||||
toSeason, _ = strconv.Atoi(match[8])
|
|
||||||
}
|
|
||||||
if match[10] != "" {
|
|
||||||
toEpisode, _ = strconv.Atoi(match[10])
|
|
||||||
}
|
|
||||||
|
|
||||||
if match[6] != "-" {
|
|
||||||
toSeason = fromSeason
|
|
||||||
toEpisode = fromEpisode
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpEps := make([]*crunchyroll.Episode, 0)
|
|
||||||
for _, episode := range episodes {
|
|
||||||
if fromSeason != -1 && (episode.SeasonNumber < fromSeason || (fromEpisode != -1 && episode.EpisodeNumber < fromEpisode)) {
|
|
||||||
continue
|
|
||||||
} else if fromSeason == -1 && fromEpisode != -1 && episode.EpisodeNumber < fromEpisode {
|
|
||||||
continue
|
|
||||||
} else if toSeason != -1 && (episode.SeasonNumber > toSeason || (toEpisode != -1 && episode.EpisodeNumber > toEpisode)) {
|
|
||||||
continue
|
|
||||||
} else if toSeason == -1 && toEpisode != -1 && episode.EpisodeNumber > toEpisode {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
tmpEps = append(tmpEps, episode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(tmpEps) == 0 {
|
|
||||||
return nil, fmt.Errorf("no episodes are matching the given filter")
|
|
||||||
}
|
|
||||||
|
|
||||||
episodes = tmpEps
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
localeSorted, err := utils.SortEpisodesByAudio(episodes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get audio locale: %v", err)
|
|
||||||
}
|
|
||||||
for i, locale := range locales {
|
|
||||||
final[i] = append(final[i], localeSorted[locale]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return final, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type formatInformation struct {
|
|
||||||
// the format to download
|
|
||||||
format *crunchyroll.Format
|
|
||||||
|
|
||||||
// additional formats which are only used by archive.go
|
|
||||||
additionalFormats []*crunchyroll.Format
|
|
||||||
|
|
||||||
Title string `json:"title"`
|
|
||||||
SeriesName string `json:"series_name"`
|
|
||||||
SeasonName string `json:"season_name"`
|
|
||||||
SeasonNumber int `json:"season_number"`
|
|
||||||
EpisodeNumber int `json:"episode_number"`
|
|
||||||
Resolution string `json:"resolution"`
|
|
||||||
FPS float64 `json:"fps"`
|
|
||||||
Audio crunchyroll.LOCALE `json:"audio"`
|
|
||||||
Subtitle crunchyroll.LOCALE `json:"subtitle"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fi formatInformation) Format(source string) string {
|
|
||||||
fields := reflect.TypeOf(fi)
|
|
||||||
values := reflect.ValueOf(fi)
|
|
||||||
|
|
||||||
for i := 0; i < fields.NumField(); i++ {
|
|
||||||
var valueAsString string
|
|
||||||
switch value := values.Field(i); value.Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
valueAsString = value.String()
|
|
||||||
case reflect.Int:
|
|
||||||
valueAsString = fmt.Sprintf("%02d", value.Int())
|
|
||||||
case reflect.Float64:
|
|
||||||
valueAsString = fmt.Sprintf("%.2f", value.Float())
|
|
||||||
case reflect.Bool:
|
|
||||||
valueAsString = fields.Field(i).Tag.Get("json")
|
|
||||||
if !value.Bool() {
|
|
||||||
valueAsString = "no " + valueAsString
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
for _, char := range invalidNotWindowsChars {
|
|
||||||
valueAsString = strings.ReplaceAll(valueAsString, char, "")
|
|
||||||
}
|
|
||||||
out.Debug("Replaced invalid characters (not windows)")
|
|
||||||
} else {
|
|
||||||
for _, char := range invalidWindowsChars {
|
|
||||||
valueAsString = strings.ReplaceAll(valueAsString, char, "")
|
|
||||||
}
|
|
||||||
out.Debug("Replaced invalid characters (windows)")
|
|
||||||
}
|
|
||||||
|
|
||||||
source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString)
|
|
||||||
}
|
|
||||||
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2/cmd/crunchyroll-go/cmd"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cmd.Execute()
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +1,24 @@
|
||||||
.TH crunchyroll-go 1 "21 March 2022" "crunchyroll-go" "Crunchyroll Downloader"
|
.TH crunchy-cli 1 "27 June 2022" "crunchy-cli" "Crunchyroll Cli Client"
|
||||||
|
|
||||||
.SH NAME
|
.SH NAME
|
||||||
crunchyroll-go - A cli for downloading videos and entire series from crunchyroll.
|
crunchy-cli - A cli for downloading videos and entire series from crunchyroll.
|
||||||
|
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
crunchyroll-go [\fB-h\fR] [\fB-p\fR \fIPROXY\fR] [\fB-q\fR] [\fB-v\fR]
|
crunchy-cli [\fB-h\fR] [\fB-p\fR \fIPROXY\fR] [\fB-q\fR] [\fB-v\fR]
|
||||||
.br
|
.br
|
||||||
crunchyroll-go help
|
crunchy-cli help
|
||||||
.br
|
.br
|
||||||
crunchyroll-go login [\fB--persistent\fR] [\fB--session-id\fR \fISESSION_ID\fR] [\fIusername\fR, \fIpassword\fR]
|
crunchy-cli login [\fB--persistent\fR] [\fB--session-id\fR \fISESSION_ID\fR] [\fIusername\fR, \fIpassword\fR]
|
||||||
.br
|
.br
|
||||||
crunchyroll-go download [\fB-a\fR \fIAUDIO\fR] [\fB-s\fR \fISUBTITLE\fR] [\fB-d\fR \fIDIRECTORY\fR] [\fB-o\fR \fIOUTPUT\fR] [\fB-r\fR \fIRESOLUTION\fR] [\fB-g\fR \fIGOROUTINES\fR] \fIURLs...\fR
|
crunchy-cli download [\fB-a\fR \fIAUDIO\fR] [\fB-s\fR \fISUBTITLE\fR] [\fB-d\fR \fIDIRECTORY\fR] [\fB-o\fR \fIOUTPUT\fR] [\fB-r\fR \fIRESOLUTION\fR] [\fB-g\fR \fIGOROUTINES\fR] \fIURLs...\fR
|
||||||
.br
|
.br
|
||||||
crunchyroll-go archive [\fB-l\fR \fILANGUAGE\fR] [\fB-d\fR \fIDIRECTORY\fR] [\fB-o\fR \fIOUTPUT\fR] [\fB-m\fR \fIMERGE BEHAVIOR\fR] [\fB-c\fR \fICOMPRESS\fR] [\fB-r\fR \fIRESOLUTION\fR] [\fB-g\fR \fIGOROUTINES\fR] \fIURLs...\fR
|
crunchy-cli archive [\fB-l\fR \fILANGUAGE\fR] [\fB-d\fR \fIDIRECTORY\fR] [\fB-o\fR \fIOUTPUT\fR] [\fB-m\fR \fIMERGE BEHAVIOR\fR] [\fB-c\fR \fICOMPRESS\fR] [\fB-r\fR \fIRESOLUTION\fR] [\fB-g\fR \fIGOROUTINES\fR] \fIURLs...\fR
|
||||||
|
.br
|
||||||
|
crunchy-cli update [\fB-i\fR \fIINSTALL\fR]
|
||||||
|
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.TP
|
.TP
|
||||||
With \fBcrunchyroll-go\fR you can easily download video and series from crunchyroll.
|
With \fBcrunchy-cli\fR you can easily download video and series from crunchyroll.
|
||||||
.TP
|
.TP
|
||||||
|
|
||||||
Note that you need an \fBcrunchyroll premium\fR account in order to use this tool!
|
Note that you need an \fBcrunchyroll premium\fR account in order to use this tool!
|
||||||
|
|
@ -59,7 +61,7 @@ NOTE: The credentials are stored in plain text and if you not use \fB--session-i
|
||||||
Login via a session id (which can be extracted from a crunchyroll browser cookie) instead of using username and password.
|
Login via a session id (which can be extracted from a crunchyroll browser cookie) instead of using username and password.
|
||||||
|
|
||||||
.SH DOWNLOAD COMMAND
|
.SH DOWNLOAD COMMAND
|
||||||
A command to simply download videos. The output file is stored as a \fI.ts\fR file. \fIffmpeg\fR has to be installed if you want to change the format the videos are stored in.
|
A command to simply download videos. The output file is stored as a \fI.ts\fR file. \fIffmpeg\fR has to be installed if you want to change the Format the videos are stored in.
|
||||||
.TP
|
.TP
|
||||||
|
|
||||||
\fB-a, --audio AUDIO\fR
|
\fB-a, --audio AUDIO\fR
|
||||||
|
|
@ -141,6 +143,13 @@ The video resolution. Can either be specified via the pixels (e.g. 1920x1080), t
|
||||||
\fB-g, --goroutines GOROUTINES\fR
|
\fB-g, --goroutines GOROUTINES\fR
|
||||||
Sets the number of parallel downloads for the segments the final video is made of. Default is the number of cores the computer has.
|
Sets the number of parallel downloads for the segments the final video is made of. Default is the number of cores the computer has.
|
||||||
|
|
||||||
|
.SH UPDATE COMMAND
|
||||||
|
Checks if a newer version is available.
|
||||||
|
.TP
|
||||||
|
|
||||||
|
\fB-i, --install INSTALL\fR
|
||||||
|
If given, the command tries to update the executable with the newer version (if a newer is available).
|
||||||
|
|
||||||
.SH URL OPTIONS
|
.SH URL OPTIONS
|
||||||
If you want to download only specific episode of a series, you could either pass every single episode url to the downloader (which is fine for 1 - 3 episodes) or use filtering.
|
If you want to download only specific episode of a series, you could either pass every single episode url to the downloader (which is fine for 1 - 3 episodes) or use filtering.
|
||||||
It works pretty simple, just put a specific pattern surrounded by square brackets at the end of the url from the anime you want to download. A season and / or episode as well as a range from where to where episodes should be downloaded can be specified.
|
It works pretty simple, just put a specific pattern surrounded by square brackets at the end of the url from the anime you want to download. A season and / or episode as well as a range from where to where episodes should be downloaded can be specified.
|
||||||
|
|
@ -158,50 +167,49 @@ The \fBS\fR, followed by the number indicates the season number, \fBE\fR, follow
|
||||||
.SH EXAMPLES
|
.SH EXAMPLES
|
||||||
Login via crunchyroll account email and password.
|
Login via crunchyroll account email and password.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go login user@example.com 12345678
|
$ crunchy-cli login user@example.com 12345678
|
||||||
|
|
||||||
Download a episode normally. Your system locale will be used for the video's audio.
|
Download a episode normally. Your system locale will be used for the video's audio.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go download https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
$ crunchy-cli download https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
||||||
|
|
||||||
Download a episode with 720p and name it to 'darling.mp4'. Note that you need \fBffmpeg\fR to save files which do not have '.ts' as file extension.
|
Download a episode with 720p and name it to 'darling.mp4'. Note that you need \fBffmpeg\fR to save files which do not have '.ts' as file extension.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go download -o "darling.mp4" -r 720p https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
$ crunchy-cli download -o "darling.mp4" -r 720p https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
||||||
|
|
||||||
Download a episode with japanese audio and american subtitles.
|
Download a episode with japanese audio and american subtitles.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go download -a ja-JP -s en-US https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx[E3-E5]
|
$ crunchy-cli download -a ja-JP -s en-US https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx[E3-E5]
|
||||||
|
|
||||||
Stores the episode in a .mkv file.
|
Stores the episode in a .mkv file.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go archive https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
$ crunchy-cli archive https://beta.crunchyroll.com/watch/GRDKJZ81Y/alone-and-lonesome
|
||||||
|
|
||||||
Downloads the first two episode of Darling in the FranXX and stores it compressed in a file.
|
Downloads the first two episode of Darling in the FranXX and stores it compressed in a file.
|
||||||
.br
|
.br
|
||||||
$ crunchyroll-go archive -c "ditf.tar.gz" https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx[E1-E2]
|
$ crunchy-cli archive -c "ditf.tar.gz" https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx[E1-E2]
|
||||||
|
|
||||||
.SH BUGS
|
.SH BUGS
|
||||||
If you notice any bug or want an enhancement, feel free to create a new issue or pull request in the GitHub repository.
|
If you notice any bug or want an enhancement, feel free to create a new issue or pull request in the GitHub repository.
|
||||||
|
|
||||||
.SH AUTHOR
|
.SH AUTHOR
|
||||||
Crunchy Labs
|
Crunchy Labs Maintainers
|
||||||
.br
|
.br
|
||||||
Source: https://github.com/crunchy-labs/crunchyroll-go
|
Source: https://github.com/crunchy-labs/crunchy-cli
|
||||||
|
|
||||||
.SH COPYRIGHT
|
.SH COPYRIGHT
|
||||||
Copyright (C) 2022 Crunchy Labs
|
Copyright (C) 2022 Crunchy Labs Maintainers
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
This program is free software: you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU General Public License
|
||||||
License as published by the Free Software Foundation; either
|
as published by the Free Software Foundation, either version 3
|
||||||
version 3 of the License, or (at your option) any later version.
|
of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
Lesser General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program; if not, write to the Free Software Foundation,
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
|
|
||||||
448
crunchyroll.go
448
crunchyroll.go
|
|
@ -1,448 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LOCALE represents a locale / language.
|
|
||||||
type LOCALE string
|
|
||||||
|
|
||||||
const (
|
|
||||||
JP LOCALE = "ja-JP"
|
|
||||||
US = "en-US"
|
|
||||||
LA = "es-419"
|
|
||||||
ES = "es-ES"
|
|
||||||
FR = "fr-FR"
|
|
||||||
PT = "pt-PT"
|
|
||||||
BR = "pt-BR"
|
|
||||||
IT = "it-IT"
|
|
||||||
DE = "de-DE"
|
|
||||||
RU = "ru-RU"
|
|
||||||
AR = "ar-SA"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Crunchyroll struct {
|
|
||||||
// Client is the http.Client to perform all requests over.
|
|
||||||
Client *http.Client
|
|
||||||
// Context can be used to stop requests with Client and is context.Background by default.
|
|
||||||
Context context.Context
|
|
||||||
// Locale specifies in which language all results should be returned / requested.
|
|
||||||
Locale LOCALE
|
|
||||||
// SessionID is the crunchyroll session id which was used for authentication.
|
|
||||||
SessionID string
|
|
||||||
|
|
||||||
// Config stores parameters which are needed by some api calls.
|
|
||||||
Config struct {
|
|
||||||
TokenType string
|
|
||||||
AccessToken string
|
|
||||||
|
|
||||||
CountryCode string
|
|
||||||
Premium bool
|
|
||||||
Channel string
|
|
||||||
Policy string
|
|
||||||
Signature string
|
|
||||||
KeyPairID string
|
|
||||||
AccountID string
|
|
||||||
ExternalID string
|
|
||||||
MaturityRating string
|
|
||||||
}
|
|
||||||
|
|
||||||
// If cache is true, internal caching is enabled.
|
|
||||||
cache bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoginWithCredentials logs in via crunchyroll username or email and password.
|
|
||||||
func LoginWithCredentials(user string, password string, locale LOCALE, client *http.Client) (*Crunchyroll, error) {
|
|
||||||
sessionIDEndpoint := fmt.Sprintf("https://api.crunchyroll.com/start_session.0.json?version=1.0&access_token=%s&device_type=%s&device_id=%s",
|
|
||||||
"LNDJgOit5yaRIWN", "com.crunchyroll.windows.desktop", "Az2srGnChW65fuxYz2Xxl1GcZQgtGgI")
|
|
||||||
sessResp, err := client.Get(sessionIDEndpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer sessResp.Body.Close()
|
|
||||||
|
|
||||||
if sessResp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to start session for credentials login: %s", sessResp.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
var data map[string]interface{}
|
|
||||||
body, _ := io.ReadAll(sessResp.Body)
|
|
||||||
if err = json.Unmarshal(body, &data); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse start session with credentials response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionID := data["data"].(map[string]interface{})["session_id"].(string)
|
|
||||||
|
|
||||||
loginEndpoint := "https://api.crunchyroll.com/login.0.json"
|
|
||||||
authValues := url.Values{}
|
|
||||||
authValues.Set("session_id", sessionID)
|
|
||||||
authValues.Set("account", user)
|
|
||||||
authValues.Set("password", password)
|
|
||||||
loginResp, err := client.Post(loginEndpoint, "application/x-www-form-urlencoded", bytes.NewBufferString(authValues.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer loginResp.Body.Close()
|
|
||||||
|
|
||||||
if loginResp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to auth with credentials: %s", loginResp.Status)
|
|
||||||
} else {
|
|
||||||
var loginRespBody map[string]interface{}
|
|
||||||
json.NewDecoder(loginResp.Body).Decode(&loginRespBody)
|
|
||||||
|
|
||||||
if loginRespBody["error"].(bool) {
|
|
||||||
return nil, fmt.Errorf("an unexpected login error occoured: %s", loginRespBody["message"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return LoginWithSessionID(sessionID, locale, client)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoginWithSessionID logs in via a crunchyroll session id.
|
|
||||||
// Session ids are automatically generated as a cookie when visiting https://www.crunchyroll.com.
|
|
||||||
func LoginWithSessionID(sessionID string, locale LOCALE, client *http.Client) (*Crunchyroll, error) {
|
|
||||||
crunchy := &Crunchyroll{
|
|
||||||
Client: client,
|
|
||||||
Context: context.Background(),
|
|
||||||
Locale: locale,
|
|
||||||
SessionID: sessionID,
|
|
||||||
cache: true,
|
|
||||||
}
|
|
||||||
var endpoint string
|
|
||||||
var err error
|
|
||||||
var resp *http.Response
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
|
|
||||||
// start session
|
|
||||||
endpoint = fmt.Sprintf("https://api.crunchyroll.com/start_session.0.json?session_id=%s",
|
|
||||||
sessionID)
|
|
||||||
resp, err = client.Get(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to start session: %s", resp.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse start session with session id response: %w", err)
|
|
||||||
}
|
|
||||||
if isError, ok := jsonBody["error"]; ok && isError.(bool) {
|
|
||||||
return nil, fmt.Errorf("invalid session id (%s): %s", jsonBody["message"].(string), jsonBody["code"])
|
|
||||||
}
|
|
||||||
data := jsonBody["data"].(map[string]interface{})
|
|
||||||
|
|
||||||
crunchy.Config.CountryCode = data["country_code"].(string)
|
|
||||||
|
|
||||||
var etpRt string
|
|
||||||
for _, cookie := range resp.Cookies() {
|
|
||||||
if cookie.Name == "etp_rt" {
|
|
||||||
etpRt = cookie.Value
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// token
|
|
||||||
endpoint = "https://beta-api.crunchyroll.com/auth/v1/token"
|
|
||||||
grantType := url.Values{}
|
|
||||||
grantType.Set("grant_type", "etp_rt_cookie")
|
|
||||||
|
|
||||||
authRequest, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBufferString(grantType.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
authRequest.Header.Add("Authorization", "Basic bm9haWhkZXZtXzZpeWcwYThsMHE6")
|
|
||||||
authRequest.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
authRequest.AddCookie(&http.Cookie{
|
|
||||||
Name: "session_id",
|
|
||||||
Value: sessionID,
|
|
||||||
})
|
|
||||||
authRequest.AddCookie(&http.Cookie{
|
|
||||||
Name: "etp_rt",
|
|
||||||
Value: etpRt,
|
|
||||||
})
|
|
||||||
|
|
||||||
resp, err = client.Do(authRequest)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse 'token' response: %w", err)
|
|
||||||
}
|
|
||||||
crunchy.Config.TokenType = jsonBody["token_type"].(string)
|
|
||||||
crunchy.Config.AccessToken = jsonBody["access_token"].(string)
|
|
||||||
|
|
||||||
// index
|
|
||||||
endpoint = "https://beta-api.crunchyroll.com/index/v2"
|
|
||||||
resp, err = crunchy.request(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse 'index' response: %w", err)
|
|
||||||
}
|
|
||||||
cms := jsonBody["cms"].(map[string]interface{})
|
|
||||||
|
|
||||||
if strings.Contains(cms["bucket"].(string), "crunchyroll") {
|
|
||||||
crunchy.Config.Premium = true
|
|
||||||
crunchy.Config.Channel = "crunchyroll"
|
|
||||||
} else {
|
|
||||||
crunchy.Config.Premium = false
|
|
||||||
crunchy.Config.Channel = "-"
|
|
||||||
}
|
|
||||||
crunchy.Config.Policy = cms["policy"].(string)
|
|
||||||
crunchy.Config.Signature = cms["signature"].(string)
|
|
||||||
crunchy.Config.KeyPairID = cms["key_pair_id"].(string)
|
|
||||||
|
|
||||||
// me
|
|
||||||
endpoint = "https://beta-api.crunchyroll.com/accounts/v1/me"
|
|
||||||
resp, err = crunchy.request(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse 'me' response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
crunchy.Config.AccountID = jsonBody["account_id"].(string)
|
|
||||||
crunchy.Config.ExternalID = jsonBody["external_id"].(string)
|
|
||||||
|
|
||||||
//profile
|
|
||||||
endpoint = "https://beta-api.crunchyroll.com/accounts/v1/me/profile"
|
|
||||||
resp, err = crunchy.request(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse 'profile' response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
crunchy.Config.MaturityRating = jsonBody["maturity_rating"].(string)
|
|
||||||
|
|
||||||
return crunchy, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// request is a base function which handles api requests.
|
|
||||||
func (c *Crunchyroll) request(endpoint string) (*http.Response, error) {
|
|
||||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("%s %s", c.Config.TokenType, c.Config.AccessToken))
|
|
||||||
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err == nil {
|
|
||||||
defer resp.Body.Close()
|
|
||||||
bodyAsBytes, _ := io.ReadAll(resp.Body)
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode == http.StatusUnauthorized {
|
|
||||||
return nil, fmt.Errorf("invalid access token")
|
|
||||||
} else {
|
|
||||||
var errStruct struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
json.NewDecoder(bytes.NewBuffer(bodyAsBytes)).Decode(&errStruct)
|
|
||||||
if errStruct.Message != "" {
|
|
||||||
return nil, fmt.Errorf(errStruct.Message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp.Body = io.NopCloser(bytes.NewBuffer(bodyAsBytes))
|
|
||||||
}
|
|
||||||
return resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsCaching returns if data gets cached or not.
|
|
||||||
// See SetCaching for more information.
|
|
||||||
func (c *Crunchyroll) IsCaching() bool {
|
|
||||||
return c.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetCaching enables or disables internal caching of requests made.
|
|
||||||
// Caching is enabled by default.
|
|
||||||
// If it is disabled the already cached data still gets called.
|
|
||||||
// The best way to prevent this is to create a complete new Crunchyroll struct.
|
|
||||||
func (c *Crunchyroll) SetCaching(caching bool) {
|
|
||||||
c.cache = caching
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search searches a query and returns all found series and movies within the given limit.
|
|
||||||
func (c *Crunchyroll) Search(query string, limit uint) (s []*Series, m []*Movie, err error) {
|
|
||||||
searchEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/search?q=%s&n=%d&type=&locale=%s",
|
|
||||||
query, limit, c.Locale)
|
|
||||||
resp, err := c.request(searchEndpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("failed to parse 'search' response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range jsonBody["items"].([]interface{}) {
|
|
||||||
item := item.(map[string]interface{})
|
|
||||||
if item["total"].(float64) > 0 {
|
|
||||||
switch item["type"] {
|
|
||||||
case "series":
|
|
||||||
for _, series := range item["items"].([]interface{}) {
|
|
||||||
series2 := &Series{
|
|
||||||
crunchy: c,
|
|
||||||
}
|
|
||||||
if err := decodeMapToStruct(series, series2); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if err := decodeMapToStruct(series.(map[string]interface{})["series_metadata"].(map[string]interface{}), series2); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
s = append(s, series2)
|
|
||||||
}
|
|
||||||
case "movie_listing":
|
|
||||||
for _, movie := range item["items"].([]interface{}) {
|
|
||||||
movie2 := &Movie{
|
|
||||||
crunchy: c,
|
|
||||||
}
|
|
||||||
if err := decodeMapToStruct(movie, movie2); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
m = append(m, movie2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return s, m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindVideoByName finds a Video (Season or Movie) by its name.
|
|
||||||
// Use this in combination with ParseVideoURL and hand over the corresponding results
|
|
||||||
// to this function.
|
|
||||||
//
|
|
||||||
// Deprecated: Use Search instead. The first result sometimes isn't the correct one
|
|
||||||
// so this function is inaccurate in some cases.
|
|
||||||
// See https://github.com/crunchy-labs/crunchyroll-go/issues/22 for more information.
|
|
||||||
func (c *Crunchyroll) FindVideoByName(seriesName string) (Video, error) {
|
|
||||||
s, m, err := c.Search(seriesName, 1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(s) > 0 {
|
|
||||||
return s[0], nil
|
|
||||||
} else if len(m) > 0 {
|
|
||||||
return m[0], nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("no series or movie could be found")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindEpisodeByName finds an episode by its crunchyroll series name and episode title.
|
|
||||||
// Use this in combination with ParseEpisodeURL and hand over the corresponding results
|
|
||||||
// to this function.
|
|
||||||
func (c *Crunchyroll) FindEpisodeByName(seriesName, episodeTitle string) ([]*Episode, error) {
|
|
||||||
series, _, err := c.Search(seriesName, 5)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var matchingEpisodes []*Episode
|
|
||||||
for _, s := range series {
|
|
||||||
seasons, err := s.Seasons()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, season := range seasons {
|
|
||||||
episodes, err := season.Episodes()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, episode := range episodes {
|
|
||||||
if episode.SlugTitle == episodeTitle {
|
|
||||||
matchingEpisodes = append(matchingEpisodes, episode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return matchingEpisodes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseVideoURL tries to extract the crunchyroll series / movie name out of the given url.
|
|
||||||
//
|
|
||||||
// Deprecated: Crunchyroll classic urls are sometimes not safe to use, use ParseBetaSeriesURL
|
|
||||||
// if possible since beta url are always safe to use.
|
|
||||||
// The method will stay in the library until only beta urls are supported by crunchyroll itself.
|
|
||||||
func ParseVideoURL(url string) (seriesName string, ok bool) {
|
|
||||||
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P<series>[^/]+)(/videos)?/?$`)
|
|
||||||
if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 {
|
|
||||||
groups := regexGroups(urlMatch, pattern.SubexpNames()...)
|
|
||||||
seriesName = groups["series"]
|
|
||||||
|
|
||||||
if seriesName != "" {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseEpisodeURL tries to extract the crunchyroll series name, title, episode number and web id out of the given crunchyroll url
|
|
||||||
// Note that the episode number can be misleading. For example if an episode has the episode number 23.5 (slime isekai)
|
|
||||||
// the episode number will be 235.
|
|
||||||
//
|
|
||||||
// Deprecated: Crunchyroll classic urls are sometimes not safe to use, use ParseBetaEpisodeURL
|
|
||||||
// if possible since beta url are always safe to use.
|
|
||||||
// The method will stay in the library until only beta urls are supported by crunchyroll itself.
|
|
||||||
func ParseEpisodeURL(url string) (seriesName, title string, episodeNumber int, webId int, ok bool) {
|
|
||||||
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P<series>[^/]+)/episode-(?P<number>\d+)-(?P<title>.+)-(?P<webId>\d+).*`)
|
|
||||||
if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 {
|
|
||||||
groups := regexGroups(urlMatch, pattern.SubexpNames()...)
|
|
||||||
seriesName = groups["series"]
|
|
||||||
episodeNumber, _ = strconv.Atoi(groups["number"])
|
|
||||||
title = groups["title"]
|
|
||||||
webId, _ = strconv.Atoi(groups["webId"])
|
|
||||||
|
|
||||||
if seriesName != "" && title != "" && webId != 0 {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseBetaSeriesURL tries to extract the season id of the given crunchyroll beta url, pointing to a season.
|
|
||||||
func ParseBetaSeriesURL(url string) (seasonId string, ok bool) {
|
|
||||||
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?beta\.crunchyroll\.com/(\w{2}/)?series/(?P<seasonId>\w+).*`)
|
|
||||||
if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 {
|
|
||||||
groups := regexGroups(urlMatch, pattern.SubexpNames()...)
|
|
||||||
seasonId = groups["seasonId"]
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseBetaEpisodeURL tries to extract the episode id of the given crunchyroll beta url, pointing to an episode.
|
|
||||||
func ParseBetaEpisodeURL(url string) (episodeId string, ok bool) {
|
|
||||||
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?beta\.crunchyroll\.com/(\w{2}/)?watch/(?P<episodeId>\w+).*`)
|
|
||||||
if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 {
|
|
||||||
groups := regexGroups(urlMatch, pattern.SubexpNames()...)
|
|
||||||
episodeId = groups["episodeId"]
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
395
downloader.go
395
downloader.go
|
|
@ -1,395 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"crypto/aes"
|
|
||||||
"crypto/cipher"
|
|
||||||
"fmt"
|
|
||||||
"github.com/grafov/m3u8"
|
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewDownloader creates a downloader with default settings which should
|
|
||||||
// fit the most needs.
|
|
||||||
func NewDownloader(context context.Context, writer io.Writer, goroutines int, onSegmentDownload func(segment *m3u8.MediaSegment, current, total int, file *os.File) error) Downloader {
|
|
||||||
tmp, _ := os.MkdirTemp("", "crunchy_")
|
|
||||||
|
|
||||||
return Downloader{
|
|
||||||
Writer: writer,
|
|
||||||
TempDir: tmp,
|
|
||||||
DeleteTempAfter: true,
|
|
||||||
Context: context,
|
|
||||||
Goroutines: goroutines,
|
|
||||||
OnSegmentDownload: onSegmentDownload,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Downloader is used to download Format's
|
|
||||||
type Downloader struct {
|
|
||||||
// The output is all written to Writer.
|
|
||||||
Writer io.Writer
|
|
||||||
|
|
||||||
// TempDir is the directory where the temporary segment files should be stored.
|
|
||||||
// The files will be placed directly into the root of the directory.
|
|
||||||
// If empty a random temporary directory on the system's default tempdir
|
|
||||||
// will be created.
|
|
||||||
// If the directory does not exist, it will be created.
|
|
||||||
TempDir string
|
|
||||||
// If DeleteTempAfter is true, the temp directory gets deleted afterwards.
|
|
||||||
// Note that in case of a hard signal exit (os.Interrupt, ...) the directory
|
|
||||||
// will NOT be deleted. In such situations try to catch the signal and
|
|
||||||
// cancel Context.
|
|
||||||
DeleteTempAfter bool
|
|
||||||
|
|
||||||
// Context to control the download process with.
|
|
||||||
// There is a tiny delay when canceling the context and the actual stop of the
|
|
||||||
// process. So it is not recommend stopping the program immediately after calling
|
|
||||||
// the cancel function. It's better when canceling it and then exit the program
|
|
||||||
// when Format.Download throws an error. See the signal handling section in
|
|
||||||
// cmd/crunchyroll-go/cmd/download.go for an example.
|
|
||||||
Context context.Context
|
|
||||||
|
|
||||||
// Goroutines is the number of goroutines to download segments with.
|
|
||||||
Goroutines int
|
|
||||||
|
|
||||||
// A method to call when a segment was downloaded.
|
|
||||||
// Note that the segments are downloaded asynchronously (depending on the count of
|
|
||||||
// Goroutines) and the function gets called asynchronously too, so for example it is
|
|
||||||
// first called on segment 1, then segment 254, then segment 3 and so on.
|
|
||||||
OnSegmentDownload func(segment *m3u8.MediaSegment, current, total int, file *os.File) error
|
|
||||||
// If LockOnSegmentDownload is true, only one OnSegmentDownload function can be called at
|
|
||||||
// once. Normally (because of the use of goroutines while downloading) multiple could get
|
|
||||||
// called simultaneously.
|
|
||||||
LockOnSegmentDownload bool
|
|
||||||
|
|
||||||
// If FFmpegOpts is not nil, ffmpeg will be used to merge and convert files.
|
|
||||||
// The given opts will be used as ffmpeg parameters while merging.
|
|
||||||
//
|
|
||||||
// If Writer is *os.File and -f (which sets the output format) is not specified, the output
|
|
||||||
// format will be retrieved by its file ending. If this is not the case and -f is not given,
|
|
||||||
// the output format will be mpegts / mpeg transport stream.
|
|
||||||
// Execute 'ffmpeg -muxers' to see all available output formats.
|
|
||||||
FFmpegOpts []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// download downloads the given format.
|
|
||||||
func (d Downloader) download(format *Format) error {
|
|
||||||
if err := format.InitVideo(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(d.TempDir); os.IsNotExist(err) {
|
|
||||||
if err = os.Mkdir(d.TempDir, 0700); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if d.DeleteTempAfter {
|
|
||||||
defer os.RemoveAll(d.TempDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
files, err := d.downloadSegments(format)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if d.FFmpegOpts == nil {
|
|
||||||
return d.mergeSegments(files)
|
|
||||||
} else {
|
|
||||||
return d.mergeSegmentsFFmpeg(files)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// mergeSegments reads every file in tempDir and writes their content to Downloader.Writer.
|
|
||||||
// The given output file gets created or overwritten if already existing.
|
|
||||||
func (d Downloader) mergeSegments(files []string) error {
|
|
||||||
for _, file := range files {
|
|
||||||
select {
|
|
||||||
case <-d.Context.Done():
|
|
||||||
return d.Context.Err()
|
|
||||||
default:
|
|
||||||
f, err := os.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err = io.Copy(d.Writer, f); err != nil {
|
|
||||||
f.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mergeSegmentsFFmpeg reads every file in tempDir and merges their content to the outputFile
|
|
||||||
// with ffmpeg (https://ffmpeg.org/).
|
|
||||||
// The given output file gets created or overwritten if already existing.
|
|
||||||
func (d Downloader) mergeSegmentsFFmpeg(files []string) error {
|
|
||||||
list, err := os.Create(filepath.Join(d.TempDir, "list.txt"))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range files {
|
|
||||||
if _, err = fmt.Fprintf(list, "file '%s'\n", file); err != nil {
|
|
||||||
list.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
list.Close()
|
|
||||||
|
|
||||||
// predefined options ... custom options ... predefined output filename
|
|
||||||
command := []string{
|
|
||||||
"-y",
|
|
||||||
"-f", "concat",
|
|
||||||
"-safe", "0",
|
|
||||||
"-i", list.Name(),
|
|
||||||
"-c", "copy",
|
|
||||||
}
|
|
||||||
if d.FFmpegOpts != nil {
|
|
||||||
command = append(command, d.FFmpegOpts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmpfile string
|
|
||||||
if _, ok := d.Writer.(*io.PipeWriter); !ok {
|
|
||||||
if file, ok := d.Writer.(*os.File); ok {
|
|
||||||
tmpfile = file.Name()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if filepath.Ext(tmpfile) == "" {
|
|
||||||
// checks if the -f flag is set (overwrites the output format)
|
|
||||||
var hasF bool
|
|
||||||
for _, opts := range d.FFmpegOpts {
|
|
||||||
if strings.TrimSpace(opts) == "-f" {
|
|
||||||
hasF = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !hasF {
|
|
||||||
command = append(command, "-f", "matroska")
|
|
||||||
f, err := os.CreateTemp(d.TempDir, "")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
tmpfile = f.Name()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
command = append(command, tmpfile)
|
|
||||||
|
|
||||||
var errBuf bytes.Buffer
|
|
||||||
cmd := exec.CommandContext(d.Context, "ffmpeg",
|
|
||||||
command...)
|
|
||||||
cmd.Stderr = &errBuf
|
|
||||||
|
|
||||||
if err = cmd.Run(); err != nil {
|
|
||||||
if errBuf.Len() > 0 {
|
|
||||||
return fmt.Errorf(errBuf.String())
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if f, ok := d.Writer.(*os.File); !ok || f.Name() != tmpfile {
|
|
||||||
var file *os.File
|
|
||||||
if file, err = os.Open(tmpfile); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
_, err = io.Copy(d.Writer, file)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// downloadSegments downloads every mpeg transport stream segment to a given
|
|
||||||
// directory (more information below).
|
|
||||||
// After every segment download onSegmentDownload will be called with:
|
|
||||||
// the downloaded segment, the current position, the total size of segments to download,
|
|
||||||
// the file where the segment content was written to an error (if occurred).
|
|
||||||
// The filename is always <number of downloaded segment>.ts.
|
|
||||||
//
|
|
||||||
// Short explanation:
|
|
||||||
// The actual crunchyroll video is split up in multiple segments (or video files) which
|
|
||||||
// have to be downloaded and merged after to generate a single video file.
|
|
||||||
// And this function just downloads each of this segment into the given directory.
|
|
||||||
// See https://en.wikipedia.org/wiki/MPEG_transport_stream for more information.
|
|
||||||
func (d Downloader) downloadSegments(format *Format) ([]string, error) {
|
|
||||||
if err := format.InitVideo(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var lock sync.Mutex
|
|
||||||
chunkSize := int(math.Ceil(float64(format.Video.Chunklist.Count()) / float64(d.Goroutines)))
|
|
||||||
|
|
||||||
// when a onSegmentDownload call returns an error, this context will be set cancelled and stop all goroutines
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// receives the decrypt block and iv from the first segment.
|
|
||||||
// in my tests, only the first segment has specified this data, so the decryption data from this first segments will be used in every other segment too
|
|
||||||
block, iv, err := getCrypt(format, format.Video.Chunklist.Segments[0])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var total int32
|
|
||||||
for i := 0; i < int(format.Video.Chunklist.Count()); i += chunkSize {
|
|
||||||
wg.Add(1)
|
|
||||||
end := i + chunkSize
|
|
||||||
if end > int(format.Video.Chunklist.Count()) {
|
|
||||||
end = int(format.Video.Chunklist.Count())
|
|
||||||
}
|
|
||||||
i := i
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
for j, segment := range format.Video.Chunklist.Segments[i:end] {
|
|
||||||
select {
|
|
||||||
case <-d.Context.Done():
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
var file *os.File
|
|
||||||
for k := 0; k < 3; k++ {
|
|
||||||
filename := filepath.Join(d.TempDir, fmt.Sprintf("%d.ts", i+j))
|
|
||||||
file, err = d.downloadSegment(format, segment, filename, block, iv)
|
|
||||||
if err == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if k == 2 {
|
|
||||||
file.Close()
|
|
||||||
cancel()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-d.Context.Done():
|
|
||||||
case <-ctx.Done():
|
|
||||||
file.Close()
|
|
||||||
return
|
|
||||||
case <-time.After(5 * time.Duration(k) * time.Second):
|
|
||||||
// sleep if an error occurs. very useful because sometimes the connection times out
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if d.OnSegmentDownload != nil {
|
|
||||||
if d.LockOnSegmentDownload {
|
|
||||||
lock.Lock()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = d.OnSegmentDownload(segment, int(atomic.AddInt32(&total, 1)), int(format.Video.Chunklist.Count()), file); err != nil {
|
|
||||||
if d.LockOnSegmentDownload {
|
|
||||||
lock.Unlock()
|
|
||||||
}
|
|
||||||
file.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if d.LockOnSegmentDownload {
|
|
||||||
lock.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-d.Context.Done():
|
|
||||||
return nil, d.Context.Err()
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil, err
|
|
||||||
default:
|
|
||||||
var files []string
|
|
||||||
for i := 0; i < int(total); i++ {
|
|
||||||
files = append(files, filepath.Join(d.TempDir, fmt.Sprintf("%d.ts", i)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return files, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getCrypt extracts the key and iv of a m3u8 segment and converts it into a cipher.Block and an iv byte sequence.
|
|
||||||
func getCrypt(format *Format, segment *m3u8.MediaSegment) (block cipher.Block, iv []byte, err error) {
|
|
||||||
var resp *http.Response
|
|
||||||
|
|
||||||
resp, err = format.crunchy.Client.Get(segment.Key.URI)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
key, err := io.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
block, err = aes.NewCipher(key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
iv = []byte(segment.Key.IV)
|
|
||||||
if len(iv) == 0 {
|
|
||||||
iv = key
|
|
||||||
}
|
|
||||||
|
|
||||||
return block, iv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// downloadSegment downloads a segment, decrypts it and names it after the given index.
|
|
||||||
func (d Downloader) downloadSegment(format *Format, segment *m3u8.MediaSegment, filename string, block cipher.Block, iv []byte) (*os.File, error) {
|
|
||||||
// every segment is aes-128 encrypted and has to be decrypted when downloaded
|
|
||||||
content, err := d.decryptSegment(format.crunchy.Client, segment, block, iv)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
if _, err = file.Write(content); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return file, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/tool/crypt.go#L25.
|
|
||||||
func (d Downloader) decryptSegment(client *http.Client, segment *m3u8.MediaSegment, block cipher.Block, iv []byte) ([]byte, error) {
|
|
||||||
req, err := http.NewRequestWithContext(d.Context, http.MethodGet, segment.URI, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
raw, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
blockMode := cipher.NewCBCDecrypter(block, iv[:block.BlockSize()])
|
|
||||||
decrypted := make([]byte, len(raw))
|
|
||||||
blockMode.CryptBlocks(decrypted, raw)
|
|
||||||
raw = d.pkcs5UnPadding(decrypted)
|
|
||||||
|
|
||||||
return raw, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/tool/crypt.go#L47.
|
|
||||||
func (d Downloader) pkcs5UnPadding(origData []byte) []byte {
|
|
||||||
length := len(origData)
|
|
||||||
unPadding := int(origData[length-1])
|
|
||||||
return origData[:(length - unPadding)]
|
|
||||||
}
|
|
||||||
214
episode.go
214
episode.go
|
|
@ -1,214 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Episode contains all information about an episode.
|
|
||||||
type Episode struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
children []*Stream
|
|
||||||
|
|
||||||
ID string `json:"id"`
|
|
||||||
ChannelID string `json:"channel_id"`
|
|
||||||
|
|
||||||
SeriesID string `json:"series_id"`
|
|
||||||
SeriesTitle string `json:"series_title"`
|
|
||||||
SeriesSlugTitle string `json:"series_slug_title"`
|
|
||||||
|
|
||||||
SeasonID string `json:"season_id"`
|
|
||||||
SeasonTitle string `json:"season_title"`
|
|
||||||
SeasonSlugTitle string `json:"season_slug_title"`
|
|
||||||
SeasonNumber int `json:"season_number"`
|
|
||||||
|
|
||||||
Episode string `json:"episode"`
|
|
||||||
EpisodeNumber int `json:"episode_number"`
|
|
||||||
SequenceNumber float64 `json:"sequence_number"`
|
|
||||||
ProductionEpisodeID string `json:"production_episode_id"`
|
|
||||||
|
|
||||||
Title string `json:"title"`
|
|
||||||
SlugTitle string `json:"slug_title"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
NextEpisodeID string `json:"next_episode_id"`
|
|
||||||
NextEpisodeTitle string `json:"next_episode_title"`
|
|
||||||
|
|
||||||
HDFlag bool `json:"hd_flag"`
|
|
||||||
IsMature bool `json:"is_mature"`
|
|
||||||
MatureBlocked bool `json:"mature_blocked"`
|
|
||||||
|
|
||||||
EpisodeAirDate time.Time `json:"episode_air_date"`
|
|
||||||
|
|
||||||
IsSubbed bool `json:"is_subbed"`
|
|
||||||
IsDubbed bool `json:"is_dubbed"`
|
|
||||||
IsClip bool `json:"is_clip"`
|
|
||||||
SeoTitle string `json:"seo_title"`
|
|
||||||
SeoDescription string `json:"seo_description"`
|
|
||||||
SeasonTags []string `json:"season_tags"`
|
|
||||||
|
|
||||||
AvailableOffline bool `json:"available_offline"`
|
|
||||||
Slug string `json:"slug"`
|
|
||||||
|
|
||||||
Images struct {
|
|
||||||
Thumbnail [][]struct {
|
|
||||||
Width int `json:"width"`
|
|
||||||
Height int `json:"height"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Source string `json:"source"`
|
|
||||||
} `json:"thumbnail"`
|
|
||||||
} `json:"images"`
|
|
||||||
|
|
||||||
DurationMS int `json:"duration_ms"`
|
|
||||||
IsPremiumOnly bool `json:"is_premium_only"`
|
|
||||||
ListingID string `json:"listing_id"`
|
|
||||||
|
|
||||||
SubtitleLocales []LOCALE `json:"subtitle_locales"`
|
|
||||||
Playback string `json:"playback"`
|
|
||||||
|
|
||||||
AvailabilityNotes string `json:"availability_notes"`
|
|
||||||
|
|
||||||
StreamID string
|
|
||||||
}
|
|
||||||
|
|
||||||
// EpisodeFromID returns an episode by its api id.
|
|
||||||
func EpisodeFromID(crunchy *Crunchyroll, id string) (*Episode, error) {
|
|
||||||
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/episodes/%s?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
episode := &Episode{
|
|
||||||
crunchy: crunchy,
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
if err := decodeMapToStruct(jsonBody, episode); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if episode.Playback != "" {
|
|
||||||
streamHref := jsonBody["__links__"].(map[string]interface{})["streams"].(map[string]interface{})["href"].(string)
|
|
||||||
if match := regexp.MustCompile(`(?m)^/cms/v2/\S+videos/(\w+)/streams$`).FindAllStringSubmatch(streamHref, -1); len(match) > 0 {
|
|
||||||
episode.StreamID = match[0][1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return episode, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AudioLocale returns the audio locale of the episode.
|
|
||||||
// Every episode in a season (should) have the same audio locale,
|
|
||||||
// so if you want to get the audio locale of a season, just call
|
|
||||||
// this method on the first episode of the season.
|
|
||||||
func (e *Episode) AudioLocale() (LOCALE, error) {
|
|
||||||
streams, err := e.Streams()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return streams[0].AudioLocale, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFormat returns the format which matches the given resolution and subtitle locale.
|
|
||||||
func (e *Episode) GetFormat(resolution string, subtitle LOCALE, hardsub bool) (*Format, error) {
|
|
||||||
streams, err := e.Streams()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var foundStream *Stream
|
|
||||||
for _, stream := range streams {
|
|
||||||
if hardsub && stream.HardsubLocale == subtitle || stream.HardsubLocale == "" && subtitle == "" {
|
|
||||||
foundStream = stream
|
|
||||||
break
|
|
||||||
} else if !hardsub {
|
|
||||||
for _, streamSubtitle := range stream.Subtitles {
|
|
||||||
if streamSubtitle.Locale == subtitle {
|
|
||||||
foundStream = stream
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if foundStream != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if foundStream == nil {
|
|
||||||
return nil, fmt.Errorf("no matching stream found")
|
|
||||||
}
|
|
||||||
formats, err := foundStream.Formats()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var res *Format
|
|
||||||
for _, format := range formats {
|
|
||||||
if resolution == "worst" || resolution == "best" {
|
|
||||||
if res == nil {
|
|
||||||
res = format
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
curSplitRes := strings.SplitN(format.Video.Resolution, "x", 2)
|
|
||||||
curResX, _ := strconv.Atoi(curSplitRes[0])
|
|
||||||
curResY, _ := strconv.Atoi(curSplitRes[1])
|
|
||||||
|
|
||||||
resSplitRes := strings.SplitN(res.Video.Resolution, "x", 2)
|
|
||||||
resResX, _ := strconv.Atoi(resSplitRes[0])
|
|
||||||
resResY, _ := strconv.Atoi(resSplitRes[1])
|
|
||||||
|
|
||||||
if resolution == "worst" && curResX+curResY < resResX+resResY {
|
|
||||||
res = format
|
|
||||||
} else if resolution == "best" && curResX+curResY > resResX+resResY {
|
|
||||||
res = format
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if format.Video.Resolution == resolution {
|
|
||||||
return format, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if res != nil {
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("no matching resolution found")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Streams returns all streams which are available for the episode.
|
|
||||||
func (e *Episode) Streams() ([]*Stream, error) {
|
|
||||||
if e.children != nil {
|
|
||||||
return e.children, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
streams, err := fromVideoStreams(e.crunchy, fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
e.crunchy.Config.CountryCode,
|
|
||||||
e.crunchy.Config.MaturityRating,
|
|
||||||
e.crunchy.Config.Channel,
|
|
||||||
e.StreamID,
|
|
||||||
e.crunchy.Locale,
|
|
||||||
e.crunchy.Config.Signature,
|
|
||||||
e.crunchy.Config.Policy,
|
|
||||||
e.crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.crunchy.cache {
|
|
||||||
e.children = streams
|
|
||||||
}
|
|
||||||
return streams, nil
|
|
||||||
}
|
|
||||||
52
format.go
52
format.go
|
|
@ -1,52 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/grafov/m3u8"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FormatType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
EPISODE FormatType = "episodes"
|
|
||||||
MOVIE = "movies"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Format contains detailed information about an episode video stream.
|
|
||||||
type Format struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
ID string
|
|
||||||
// FormatType represents if the format parent is an episode or a movie.
|
|
||||||
FormatType FormatType
|
|
||||||
Video *m3u8.Variant
|
|
||||||
AudioLocale LOCALE
|
|
||||||
Hardsub LOCALE
|
|
||||||
Subtitles []*Subtitle
|
|
||||||
}
|
|
||||||
|
|
||||||
// InitVideo initializes the Format.Video completely.
|
|
||||||
// The Format.Video.Chunklist pointer is, by default, nil because an additional
|
|
||||||
// request must be made to receive its content. The request is not made when
|
|
||||||
// initializing a Format struct because it would probably cause an intense overhead
|
|
||||||
// since Format.Video.Chunklist is only used sometimes.
|
|
||||||
func (f *Format) InitVideo() error {
|
|
||||||
if f.Video.Chunklist == nil {
|
|
||||||
resp, err := f.crunchy.Client.Get(f.Video.URI)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
playlist, _, err := m3u8.DecodeFrom(resp.Body, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
f.Video.Chunklist = playlist.(*m3u8.MediaPlaylist)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Download downloads the Format with the via Downloader specified options.
|
|
||||||
func (f *Format) Download(downloader Downloader) error {
|
|
||||||
return downloader.download(f)
|
|
||||||
}
|
|
||||||
3
go.mod
3
go.mod
|
|
@ -1,8 +1,9 @@
|
||||||
module github.com/crunchy-labs/crunchyroll-go/v2
|
module github.com/crunchy-labs/crunchy-cli
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/crunchy-labs/crunchyroll-go/v3 v3.0.0-20220812161741-903599bcbe60
|
||||||
github.com/grafov/m3u8 v0.11.1
|
github.com/grafov/m3u8 v0.11.1
|
||||||
github.com/spf13/cobra v1.5.0
|
github.com/spf13/cobra v1.5.0
|
||||||
)
|
)
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,4 +1,6 @@
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/crunchy-labs/crunchyroll-go/v3 v3.0.0-20220812161741-903599bcbe60 h1:cvEKs8D8816yWJDXYl8V7bYLYsAcbNbGGcUZDUofwTI=
|
||||||
|
github.com/crunchy-labs/crunchyroll-go/v3 v3.0.0-20220812161741-903599bcbe60/go.mod h1:SjTQD3IX7Z+MLsMSd2fP5ttsJ4KtpXY6r08bHLwrOLM=
|
||||||
github.com/grafov/m3u8 v0.11.1 h1:igZ7EBIB2IAsPPazKwRKdbhxcoBKO3lO1UY57PZDeNA=
|
github.com/grafov/m3u8 v0.11.1 h1:igZ7EBIB2IAsPPazKwRKdbhxcoBKO3lO1UY57PZDeNA=
|
||||||
github.com/grafov/m3u8 v0.11.1/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080=
|
github.com/grafov/m3u8 v0.11.1/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
|
|
|
||||||
9
main.go
Normal file
9
main.go
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/crunchy-labs/crunchy-cli/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cli.Execute()
|
||||||
|
}
|
||||||
102
movie_listing.go
102
movie_listing.go
|
|
@ -1,102 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MovieListing contains information about something which is called
|
|
||||||
// movie listing. I don't know what this means thb.
|
|
||||||
type MovieListing struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
ID string `json:"id"`
|
|
||||||
|
|
||||||
Title string `json:"title"`
|
|
||||||
Slug string `json:"slug"`
|
|
||||||
SlugTitle string `json:"slug_title"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
|
|
||||||
Images struct {
|
|
||||||
Thumbnail [][]struct {
|
|
||||||
Width int `json:"width"`
|
|
||||||
Height int `json:"height"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Source string `json:"source"`
|
|
||||||
} `json:"thumbnail"`
|
|
||||||
} `json:"images"`
|
|
||||||
|
|
||||||
DurationMS int `json:"duration_ms"`
|
|
||||||
IsPremiumOnly bool `json:"is_premium_only"`
|
|
||||||
ListeningID string `json:"listening_id"`
|
|
||||||
IsMature bool `json:"is_mature"`
|
|
||||||
AvailableOffline bool `json:"available_offline"`
|
|
||||||
IsSubbed bool `json:"is_subbed"`
|
|
||||||
IsDubbed bool `json:"is_dubbed"`
|
|
||||||
|
|
||||||
Playback string `json:"playback"`
|
|
||||||
AvailabilityNotes string `json:"availability_notes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// MovieListingFromID returns a movie listing by its api id.
|
|
||||||
func MovieListingFromID(crunchy *Crunchyroll, id string) (*MovieListing, error) {
|
|
||||||
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/movie_listing/%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
movieListing := &MovieListing{
|
|
||||||
crunchy: crunchy,
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
if err = decodeMapToStruct(jsonBody, movieListing); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return movieListing, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AudioLocale is same as Episode.AudioLocale.
|
|
||||||
func (ml *MovieListing) AudioLocale() (LOCALE, error) {
|
|
||||||
resp, err := ml.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
ml.crunchy.Config.CountryCode,
|
|
||||||
ml.crunchy.Config.MaturityRating,
|
|
||||||
ml.crunchy.Config.Channel,
|
|
||||||
ml.ID,
|
|
||||||
ml.crunchy.Locale,
|
|
||||||
ml.crunchy.Config.Signature,
|
|
||||||
ml.crunchy.Config.Policy,
|
|
||||||
ml.crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
return LOCALE(jsonBody["audio_locale"].(string)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Streams returns all streams which are available for the movie listing.
|
|
||||||
func (ml *MovieListing) Streams() ([]*Stream, error) {
|
|
||||||
return fromVideoStreams(ml.crunchy, fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
ml.crunchy.Config.CountryCode,
|
|
||||||
ml.crunchy.Config.MaturityRating,
|
|
||||||
ml.crunchy.Config.Channel,
|
|
||||||
ml.ID,
|
|
||||||
ml.crunchy.Locale,
|
|
||||||
ml.crunchy.Config.Signature,
|
|
||||||
ml.crunchy.Config.Policy,
|
|
||||||
ml.crunchy.Config.KeyPairID))
|
|
||||||
}
|
|
||||||
125
season.go
125
season.go
|
|
@ -1,125 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Season contains information about an anime season.
|
|
||||||
type Season struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
children []*Episode
|
|
||||||
|
|
||||||
ID string `json:"id"`
|
|
||||||
ChannelID string `json:"channel_id"`
|
|
||||||
|
|
||||||
Title string `json:"title"`
|
|
||||||
SlugTitle string `json:"slug_title"`
|
|
||||||
|
|
||||||
SeriesID string `json:"series_id"`
|
|
||||||
SeasonNumber int `json:"season_number"`
|
|
||||||
|
|
||||||
IsComplete bool `json:"is_complete"`
|
|
||||||
|
|
||||||
Description string `json:"description"`
|
|
||||||
Keywords []string `json:"keywords"`
|
|
||||||
SeasonTags []string `json:"season_tags"`
|
|
||||||
IsMature bool `json:"is_mature"`
|
|
||||||
MatureBlocked bool `json:"mature_blocked"`
|
|
||||||
IsSubbed bool `json:"is_subbed"`
|
|
||||||
IsDubbed bool `json:"is_dubbed"`
|
|
||||||
IsSimulcast bool `json:"is_simulcast"`
|
|
||||||
|
|
||||||
SeoTitle string `json:"seo_title"`
|
|
||||||
SeoDescription string `json:"seo_description"`
|
|
||||||
|
|
||||||
AvailabilityNotes string `json:"availability_notes"`
|
|
||||||
|
|
||||||
// the locales are always empty, idk why this may change in the future
|
|
||||||
AudioLocales []LOCALE
|
|
||||||
SubtitleLocales []LOCALE
|
|
||||||
}
|
|
||||||
|
|
||||||
// SeasonFromID returns a season by its api id.
|
|
||||||
func SeasonFromID(crunchy *Crunchyroll, id string) (*Season, error) {
|
|
||||||
resp, err := crunchy.Client.Get(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/seasons/%s?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
season := &Season{
|
|
||||||
crunchy: crunchy,
|
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
if err := decodeMapToStruct(jsonBody, season); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return season, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AudioLocale returns the audio locale of the season.
|
|
||||||
func (s *Season) AudioLocale() (LOCALE, error) {
|
|
||||||
episodes, err := s.Episodes()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return episodes[0].AudioLocale()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Episodes returns all episodes which are available for the season.
|
|
||||||
func (s *Season) Episodes() (episodes []*Episode, err error) {
|
|
||||||
if s.children != nil {
|
|
||||||
return s.children, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := s.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/episodes?season_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
s.crunchy.Config.CountryCode,
|
|
||||||
s.crunchy.Config.MaturityRating,
|
|
||||||
s.crunchy.Config.Channel,
|
|
||||||
s.ID,
|
|
||||||
s.crunchy.Locale,
|
|
||||||
s.crunchy.Config.Signature,
|
|
||||||
s.crunchy.Config.Policy,
|
|
||||||
s.crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
for _, item := range jsonBody["items"].([]interface{}) {
|
|
||||||
episode := &Episode{
|
|
||||||
crunchy: s.crunchy,
|
|
||||||
}
|
|
||||||
if err = decodeMapToStruct(item, episode); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if episode.Playback != "" {
|
|
||||||
streamHref := item.(map[string]interface{})["__links__"].(map[string]interface{})["streams"].(map[string]interface{})["href"].(string)
|
|
||||||
if match := regexp.MustCompile(`(?m)^/cms/v2/\S+videos/(\w+)/streams$`).FindAllStringSubmatch(streamHref, -1); len(match) > 0 {
|
|
||||||
episode.StreamID = match[0][1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
episodes = append(episodes, episode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.crunchy.cache {
|
|
||||||
s.children = episodes
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
126
stream.go
126
stream.go
|
|
@ -1,126 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"github.com/grafov/m3u8"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Stream contains information about all available video stream of an episode.
|
|
||||||
type Stream struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
children []*Format
|
|
||||||
|
|
||||||
HardsubLocale LOCALE
|
|
||||||
AudioLocale LOCALE
|
|
||||||
Subtitles []*Subtitle
|
|
||||||
|
|
||||||
formatType FormatType
|
|
||||||
id string
|
|
||||||
streamURL string
|
|
||||||
}
|
|
||||||
|
|
||||||
// StreamsFromID returns a stream by its api id.
|
|
||||||
func StreamsFromID(crunchy *Crunchyroll, id string) ([]*Stream, error) {
|
|
||||||
return fromVideoStreams(crunchy, fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/videos/%s/streams?locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Formats returns all formats which are available for the stream.
|
|
||||||
func (s *Stream) Formats() ([]*Format, error) {
|
|
||||||
if s.children != nil {
|
|
||||||
return s.children, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := s.crunchy.Client.Get(s.streamURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
playlist, _, err := m3u8.DecodeFrom(resp.Body, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var formats []*Format
|
|
||||||
for _, variant := range playlist.(*m3u8.MasterPlaylist).Variants {
|
|
||||||
formats = append(formats, &Format{
|
|
||||||
crunchy: s.crunchy,
|
|
||||||
ID: s.id,
|
|
||||||
FormatType: s.formatType,
|
|
||||||
Video: variant,
|
|
||||||
AudioLocale: s.AudioLocale,
|
|
||||||
Hardsub: s.HardsubLocale,
|
|
||||||
Subtitles: s.Subtitles,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.crunchy.cache {
|
|
||||||
s.children = formats
|
|
||||||
}
|
|
||||||
return formats, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromVideoStreams returns all streams which are accessible via the endpoint.
|
|
||||||
func fromVideoStreams(crunchy *Crunchyroll, endpoint string) (streams []*Stream, err error) {
|
|
||||||
resp, err := crunchy.request(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
if len(jsonBody) == 0 {
|
|
||||||
// this may get thrown when the crunchyroll account has just a normal account and not one with premium
|
|
||||||
return nil, fmt.Errorf("no stream available")
|
|
||||||
}
|
|
||||||
|
|
||||||
audioLocale := jsonBody["audio_locale"].(string)
|
|
||||||
|
|
||||||
var subtitles []*Subtitle
|
|
||||||
for _, rawSubtitle := range jsonBody["subtitles"].(map[string]interface{}) {
|
|
||||||
subtitle := &Subtitle{
|
|
||||||
crunchy: crunchy,
|
|
||||||
}
|
|
||||||
decodeMapToStruct(rawSubtitle.(map[string]interface{}), subtitle)
|
|
||||||
subtitles = append(subtitles, subtitle)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, streamData := range jsonBody["streams"].(map[string]interface{})["adaptive_hls"].(map[string]interface{}) {
|
|
||||||
streamData := streamData.(map[string]interface{})
|
|
||||||
|
|
||||||
hardsubLocale := streamData["hardsub_locale"].(string)
|
|
||||||
|
|
||||||
var id string
|
|
||||||
var formatType FormatType
|
|
||||||
href := jsonBody["__links__"].(map[string]interface{})["resource"].(map[string]interface{})["href"].(string)
|
|
||||||
if match := regexp.MustCompile(`(?sm)^/cms/v2/\S+/crunchyroll/(\w+)/(\w+)$`).FindAllStringSubmatch(href, -1); len(match) > 0 {
|
|
||||||
formatType = FormatType(match[0][1])
|
|
||||||
id = match[0][2]
|
|
||||||
}
|
|
||||||
|
|
||||||
stream := &Stream{
|
|
||||||
crunchy: crunchy,
|
|
||||||
HardsubLocale: LOCALE(hardsubLocale),
|
|
||||||
formatType: formatType,
|
|
||||||
id: id,
|
|
||||||
streamURL: streamData["url"].(string),
|
|
||||||
AudioLocale: LOCALE(audioLocale),
|
|
||||||
Subtitles: subtitles,
|
|
||||||
}
|
|
||||||
|
|
||||||
streams = append(streams, stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
32
subtitle.go
32
subtitle.go
|
|
@ -1,32 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Subtitle contains the information about a video subtitle.
|
|
||||||
type Subtitle struct {
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
Locale LOCALE `json:"locale"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
Format string `json:"format"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save writes the subtitle to the given io.Writer.
|
|
||||||
func (s Subtitle) Save(writer io.Writer) error {
|
|
||||||
req, err := http.NewRequestWithContext(s.crunchy.Context, http.MethodGet, s.URL, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := s.crunchy.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(writer, resp.Body)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
110
url.go
110
url.go
|
|
@ -1,110 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ExtractEpisodesFromUrl extracts all episodes from an url.
|
|
||||||
// If audio is not empty, the episodes gets filtered after the given locale.
|
|
||||||
func (c *Crunchyroll) ExtractEpisodesFromUrl(url string, audio ...LOCALE) ([]*Episode, error) {
|
|
||||||
series, episodes, err := c.ParseUrl(url)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var eps []*Episode
|
|
||||||
|
|
||||||
if series != nil {
|
|
||||||
seasons, err := series.Seasons()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, season := range seasons {
|
|
||||||
if audio != nil {
|
|
||||||
locale, err := season.AudioLocale()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var found bool
|
|
||||||
for _, l := range audio {
|
|
||||||
if locale == l {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e, err := season.Episodes()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
eps = append(eps, e...)
|
|
||||||
}
|
|
||||||
} else if episodes != nil {
|
|
||||||
if audio == nil {
|
|
||||||
return episodes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, episode := range episodes {
|
|
||||||
locale, err := episode.AudioLocale()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if audio != nil {
|
|
||||||
var found bool
|
|
||||||
for _, l := range audio {
|
|
||||||
if locale == l {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eps = append(eps, episode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(eps) == 0 {
|
|
||||||
return nil, fmt.Errorf("could not find any matching episode")
|
|
||||||
}
|
|
||||||
|
|
||||||
return eps, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseUrl parses the given url into a series or episode.
|
|
||||||
// The returning episode is a slice because non-beta urls have the same episode with different languages.
|
|
||||||
func (c *Crunchyroll) ParseUrl(url string) (*Series, []*Episode, error) {
|
|
||||||
if seriesId, ok := ParseBetaSeriesURL(url); ok {
|
|
||||||
series, err := SeriesFromID(c, seriesId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return series, nil, nil
|
|
||||||
} else if episodeId, ok := ParseBetaEpisodeURL(url); ok {
|
|
||||||
episode, err := EpisodeFromID(c, episodeId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return nil, []*Episode{episode}, nil
|
|
||||||
} else if seriesName, ok := ParseVideoURL(url); ok {
|
|
||||||
video, err := c.FindVideoByName(seriesName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return video.(*Series), nil, nil
|
|
||||||
} else if seriesName, title, _, _, ok := ParseEpisodeURL(url); ok {
|
|
||||||
episodes, err := c.FindEpisodeByName(seriesName, title)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return nil, episodes, nil
|
|
||||||
} else {
|
|
||||||
return nil, nil, fmt.Errorf("invalid url %s", url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
25
utils.go
25
utils.go
|
|
@ -1,25 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
func decodeMapToStruct(m interface{}, s interface{}) error {
|
|
||||||
jsonBody, err := json.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return json.Unmarshal(jsonBody, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func regexGroups(parsed [][]string, subexpNames ...string) map[string]string {
|
|
||||||
groups := map[string]string{}
|
|
||||||
for _, match := range parsed {
|
|
||||||
for i, content := range match {
|
|
||||||
if subexpName := subexpNames[i]; subexpName != "" {
|
|
||||||
groups[subexpName] = content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return groups
|
|
||||||
}
|
|
||||||
99
utils/extract.go
Normal file
99
utils/extract.go
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3/utils"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var urlFilter = regexp.MustCompile(`(S(\d+))?(E(\d+))?((-)(S(\d+))?(E(\d+))?)?(,|$)`)
|
||||||
|
|
||||||
|
func ExtractEpisodes(url string, locales ...crunchyroll.LOCALE) ([][]*crunchyroll.Episode, error) {
|
||||||
|
var matches [][]string
|
||||||
|
|
||||||
|
lastOpen := strings.LastIndex(url, "[")
|
||||||
|
if strings.HasSuffix(url, "]") && lastOpen != -1 && lastOpen < len(url) {
|
||||||
|
matches = urlFilter.FindAllStringSubmatch(url[lastOpen+1:len(url)-1], -1)
|
||||||
|
|
||||||
|
var all string
|
||||||
|
for _, match := range matches {
|
||||||
|
all += match[0]
|
||||||
|
}
|
||||||
|
if all != url[lastOpen+1:len(url)-1] {
|
||||||
|
return nil, fmt.Errorf("invalid episode filter")
|
||||||
|
}
|
||||||
|
url = url[:lastOpen]
|
||||||
|
}
|
||||||
|
|
||||||
|
episodes, err := Crunchy.ExtractEpisodesFromUrl(url, locales...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get episodes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(episodes) == 0 {
|
||||||
|
return nil, fmt.Errorf("no episodes found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches != nil {
|
||||||
|
for _, match := range matches {
|
||||||
|
fromSeason, fromEpisode, toSeason, toEpisode := -1, -1, -1, -1
|
||||||
|
if match[2] != "" {
|
||||||
|
fromSeason, _ = strconv.Atoi(match[2])
|
||||||
|
}
|
||||||
|
if match[4] != "" {
|
||||||
|
fromEpisode, _ = strconv.Atoi(match[4])
|
||||||
|
}
|
||||||
|
if match[8] != "" {
|
||||||
|
toSeason, _ = strconv.Atoi(match[8])
|
||||||
|
}
|
||||||
|
if match[10] != "" {
|
||||||
|
toEpisode, _ = strconv.Atoi(match[10])
|
||||||
|
}
|
||||||
|
|
||||||
|
if match[6] != "-" {
|
||||||
|
toSeason = fromSeason
|
||||||
|
toEpisode = fromEpisode
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpEps := make([]*crunchyroll.Episode, 0)
|
||||||
|
for _, episode := range episodes {
|
||||||
|
if fromSeason != -1 && (episode.SeasonNumber < fromSeason || (fromEpisode != -1 && episode.EpisodeNumber < fromEpisode)) {
|
||||||
|
continue
|
||||||
|
} else if fromSeason == -1 && fromEpisode != -1 && episode.EpisodeNumber < fromEpisode {
|
||||||
|
continue
|
||||||
|
} else if toSeason != -1 && (episode.SeasonNumber > toSeason || (toEpisode != -1 && episode.EpisodeNumber > toEpisode)) {
|
||||||
|
continue
|
||||||
|
} else if toSeason == -1 && toEpisode != -1 && episode.EpisodeNumber > toEpisode {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
tmpEps = append(tmpEps, episode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tmpEps) == 0 {
|
||||||
|
return nil, fmt.Errorf("no episodes are matching the given filter")
|
||||||
|
}
|
||||||
|
|
||||||
|
episodes = tmpEps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var final [][]*crunchyroll.Episode
|
||||||
|
if len(locales) > 0 {
|
||||||
|
final = make([][]*crunchyroll.Episode, len(locales))
|
||||||
|
localeSorted, err := utils.SortEpisodesByAudio(episodes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get audio locale: %v", err)
|
||||||
|
}
|
||||||
|
for i, locale := range locales {
|
||||||
|
final[i] = append(final[i], localeSorted[locale]...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final = [][]*crunchyroll.Episode{episodes}
|
||||||
|
}
|
||||||
|
|
||||||
|
return final, nil
|
||||||
|
}
|
||||||
49
utils/file.go
Normal file
49
utils/file.go
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FreeFileName(filename string) (string, bool) {
|
||||||
|
ext := filepath.Ext(filename)
|
||||||
|
base := strings.TrimSuffix(filename, ext)
|
||||||
|
// checks if a .tar stands before the "actual" file ending
|
||||||
|
if extraExt := filepath.Ext(base); extraExt == ".tar" {
|
||||||
|
ext = extraExt + ext
|
||||||
|
base = strings.TrimSuffix(base, extraExt)
|
||||||
|
}
|
||||||
|
j := 0
|
||||||
|
for ; ; j++ {
|
||||||
|
if _, stat := os.Stat(filename); stat != nil && !os.IsExist(stat) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
filename = fmt.Sprintf("%s (%d)%s", base, j+1, ext)
|
||||||
|
}
|
||||||
|
return filename, j != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateFilename(name, directory string) string {
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
for _, char := range []string{"/"} {
|
||||||
|
name = strings.ReplaceAll(name, char, "")
|
||||||
|
}
|
||||||
|
Log.Debug("Replaced invalid characters (not windows)")
|
||||||
|
} else {
|
||||||
|
// ahh i love windows :)))
|
||||||
|
for _, char := range []string{"\\", "<", ">", ":", "\"", "/", "|", "?", "*"} {
|
||||||
|
name = strings.ReplaceAll(name, char, "")
|
||||||
|
}
|
||||||
|
Log.Debug("Replaced invalid characters (windows)")
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, changed := FreeFileName(filepath.Join(directory, name))
|
||||||
|
if changed {
|
||||||
|
Log.Debug("File `%s` already exists, changing name to `%s`", filepath.Base(name), filepath.Base(filename))
|
||||||
|
}
|
||||||
|
|
||||||
|
return filename
|
||||||
|
}
|
||||||
63
utils/format.go
Normal file
63
utils/format.go
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FormatInformation struct {
|
||||||
|
// the Format to download
|
||||||
|
Format *crunchyroll.Format
|
||||||
|
|
||||||
|
// additional formats which are only used by archive.go
|
||||||
|
AdditionalFormats []*crunchyroll.Format
|
||||||
|
|
||||||
|
Title string `json:"title"`
|
||||||
|
SeriesName string `json:"series_name"`
|
||||||
|
SeasonName string `json:"season_name"`
|
||||||
|
SeasonNumber int `json:"season_number"`
|
||||||
|
EpisodeNumber int `json:"episode_number"`
|
||||||
|
Resolution string `json:"resolution"`
|
||||||
|
FPS float64 `json:"fps"`
|
||||||
|
Audio crunchyroll.LOCALE `json:"audio"`
|
||||||
|
Subtitle crunchyroll.LOCALE `json:"subtitle"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fi FormatInformation) FormatString(source string) string {
|
||||||
|
fields := reflect.TypeOf(fi)
|
||||||
|
values := reflect.ValueOf(fi)
|
||||||
|
|
||||||
|
for i := 0; i < fields.NumField(); i++ {
|
||||||
|
var valueAsString string
|
||||||
|
switch value := values.Field(i); value.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
valueAsString = value.String()
|
||||||
|
case reflect.Int:
|
||||||
|
valueAsString = fmt.Sprintf("%02d", value.Int())
|
||||||
|
case reflect.Float64:
|
||||||
|
valueAsString = fmt.Sprintf("%.2f", value.Float())
|
||||||
|
case reflect.Bool:
|
||||||
|
valueAsString = fields.Field(i).Tag.Get("json")
|
||||||
|
if !value.Bool() {
|
||||||
|
valueAsString = "no " + valueAsString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
for _, char := range []string{"/"} {
|
||||||
|
valueAsString = strings.ReplaceAll(valueAsString, char, "")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, char := range []string{"\\", "<", ">", ":", "\"", "/", "|", "?", "*"} {
|
||||||
|
valueAsString = strings.ReplaceAll(valueAsString, char, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString)
|
||||||
|
}
|
||||||
|
|
||||||
|
return source
|
||||||
|
}
|
||||||
51
utils/http.go
Normal file
51
utils/http.go
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type headerRoundTripper struct {
|
||||||
|
http.RoundTripper
|
||||||
|
header map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rht headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
|
resp, err := rht.RoundTripper.RoundTrip(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for k, v := range rht.header {
|
||||||
|
resp.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrDefaultClient(proxy, useragent string) (*http.Client, error) {
|
||||||
|
if proxy == "" {
|
||||||
|
return http.DefaultClient, nil
|
||||||
|
} else {
|
||||||
|
proxyURL, err := url.Parse(proxy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var rt http.RoundTripper = &http.Transport{
|
||||||
|
DisableCompression: true,
|
||||||
|
Proxy: http.ProxyURL(proxyURL),
|
||||||
|
}
|
||||||
|
if useragent != "" {
|
||||||
|
rt = headerRoundTripper{
|
||||||
|
RoundTripper: rt,
|
||||||
|
header: map[string]string{"User-Agent": useragent},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Transport: rt,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,58 +1,59 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "github.com/crunchy-labs/crunchyroll-go/v2"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3/utils"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// AllLocales is an array of all available locales.
|
// SystemLocale receives the system locale
|
||||||
var AllLocales = []crunchyroll.LOCALE{
|
// https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590
|
||||||
crunchyroll.JP,
|
func SystemLocale(verbose bool) crunchyroll.LOCALE {
|
||||||
crunchyroll.US,
|
if runtime.GOOS != "windows" {
|
||||||
crunchyroll.LA,
|
if lang, ok := os.LookupEnv("LANG"); ok {
|
||||||
crunchyroll.ES,
|
var l crunchyroll.LOCALE
|
||||||
crunchyroll.FR,
|
if preSuffix := strings.Split(strings.Split(lang, ".")[0], "_"); len(preSuffix) == 1 {
|
||||||
crunchyroll.PT,
|
l = crunchyroll.LOCALE(preSuffix[0])
|
||||||
crunchyroll.BR,
|
} else {
|
||||||
crunchyroll.IT,
|
prefix := strings.Split(lang, "_")[0]
|
||||||
crunchyroll.DE,
|
l = crunchyroll.LOCALE(fmt.Sprintf("%s-%s", prefix, preSuffix[1]))
|
||||||
crunchyroll.RU,
|
}
|
||||||
crunchyroll.AR,
|
if !utils.ValidateLocale(l) {
|
||||||
}
|
if verbose {
|
||||||
|
Log.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
|
||||||
// ValidateLocale validates if the given locale actually exist.
|
}
|
||||||
func ValidateLocale(locale crunchyroll.LOCALE) bool {
|
l = crunchyroll.US
|
||||||
for _, l := range AllLocales {
|
}
|
||||||
if l == locale {
|
return l
|
||||||
return true
|
}
|
||||||
|
} else {
|
||||||
|
cmd := exec.Command("powershell", "Get-Culture | select -exp Name")
|
||||||
|
if output, err := cmd.Output(); err == nil {
|
||||||
|
l := crunchyroll.LOCALE(strings.Trim(string(output), "\r\n"))
|
||||||
|
if !utils.ValidateLocale(l) {
|
||||||
|
if verbose {
|
||||||
|
Log.Err("%s is not a supported locale, using %s as fallback", l, crunchyroll.US)
|
||||||
|
}
|
||||||
|
l = crunchyroll.US
|
||||||
|
}
|
||||||
|
return l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
if verbose {
|
||||||
|
Log.Err("Failed to get locale, using %s", crunchyroll.US)
|
||||||
|
}
|
||||||
|
return crunchyroll.US
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocaleLanguage returns the country by its locale.
|
func LocalesAsStrings() (locales []string) {
|
||||||
func LocaleLanguage(locale crunchyroll.LOCALE) string {
|
for _, locale := range utils.AllLocales {
|
||||||
switch locale {
|
locales = append(locales, string(locale))
|
||||||
case crunchyroll.JP:
|
|
||||||
return "Japanese"
|
|
||||||
case crunchyroll.US:
|
|
||||||
return "English (US)"
|
|
||||||
case crunchyroll.LA:
|
|
||||||
return "Spanish (Latin America)"
|
|
||||||
case crunchyroll.ES:
|
|
||||||
return "Spanish (Spain)"
|
|
||||||
case crunchyroll.FR:
|
|
||||||
return "French"
|
|
||||||
case crunchyroll.PT:
|
|
||||||
return "Portuguese (Europe)"
|
|
||||||
case crunchyroll.BR:
|
|
||||||
return "Portuguese (Brazil)"
|
|
||||||
case crunchyroll.IT:
|
|
||||||
return "Italian"
|
|
||||||
case crunchyroll.DE:
|
|
||||||
return "German"
|
|
||||||
case crunchyroll.RU:
|
|
||||||
return "Russian"
|
|
||||||
case crunchyroll.AR:
|
|
||||||
return "Arabic"
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
sort.Strings(locales)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
utils/logger.go
Normal file
12
utils/logger.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
IsDev() bool
|
||||||
|
Debug(format string, v ...any)
|
||||||
|
Info(format string, v ...any)
|
||||||
|
Warn(format string, v ...any)
|
||||||
|
Err(format string, v ...any)
|
||||||
|
Empty()
|
||||||
|
SetProcess(format string, v ...any)
|
||||||
|
StopProcess(format string, v ...any)
|
||||||
|
}
|
||||||
177
utils/save.go
Normal file
177
utils/save.go
Normal file
|
|
@ -0,0 +1,177 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SaveSession(crunchy *crunchyroll.Crunchyroll) error {
|
||||||
|
file := filepath.Join(os.TempDir(), ".crunchy")
|
||||||
|
return os.WriteFile(file, []byte(crunchy.RefreshToken), 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveCredentialsPersistent(user, password string, encryptionKey []byte) error {
|
||||||
|
configDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
file := filepath.Join(configDir, "crunchy-cli", "crunchy")
|
||||||
|
|
||||||
|
var credentials []byte
|
||||||
|
if encryptionKey != nil {
|
||||||
|
hashedEncryptionKey := sha256.Sum256(encryptionKey)
|
||||||
|
block, err := aes.NewCipher(hashedEncryptionKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gcm, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nonce := make([]byte, gcm.NonceSize())
|
||||||
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b := gcm.Seal(nonce, nonce, []byte(fmt.Sprintf("%s\n%s", user, password)), nil)
|
||||||
|
credentials = append([]byte("aes:"), b...)
|
||||||
|
} else {
|
||||||
|
credentials = []byte(fmt.Sprintf("%s\n%s", user, password))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.MkdirAll(filepath.Join(configDir, "crunchy-cli"), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(file, credentials, 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveSessionPersistent(crunchy *crunchyroll.Crunchyroll) error {
|
||||||
|
configDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
file := filepath.Join(configDir, "crunchy-cli", "crunchy")
|
||||||
|
|
||||||
|
if err = os.MkdirAll(filepath.Join(configDir, "crunchy-cli"), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(file, []byte(crunchy.RefreshToken), 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsTempSession() bool {
|
||||||
|
file := filepath.Join(os.TempDir(), ".crunchy")
|
||||||
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsSavedSessionEncrypted() (bool, error) {
|
||||||
|
configDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
file := filepath.Join(configDir, "crunchy-cli", "crunchy")
|
||||||
|
body, err := os.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return strings.HasPrefix(string(body), "aes:"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadSession(encryptionKey []byte) (*crunchyroll.Crunchyroll, error) {
|
||||||
|
file := filepath.Join(os.TempDir(), ".crunchy")
|
||||||
|
crunchy, err := loadTempSession(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if crunchy != nil {
|
||||||
|
return crunchy, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
configDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
file = filepath.Join(configDir, "crunchy-cli", "crunchy")
|
||||||
|
crunchy, err = loadPersistentSession(file, encryptionKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if crunchy != nil {
|
||||||
|
return crunchy, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("not logged in")
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadTempSession(file string) (*crunchyroll.Crunchyroll, error) {
|
||||||
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
||||||
|
body, err := os.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
crunchy, err := crunchyroll.LoginWithRefreshToken(string(body), SystemLocale(true), Client)
|
||||||
|
if err != nil {
|
||||||
|
Log.Debug("Failed to login with temp refresh token: %v", err)
|
||||||
|
} else {
|
||||||
|
Log.Debug("Logged in with refresh token %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", body)
|
||||||
|
return crunchy, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPersistentSession(file string, encryptionKey []byte) (crunchy *crunchyroll.Crunchyroll, err error) {
|
||||||
|
if _, err = os.Stat(file); !os.IsNotExist(err) {
|
||||||
|
body, err := os.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
split := strings.SplitN(string(body), "\n", 2)
|
||||||
|
if len(split) == 1 || split[1] == "" && strings.HasPrefix(split[0], "aes:") {
|
||||||
|
encrypted := body[4:]
|
||||||
|
hashedEncryptionKey := sha256.Sum256(encryptionKey)
|
||||||
|
block, err := aes.NewCipher(hashedEncryptionKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
gcm, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
nonce, cypherText := encrypted[:gcm.NonceSize()], encrypted[gcm.NonceSize():]
|
||||||
|
b, err := gcm.Open(nil, nonce, cypherText, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
split = strings.SplitN(string(b), "\n", 2)
|
||||||
|
}
|
||||||
|
if len(split) == 2 {
|
||||||
|
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], SystemLocale(true), Client); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
Log.Debug("Logged in with credentials")
|
||||||
|
} else {
|
||||||
|
if crunchy, err = crunchyroll.LoginWithRefreshToken(split[0], SystemLocale(true), Client); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
Log.Debug("Logged in with refresh token %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// the refresh token is written to a temp file to reduce the amount of re-logging in.
|
||||||
|
// it seems like that crunchyroll has also a little cooldown if a user logs in too often in a short time
|
||||||
|
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(crunchy.RefreshToken), 0600); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
155
utils/sort.go
155
utils/sort.go
|
|
@ -1,155 +0,0 @@
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/crunchy-labs/crunchyroll-go/v2"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SortEpisodesBySeason sorts the given episodes by their seasons.
|
|
||||||
// Note that the same episodes just with different audio locales will cause problems.
|
|
||||||
func SortEpisodesBySeason(episodes []*crunchyroll.Episode) [][]*crunchyroll.Episode {
|
|
||||||
sortMap := map[string]map[int][]*crunchyroll.Episode{}
|
|
||||||
|
|
||||||
for _, episode := range episodes {
|
|
||||||
if _, ok := sortMap[episode.SeriesID]; !ok {
|
|
||||||
sortMap[episode.SeriesID] = map[int][]*crunchyroll.Episode{}
|
|
||||||
}
|
|
||||||
if _, ok := sortMap[episode.SeriesID][episode.SeasonNumber]; !ok {
|
|
||||||
sortMap[episode.SeriesID][episode.SeasonNumber] = make([]*crunchyroll.Episode, 0)
|
|
||||||
}
|
|
||||||
sortMap[episode.SeriesID][episode.SeasonNumber] = append(sortMap[episode.SeriesID][episode.SeasonNumber], episode)
|
|
||||||
}
|
|
||||||
|
|
||||||
var eps [][]*crunchyroll.Episode
|
|
||||||
for _, series := range sortMap {
|
|
||||||
var keys []int
|
|
||||||
for seriesNumber := range series {
|
|
||||||
keys = append(keys, seriesNumber)
|
|
||||||
}
|
|
||||||
sort.Ints(keys)
|
|
||||||
|
|
||||||
for _, key := range keys {
|
|
||||||
es := series[key]
|
|
||||||
if len(es) > 0 {
|
|
||||||
sort.Sort(EpisodesByNumber(es))
|
|
||||||
eps = append(eps, es)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return eps
|
|
||||||
}
|
|
||||||
|
|
||||||
// SortEpisodesByAudio sort the given episodes by their audio locale.
|
|
||||||
func SortEpisodesByAudio(episodes []*crunchyroll.Episode) (map[crunchyroll.LOCALE][]*crunchyroll.Episode, error) {
|
|
||||||
eps := map[crunchyroll.LOCALE][]*crunchyroll.Episode{}
|
|
||||||
|
|
||||||
errChan := make(chan error)
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var lock sync.Mutex
|
|
||||||
for _, episode := range episodes {
|
|
||||||
episode := episode
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
audioLocale, err := episode.AudioLocale()
|
|
||||||
if err != nil {
|
|
||||||
errChan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
lock.Lock()
|
|
||||||
defer lock.Unlock()
|
|
||||||
|
|
||||||
if _, ok := eps[audioLocale]; !ok {
|
|
||||||
eps[audioLocale] = make([]*crunchyroll.Episode, 0)
|
|
||||||
}
|
|
||||||
eps[audioLocale] = append(eps[audioLocale], episode)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
errChan <- nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := <-errChan; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return eps, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MovieListingsByDuration sorts movie listings by their duration.
|
|
||||||
type MovieListingsByDuration []*crunchyroll.MovieListing
|
|
||||||
|
|
||||||
func (mlbd MovieListingsByDuration) Len() int {
|
|
||||||
return len(mlbd)
|
|
||||||
}
|
|
||||||
func (mlbd MovieListingsByDuration) Swap(i, j int) {
|
|
||||||
mlbd[i], mlbd[j] = mlbd[j], mlbd[i]
|
|
||||||
}
|
|
||||||
func (mlbd MovieListingsByDuration) Less(i, j int) bool {
|
|
||||||
return mlbd[i].DurationMS < mlbd[j].DurationMS
|
|
||||||
}
|
|
||||||
|
|
||||||
// EpisodesByDuration sorts episodes by their duration.
|
|
||||||
type EpisodesByDuration []*crunchyroll.Episode
|
|
||||||
|
|
||||||
func (ebd EpisodesByDuration) Len() int {
|
|
||||||
return len(ebd)
|
|
||||||
}
|
|
||||||
func (ebd EpisodesByDuration) Swap(i, j int) {
|
|
||||||
ebd[i], ebd[j] = ebd[j], ebd[i]
|
|
||||||
}
|
|
||||||
func (ebd EpisodesByDuration) Less(i, j int) bool {
|
|
||||||
return ebd[i].DurationMS < ebd[j].DurationMS
|
|
||||||
}
|
|
||||||
|
|
||||||
// EpisodesByNumber sorts episodes after their episode number.
|
|
||||||
type EpisodesByNumber []*crunchyroll.Episode
|
|
||||||
|
|
||||||
func (ebn EpisodesByNumber) Len() int {
|
|
||||||
return len(ebn)
|
|
||||||
}
|
|
||||||
func (ebn EpisodesByNumber) Swap(i, j int) {
|
|
||||||
ebn[i], ebn[j] = ebn[j], ebn[i]
|
|
||||||
}
|
|
||||||
func (ebn EpisodesByNumber) Less(i, j int) bool {
|
|
||||||
return ebn[i].EpisodeNumber < ebn[j].EpisodeNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatsByResolution sorts formats after their resolution.
|
|
||||||
type FormatsByResolution []*crunchyroll.Format
|
|
||||||
|
|
||||||
func (fbr FormatsByResolution) Len() int {
|
|
||||||
return len(fbr)
|
|
||||||
}
|
|
||||||
func (fbr FormatsByResolution) Swap(i, j int) {
|
|
||||||
fbr[i], fbr[j] = fbr[j], fbr[i]
|
|
||||||
}
|
|
||||||
func (fbr FormatsByResolution) Less(i, j int) bool {
|
|
||||||
iSplitRes := strings.SplitN(fbr[i].Video.Resolution, "x", 2)
|
|
||||||
iResX, _ := strconv.Atoi(iSplitRes[0])
|
|
||||||
iResY, _ := strconv.Atoi(iSplitRes[1])
|
|
||||||
|
|
||||||
jSplitRes := strings.SplitN(fbr[j].Video.Resolution, "x", 2)
|
|
||||||
jResX, _ := strconv.Atoi(jSplitRes[0])
|
|
||||||
jResY, _ := strconv.Atoi(jSplitRes[1])
|
|
||||||
|
|
||||||
return iResX+iResY < jResX+jResY
|
|
||||||
}
|
|
||||||
|
|
||||||
// SubtitlesByLocale sorts subtitles after their locale.
|
|
||||||
type SubtitlesByLocale []*crunchyroll.Subtitle
|
|
||||||
|
|
||||||
func (sbl SubtitlesByLocale) Len() int {
|
|
||||||
return len(sbl)
|
|
||||||
}
|
|
||||||
func (sbl SubtitlesByLocale) Swap(i, j int) {
|
|
||||||
sbl[i], sbl[j] = sbl[j], sbl[i]
|
|
||||||
}
|
|
||||||
func (sbl SubtitlesByLocale) Less(i, j int) bool {
|
|
||||||
return LocaleLanguage(sbl[i].Locale) < LocaleLanguage(sbl[j].Locale)
|
|
||||||
}
|
|
||||||
7
utils/system.go
Normal file
7
utils/system.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import "os/exec"
|
||||||
|
|
||||||
|
func HasFFmpeg() bool {
|
||||||
|
return exec.Command("ffmpeg", "-h").Run() == nil
|
||||||
|
}
|
||||||
14
utils/vars.go
Normal file
14
utils/vars.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/crunchy-labs/crunchyroll-go/v3"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Version = "development"
|
||||||
|
|
||||||
|
var (
|
||||||
|
Crunchy *crunchyroll.Crunchyroll
|
||||||
|
Client *http.Client
|
||||||
|
Log Logger
|
||||||
|
)
|
||||||
231
video.go
231
video.go
|
|
@ -1,231 +0,0 @@
|
||||||
package crunchyroll
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type video struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
ExternalID string `json:"external_id"`
|
|
||||||
|
|
||||||
Description string `json:"description"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Slug string `json:"slug"`
|
|
||||||
SlugTitle string `json:"slug_title"`
|
|
||||||
|
|
||||||
Images struct {
|
|
||||||
PosterTall [][]struct {
|
|
||||||
Height int `json:"height"`
|
|
||||||
Source string `json:"source"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Width int `json:"width"`
|
|
||||||
} `json:"poster_tall"`
|
|
||||||
PosterWide [][]struct {
|
|
||||||
Height int `json:"height"`
|
|
||||||
Source string `json:"source"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Width int `json:"width"`
|
|
||||||
} `json:"poster_wide"`
|
|
||||||
} `json:"images"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Video is the base for Movie and Season.
|
|
||||||
type Video interface{}
|
|
||||||
|
|
||||||
// Movie contains information about a movie.
|
|
||||||
type Movie struct {
|
|
||||||
video
|
|
||||||
Video
|
|
||||||
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
children []*MovieListing
|
|
||||||
|
|
||||||
// not generated when calling MovieFromID.
|
|
||||||
MovieListingMetadata struct {
|
|
||||||
AvailabilityNotes string `json:"availability_notes"`
|
|
||||||
AvailableOffline bool `json:"available_offline"`
|
|
||||||
DurationMS int `json:"duration_ms"`
|
|
||||||
ExtendedDescription string `json:"extended_description"`
|
|
||||||
FirstMovieID string `json:"first_movie_id"`
|
|
||||||
IsDubbed bool `json:"is_dubbed"`
|
|
||||||
IsMature bool `json:"is_mature"`
|
|
||||||
IsPremiumOnly bool `json:"is_premium_only"`
|
|
||||||
IsSubbed bool `json:"is_subbed"`
|
|
||||||
MatureRatings []string `json:"mature_ratings"`
|
|
||||||
MovieReleaseYear int `json:"movie_release_year"`
|
|
||||||
SubtitleLocales []LOCALE `json:"subtitle_locales"`
|
|
||||||
} `json:"movie_listing_metadata"`
|
|
||||||
|
|
||||||
Playback string `json:"playback"`
|
|
||||||
|
|
||||||
PromoDescription string `json:"promo_description"`
|
|
||||||
PromoTitle string `json:"promo_title"`
|
|
||||||
SearchMetadata struct {
|
|
||||||
Score float64 `json:"score"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MovieFromID returns a movie by its api id.
|
|
||||||
func MovieFromID(crunchy *Crunchyroll, id string) (*Movie, error) {
|
|
||||||
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/movies/%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
movieListing := &Movie{
|
|
||||||
crunchy: crunchy,
|
|
||||||
}
|
|
||||||
movieListing.ID = id
|
|
||||||
if err = decodeMapToStruct(jsonBody, movieListing); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return movieListing, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MovieListing returns all videos corresponding with the movie.
|
|
||||||
func (m *Movie) MovieListing() (movieListings []*MovieListing, err error) {
|
|
||||||
if m.children != nil {
|
|
||||||
return m.children, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := m.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/movies?movie_listing_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
m.crunchy.Config.CountryCode,
|
|
||||||
m.crunchy.Config.MaturityRating,
|
|
||||||
m.crunchy.Config.Channel,
|
|
||||||
m.ID,
|
|
||||||
m.crunchy.Locale,
|
|
||||||
m.crunchy.Config.Signature,
|
|
||||||
m.crunchy.Config.Policy,
|
|
||||||
m.crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
for _, item := range jsonBody["items"].([]interface{}) {
|
|
||||||
movieListing := &MovieListing{
|
|
||||||
crunchy: m.crunchy,
|
|
||||||
}
|
|
||||||
if err = decodeMapToStruct(item, movieListing); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
movieListings = append(movieListings, movieListing)
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.crunchy.cache {
|
|
||||||
m.children = movieListings
|
|
||||||
}
|
|
||||||
return movieListings, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Series contains information about an anime series.
|
|
||||||
type Series struct {
|
|
||||||
video
|
|
||||||
Video
|
|
||||||
|
|
||||||
crunchy *Crunchyroll
|
|
||||||
|
|
||||||
children []*Season
|
|
||||||
|
|
||||||
PromoDescription string `json:"promo_description"`
|
|
||||||
PromoTitle string `json:"promo_title"`
|
|
||||||
|
|
||||||
AvailabilityNotes string `json:"availability_notes"`
|
|
||||||
EpisodeCount int `json:"episode_count"`
|
|
||||||
ExtendedDescription string `json:"extended_description"`
|
|
||||||
IsDubbed bool `json:"is_dubbed"`
|
|
||||||
IsMature bool `json:"is_mature"`
|
|
||||||
IsSimulcast bool `json:"is_simulcast"`
|
|
||||||
IsSubbed bool `json:"is_subbed"`
|
|
||||||
MatureBlocked bool `json:"mature_blocked"`
|
|
||||||
MatureRatings []string `json:"mature_ratings"`
|
|
||||||
SeasonCount int `json:"season_count"`
|
|
||||||
|
|
||||||
// not generated when calling SeriesFromID.
|
|
||||||
SearchMetadata struct {
|
|
||||||
Score float64 `json:"score"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SeriesFromID returns a series by its api id.
|
|
||||||
func SeriesFromID(crunchy *Crunchyroll, id string) (*Series, error) {
|
|
||||||
resp, err := crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/movies?movie_listing_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
crunchy.Config.CountryCode,
|
|
||||||
crunchy.Config.MaturityRating,
|
|
||||||
crunchy.Config.Channel,
|
|
||||||
id,
|
|
||||||
crunchy.Locale,
|
|
||||||
crunchy.Config.Signature,
|
|
||||||
crunchy.Config.Policy,
|
|
||||||
crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
series := &Series{
|
|
||||||
crunchy: crunchy,
|
|
||||||
}
|
|
||||||
series.ID = id
|
|
||||||
if err = decodeMapToStruct(jsonBody, series); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return series, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seasons returns all seasons of a series.
|
|
||||||
func (s *Series) Seasons() (seasons []*Season, err error) {
|
|
||||||
if s.children != nil {
|
|
||||||
return s.children, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := s.crunchy.request(fmt.Sprintf("https://beta-api.crunchyroll.com/cms/v2/%s/%s/%s/seasons?series_id=%s&locale=%s&Signature=%s&Policy=%s&Key-Pair-Id=%s",
|
|
||||||
s.crunchy.Config.CountryCode,
|
|
||||||
s.crunchy.Config.MaturityRating,
|
|
||||||
s.crunchy.Config.Channel,
|
|
||||||
s.ID,
|
|
||||||
s.crunchy.Locale,
|
|
||||||
s.crunchy.Config.Signature,
|
|
||||||
s.crunchy.Config.Policy,
|
|
||||||
s.crunchy.Config.KeyPairID))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
var jsonBody map[string]interface{}
|
|
||||||
json.NewDecoder(resp.Body).Decode(&jsonBody)
|
|
||||||
|
|
||||||
for _, item := range jsonBody["items"].([]interface{}) {
|
|
||||||
season := &Season{
|
|
||||||
crunchy: s.crunchy,
|
|
||||||
}
|
|
||||||
if err = decodeMapToStruct(item, season); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
seasons = append(seasons, season)
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.crunchy.cache {
|
|
||||||
s.children = seasons
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue