@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
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.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
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.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
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.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
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>.
|
@ -1,111 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
"""
|
||||
cloudscraper.exceptions
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
This module contains the set of cloudscraper exceptions.
|
||||
"""
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class CloudflareException(Exception):
|
||||
"""
|
||||
Base exception class for cloudscraper for Cloudflare
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareLoopProtection(CloudflareException):
|
||||
"""
|
||||
Raise an exception for recursive depth protection
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareCode1020(CloudflareException):
|
||||
"""
|
||||
Raise an exception for Cloudflare code 1020 block
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareIUAMError(CloudflareException):
|
||||
"""
|
||||
Raise an error for problem extracting IUAM paramters
|
||||
from Cloudflare payload
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareChallengeError(CloudflareException):
|
||||
"""
|
||||
Raise an error when detected new Cloudflare challenge
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareSolveError(CloudflareException):
|
||||
"""
|
||||
Raise an error when issue with solving Cloudflare challenge
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareReCaptchaError(CloudflareException):
|
||||
"""
|
||||
Raise an error for problem extracting reCaptcha paramters
|
||||
from Cloudflare payload
|
||||
"""
|
||||
|
||||
|
||||
class CloudflareReCaptchaProvider(CloudflareException):
|
||||
"""
|
||||
Raise an exception for no reCaptcha provider loaded for Cloudflare.
|
||||
"""
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class reCaptchaException(Exception):
|
||||
"""
|
||||
Base exception class for cloudscraper reCaptcha Providers
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaServiceUnavailable(reCaptchaException):
|
||||
"""
|
||||
Raise an exception for external services that cannot be reached
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaAPIError(reCaptchaException):
|
||||
"""
|
||||
Raise an error for error from API response.
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaAccountError(reCaptchaException):
|
||||
"""
|
||||
Raise an error for reCaptcha provider account problem.
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaTimeout(reCaptchaException):
|
||||
"""
|
||||
Raise an exception for reCaptcha provider taking too long.
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaParameter(reCaptchaException):
|
||||
"""
|
||||
Raise an exception for bad or missing Parameter.
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaBadJobID(reCaptchaException):
|
||||
"""
|
||||
Raise an exception for invalid job id.
|
||||
"""
|
||||
|
||||
|
||||
class reCaptchaReportError(reCaptchaException):
|
||||
"""
|
||||
Raise an error for reCaptcha provider unable to report bad solve.
|
||||
"""
|
@ -1,79 +0,0 @@
|
||||
import json
|
||||
import platform
|
||||
import requests
|
||||
import ssl
|
||||
import sys
|
||||
import urllib3
|
||||
|
||||
from collections import OrderedDict
|
||||
from . import __version__ as cloudscraper_version
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def getPossibleCiphers():
|
||||
try:
|
||||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
|
||||
context.set_ciphers('ALL')
|
||||
return sorted([cipher['name'] for cipher in context.get_ciphers()])
|
||||
except AttributeError:
|
||||
return 'get_ciphers() is unsupported'
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def _pythonVersion():
|
||||
interpreter = platform.python_implementation()
|
||||
interpreter_version = platform.python_version()
|
||||
|
||||
if interpreter == 'PyPy':
|
||||
interpreter_version = '{}.{}.{}'.format(
|
||||
sys.pypy_version_info.major,
|
||||
sys.pypy_version_info.minor,
|
||||
sys.pypy_version_info.micro
|
||||
)
|
||||
if sys.pypy_version_info.releaselevel != 'final':
|
||||
interpreter_version = '{}{}'.format(
|
||||
interpreter_version,
|
||||
sys.pypy_version_info.releaselevel
|
||||
)
|
||||
|
||||
return {
|
||||
'name': interpreter,
|
||||
'version': interpreter_version
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def systemInfo():
|
||||
try:
|
||||
platform_info = {
|
||||
'system': platform.system(),
|
||||
'release': platform.release(),
|
||||
}
|
||||
except IOError:
|
||||
platform_info = {
|
||||
'system': 'Unknown',
|
||||
'release': 'Unknown',
|
||||
}
|
||||
|
||||
return OrderedDict([
|
||||
('platform', platform_info),
|
||||
('interpreter', _pythonVersion()),
|
||||
('cloudscraper', cloudscraper_version),
|
||||
('requests', requests.__version__),
|
||||
('urllib3', urllib3.__version__),
|
||||
('OpenSSL', OrderedDict(
|
||||
[
|
||||
('version', ssl.OPENSSL_VERSION),
|
||||
('ciphers', getPossibleCiphers())
|
||||
]
|
||||
))
|
||||
])
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(json.dumps(systemInfo(), indent=4))
|
@ -1,103 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
import sys
|
||||
import ctypes.util
|
||||
|
||||
from ctypes import c_void_p, c_size_t, byref, create_string_buffer, CDLL
|
||||
|
||||
from . import JavaScriptInterpreter
|
||||
from .encapsulated import template
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class ChallengeInterpreter(JavaScriptInterpreter):
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def __init__(self):
|
||||
super(ChallengeInterpreter, self).__init__('chakracore')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def eval(self, body, domain):
|
||||
chakraCoreLibrary = None
|
||||
|
||||
# check current working directory.
|
||||
for _libraryFile in ['libChakraCore.so', 'libChakraCore.dylib', 'ChakraCore.dll']:
|
||||
if os.path.isfile(os.path.join(os.getcwd(), _libraryFile)):
|
||||
chakraCoreLibrary = os.path.join(os.getcwd(), _libraryFile)
|
||||
continue
|
||||
|
||||
if not chakraCoreLibrary:
|
||||
chakraCoreLibrary = ctypes.util.find_library('ChakraCore')
|
||||
|
||||
if not chakraCoreLibrary:
|
||||
sys.tracebacklimit = 0
|
||||
raise RuntimeError(
|
||||
'ChakraCore library not found in current path or any of your system library paths, '
|
||||
'please download from https://www.github.com/VeNoMouS/cloudscraper/tree/ChakraCore/, '
|
||||
'or https://github.com/Microsoft/ChakraCore/'
|
||||
)
|
||||
|
||||
try:
|
||||
chakraCore = CDLL(chakraCoreLibrary)
|
||||
except OSError:
|
||||
sys.tracebacklimit = 0
|
||||
raise RuntimeError('There was an error loading the ChakraCore library {}'.format(chakraCoreLibrary))
|
||||
|
||||
if sys.platform != 'win32':
|
||||
chakraCore.DllMain(0, 1, 0)
|
||||
chakraCore.DllMain(0, 2, 0)
|
||||
|
||||
script = create_string_buffer(template(body, domain).encode('utf-16'))
|
||||
|
||||
runtime = c_void_p()
|
||||
chakraCore.JsCreateRuntime(0, 0, byref(runtime))
|
||||
|
||||
context = c_void_p()
|
||||
chakraCore.JsCreateContext(runtime, byref(context))
|
||||
chakraCore.JsSetCurrentContext(context)
|
||||
|
||||
fname = c_void_p()
|
||||
chakraCore.JsCreateString(
|
||||
'iuam-challenge.js',
|
||||
len('iuam-challenge.js'),
|
||||
byref(fname)
|
||||
)
|
||||
|
||||
scriptSource = c_void_p()
|
||||
chakraCore.JsCreateExternalArrayBuffer(
|
||||
script,
|
||||
len(script),
|
||||
0,
|
||||
0,
|
||||
byref(scriptSource)
|
||||
)
|
||||
|
||||
jsResult = c_void_p()
|
||||
chakraCore.JsRun(scriptSource, 0, fname, 0x02, byref(jsResult))
|
||||
|
||||
resultJSString = c_void_p()
|
||||
chakraCore.JsConvertValueToString(jsResult, byref(resultJSString))
|
||||
|
||||
stringLength = c_size_t()
|
||||
chakraCore.JsCopyString(resultJSString, 0, 0, byref(stringLength))
|
||||
|
||||
resultSTR = create_string_buffer(stringLength.value + 1)
|
||||
chakraCore.JsCopyString(
|
||||
resultJSString,
|
||||
byref(resultSTR),
|
||||
stringLength.value + 1,
|
||||
0
|
||||
)
|
||||
|
||||
chakraCore.JsDisposeRuntime(runtime)
|
||||
|
||||
return resultSTR.value
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
ChallengeInterpreter()
|
@ -1,62 +0,0 @@
|
||||
import logging
|
||||
import re
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def template(body, domain):
|
||||
BUG_REPORT = 'Cloudflare may have changed their technique, or there may be a bug in the script.'
|
||||
|
||||
try:
|
||||
js = re.search(
|
||||
r'setTimeout\(function\(\){\s+(.*?a\.value\s*=\s*\S+toFixed\(10\);)',
|
||||
body,
|
||||
re.M | re.S
|
||||
).group(1)
|
||||
except Exception:
|
||||
raise ValueError('Unable to identify Cloudflare IUAM Javascript on website. {}'.format(BUG_REPORT))
|
||||
|
||||
jsEnv = '''String.prototype.italics=function(str) {{return "<i>" + this + "</i>";}};
|
||||
var subVars= {{{subVars}}};
|
||||
var document = {{
|
||||
createElement: function () {{
|
||||
return {{ firstChild: {{ href: "https://{domain}/" }} }}
|
||||
}},
|
||||
getElementById: function (str) {{
|
||||
return {{"innerHTML": subVars[str]}};
|
||||
}}
|
||||
}};
|
||||
'''
|
||||
|
||||
try:
|
||||
js = js.replace(
|
||||
r"(setInterval(function(){}, 100),t.match(/https?:\/\//)[0]);",
|
||||
r"t.match(/https?:\/\//)[0];"
|
||||
)
|
||||
|
||||
k = re.search(r" k\s*=\s*'(?P<k>\S+)';", body).group('k')
|
||||
r = re.compile(r'<div id="{}(?P<id>\d+)">\s*(?P<jsfuck>[^<>]*)</div>'.format(k))
|
||||
|
||||
subVars = ''
|
||||
for m in r.finditer(body):
|
||||
subVars = '{}\n\t\t{}{}: {},\n'.format(subVars, k, m.group('id'), m.group('jsfuck'))
|
||||
subVars = subVars[:-2]
|
||||
|
||||
except: # noqa
|
||||
logging.error('Error extracting Cloudflare IUAM Javascript. {}'.format(BUG_REPORT))
|
||||
raise
|
||||
|
||||
return '{}{}'.format(
|
||||
re.sub(
|
||||
r'\s{2,}',
|
||||
' ',
|
||||
jsEnv.format(
|
||||
domain=domain,
|
||||
subVars=subVars
|
||||
),
|
||||
re.MULTILINE | re.DOTALL
|
||||
),
|
||||
js
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
@ -1,233 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import ast
|
||||
import re
|
||||
import operator as op
|
||||
import pyparsing
|
||||
|
||||
from ..exceptions import CloudflareSolveError
|
||||
from . import JavaScriptInterpreter
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
_OP_MAP = {
|
||||
ast.Add: op.add,
|
||||
ast.Sub: op.sub,
|
||||
ast.Mult: op.mul,
|
||||
ast.Div: op.truediv,
|
||||
ast.Invert: op.neg,
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class Calc(ast.NodeVisitor):
|
||||
|
||||
def visit_BinOp(self, node):
|
||||
return _OP_MAP[type(node.op)](self.visit(node.left), self.visit(node.right))
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def visit_Num(self, node):
|
||||
return node.n
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def visit_Expr(self, node):
|
||||
return self.visit(node.value)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@classmethod
|
||||
def doMath(cls, expression):
|
||||
tree = ast.parse(expression)
|
||||
calc = cls()
|
||||
return calc.visit(tree.body[0])
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class Parentheses(object):
|
||||
|
||||
def fix(self, s):
|
||||
res = []
|
||||
self.visited = set([s])
|
||||
self.dfs(s, self.invalid(s), res)
|
||||
return res
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def dfs(self, s, n, res):
|
||||
if n == 0:
|
||||
res.append(s)
|
||||
return
|
||||
for i in range(len(s)):
|
||||
if s[i] in ['(', ')']:
|
||||
s_new = s[:i] + s[i + 1:]
|
||||
if s_new not in self.visited and self.invalid(s_new) < n:
|
||||
self.visited.add(s_new)
|
||||
self.dfs(s_new, self.invalid(s_new), res)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def invalid(self, s):
|
||||
plus = minus = 0
|
||||
memo = {"(": 1, ")": -1}
|
||||
for c in s:
|
||||
plus += memo.get(c, 0)
|
||||
minus += 1 if plus < 0 else 0
|
||||
plus = max(0, plus)
|
||||
return plus + minus
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class ChallengeInterpreter(JavaScriptInterpreter):
|
||||
|
||||
def __init__(self):
|
||||
super(ChallengeInterpreter, self).__init__('native')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def eval(self, body, domain):
|
||||
|
||||
operators = {
|
||||
'+': op.add,
|
||||
'-': op.sub,
|
||||
'*': op.mul,
|
||||
'/': op.truediv
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def flatten(lists):
|
||||
return sum(map(flatten, lists), []) if isinstance(lists, list) else [lists]
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def jsfuckToNumber(jsFuck):
|
||||
# "Clean Up" JSFuck
|
||||
jsFuck = jsFuck.replace('!+[]', '1').replace('!![]', '1').replace('[]', '0')
|
||||
jsFuck = jsFuck.lstrip('+').replace('(+', '(').replace(' ', '')
|
||||
jsFuck = Parentheses().fix(jsFuck)[0]
|
||||
|
||||
# Hackery Parser for Math
|
||||
stack = []
|
||||
bstack = []
|
||||
|
||||
for i in flatten(pyparsing.nestedExpr().parseString(jsFuck).asList()):
|
||||
if i == '+':
|
||||
stack.append(bstack)
|
||||
bstack = []
|
||||
continue
|
||||
bstack.append(i)
|
||||
stack.append(bstack)
|
||||
|
||||
return int(''.join([str(Calc.doMath(''.join(i))) for i in stack]))
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def divisorMath(payload, needle, domain):
|
||||
jsfuckMath = payload.split('/')
|
||||
if needle in jsfuckMath[1]:
|
||||
expression = re.findall(r"^(.*?)(.)\(function", jsfuckMath[1])[0]
|
||||
|
||||
expression_value = operators[expression[1]](
|
||||
float(jsfuckToNumber(expression[0])),
|
||||
float(ord(domain[jsfuckToNumber(jsfuckMath[1][
|
||||
jsfuckMath[1].find('"("+p+")")}') + len('"("+p+")")}'):-2
|
||||
])]))
|
||||
)
|
||||
else:
|
||||
expression_value = jsfuckToNumber(jsfuckMath[1])
|
||||
|
||||
expression_value = jsfuckToNumber(jsfuckMath[0]) / float(expression_value)
|
||||
|
||||
return expression_value
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def challengeSolve(body, domain):
|
||||
jschl_answer = 0
|
||||
|
||||
try:
|
||||
jsfuckChallenge = re.search(
|
||||
r"setTimeout\(function\(\){\s+var.*?f,\s*(?P<variable>\w+).*?:(?P<init>\S+)};"
|
||||
r".*?\('challenge-form'\);.*?;(?P<challenge>.*?a\.value)\s*=\s*\S+\.toFixed\(10\);",
|
||||
body,
|
||||
re.DOTALL | re.MULTILINE
|
||||
).groupdict()
|
||||
except AttributeError:
|
||||
raise CloudflareSolveError('There was an issue extracting "jsfuckChallenge" from the Cloudflare challenge.')
|
||||
|
||||
kJSFUCK = re.search(r'(;|)\s*k.=(?P<kJSFUCK>\S+);', jsfuckChallenge['challenge'], re.S | re.M)
|
||||
if kJSFUCK:
|
||||
try:
|
||||
kJSFUCK = jsfuckToNumber(kJSFUCK.group('kJSFUCK'))
|
||||
except IndexError:
|
||||
raise CloudflareSolveError('There was an issue extracting "kJSFUCK" from the Cloudflare challenge.')
|
||||
|
||||
try:
|
||||
kID = re.search(r"\s*k\s*=\s*'(?P<kID>\S+)';", body).group('kID')
|
||||
except IndexError:
|
||||
raise CloudflareSolveError('There was an issue extracting "kID" from the Cloudflare challenge.')
|
||||
|
||||
try:
|
||||
r = re.compile(r'<div id="{}(?P<id>\d+)">\s*(?P<jsfuck>[^<>]*)</div>'.format(kID))
|
||||
|
||||
kValues = {}
|
||||
for m in r.finditer(body):
|
||||
kValues[int(m.group('id'))] = m.group('jsfuck')
|
||||
|
||||
jsfuckChallenge['k'] = kValues[kJSFUCK]
|
||||
except (AttributeError, IndexError):
|
||||
raise CloudflareSolveError('There was an issue extracting "kValues" from the Cloudflare challenge.')
|
||||
|
||||
jsfuckChallenge['challenge'] = re.finditer(
|
||||
r'{}.*?([+\-*/])=(.*?);(?=a\.value|{})'.format(
|
||||
jsfuckChallenge['variable'],
|
||||
jsfuckChallenge['variable']
|
||||
),
|
||||
jsfuckChallenge['challenge']
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
if '/' in jsfuckChallenge['init']:
|
||||
val = jsfuckChallenge['init'].split('/')
|
||||
jschl_answer = jsfuckToNumber(val[0]) / float(jsfuckToNumber(val[1]))
|
||||
else:
|
||||
jschl_answer = jsfuckToNumber(jsfuckChallenge['init'])
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
for expressionMatch in jsfuckChallenge['challenge']:
|
||||
oper, expression = expressionMatch.groups()
|
||||
|
||||
if '/' in expression:
|
||||
expression_value = divisorMath(expression, 'function(p)', domain)
|
||||
else:
|
||||
if 'Element' in expression:
|
||||
expression_value = divisorMath(jsfuckChallenge['k'], '"("+p+")")}', domain)
|
||||
else:
|
||||
expression_value = jsfuckToNumber(expression)
|
||||
|
||||
jschl_answer = operators[oper](jschl_answer, expression_value)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
# if not jsfuckChallenge['k'] and '+ t.length' in body:
|
||||
# jschl_answer += len(domain)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
return '{0:.10f}'.format(jschl_answer)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
return challengeSolve(body, domain)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
ChallengeInterpreter()
|
@ -1,33 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
try:
|
||||
import v8eval
|
||||
except ImportError:
|
||||
sys.tracebacklimit = 0
|
||||
raise RuntimeError('Please install the python module v8eval either via pip or download it from https://github.com/sony/v8eval')
|
||||
|
||||
from . import JavaScriptInterpreter
|
||||
from .encapsulated import template
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class ChallengeInterpreter(JavaScriptInterpreter):
|
||||
|
||||
def __init__(self):
|
||||
super(ChallengeInterpreter, self).__init__('v8')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def eval(self, body, domain):
|
||||
try:
|
||||
return v8eval.V8().eval(template(body, domain))
|
||||
except (TypeError, v8eval.V8Error):
|
||||
RuntimeError('We encountered an error running the V8 Engine.')
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
ChallengeInterpreter()
|
@ -1,248 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import requests
|
||||
|
||||
from ..exceptions import (
|
||||
reCaptchaServiceUnavailable,
|
||||
reCaptchaAPIError,
|
||||
reCaptchaTimeout,
|
||||
reCaptchaParameter,
|
||||
reCaptchaBadJobID,
|
||||
reCaptchaReportError
|
||||
)
|
||||
|
||||
try:
|
||||
import polling
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Please install the python module 'polling' via pip or download it from "
|
||||
"https://github.com/justiniso/polling/"
|
||||
)
|
||||
|
||||
from . import reCaptcha
|
||||
|
||||
|
||||
class captchaSolver(reCaptcha):
|
||||
|
||||
def __init__(self):
|
||||
super(captchaSolver, self).__init__('2captcha')
|
||||
self.host = 'https://2captcha.com'
|
||||
self.session = requests.Session()
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@staticmethod
|
||||
def checkErrorStatus(response, request_type):
|
||||
if response.status_code in [500, 502]:
|
||||
raise reCaptchaServiceUnavailable('2Captcha: Server Side Error {}'.format(response.status_code))
|
||||
|
||||
errors = {
|
||||
'in.php': {
|
||||
"ERROR_WRONG_USER_KEY": "You've provided api_key parameter value is in incorrect format, it should contain 32 symbols.",
|
||||
"ERROR_KEY_DOES_NOT_EXIST": "The api_key you've provided does not exists.",
|
||||
"ERROR_ZERO_BALANCE": "You don't have sufficient funds on your account.",
|
||||
"ERROR_PAGEURL": "pageurl parameter is missing in your request.",
|
||||
"ERROR_NO_SLOT_AVAILABLE":
|
||||
"No Slots Available.\nYou can receive this error in two cases:\n"
|
||||
"1. If you solve ReCaptcha: the queue of your captchas that are not distributed to workers is too long. "
|
||||
"Queue limit changes dynamically and depends on total amount of captchas awaiting solution and usually it's between 50 and 100 captchas.\n"
|
||||
"2. If you solve Normal Captcha: your maximum rate for normal captchas is lower than current rate on the server."
|
||||
"You can change your maximum rate in your account's settings.",
|
||||
"ERROR_IP_NOT_ALLOWED": "The request is sent from the IP that is not on the list of your allowed IPs.",
|
||||
"IP_BANNED": "Your IP address is banned due to many frequent attempts to access the server using wrong authorization keys.",
|
||||
"ERROR_BAD_TOKEN_OR_PAGEURL":
|
||||
"You can get this error code when sending ReCaptcha V2. "
|
||||
"That happens if your request contains invalid pair of googlekey and pageurl. "
|
||||
"The common reason for that is that ReCaptcha is loaded inside an iframe hosted on another domain/subdomain.",
|
||||
"ERROR_GOOGLEKEY":
|
||||
"You can get this error code when sending ReCaptcha V2. "
|
||||
"That means that sitekey value provided in your request is incorrect: it's blank or malformed.",
|
||||
"MAX_USER_TURN": "You made more than 60 requests within 3 seconds.Your account is banned for 10 seconds. Ban will be lifted automatically."
|
||||
},
|
||||
'res.php': {
|
||||
"ERROR_CAPTCHA_UNSOLVABLE":
|
||||
"We are unable to solve your captcha - three of our workers were unable solve it "
|
||||
"or we didn't get an answer within 90 seconds (300 seconds for ReCaptcha V2). "
|
||||
"We will not charge you for that request.",
|
||||
"ERROR_WRONG_USER_KEY": "You've provided api_key parameter value in incorrect format, it should contain 32 symbols.",
|
||||
"ERROR_KEY_DOES_NOT_EXIST": "The api_key you've provided does not exists.",
|
||||
"ERROR_WRONG_ID_FORMAT": "You've provided captcha ID in wrong format. The ID can contain numbers only.",
|
||||
"ERROR_WRONG_CAPTCHA_ID": "You've provided incorrect captcha ID.",
|
||||
"ERROR_BAD_DUPLICATES":
|
||||
"Error is returned when 100% accuracy feature is enabled. "
|
||||
"The error means that max numbers of tries is reached but min number of matches not found.",
|
||||
"REPORT_NOT_RECORDED": "Error is returned to your complain request if you already complained lots of correctly solved captchas.",
|
||||
"ERROR_IP_ADDRES":
|
||||
"You can receive this error code when registering a pingback (callback) IP or domain."
|
||||
"That happes if your request is coming from an IP address that doesn't match the IP address of your pingback IP or domain.",
|
||||
"ERROR_TOKEN_EXPIRED": "You can receive this error code when sending GeeTest. That error means that challenge value you provided is expired.",
|
||||
"ERROR_EMPTY_ACTION": "Action parameter is missing or no value is provided for action parameter."
|
||||
}
|
||||
}
|
||||
|
||||
if response.json().get('status') == 0 and response.json().get('request') in errors.get(request_type):
|
||||
raise reCaptchaAPIError(
|
||||
'{} {}'.format(
|
||||
response.json().get('request'),
|
||||
errors.get(request_type).get(response.json().get('request'))
|
||||
)
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def reportJob(self, jobID):
|
||||
if not jobID:
|
||||
raise reCaptchaBadJobID(
|
||||
"2Captcha: Error bad job id to request reCaptcha."
|
||||
)
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get('status') == 1:
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response, 'res.php')
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.get(
|
||||
'{}/res.php'.format(self.host),
|
||||
params={
|
||||
'key': self.api_key,
|
||||
'action': 'reportbad',
|
||||
'id': jobID,
|
||||
'json': '1'
|
||||
},
|
||||
timeout=30
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=5,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return True
|
||||
else:
|
||||
raise reCaptchaReportError(
|
||||
"2Captcha: Error - Failed to report bad reCaptcha solve."
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestJob(self, jobID):
|
||||
if not jobID:
|
||||
raise reCaptchaBadJobID("2Captcha: Error bad job id to request reCaptcha.")
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get('status') == 1:
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response, 'res.php')
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.get(
|
||||
'{}/res.php'.format(self.host),
|
||||
params={
|
||||
'key': self.api_key,
|
||||
'action': 'get',
|
||||
'id': jobID,
|
||||
'json': '1'
|
||||
},
|
||||
timeout=30
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=5,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('request')
|
||||
else:
|
||||
raise reCaptchaTimeout(
|
||||
"2Captcha: Error failed to solve reCaptcha."
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestSolve(self, captchaType, url, siteKey):
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get("status") == 1 and response.json().get('request'):
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response, 'in.php')
|
||||
|
||||
return None
|
||||
|
||||
data = {
|
||||
'key': self.api_key,
|
||||
'pageurl': url,
|
||||
'json': 1,
|
||||
'soft_id': 5507698
|
||||
}
|
||||
|
||||
data.update(
|
||||
{
|
||||
'method': 'userrcaptcha',
|
||||
'googlekey': siteKey
|
||||
} if captchaType == 'reCaptcha' else {
|
||||
'method': 'hcaptcha',
|
||||
'sitekey': siteKey
|
||||
}
|
||||
)
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.post(
|
||||
'{}/in.php'.format(self.host),
|
||||
data=data,
|
||||
allow_redirects=False,
|
||||
timeout=30
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=5,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('request')
|
||||
else:
|
||||
raise reCaptchaBadJobID(
|
||||
'2Captcha: Error no job id was returned.'
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def getCaptchaAnswer(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
jobID = None
|
||||
|
||||
if not reCaptchaParams.get('api_key'):
|
||||
raise reCaptchaParameter(
|
||||
"2Captcha: Missing api_key parameter."
|
||||
)
|
||||
|
||||
self.api_key = reCaptchaParams.get('api_key')
|
||||
|
||||
if reCaptchaParams.get('proxy'):
|
||||
self.session.proxies = reCaptchaParams.get('proxies')
|
||||
|
||||
try:
|
||||
jobID = self.requestSolve(captchaType, url, siteKey)
|
||||
return self.requestJob(jobID)
|
||||
except polling.TimeoutException:
|
||||
try:
|
||||
if jobID:
|
||||
self.reportJob(jobID)
|
||||
except polling.TimeoutException:
|
||||
raise reCaptchaTimeout(
|
||||
"2Captcha: reCaptcha solve took to long and also failed reporting the job the job id {}.".format(jobID)
|
||||
)
|
||||
|
||||
raise reCaptchaTimeout(
|
||||
"2Captcha: reCaptcha solve took to long to execute job id {}, aborting.".format(jobID)
|
||||
)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
captchaSolver()
|
@ -1,212 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import re
|
||||
import requests
|
||||
|
||||
try:
|
||||
import polling
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Please install the python module 'polling' via pip or download it from "
|
||||
"https://github.com/justiniso/polling/"
|
||||
)
|
||||
|
||||
from ..exceptions import (
|
||||
reCaptchaServiceUnavailable,
|
||||
reCaptchaAPIError,
|
||||
reCaptchaTimeout,
|
||||
reCaptchaParameter,
|
||||
reCaptchaBadJobID
|
||||
)
|
||||
|
||||
from . import reCaptcha
|
||||
|
||||
|
||||
class captchaSolver(reCaptcha):
|
||||
|
||||
def __init__(self):
|
||||
super(captchaSolver, self).__init__('9kw')
|
||||
self.host = 'https://www.9kw.eu/index.cgi'
|
||||
self.maxtimeout = 180
|
||||
self.session = requests.Session()
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@staticmethod
|
||||
def checkErrorStatus(response):
|
||||
if response.status_code in [500, 502]:
|
||||
raise reCaptchaServiceUnavailable(
|
||||
'9kw: Server Side Error {}'.format(response.status_code)
|
||||
)
|
||||
|
||||
error_codes = {
|
||||
1: 'No API Key available.',
|
||||
2: 'No API key found.',
|
||||
3: 'No active API key found.',
|
||||
4: 'API Key has been disabled by the operator. ',
|
||||
5: 'No user found.',
|
||||
6: 'No data found.',
|
||||
7: 'Found No ID.',
|
||||
8: 'found No captcha.',
|
||||
9: 'No image found.',
|
||||
10: 'Image size not allowed.',
|
||||
11: 'credit is not sufficient.',
|
||||
12: 'what was done.',
|
||||
13: 'No answer contain.',
|
||||
14: 'Captcha already been answered.',
|
||||
15: 'Captcha to quickly filed.',
|
||||
16: 'JD check active.',
|
||||
17: 'Unknown problem.',
|
||||
18: 'Found No ID.',
|
||||
19: 'Incorrect answer.',
|
||||
20: 'Do not timely filed (Incorrect UserID).',
|
||||
21: 'Link not allowed.',
|
||||
22: 'Prohibited submit.',
|
||||
23: 'Entering prohibited.',
|
||||
24: 'Too little credit.',
|
||||
25: 'No entry found.',
|
||||
26: 'No Conditions accepted.',
|
||||
27: 'No coupon code found in the database.',
|
||||
28: 'Already unused voucher code.',
|
||||
29: 'maxTimeout under 60 seconds.',
|
||||
30: 'User not found.',
|
||||
31: 'An account is not yet 24 hours in system.',
|
||||
32: 'An account does not have the full rights.',
|
||||
33: 'Plugin needed a update.',
|
||||
34: 'No HTTPS allowed.',
|
||||
35: 'No HTTP allowed.',
|
||||
36: 'Source not allowed.',
|
||||
37: 'Transfer denied.',
|
||||
38: 'Incorrect answer without space',
|
||||
39: 'Incorrect answer with space',
|
||||
40: 'Incorrect answer with not only numbers',
|
||||
41: 'Incorrect answer with not only A-Z, a-z',
|
||||
42: 'Incorrect answer with not only 0-9, A-Z, a-z',
|
||||
43: 'Incorrect answer with not only [0-9,- ]',
|
||||
44: 'Incorrect answer with not only [0-9A-Za-z,- ]',
|
||||
45: 'Incorrect answer with not only coordinates',
|
||||
46: 'Incorrect answer with not only multiple coordinates',
|
||||
47: 'Incorrect answer with not only data',
|
||||
48: 'Incorrect answer with not only rotate number',
|
||||
49: 'Incorrect answer with not only text',
|
||||
50: 'Incorrect answer with not only text and too short',
|
||||
51: 'Incorrect answer with not enough chars',
|
||||
52: 'Incorrect answer with too many chars',
|
||||
53: 'Incorrect answer without no or yes',
|
||||
54: 'Assignment was not found.'
|
||||
}
|
||||
|
||||
if response.text.startswith('{'):
|
||||
if response.json().get('error'):
|
||||
raise reCaptchaAPIError(error_codes.get(int(response.json().get('error'))))
|
||||
else:
|
||||
error_code = int(re.search(r'^00(?P<error_code>\d+)', response.text).groupdict().get('error_code', 0))
|
||||
if error_code:
|
||||
raise reCaptchaAPIError(error_codes.get(error_code))
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestJob(self, jobID):
|
||||
if not jobID:
|
||||
raise reCaptchaBadJobID(
|
||||
"9kw: Error bad job id to request reCaptcha against."
|
||||
)
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get('answer') != 'NO DATA':
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.get(
|
||||
self.host,
|
||||
params={
|
||||
'apikey': self.api_key,
|
||||
'action': 'usercaptchacorrectdata',
|
||||
'id': jobID,
|
||||
'info': 1,
|
||||
'json': 1
|
||||
}
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=10,
|
||||
timeout=(self.maxtimeout + 10)
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('answer')
|
||||
else:
|
||||
raise reCaptchaTimeout("9kw: Error failed to solve reCaptcha.")
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestSolve(self, captchaType, url, siteKey):
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.text.startswith('{') and response.json().get('captchaid'):
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
captchaMap = {
|
||||
'reCaptcha': 'recaptchav2',
|
||||
'hCaptcha': 'hcaptcha'
|
||||
}
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.post(
|
||||
self.host,
|
||||
data={
|
||||
'apikey': self.api_key,
|
||||
'action': 'usercaptchaupload',
|
||||
'interactive': 1,
|
||||
'file-upload-01': siteKey,
|
||||
'oldsource': captchaMap[captchaType],
|
||||
'pageurl': url,
|
||||
'maxtimeout': self.maxtimeout,
|
||||
'json': 1
|
||||
},
|
||||
allow_redirects=False
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=5,
|
||||
timeout=(self.maxtimeout + 10)
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('captchaid')
|
||||
else:
|
||||
raise reCaptchaBadJobID('9kw: Error no valid job id was returned.')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def getCaptchaAnswer(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
jobID = None
|
||||
|
||||
if not reCaptchaParams.get('api_key'):
|
||||
raise reCaptchaParameter("9kw: Missing api_key parameter.")
|
||||
|
||||
self.api_key = reCaptchaParams.get('api_key')
|
||||
|
||||
if reCaptchaParams.get('maxtimeout'):
|
||||
self.maxtimeout = reCaptchaParams.get('maxtimeout')
|
||||
|
||||
if reCaptchaParams.get('proxy'):
|
||||
self.session.proxies = reCaptchaParams.get('proxies')
|
||||
|
||||
try:
|
||||
jobID = self.requestSolve(captchaType, url, siteKey)
|
||||
return self.requestJob(jobID)
|
||||
except polling.TimeoutException:
|
||||
raise reCaptchaTimeout(
|
||||
"9kw: reCaptcha solve took to long to execute 'captchaid' {}, aborting.".format(jobID)
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
captchaSolver()
|
@ -1,46 +0,0 @@
|
||||
import abc
|
||||
import logging
|
||||
import sys
|
||||
|
||||
if sys.version_info >= (3, 4):
|
||||
ABC = abc.ABC # noqa
|
||||
else:
|
||||
ABC = abc.ABCMeta('ABC', (), {})
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
captchaSolvers = {}
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
class reCaptcha(ABC):
|
||||
@abc.abstractmethod
|
||||
def __init__(self, name):
|
||||
captchaSolvers[name] = self
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@classmethod
|
||||
def dynamicImport(cls, name):
|
||||
if name not in captchaSolvers:
|
||||
try:
|
||||
__import__('{}.{}'.format(cls.__module__, name))
|
||||
if not isinstance(captchaSolvers.get(name), reCaptcha):
|
||||
raise ImportError('The anti reCaptcha provider was not initialized.')
|
||||
except ImportError:
|
||||
logging.error("Unable to load {} anti reCaptcha provider".format(name))
|
||||
raise
|
||||
|
||||
return captchaSolvers[name]
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@abc.abstractmethod
|
||||
def getCaptchaAnswer(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
pass
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def solveCaptcha(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
return self.getCaptchaAnswer(captchaType, url, siteKey, reCaptchaParams)
|
@ -1,75 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
from ..exceptions import (
|
||||
reCaptchaParameter,
|
||||
reCaptchaTimeout,
|
||||
reCaptchaAPIError
|
||||
)
|
||||
|
||||
try:
|
||||
from python_anticaptcha import (
|
||||
AnticaptchaClient,
|
||||
NoCaptchaTaskProxylessTask,
|
||||
HCaptchaTaskProxyless,
|
||||
AnticaptchaException
|
||||
)
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Please install/upgrade the python module 'python_anticaptcha' via "
|
||||
"pip install python-anticaptcha or https://github.com/ad-m/python-anticaptcha/"
|
||||
)
|
||||
|
||||
import sys
|
||||
|
||||
from . import reCaptcha
|
||||
|
||||
|
||||
class captchaSolver(reCaptcha):
|
||||
|
||||
def __init__(self):
|
||||
if sys.modules['python_anticaptcha'].__version__ < '0.6':
|
||||
raise ImportError(
|
||||
"Please upgrade the python module 'python_anticaptcha' via "
|
||||
"pip install -U python-anticaptcha or https://github.com/ad-m/python-anticaptcha/"
|
||||
)
|
||||
super(captchaSolver, self).__init__('anticaptcha')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def getCaptchaAnswer(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
if not reCaptchaParams.get('api_key'):
|
||||
raise reCaptchaParameter("anticaptcha: Missing api_key parameter.")
|
||||
|
||||
client = AnticaptchaClient(reCaptchaParams.get('api_key'))
|
||||
|
||||
if reCaptchaParams.get('proxy'):
|
||||
client.session.proxies = reCaptchaParams.get('proxies')
|
||||
|
||||
captchaMap = {
|
||||
'reCaptcha': NoCaptchaTaskProxylessTask,
|
||||
'hCaptcha': HCaptchaTaskProxyless
|
||||
}
|
||||
|
||||
task = captchaMap[captchaType](url, siteKey)
|
||||
|
||||
if not hasattr(client, 'createTaskSmee'):
|
||||
raise NotImplementedError(
|
||||
"Please upgrade 'python_anticaptcha' via pip or download it from "
|
||||
"https://github.com/ad-m/python-anticaptcha/tree/hcaptcha"
|
||||
)
|
||||
|
||||
job = client.createTaskSmee(task, timeout=180)
|
||||
|
||||
try:
|
||||
job.join(maximum_time=180)
|
||||
except (AnticaptchaException) as e:
|
||||
raise reCaptchaTimeout('{}'.format(getattr(e, 'message', e)))
|
||||
|
||||
if 'solution' in job._last_result:
|
||||
return job.get_solution_response()
|
||||
else:
|
||||
raise reCaptchaAPIError('Job did not return `solution` key in payload.')
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
captchaSolver()
|
@ -1,233 +0,0 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import json
|
||||
import requests
|
||||
|
||||
try:
|
||||
import polling
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"Please install the python module 'polling' via pip or download it from "
|
||||
"https://github.com/justiniso/polling/"
|
||||
)
|
||||
|
||||
from ..exceptions import (
|
||||
reCaptchaException,
|
||||
reCaptchaServiceUnavailable,
|
||||
reCaptchaAccountError,
|
||||
reCaptchaTimeout,
|
||||
reCaptchaParameter,
|
||||
reCaptchaBadJobID,
|
||||
reCaptchaReportError
|
||||
)
|
||||
|
||||
from . import reCaptcha
|
||||
|
||||
|
||||
class captchaSolver(reCaptcha):
|
||||
|
||||
def __init__(self):
|
||||
super(captchaSolver, self).__init__('deathbycaptcha')
|
||||
self.host = 'http://api.dbcapi.me/api'
|
||||
self.session = requests.Session()
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
@staticmethod
|
||||
def checkErrorStatus(response):
|
||||
errors = dict(
|
||||
[
|
||||
(400, "DeathByCaptcha: 400 Bad Request"),
|
||||
(403, "DeathByCaptcha: 403 Forbidden - Invalid credentails or insufficient credits."),
|
||||
# (500, "DeathByCaptcha: 500 Internal Server Error."),
|
||||
(503, "DeathByCaptcha: 503 Service Temporarily Unavailable.")
|
||||
]
|
||||
)
|
||||
|
||||
if response.status_code in errors:
|
||||
raise reCaptchaServiceUnavailable(errors.get(response.status_code))
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def login(self, username, password):
|
||||
self.username = username
|
||||
self.password = password
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.ok:
|
||||
if response.json().get('is_banned'):
|
||||
raise reCaptchaAccountError('DeathByCaptcha: Your account is banned.')
|
||||
|
||||
if response.json().get('balanace') == 0:
|
||||
raise reCaptchaAccountError('DeathByCaptcha: insufficient credits.')
|
||||
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.post(
|
||||
'{}/user'.format(self.host),
|
||||
headers={'Accept': 'application/json'},
|
||||
data={
|
||||
'username': self.username,
|
||||
'password': self.password
|
||||
}
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=10,
|
||||
timeout=120
|
||||
)
|
||||
|
||||
self.debugRequest(response)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def reportJob(self, jobID):
|
||||
if not jobID:
|
||||
raise reCaptchaBadJobID(
|
||||
"DeathByCaptcha: Error bad job id to report failed reCaptcha."
|
||||
)
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.status_code == 200:
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.post(
|
||||
'{}/captcha/{}/report'.format(self.host, jobID),
|
||||
headers={'Accept': 'application/json'},
|
||||
data={
|
||||
'username': self.username,
|
||||
'password': self.password
|
||||
}
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=10,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return True
|
||||
else:
|
||||
raise reCaptchaReportError(
|
||||
"DeathByCaptcha: Error report failed reCaptcha."
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestJob(self, jobID):
|
||||
if not jobID:
|
||||
raise reCaptchaBadJobID(
|
||||
"DeathByCaptcha: Error bad job id to request reCaptcha."
|
||||
)
|
||||
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get('text'):
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.get(
|
||||
'{}/captcha/{}'.format(self.host, jobID),
|
||||
headers={'Accept': 'application/json'}
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=10,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('text')
|
||||
else:
|
||||
raise reCaptchaTimeout(
|
||||
"DeathByCaptcha: Error failed to solve reCaptcha."
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def requestSolve(self, url, siteKey):
|
||||
def _checkRequest(response):
|
||||
if response.ok and response.json().get("is_correct") and response.json().get('captcha'):
|
||||
return response
|
||||
|
||||
self.checkErrorStatus(response)
|
||||
|
||||
return None
|
||||
|
||||
response = polling.poll(
|
||||
lambda: self.session.post(
|
||||
'{}/captcha'.format(self.host),
|
||||
headers={'Accept': 'application/json'},
|
||||
data={
|
||||
'username': self.username,
|
||||
'password': self.password,
|
||||
'type': '4',
|
||||
'token_params': json.dumps({
|
||||
'googlekey': siteKey,
|
||||
'pageurl': url
|
||||
})
|
||||
},
|
||||
allow_redirects=False
|
||||
),
|
||||
check_success=_checkRequest,
|
||||
step=10,
|
||||
timeout=180
|
||||
)
|
||||
|
||||
if response:
|
||||
return response.json().get('captcha')
|
||||
else:
|
||||
raise reCaptchaBadJobID(
|
||||
'DeathByCaptcha: Error no job id was returned.'
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def getCaptchaAnswer(self, captchaType, url, siteKey, reCaptchaParams):
|
||||
jobID = None
|
||||
|
||||
for param in ['username', 'password']:
|
||||
if not reCaptchaParams.get(param):
|
||||
raise reCaptchaParameter(
|
||||
"DeathByCaptcha: Missing '{}' parameter.".format(param)
|
||||
)
|
||||
setattr(self, param, reCaptchaParams.get(param))
|
||||
|
||||
if captchaType == 'hCaptcha':
|
||||
raise reCaptchaException(
|
||||
'Provider does not support hCaptcha.'
|
||||
)
|
||||
|
||||
if reCaptchaParams.get('proxy'):
|
||||
self.session.proxies = reCaptchaParams.get('proxies')
|
||||
|
||||
try:
|
||||
jobID = self.requestSolve(url, siteKey)
|
||||
return self.requestJob(jobID)
|
||||
except polling.TimeoutException:
|
||||
try:
|
||||
if jobID:
|
||||
self.reportJob(jobID)
|
||||
except polling.TimeoutException:
|
||||
raise reCaptchaTimeout(
|
||||
"DeathByCaptcha: reCaptcha solve took to long and also failed reporting the job id {}.".format(jobID)
|
||||
)
|
||||
|
||||
raise reCaptchaTimeout(
|
||||
"DeathByCaptcha: reCaptcha solve took to long to execute job id {}, aborting.".format(jobID)
|
||||
)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
captchaSolver()
|
@ -1,117 +1,38 @@
|
||||
import json
|
||||
import os
|
||||
import json
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
import ssl
|
||||
import logging
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
##########################################################################################################################################################
|
||||
|
||||
|
||||
class User_Agent():
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
##########################################################################################################################################################
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.headers = None
|
||||
self.cipherSuite = []
|
||||
self.loadUserAgent(*args, **kwargs)
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def loadHeaders(self, user_agents, user_agent_version):
|
||||
if user_agents.get(self.browser).get('releases').get(user_agent_version).get('headers'):
|
||||
self.headers = user_agents.get(self.browser).get('releases').get(user_agent_version).get('headers')
|
||||
else:
|
||||
self.headers = user_agents.get(self.browser).get('default_headers')
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def filterAgents(self, releases):
|
||||
filtered = {}
|
||||
|
||||
for release in releases:
|
||||
if self.mobile and releases[release]['User-Agent']['mobile']:
|
||||
filtered[release] = filtered.get(release, []) + releases[release]['User-Agent']['mobile']
|
||||
|
||||
if self.desktop and releases[release]['User-Agent']['desktop']:
|
||||
filtered[release] = filtered.get(release, []) + releases[release]['User-Agent']['desktop']
|
||||
|
||||
return filtered
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
|
||||
def tryMatchCustom(self, user_agents):
|
||||
for browser in user_agents:
|
||||
for release in user_agents[browser]['releases']:
|
||||
for platform in ['mobile', 'desktop']:
|
||||
if re.search(re.escape(self.custom), ' '.join(user_agents[browser]['releases'][release]['User-Agent'][platform])):
|
||||
self.browser = browser
|
||||
self.loadHeaders(user_agents, release)
|
||||
self.headers['User-Agent'] = self.custom
|
||||
self.cipherSuite = user_agents[self.browser].get('cipherSuite', [])
|
||||
return True
|
||||
return False
|
||||
|
||||
# ------------------------------------------------------------------------------- #
|
||||
##########################################################################################################################################################
|
||||
|
||||
def loadUserAgent(self, *args, **kwargs):
|
||||
self.browser = kwargs.pop('browser', None)
|
||||
|
||||
if isinstance(self.browser, dict):
|
||||
self.custom = self.browser.get('custom', None)
|
||||
self.desktop = self.browser.get('desktop', True)
|
||||
self.mobile = self.browser.get('mobile', True)
|
||||
self.browser = self.browser.get('browser', None)
|
||||
else:
|
||||
self.custom = kwargs.pop('custom', None)
|
||||
self.desktop = kwargs.pop('desktop', True)
|
||||
self.mobile = kwargs.pop('mobile', True)
|
||||
|
||||
if not self.desktop and not self.mobile:
|
||||
sys.tracebacklimit = 0
|
||||
raise RuntimeError("Sorry you can't have mobile and desktop disabled at the same time.")
|
||||
|
||||
with open(os.path.join(os.path.dirname(__file__), 'browsers.json'), 'r') as fp:
|
||||
user_agents = json.load(
|
||||
fp,
|
||||
object_pairs_hook=OrderedDict
|
||||
)
|
||||
|
||||
if self.custom:
|
||||
if not self.tryMatchCustom(user_agents):
|
||||
self.cipherSuite = [
|
||||
ssl._DEFAULT_CIPHERS,
|
||||
'!AES128-SHA',
|
||||
'!ECDHE-RSA-AES256-SHA',
|
||||
]
|
||||
self.headers = OrderedDict([
|
||||
('User-Agent', self.custom),
|
||||
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'),
|
||||
('Accept-Language', 'en-US,en;q=0.9'),
|
||||
('Accept-Encoding', 'gzip, deflate, br')
|
||||
])
|
||||
else:
|
||||
if self.browser and not user_agents.get(self.browser):
|
||||
sys.tracebacklimit = 0
|
||||
raise RuntimeError('Sorry "{}" browser User-Agent was not found.'.format(self.browser))
|
||||
|
||||
if not self.browser:
|
||||
self.browser = random.SystemRandom().choice(list(user_agents))
|
||||
|
||||
self.cipherSuite = user_agents.get(self.browser).get('cipherSuite', [])
|
||||
browser = kwargs.pop('browser', 'chrome')
|
||||
|
||||
filteredAgents = self.filterAgents(user_agents.get(self.browser).get('releases'))
|
||||
with open(os.path.join(os.path.dirname(__file__), 'browsers.json'), 'r') as file:
|
||||
user_agents = json.load(file, object_pairs_hook=OrderedDict)
|
||||
|
||||
user_agent_version = random.SystemRandom().choice(list(filteredAgents))
|
||||
if not user_agents.get(browser):
|
||||
logging.error('Sorry "{}" browser User-Agent was not found.'.format(browser))
|
||||
raise
|
||||
|
||||
self.loadHeaders(user_agents, user_agent_version)
|
||||
user_agent = random.choice(user_agents.get(browser))
|
||||
|
||||
self.headers['User-Agent'] = random.SystemRandom().choice(filteredAgents[user_agent_version])
|
||||
self.headers = user_agent.get('headers')
|
||||
self.headers['User-Agent'] = random.choice(user_agent.get('User-Agent'))
|
||||
|
||||
if not kwargs.get('allow_brotli', False) and 'br' in self.headers['Accept-Encoding']:
|
||||
self.headers['Accept-Encoding'] = ','.join([
|
||||
encoding for encoding in self.headers['Accept-Encoding'].split(',') if encoding.strip() != 'br'
|
||||
]).strip()
|
||||
if not kwargs.get('allow_brotli', False):
|
||||
if 'br' in self.headers['Accept-Encoding']:
|
||||
self.headers['Accept-Encoding'] = ','.join([encoding for encoding in self.headers['Accept-Encoding'].split(',') if encoding.strip() != 'br']).strip()
|
||||
|
@ -0,0 +1,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
from .constants import SUBSYNC_RESOURCES_ENV_MAGIC
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ref: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess
|
||||
# Create a set of arguments which make a ``subprocess.Popen`` (and
|
||||
# variants) call work with or without Pyinstaller, ``--noconsole`` or
|
||||
# not, on Windows and Linux. Typical use::
|
||||
#
|
||||
# subprocess.call(['program_to_run', 'arg_1'], **subprocess_args())
|
||||
#
|
||||
# When calling ``check_output``::
|
||||
#
|
||||
# subprocess.check_output(['program_to_run', 'arg_1'],
|
||||
# **subprocess_args(False))
|
||||
def subprocess_args(include_stdout=True):
|
||||
# The following is true only on Windows.
|
||||
if hasattr(subprocess, 'STARTUPINFO'):
|
||||
# On Windows, subprocess calls will pop up a command window by default
|
||||
# when run from Pyinstaller with the ``--noconsole`` option. Avoid this
|
||||
# distraction.
|
||||
si = subprocess.STARTUPINFO()
|
||||
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||
# Windows doesn't search the path by default. Pass it an environment so
|
||||
# it will.
|
||||
env = os.environ
|
||||
else:
|
||||
si = None
|
||||
env = None
|
||||
|
||||
# ``subprocess.check_output`` doesn't allow specifying ``stdout``::
|
||||
#
|
||||
# Traceback (most recent call last):
|
||||
# File "test_subprocess.py", line 58, in <module>
|
||||
# **subprocess_args(stdout=None))
|
||||
# File "C:\Python27\lib\subprocess.py", line 567, in check_output
|
||||
# raise ValueError('stdout argument not allowed, it will be overridden.')
|
||||
# ValueError: stdout argument not allowed, it will be overridden.
|
||||
#
|
||||
# So, add it only if it's needed.
|
||||
if include_stdout:
|
||||
ret = {'stdout': subprocess.PIPE}
|
||||
else:
|
||||
ret = {}
|
||||
|
||||
# On Windows, running this from the binary produced by Pyinstaller
|
||||
# with the ``--noconsole`` option requires redirecting everything
|
||||
# (stdin, stdout, stderr) to avoid an OSError exception
|
||||
# "[Error 6] the handle is invalid."
|
||||
ret.update({'stdin': subprocess.PIPE,
|
||||
'stderr': subprocess.PIPE,
|
||||
'startupinfo': si,
|
||||
'env': env})
|
||||
return ret
|
||||
|
||||
|
||||
def ffmpeg_bin_path(bin_name, gui_mode, ffmpeg_resources_path=None):
|
||||
if platform.system() == 'Windows':
|
||||
bin_name = '{}.exe'.format(bin_name)
|
||||
if ffmpeg_resources_path is not None:
|
||||
return os.path.join(ffmpeg_resources_path, bin_name)
|
||||
try:
|
||||
resource_path = os.environ[SUBSYNC_RESOURCES_ENV_MAGIC]
|
||||
if len(resource_path) > 0:
|
||||
return os.path.join(resource_path, 'ffmpeg-bin', bin_name)
|
||||
except KeyError:
|
||||
if gui_mode:
|
||||
logger.info("Couldn't find resource path; falling back to searching system path")
|
||||
return bin_name
|
@ -0,0 +1,7 @@
|
||||
# coding=utf-8
|
||||
class EmptyEntryError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class EmptyLineError(Exception):
|
||||
pass
|
@ -0,0 +1,24 @@
|
||||
from .utils import (
|
||||
get_fld,
|
||||
get_tld,
|
||||
get_tld_names,
|
||||
is_tld,
|
||||
parse_tld,
|
||||
Result,
|
||||
update_tld_names,
|
||||
)
|
||||
|
||||
__title__ = 'tld'
|
||||
__version__ = '0.12.2'
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'get_fld',
|
||||
'get_tld',
|
||||
'get_tld_names',
|
||||
'is_tld',
|
||||
'parse_tld',
|
||||
'Result',
|
||||
'update_tld_names',
|
||||
)
|
@ -0,0 +1,68 @@
|
||||
from codecs import open as codecs_open
|
||||
from urllib.request import urlopen
|
||||
from typing import Optional
|
||||
|
||||
from .exceptions import (
|
||||
TldIOError,
|
||||
TldImproperlyConfigured,
|
||||
)
|
||||
from .helpers import project_dir
|
||||
from .registry import Registry
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = ('BaseTLDSourceParser',)
|
||||
|
||||
|
||||
class BaseTLDSourceParser(metaclass=Registry):
|
||||
"""Base TLD source parser."""
|
||||
|
||||
uid: Optional[str] = None
|
||||
source_url: str
|
||||
local_path: str
|
||||
|
||||
@classmethod
|
||||
def validate(cls):
|
||||
"""Constructor."""
|
||||
if not cls.uid:
|
||||
raise TldImproperlyConfigured(
|
||||
"The `uid` property of the TLD source parser shall be defined."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_tld_names(cls, fail_silently: bool = False, retry_count: int = 0):
|
||||
"""Get tld names.
|
||||
|
||||
:param fail_silently:
|
||||
:param retry_count:
|
||||
:return:
|
||||
"""
|
||||
cls.validate()
|
||||
raise NotImplementedError(
|
||||
"Your TLD source parser shall implement `get_tld_names` method."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def update_tld_names(cls, fail_silently: bool = False) -> bool:
|
||||
"""Update the local copy of the TLD file.
|
||||
|
||||
:param fail_silently:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
remote_file = urlopen(cls.source_url)
|
||||
local_file = codecs_open(
|
||||
project_dir(cls.local_path),
|
||||
'wb',
|
||||
encoding='utf8'
|
||||
)
|
||||
local_file.write(remote_file.read().decode('utf8'))
|
||||
local_file.close()
|
||||
remote_file.close()
|
||||
except Exception as err:
|
||||
if fail_silently:
|
||||
return False
|
||||
raise TldIOError(err)
|
||||
|
||||
return True
|
@ -0,0 +1,58 @@
|
||||
from typing import Any
|
||||
from . import defaults
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'get_setting',
|
||||
'reset_settings',
|
||||
'set_setting',
|
||||
'settings',
|
||||
)
|
||||
|
||||
|
||||
class Settings(object):
|
||||
"""Settings registry."""
|
||||
|
||||
def __init__(self):
|
||||
self._settings = {}
|
||||
self._settings_get = self._settings.get
|
||||
|
||||
def set(self, name: str, value: Any) -> None:
|
||||
"""
|
||||
Override default settings.
|
||||
|
||||
:param str name:
|
||||
:param mixed value:
|
||||
"""
|
||||
self._settings[name] = value
|
||||
|
||||
def get(self, name: str, default: Any = None) -> Any:
|
||||
"""
|
||||
Gets a variable from local settings.
|
||||
|
||||
:param str name:
|
||||
:param mixed default: Default value.
|
||||
:return mixed:
|
||||
"""
|
||||
if name in self._settings:
|
||||
return self._settings_get(name, default)
|
||||
elif hasattr(defaults, name):
|
||||
return getattr(defaults, name, default)
|
||||
|
||||
return default
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Reset settings."""
|
||||
for name in defaults.__all__:
|
||||
self.set(name, getattr(defaults, name))
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
get_setting = settings.get
|
||||
|
||||
set_setting = settings.set
|
||||
|
||||
reset_settings = settings.reset
|
@ -0,0 +1,14 @@
|
||||
from os.path import dirname
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'DEBUG',
|
||||
'NAMES_LOCAL_PATH_PARENT',
|
||||
)
|
||||
|
||||
# Absolute base path that is prepended to NAMES_LOCAL_PATH
|
||||
NAMES_LOCAL_PATH_PARENT = dirname(__file__)
|
||||
|
||||
DEBUG = False
|
@ -0,0 +1,56 @@
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'TldBadUrl',
|
||||
'TldDomainNotFound',
|
||||
'TldImproperlyConfigured',
|
||||
'TldIOError',
|
||||
)
|
||||
|
||||
|
||||
class TldIOError(IOError):
|
||||
"""TldIOError.
|
||||
|
||||
Supposed to be thrown when problems with reading/writing occur.
|
||||
"""
|
||||
|
||||
|
||||
class TldDomainNotFound(ValueError):
|
||||
"""TldDomainNotFound.
|
||||
|
||||
Supposed to be thrown when domain name is not found (didn't match) the
|
||||
local TLD policy.
|
||||
"""
|
||||
|
||||
def __init__(self, domain_name):
|
||||
super(TldDomainNotFound, self).__init__(
|
||||
"Domain %s didn't match any existing TLD name!" % domain_name
|
||||
)
|
||||
|
||||
|
||||
class TldBadUrl(ValueError):
|
||||
"""TldBadUrl.
|
||||
|
||||
Supposed to be thrown when bad URL is given.
|
||||
"""
|
||||
|
||||
def __init__(self, url):
|
||||
super(TldBadUrl, self).__init__("Is not a valid URL %s!" % url)
|
||||
|
||||
|
||||
class TldImproperlyConfigured(Exception):
|
||||
"""TldImproperlyConfigured.
|
||||
|
||||
Supposed to be thrown when code is improperly configured. Typical use-case
|
||||
is when user tries to use `get_tld` function with both `search_public` and
|
||||
`search_private` set to False.
|
||||
"""
|
||||
|
||||
def __init__(self, msg=None):
|
||||
if msg is None:
|
||||
msg = "Improperly configured."
|
||||
else:
|
||||
msg = "Improperly configured. %s" % msg
|
||||
|
||||
super(TldImproperlyConfigured, self).__init__(msg)
|
@ -0,0 +1,22 @@
|
||||
from os.path import abspath, join
|
||||
|
||||
from .conf import get_setting
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'project_dir',
|
||||
'PROJECT_DIR',
|
||||
)
|
||||
|
||||
|
||||
def project_dir(base: str) -> str:
|
||||
"""Project dir."""
|
||||
tld_names_local_path_parent = get_setting('NAMES_LOCAL_PATH_PARENT')
|
||||
return abspath(
|
||||
join(tld_names_local_path_parent, base).replace('\\', '/')
|
||||
)
|
||||
|
||||
|
||||
PROJECT_DIR = project_dir
|
@ -0,0 +1,45 @@
|
||||
from typing import Dict
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'Registry',
|
||||
)
|
||||
|
||||
|
||||
class Registry(type):
|
||||
|
||||
REGISTRY = {} # type: Dict[str, Registry]
|
||||
|
||||
def __new__(mcs, name, bases, attrs):
|
||||
new_cls = type.__new__(mcs, name, bases, attrs)
|
||||
# Here the name of the class is used as key but it could be any class
|
||||
# parameter.
|
||||
if getattr(new_cls, '_uid', None):
|
||||
mcs.REGISTRY[new_cls._uid] = new_cls
|
||||
return new_cls
|
||||
|
||||
@property
|
||||
def _uid(cls) -> str:
|
||||
return getattr(cls, 'uid', cls.__name__)
|
||||
|
||||
@classmethod
|
||||
def reset(mcs) -> None:
|
||||
mcs.REGISTRY = {}
|
||||
|
||||
@classmethod
|
||||
def get(mcs, key, default=None):
|
||||
return mcs.REGISTRY.get(key, default)
|
||||
|
||||
@classmethod
|
||||
def items(mcs):
|
||||
return mcs.REGISTRY.items()
|
||||
|
||||
# @classmethod
|
||||
# def get_registry(mcs) -> Dict[str, Type]:
|
||||
# return dict(mcs.REGISTRY)
|
||||
#
|
||||
# @classmethod
|
||||
# def pop(mcs, uid) -> None:
|
||||
# mcs.REGISTRY.pop(uid)
|
@ -0,0 +1,67 @@
|
||||
from typing import Any, Dict
|
||||
from urllib.parse import SplitResult
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'Result',
|
||||
)
|
||||
|
||||
|
||||
class Result(object):
|
||||
"""Container."""
|
||||
|
||||
__slots__ = ('subdomain', 'domain', 'tld', '__fld', 'parsed_url')
|
||||
|
||||
def __init__(self,
|
||||
tld: str,
|
||||
domain: str,
|
||||
subdomain: str,
|
||||
parsed_url: SplitResult):
|
||||
self.tld = tld
|
||||
self.domain = domain if domain != '' else tld
|
||||
self.subdomain = subdomain
|
||||
self.parsed_url = parsed_url
|
||||
|
||||
if domain:
|
||||
self.__fld = f"{self.domain}.{self.tld}"
|
||||
else:
|
||||
self.__fld = self.tld
|
||||
|
||||
@property
|
||||
def extension(self) -> str:
|
||||
"""Alias of ``tld``.
|
||||
|
||||
:return str:
|
||||
"""
|
||||
return self.tld
|
||||
suffix = extension
|
||||
|
||||
@property
|
||||
def fld(self) -> str:
|
||||
"""First level domain.
|
||||
|
||||
:return:
|
||||
:rtype: str
|
||||
"""
|
||||
return self.__fld
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.tld
|
||||
__repr__ = __str__
|
||||
|
||||
@property
|
||||
def __dict__(self) -> Dict[str, Any]: # type: ignore
|
||||
"""Mimic __dict__ functionality.
|
||||
|
||||
:return:
|
||||
:rtype: dict
|
||||
"""
|
||||
return {
|
||||
'tld': self.tld,
|
||||
'domain': self.domain,
|
||||
'subdomain': self.subdomain,
|
||||
'fld': self.fld,
|
||||
'parsed_url': self.parsed_url,
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import unittest
|
||||
|
||||
from .test_core import *
|
||||
from .test_commands import *
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,75 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from functools import lru_cache
|
||||
import logging
|
||||
import socket
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'internet_available_only',
|
||||
'log_info',
|
||||
)
|
||||
|
||||
LOG_INFO = True
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def log_info(func):
|
||||
"""Log some useful info."""
|
||||
if not LOG_INFO:
|
||||
return func
|
||||
|
||||
def inner(self, *args, **kwargs):
|
||||
"""Inner."""
|
||||
result = func(self, *args, **kwargs)
|
||||
|
||||
LOGGER.debug('\n\n%s', func.__name__)
|
||||
LOGGER.debug('============================')
|
||||
if func.__doc__:
|
||||
LOGGER.debug('""" %s """', func.__doc__.strip())
|
||||
LOGGER.debug('----------------------------')
|
||||
if result is not None:
|
||||
LOGGER.debug(result)
|
||||
LOGGER.debug('\n++++++++++++++++++++++++++++')
|
||||
|
||||
return result
|
||||
return inner
|
||||
|
||||
|
||||
@lru_cache(maxsize=32)
|
||||
def is_internet_available(host="8.8.8.8", port=53, timeout=3):
|
||||
"""Check if internet is available.
|
||||
|
||||
Host: 8.8.8.8 (google-public-dns-a.google.com)
|
||||
OpenPort: 53/tcp
|
||||
Service: domain (DNS/TCP)
|
||||
"""
|
||||
try:
|
||||
socket.setdefaulttimeout(timeout)
|
||||
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
|
||||
return True
|
||||
except socket.error as ex:
|
||||
print(ex)
|
||||
return False
|
||||
|
||||
|
||||
def internet_available_only(func):
|
||||
|
||||
def inner(self, *args, **kwargs):
|
||||
"""Inner."""
|
||||
if not is_internet_available():
|
||||
LOGGER.debug('\n\n%s', func.__name__)
|
||||
LOGGER.debug('============================')
|
||||
if func.__doc__:
|
||||
LOGGER.debug('""" %s """', func.__doc__.strip())
|
||||
LOGGER.debug('----------------------------')
|
||||
LOGGER.debug("Skipping because no Internet connection available.")
|
||||
LOGGER.debug('\n++++++++++++++++++++++++++++')
|
||||
return None
|
||||
|
||||
result = func(self, *args, **kwargs)
|
||||
return result
|
||||
|
||||
return inner
|
@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
import subprocess
|
||||
|
||||
from .base import log_info, internet_available_only
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('TestCommands',)
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestCommands(unittest.TestCase):
|
||||
"""Tld commands tests."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up."""
|
||||
|
||||
@internet_available_only
|
||||
@log_info
|
||||
def test_1_update_tld_names_command(self):
|
||||
"""Test updating the tld names (re-fetch mozilla source)."""
|
||||
res = subprocess.check_output(['update-tld-names']).strip()
|
||||
self.assertEqual(res, b'')
|
||||
return res
|
||||
|
||||
@internet_available_only
|
||||
@log_info
|
||||
def test_1_update_tld_names_mozilla_command(self):
|
||||
"""Test updating the tld names (re-fetch mozilla source)."""
|
||||
res = subprocess.check_output(['update-tld-names', 'mozilla']).strip()
|
||||
self.assertEqual(res, b'')
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,842 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import copy
|
||||
import logging
|
||||
from os.path import abspath, join
|
||||
import unittest
|
||||
from tempfile import gettempdir
|
||||
from typing import Type
|
||||
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from faker import Faker # type: ignore
|
||||
|
||||
from .. import defaults
|
||||
from ..base import BaseTLDSourceParser
|
||||
from ..conf import get_setting, reset_settings, set_setting
|
||||
from ..exceptions import (
|
||||
TldBadUrl,
|
||||
TldDomainNotFound,
|
||||
TldImproperlyConfigured,
|
||||
TldIOError,
|
||||
)
|
||||
from ..helpers import project_dir
|
||||
from ..registry import Registry
|
||||
from ..utils import (
|
||||
get_fld,
|
||||
get_tld,
|
||||
get_tld_names,
|
||||
get_tld_names_container,
|
||||
is_tld,
|
||||
MozillaTLDSourceParser,
|
||||
BaseMozillaTLDSourceParser,
|
||||
parse_tld,
|
||||
reset_tld_names,
|
||||
update_tld_names,
|
||||
update_tld_names_cli,
|
||||
)
|
||||
|
||||
from .base import internet_available_only, log_info
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = ('TestCore',)
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestCore(unittest.TestCase):
|
||||
"""Core tld functionality tests."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.faker = Faker()
|
||||
cls.temp_dir = gettempdir()
|
||||
|
||||
def setUp(self):
|
||||
"""Set up."""
|
||||
self.good_patterns = [
|
||||
{
|
||||
'url': 'http://www.google.co.uk',
|
||||
'fld': 'google.co.uk',
|
||||
'subdomain': 'www',
|
||||
'domain': 'google',
|
||||
'suffix': 'co.uk',
|
||||
'tld': 'co.uk',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://www.v2.google.co.uk',
|
||||
'fld': 'google.co.uk',
|
||||
'subdomain': 'www.v2',
|
||||
'domain': 'google',
|
||||
'suffix': 'co.uk',
|
||||
'tld': 'co.uk',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
# No longer valid
|
||||
# {
|
||||
# 'url': 'http://www.me.congresodelalengua3.ar',
|
||||
# 'tld': 'me.congresodelalengua3.ar',
|
||||
# 'subdomain': 'www',
|
||||
# 'domain': 'me',
|
||||
# 'suffix': 'congresodelalengua3.ar',
|
||||
# },
|
||||
{
|
||||
'url': u'http://хром.гугл.рф',
|
||||
'fld': u'гугл.рф',
|
||||
'subdomain': u'хром',
|
||||
'domain': u'гугл',
|
||||
'suffix': u'рф',
|
||||
'tld': u'рф',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://www.google.co.uk:8001/lorem-ipsum/',
|
||||
'fld': 'google.co.uk',
|
||||
'subdomain': 'www',
|
||||
'domain': 'google',
|
||||
'suffix': 'co.uk',
|
||||
'tld': 'co.uk',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://www.me.cloudfront.net',
|
||||
'fld': 'me.cloudfront.net',
|
||||
'subdomain': 'www',
|
||||
'domain': 'me',
|
||||
'suffix': 'cloudfront.net',
|
||||
'tld': 'cloudfront.net',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://www.v2.forum.tech.google.co.uk:8001/'
|
||||
'lorem-ipsum/',
|
||||
'fld': 'google.co.uk',
|
||||
'subdomain': 'www.v2.forum.tech',
|
||||
'domain': 'google',
|
||||
'suffix': 'co.uk',
|
||||
'tld': 'co.uk',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'https://pantheon.io/',
|
||||
'fld': 'pantheon.io',
|
||||
'subdomain': '',
|
||||
'domain': 'pantheon',
|
||||
'suffix': 'io',
|
||||
'tld': 'io',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'v2.www.google.com',
|
||||
'fld': 'google.com',
|
||||
'subdomain': 'v2.www',
|
||||
'domain': 'google',
|
||||
'suffix': 'com',
|
||||
'tld': 'com',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': '//v2.www.google.com',
|
||||
'fld': 'google.com',
|
||||
'subdomain': 'v2.www',
|
||||
'domain': 'google',
|
||||
'suffix': 'com',
|
||||
'tld': 'com',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://foo@bar.com',
|
||||
'fld': 'bar.com',
|
||||
'subdomain': '',
|
||||
'domain': 'bar',
|
||||
'suffix': 'com',
|
||||
'tld': 'com',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://user:foo@bar.com',
|
||||
'fld': 'bar.com',
|
||||
'subdomain': '',
|
||||
'domain': 'bar',
|
||||
'suffix': 'com',
|
||||
'tld': 'com',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'https://faguoren.xn--fiqs8s',
|
||||
'fld': 'faguoren.xn--fiqs8s',
|
||||
'subdomain': '',
|
||||
'domain': 'faguoren',
|
||||
'suffix': 'xn--fiqs8s',
|
||||
'tld': 'xn--fiqs8s',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'blogs.lemonde.paris',
|
||||
'fld': 'lemonde.paris',
|
||||
'subdomain': 'blogs',
|
||||
'domain': 'lemonde',
|
||||
'suffix': 'paris',
|
||||
'tld': 'paris',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': 'axel.brighton.ac.uk',
|
||||
'fld': 'brighton.ac.uk',
|
||||
'subdomain': 'axel',
|
||||
'domain': 'brighton',
|
||||
'suffix': 'ac.uk',
|
||||
'tld': 'ac.uk',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': 'm.fr.blogspot.com.au',
|
||||
'fld': 'fr.blogspot.com.au',
|
||||
'subdomain': 'm',
|
||||
'domain': 'fr',
|
||||
'suffix': 'blogspot.com.au',
|
||||
'tld': 'blogspot.com.au',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': u'help.www.福岡.jp',
|
||||
'fld': u'www.福岡.jp',
|
||||
'subdomain': 'help',
|
||||
'domain': 'www',
|
||||
'suffix': u'福岡.jp',
|
||||
'tld': u'福岡.jp',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': u'syria.arabic.variant.سوريا',
|
||||
'fld': u'variant.سوريا',
|
||||
'subdomain': 'syria.arabic',
|
||||
'domain': 'variant',
|
||||
'suffix': u'سوريا',
|
||||
'tld': u'سوريا',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True},
|
||||
},
|
||||
{
|
||||
'url': u'http://www.help.kawasaki.jp',
|
||||
'fld': u'www.help.kawasaki.jp',
|
||||
'subdomain': '',
|
||||
'domain': 'www',
|
||||
'suffix': u'help.kawasaki.jp',
|
||||
'tld': u'help.kawasaki.jp',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': u'http://www.city.kawasaki.jp',
|
||||
'fld': u'city.kawasaki.jp',
|
||||
'subdomain': 'www',
|
||||
'domain': 'city',
|
||||
'suffix': u'kawasaki.jp',
|
||||
'tld': u'kawasaki.jp',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': u'http://fedoraproject.org',
|
||||
'fld': u'fedoraproject.org',
|
||||
'subdomain': '',
|
||||
'domain': 'fedoraproject',
|
||||
'suffix': u'org',
|
||||
'tld': u'org',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': u'http://www.cloud.fedoraproject.org',
|
||||
'fld': u'www.cloud.fedoraproject.org',
|
||||
'subdomain': '',
|
||||
'domain': 'www',
|
||||
'suffix': u'cloud.fedoraproject.org',
|
||||
'tld': u'cloud.fedoraproject.org',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': u'https://www.john.app.os.fedoraproject.org',
|
||||
'fld': u'john.app.os.fedoraproject.org',
|
||||
'subdomain': 'www',
|
||||
'domain': 'john',
|
||||
'suffix': u'app.os.fedoraproject.org',
|
||||
'tld': u'app.os.fedoraproject.org',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'ftp://www.xn--mxail5aa.xn--11b4c3d',
|
||||
'fld': 'xn--mxail5aa.xn--11b4c3d',
|
||||
'subdomain': 'www',
|
||||
'domain': 'xn--mxail5aa',
|
||||
'suffix': 'xn--11b4c3d',
|
||||
'tld': 'xn--11b4c3d',
|
||||
'kwargs': {'fail_silently': True},
|
||||
},
|
||||
{
|
||||
'url': 'http://cloud.fedoraproject.org',
|
||||
'fld': 'cloud.fedoraproject.org',
|
||||
'subdomain': '',
|
||||
'domain': 'cloud.fedoraproject.org',
|
||||
'suffix': 'cloud.fedoraproject.org',
|
||||
'tld': 'cloud.fedoraproject.org',
|
||||
'kwargs': {'fail_silently': True}
|
||||
},
|
||||
{
|
||||
'url': 'github.io',
|
||||
'fld': 'github.io',
|
||||
'subdomain': '',
|
||||
'domain': 'github.io',
|
||||
'suffix': 'github.io',
|
||||
'tld': 'github.io',
|
||||
'kwargs': {'fail_silently': True, 'fix_protocol': True}
|
||||
},
|
||||
{
|
||||
'url': urlsplit('http://lemonde.fr/article.html'),
|
||||
'fld': 'lemonde.fr',
|
||||
'subdomain': '',
|
||||
'domain': 'lemonde',
|
||||
'suffix': 'fr',
|
||||
'tld': 'fr',
|
||||
'kwargs': {'fail_silently': True}
|
||||
},
|
||||
{
|
||||
'url': 'https://github.com....../barseghyanartur/tld/',
|
||||
'fld': 'github.com',
|
||||
'subdomain': '',
|
||||
'domain': 'github',
|
||||
'suffix': 'com',
|
||||
'tld': 'com',
|
||||
'kwargs': {'fail_silently': True}
|
||||
},
|
||||
]
|
||||
|
||||
self.bad_patterns = {
|
||||
'v2.www.google.com': {
|
||||
'exception': TldBadUrl,
|
||||
},
|
||||
'/index.php?a=1&b=2': {
|
||||
'exception': TldBadUrl,
|
||||
},
|
||||
'http://www.tld.doesnotexist': {
|
||||
'exception': TldDomainNotFound,
|
||||
},
|
||||
'https://2001:0db8:0000:85a3:0000:0000:ac1f:8001': {
|
||||
'exception': TldDomainNotFound,
|
||||
},
|
||||
'http://192.169.1.1': {
|
||||
'exception': TldDomainNotFound,
|
||||
},
|
||||
'http://localhost:8080': {
|
||||
'exception': TldDomainNotFound,
|
||||
},
|
||||
'https://localhost': {
|
||||
'exception': TldDomainNotFound,
|
||||
},
|
||||
'https://localhost2': {
|
||||
'exception': TldImproperlyConfigured,
|
||||
'kwargs': {'search_public': False, 'search_private': False},
|
||||
},
|
||||
}
|
||||
|
||||
self.invalid_tlds = {
|
||||
'v2.www.google.com',
|
||||
'tld.doesnotexist',
|
||||
'2001:0db8:0000:85a3:0000:0000:ac1f',
|
||||
'192.169.1.1',
|
||||
'localhost',
|
||||
'google.com',
|
||||
}
|
||||
|
||||
self.tld_names_local_path_custom = project_dir(
|
||||
join(
|
||||
'tests',
|
||||
'res',
|
||||
'effective_tld_names_custom.dat.txt'
|
||||
)
|
||||
)
|
||||
self.good_patterns_custom_parser = [
|
||||
{
|
||||
'url': 'http://www.foreverchild',
|
||||
'fld': 'www.foreverchild',
|
||||
'subdomain': '',
|
||||
'domain': 'www',
|
||||
'suffix': 'foreverchild',
|
||||
'tld': 'foreverchild',
|
||||
'kwargs': {
|
||||
'fail_silently': True,
|
||||
# 'parser_class': self.get_custom_parser_class(),
|
||||
},
|
||||
},
|
||||
{
|
||||
'url': 'http://www.v2.foreverchild',
|
||||
'fld': 'v2.foreverchild',
|
||||
'subdomain': 'www',
|
||||
'domain': 'v2',
|
||||
'suffix': 'foreverchild',
|
||||
'tld': 'foreverchild',
|
||||
'kwargs': {
|
||||
'fail_silently': True,
|
||||
# 'parser_class': self.get_custom_parser_class(),
|
||||
},
|
||||
},
|
||||
]
|
||||
reset_settings()
|
||||
|
||||
def tearDown(self):
|
||||
"""Tear down."""
|
||||
reset_settings()
|
||||
Registry.reset()
|
||||
|
||||
@property
|
||||
def good_url(self):
|
||||
return self.good_patterns[0]['url']
|
||||
|
||||
@property
|
||||
def bad_url(self):
|
||||
return list(self.bad_patterns.keys())[0]
|
||||
|
||||
def get_custom_parser_class(
|
||||
self,
|
||||
uid: str = 'custom_mozilla',
|
||||
source_url: str = None,
|
||||
local_path: str = 'tests/res/effective_tld_names_custom.dat.txt'
|
||||
) -> Type[BaseTLDSourceParser]:
|
||||
# Define a custom TLD source parser class
|
||||
parser_class = type(
|
||||
'CustomMozillaTLDSourceParser',
|
||||
(BaseMozillaTLDSourceParser,),
|
||||
{
|
||||
'uid': uid,
|
||||
'source_url': source_url,
|
||||
'local_path': local_path,
|
||||
}
|
||||
)
|
||||
return parser_class
|
||||
|
||||
@log_info
|
||||
def test_0_tld_names_loaded(self):
|
||||
"""Test if tld names are loaded."""
|
||||
get_fld('http://www.google.co.uk')
|
||||
from ..utils import tld_names
|
||||
res = len(tld_names) > 0
|
||||
self.assertTrue(res)
|
||||
return res
|
||||
|
||||
@internet_available_only
|
||||
@log_info
|
||||
def test_1_update_tld_names(self):
|
||||
"""Test updating the tld names (re-fetch mozilla source)."""
|
||||
res = update_tld_names(fail_silently=False)
|
||||
self.assertTrue(res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_2_fld_good_patterns_pass(self):
|
||||
"""Test good URL patterns."""
|
||||
res = []
|
||||
for data in self.good_patterns:
|
||||
_res = get_fld(data['url'], **data['kwargs'])
|
||||
self.assertEqual(_res, data['fld'])
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_3_fld_bad_patterns_pass(self):
|
||||
"""Test bad URL patterns."""
|
||||
res = []
|
||||
for url, params in self.bad_patterns.items():
|
||||
_res = get_fld(url, fail_silently=True)
|
||||
self.assertEqual(_res, None)
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_4_override_settings(self):
|
||||
"""Testing settings override."""
|
||||
def override_settings():
|
||||
"""Override settings."""
|
||||
return get_setting('DEBUG')
|
||||
|
||||
self.assertEqual(defaults.DEBUG, override_settings())
|
||||
|
||||
set_setting('DEBUG', True)
|
||||
|
||||
self.assertEqual(True, override_settings())
|
||||
|
||||
return override_settings()
|
||||
|
||||
@log_info
|
||||
def test_5_tld_good_patterns_pass_parsed_object(self):
|
||||
"""Test good URL patterns."""
|
||||
res = []
|
||||
for data in self.good_patterns:
|
||||
kwargs = copy.copy(data['kwargs'])
|
||||
kwargs['as_object'] = True
|
||||
_res = get_tld(data['url'], **kwargs)
|
||||
self.assertEqual(_res.tld, data['tld'])
|
||||
self.assertEqual(_res.subdomain, data['subdomain'])
|
||||
self.assertEqual(_res.domain, data['domain'])
|
||||
self.assertEqual(_res.suffix, data['suffix'])
|
||||
self.assertEqual(_res.fld, data['fld'])
|
||||
|
||||
self.assertEqual(
|
||||
str(_res).encode('utf8'),
|
||||
data['tld'].encode('utf8')
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
_res.__dict__,
|
||||
{
|
||||
'tld': _res.tld,
|
||||
'domain': _res.domain,
|
||||
'subdomain': _res.subdomain,
|
||||
'fld': _res.fld,
|
||||
'parsed_url': _res.parsed_url,
|
||||
}
|
||||
)
|
||||
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_6_override_full_names_path(self):
|
||||
default = project_dir('dummy.txt')
|
||||
override_base = '/tmp/test'
|
||||
set_setting('NAMES_LOCAL_PATH_PARENT', override_base)
|
||||
modified = project_dir('dummy.txt')
|
||||
self.assertNotEqual(default, modified)
|
||||
self.assertEqual(modified, abspath('/tmp/test/dummy.txt'))
|
||||
|
||||
@log_info
|
||||
def test_7_public_private(self):
|
||||
res = get_fld(
|
||||
'http://silly.cc.ua',
|
||||
fail_silently=True,
|
||||
search_private=False
|
||||
)
|
||||
|
||||
self.assertEqual(res, None)
|
||||
|
||||
res = get_fld(
|
||||
'http://silly.cc.ua',
|
||||
fail_silently=True,
|
||||
search_private=True
|
||||
)
|
||||
|
||||
self.assertEqual(res, 'silly.cc.ua')
|
||||
|
||||
res = get_fld(
|
||||
'mercy.compute.amazonaws.com',
|
||||
fail_silently=True,
|
||||
search_private=False,
|
||||
fix_protocol=True
|
||||
)
|
||||
|
||||
self.assertEqual(res, None)
|
||||
|
||||
res = get_fld(
|
||||
'http://whatever.com',
|
||||
fail_silently=True,
|
||||
search_public=False
|
||||
)
|
||||
|
||||
self.assertEqual(res, None)
|
||||
|
||||
@log_info
|
||||
def test_8_fld_bad_patterns_exceptions(self):
|
||||
"""Test exceptions."""
|
||||
res = []
|
||||
for url, params in self.bad_patterns.items():
|
||||
kwargs = params['kwargs'] if 'kwargs' in params else {}
|
||||
kwargs['fail_silently'] = False
|
||||
with self.assertRaises(params['exception']):
|
||||
_res = get_fld(url, **kwargs)
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_9_tld_good_patterns_pass(self):
|
||||
"""Test `get_tld` good URL patterns."""
|
||||
res = []
|
||||
for data in self.good_patterns:
|
||||
_res = get_tld(data['url'], **data['kwargs'])
|
||||
self.assertEqual(_res, data['tld'])
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_10_tld_bad_patterns_pass(self):
|
||||
"""Test `get_tld` bad URL patterns."""
|
||||
res = []
|
||||
for url, params in self.bad_patterns.items():
|
||||
_res = get_tld(url, fail_silently=True)
|
||||
self.assertEqual(_res, None)
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_11_parse_tld_good_patterns(self):
|
||||
"""Test `parse_tld` good URL patterns."""
|
||||
res = []
|
||||
for data in self.good_patterns:
|
||||
_res = parse_tld(data['url'], **data['kwargs'])
|
||||
self.assertEqual(
|
||||
_res,
|
||||
(data['tld'], data['domain'], data['subdomain'])
|
||||
)
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_12_is_tld_good_patterns(self):
|
||||
"""Test `is_tld` good URL patterns."""
|
||||
for data in self.good_patterns:
|
||||
self.assertTrue(is_tld(data['tld']))
|
||||
|
||||
@log_info
|
||||
def test_13_is_tld_bad_patterns(self):
|
||||
"""Test `is_tld` bad URL patterns."""
|
||||
for _tld in self.invalid_tlds:
|
||||
self.assertFalse(is_tld(_tld))
|
||||
|
||||
@log_info
|
||||
def test_14_fail_update_tld_names(self):
|
||||
"""Test fail `update_tld_names`."""
|
||||
parser_class = self.get_custom_parser_class(
|
||||
uid='custom_mozilla_2',
|
||||
source_url='i-do-not-exist'
|
||||
)
|
||||
# Assert raise TldIOError on wrong NAMES_SOURCE_URL
|
||||
with self.assertRaises(TldIOError):
|
||||
update_tld_names(fail_silently=False, parser_uid=parser_class.uid)
|
||||
|
||||
# Assert return False on wrong NAMES_SOURCE_URL
|
||||
self.assertFalse(
|
||||
update_tld_names(fail_silently=True, parser_uid=parser_class.uid)
|
||||
)
|
||||
|
||||
@log_info
|
||||
def test_15_fail_get_tld_names(self):
|
||||
"""Test fail `update_tld_names`."""
|
||||
parser_class = self.get_custom_parser_class(
|
||||
uid='custom_mozilla_3',
|
||||
source_url='i-do-not-exist',
|
||||
local_path='/srv/tests/res/effective_tld_names_custom_3.dat.txt'
|
||||
)
|
||||
reset_tld_names()
|
||||
# Assert raise TldIOError on wrong NAMES_SOURCE_URL
|
||||
for params in self.good_patterns:
|
||||
kwargs = {'url': params['url']}
|
||||
kwargs.update(params['kwargs'])
|
||||
kwargs['fail_silently'] = False
|
||||
kwargs['parser_class'] = parser_class
|
||||
with self.assertRaises(TldIOError):
|
||||
get_tld(**kwargs)
|
||||
|
||||
@log_info
|
||||
def test_15_fail_get_fld_wrong_kwargs(self):
|
||||
"""Test fail `get_fld` with wrong kwargs."""
|
||||
with self.assertRaises(TldImproperlyConfigured):
|
||||
get_fld(self.good_url, as_object=True)
|
||||
|
||||
@log_info
|
||||
def test_16_fail_parse_tld(self):
|
||||
"""Test fail `parse_tld`.
|
||||
|
||||
Assert raise TldIOError on wrong `NAMES_SOURCE_URL` for `parse_tld`.
|
||||
"""
|
||||
parser_class = self.get_custom_parser_class(
|
||||
source_url='i-do-not-exist'
|
||||
)
|
||||
parsed_tld = parse_tld(
|
||||
self.bad_url,
|
||||
fail_silently=False,
|
||||
parser_class=parser_class
|
||||
)
|
||||
self.assertEqual(parsed_tld, (None, None, None))
|
||||
|
||||
@log_info
|
||||
def test_17_get_tld_names_and_reset_tld_names(self):
|
||||
"""Test fail `get_tld_names` and repair using `reset_tld_names`."""
|
||||
tmp_filename = join(
|
||||
gettempdir(),
|
||||
f'{self.faker.uuid4()}.dat.txt'
|
||||
)
|
||||
parser_class = self.get_custom_parser_class(
|
||||
source_url='i-do-not-exist',
|
||||
local_path=tmp_filename
|
||||
)
|
||||
reset_tld_names()
|
||||
|
||||
with self.subTest('Assert raise TldIOError'):
|
||||
# Assert raise TldIOError on wrong NAMES_SOURCE_URL for
|
||||
# `get_tld_names`
|
||||
with self.assertRaises(TldIOError):
|
||||
get_tld_names(
|
||||
fail_silently=False,
|
||||
parser_class=parser_class
|
||||
)
|
||||
|
||||
tmp_filename = join(
|
||||
gettempdir(),
|
||||
f'{self.faker.uuid4()}.dat.txt'
|
||||
)
|
||||
parser_class_2 = self.get_custom_parser_class(
|
||||
source_url='i-do-not-exist-2',
|
||||
local_path=tmp_filename
|
||||
)
|
||||
reset_tld_names()
|
||||
|
||||
with self.subTest('Assert get None'):
|
||||
# Assert get None on wrong `NAMES_SOURCE_URL` for `get_tld_names`
|
||||
self.assertIsNone(
|
||||
get_tld_names(
|
||||
fail_silently=True,
|
||||
parser_class=parser_class_2
|
||||
)
|
||||
)
|
||||
|
||||
@internet_available_only
|
||||
@log_info
|
||||
def test_18_update_tld_names_cli(self):
|
||||
"""Test the return code of the CLI version of `update_tld_names`."""
|
||||
reset_tld_names()
|
||||
res = update_tld_names_cli()
|
||||
self.assertEqual(res, 0)
|
||||
|
||||
@log_info
|
||||
def test_19_parse_tld_custom_tld_names_good_patterns(self):
|
||||
"""Test `parse_tld` good URL patterns for custom tld names."""
|
||||
res = []
|
||||
|
||||
for data in self.good_patterns_custom_parser:
|
||||
kwargs = copy.copy(data['kwargs'])
|
||||
kwargs['parser_class'] = self.get_custom_parser_class()
|
||||
_res = parse_tld(data['url'], **kwargs)
|
||||
self.assertEqual(
|
||||
_res,
|
||||
(data['tld'], data['domain'], data['subdomain'])
|
||||
)
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_20_tld_custom_tld_names_good_patterns_pass_parsed_object(self):
|
||||
"""Test `get_tld` good URL patterns for custom tld names."""
|
||||
res = []
|
||||
for data in self.good_patterns_custom_parser:
|
||||
kwargs = copy.copy(data['kwargs'])
|
||||
kwargs.update({
|
||||
'as_object': True,
|
||||
'parser_class': self.get_custom_parser_class(),
|
||||
})
|
||||
_res = get_tld(data['url'], **kwargs)
|
||||
self.assertEqual(_res.tld, data['tld'])
|
||||
self.assertEqual(_res.subdomain, data['subdomain'])
|
||||
self.assertEqual(_res.domain, data['domain'])
|
||||
self.assertEqual(_res.suffix, data['suffix'])
|
||||
self.assertEqual(_res.fld, data['fld'])
|
||||
|
||||
self.assertEqual(
|
||||
str(_res).encode('utf8'),
|
||||
data['tld'].encode('utf8')
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
_res.__dict__,
|
||||
{
|
||||
'tld': _res.tld,
|
||||
'domain': _res.domain,
|
||||
'subdomain': _res.subdomain,
|
||||
'fld': _res.fld,
|
||||
'parsed_url': _res.parsed_url,
|
||||
}
|
||||
)
|
||||
|
||||
res.append(_res)
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_21_reset_tld_names_for_custom_parser(self):
|
||||
"""Test `reset_tld_names` for `tld_names_local_path`."""
|
||||
res = []
|
||||
parser_class = self.get_custom_parser_class()
|
||||
for data in self.good_patterns_custom_parser:
|
||||
kwargs = copy.copy(data['kwargs'])
|
||||
kwargs.update({
|
||||
'as_object': True,
|
||||
'parser_class': self.get_custom_parser_class(),
|
||||
})
|
||||
_res = get_tld(data['url'], **kwargs)
|
||||
self.assertEqual(_res.tld, data['tld'])
|
||||
self.assertEqual(_res.subdomain, data['subdomain'])
|
||||
self.assertEqual(_res.domain, data['domain'])
|
||||
self.assertEqual(_res.suffix, data['suffix'])
|
||||
self.assertEqual(_res.fld, data['fld'])
|
||||
|
||||
self.assertEqual(
|
||||
str(_res).encode('utf8'),
|
||||
data['tld'].encode('utf8')
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
_res.__dict__,
|
||||
{
|
||||
'tld': _res.tld,
|
||||
'domain': _res.domain,
|
||||
'subdomain': _res.subdomain,
|
||||
'fld': _res.fld,
|
||||
'parsed_url': _res.parsed_url,
|
||||
}
|
||||
)
|
||||
|
||||
res.append(_res)
|
||||
|
||||
tld_names = get_tld_names_container()
|
||||
self.assertIn(parser_class.local_path, tld_names)
|
||||
reset_tld_names(parser_class.local_path)
|
||||
self.assertNotIn(parser_class.local_path, tld_names)
|
||||
|
||||
return res
|
||||
|
||||
@log_info
|
||||
def test_22_fail_define_custom_parser_class_without_uid(self):
|
||||
"""Test fail define custom parser class without `uid`."""
|
||||
class CustomParser(BaseTLDSourceParser):
|
||||
pass
|
||||
|
||||
class AnotherCustomParser(BaseTLDSourceParser):
|
||||
|
||||
uid = 'another-custom-parser'
|
||||
|
||||
# Assert raise TldImproperlyConfigured
|
||||
with self.assertRaises(TldImproperlyConfigured):
|
||||
CustomParser.get_tld_names()
|
||||
|
||||
# Assert raise NotImplementedError
|
||||
with self.assertRaises(NotImplementedError):
|
||||
AnotherCustomParser.get_tld_names()
|
||||
|
||||
@log_info
|
||||
def test_23_len_trie_nodes(self):
|
||||
"""Test len of the trie nodes."""
|
||||
get_tld('http://delusionalinsanity.com')
|
||||
tld_names = get_tld_names_container()
|
||||
self.assertGreater(
|
||||
len(tld_names[MozillaTLDSourceParser.local_path]),
|
||||
0
|
||||
)
|
||||
|
||||
@log_info
|
||||
def test_24_get_tld_names_no_arguments(self):
|
||||
"""Test len of the trie nodes."""
|
||||
tld_names = get_tld_names()
|
||||
self.assertGreater(
|
||||
len(tld_names),
|
||||
0
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,64 @@
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'Trie',
|
||||
'TrieNode',
|
||||
)
|
||||
|
||||
|
||||
class TrieNode(object):
|
||||
"""Class representing a single Trie node."""
|
||||
|
||||
__slots__ = ('children', 'exception', 'leaf', 'private')
|
||||
|
||||
def __init__(self):
|
||||
self.children = None
|
||||
self.exception = None
|
||||
self.leaf = False
|
||||
self.private = False
|
||||
|
||||
|
||||
class Trie(object):
|
||||
"""An adhoc Trie data structure to store tlds in reverse notation order."""
|
||||
|
||||
def __init__(self):
|
||||
self.root = TrieNode()
|
||||
self.__nodes = 0
|
||||
|
||||
def __len__(self):
|
||||
return self.__nodes
|
||||
|
||||
def add(self, tld: str, private: bool = False) -> None:
|
||||
node = self.root
|
||||
|
||||
# Iterating over the tld parts in reverse order
|
||||
# for part in reversed(tld.split('.')):
|
||||
tld_split = tld.split('.')
|
||||
tld_split.reverse()
|
||||
for part in tld_split:
|
||||
|
||||
if part.startswith('!'):
|
||||
node.exception = part[1:]
|
||||
break
|
||||
|
||||
# To save up some RAM, we initialize the children dict only
|
||||
# when strictly necessary
|
||||
if node.children is None:
|
||||
node.children = {}
|
||||
child = TrieNode()
|
||||
else:
|
||||
child = node.children.get(part)
|
||||
if child is None:
|
||||
child = TrieNode()
|
||||
|
||||
node.children[part] = child
|
||||
|
||||
node = child
|
||||
|
||||
node.leaf = True
|
||||
|
||||
if private:
|
||||
node.private = True
|
||||
|
||||
self.__nodes += 1
|
@ -0,0 +1,624 @@
|
||||
from __future__ import unicode_literals
|
||||
import argparse
|
||||
from codecs import open as codecs_open
|
||||
from functools import lru_cache
|
||||
# codecs_open = open
|
||||
from os.path import isabs
|
||||
import sys
|
||||
from typing import Dict, Type, Union, Tuple, List, Optional
|
||||
from urllib.parse import urlsplit, SplitResult
|
||||
|
||||
from .base import BaseTLDSourceParser
|
||||
from .exceptions import (
|
||||
TldBadUrl,
|
||||
TldDomainNotFound,
|
||||
TldImproperlyConfigured,
|
||||
TldIOError,
|
||||
)
|
||||
from .helpers import project_dir
|
||||
from .trie import Trie
|
||||
from .registry import Registry
|
||||
from .result import Result
|
||||
|
||||
__author__ = 'Artur Barseghyan'
|
||||
__copyright__ = '2013-2020 Artur Barseghyan'
|
||||
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
|
||||
__all__ = (
|
||||
'BaseMozillaTLDSourceParser',
|
||||
'get_fld',
|
||||
'get_tld',
|
||||
'get_tld_names',
|
||||
'get_tld_names_container',
|
||||
'is_tld',
|
||||
'MozillaTLDSourceParser',
|
||||
'parse_tld',
|
||||
'pop_tld_names_container',
|
||||
'process_url',
|
||||
'reset_tld_names',
|
||||
'Result',
|
||||
'tld_names',
|
||||
'update_tld_names',
|
||||
'update_tld_names_cli',
|
||||
'update_tld_names_container',
|
||||
)
|
||||
|
||||
tld_names: Dict[str, Trie] = {}
|
||||
|
||||
|
||||
def get_tld_names_container() -> Dict[str, Trie]:
|
||||
"""Get container of all tld names.
|
||||
|
||||
:return:
|
||||
:rtype dict:
|
||||
"""
|
||||
global tld_names
|
||||
return tld_names
|
||||
|
||||
|
||||
def update_tld_names_container(tld_names_local_path: str,
|
||||
trie_obj: Trie) -> None:
|
||||
"""Update TLD Names container item.
|
||||
|
||||
:param tld_names_local_path:
|
||||
:param trie_obj:
|
||||
:return:
|
||||
"""
|
||||
global tld_names
|
||||
# tld_names.update({tld_names_local_path: trie_obj})
|
||||
tld_names[tld_names_local_path] = trie_obj
|
||||
|
||||
|
||||
def pop_tld_names_container(tld_names_local_path: str) -> None:
|
||||
"""Remove TLD names container item.
|
||||
|
||||
:param tld_names_local_path:
|
||||
:return:
|
||||
"""
|
||||
global tld_names
|
||||
tld_names.pop(tld_names_local_path, None)
|
||||
|
||||
|
||||
@lru_cache(maxsize=128, typed=True)
|
||||
def update_tld_names(
|
||||
fail_silently: bool = False,
|
||||
parser_uid: str = None
|
||||
) -> bool:
|
||||
"""Update TLD names.
|
||||
|
||||
:param fail_silently:
|
||||
:param parser_uid:
|
||||
:return:
|
||||
"""
|
||||
results: List[bool] = []
|
||||
results_append = results.append
|
||||
if parser_uid:
|
||||
parser_cls = Registry.get(parser_uid, None)
|
||||
if parser_cls and parser_cls.source_url:
|
||||
results_append(
|
||||
parser_cls.update_tld_names(fail_silently=fail_silently)
|
||||
)
|
||||
else:
|
||||
for parser_uid, parser_cls in Registry.items():
|
||||
if parser_cls and parser_cls.source_url:
|
||||
results_append(
|
||||
parser_cls.update_tld_names(fail_silently=fail_silently)
|
||||
)
|
||||
|
||||
return all(results)
|
||||
|
||||
|
||||
def update_tld_names_cli() -> int:
|
||||
"""CLI wrapper for update_tld_names.
|
||||
|
||||
Since update_tld_names returns True on success, we need to negate the
|
||||
result to match CLI semantics.
|
||||
"""
|
||||
parser = argparse.ArgumentParser(description='Update TLD names')
|
||||
parser.add_argument(
|
||||
'parser_uid',
|
||||
nargs='?',
|
||||
default=None,
|
||||
help="UID of the parser to update TLD names for.",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--fail-silently',
|
||||
dest="fail_silently",
|
||||
default=False,
|
||||
action='store_true',
|
||||
help="Fail silently",
|
||||
)
|
||||
args = parser.parse_args(sys.argv[1:])
|
||||
parser_uid = args.parser_uid
|
||||
fail_silently = args.fail_silently
|
||||
return int(
|
||||
not update_tld_names(
|
||||
parser_uid=parser_uid,
|
||||
fail_silently=fail_silently
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_tld_names(
|
||||
fail_silently: bool = False,
|
||||
retry_count: int = 0,
|
||||
parser_class: Type[BaseTLDSourceParser] = None
|
||||
) -> Dict[str, Trie]:
|
||||
"""Build the ``tlds`` list if empty. Recursive.
|
||||
|
||||
:param fail_silently: If set to True, no exceptions are raised and None
|
||||
is returned on failure.
|
||||
:param retry_count: If greater than 1, we raise an exception in order
|
||||
to avoid infinite loops.
|
||||
:param parser_class:
|
||||
:type fail_silently: bool
|
||||
:type retry_count: int
|
||||
:type parser_class: BaseTLDSourceParser
|
||||
:return: List of TLD names
|
||||
:rtype: obj:`tld.utils.Trie`
|
||||
"""
|
||||
if not parser_class:
|
||||
parser_class = MozillaTLDSourceParser
|
||||
|
||||
return parser_class.get_tld_names(
|
||||
fail_silently=fail_silently,
|
||||
retry_count=retry_count
|
||||
)
|
||||
|
||||
|
||||
# **************************************************************************
|
||||
# **************************** Parser classes ******************************
|
||||
# **************************************************************************
|
||||
|
||||
class BaseMozillaTLDSourceParser(BaseTLDSourceParser):
|
||||
|
||||
@classmethod
|
||||
def get_tld_names(
|
||||
cls,
|
||||
fail_silently: bool = False,
|
||||
retry_count: int = 0
|
||||
) -> Optional[Dict[str, Trie]]:
|
||||
"""Parse.
|
||||
|
||||
:param fail_silently:
|
||||
:param retry_count:
|
||||
:return:
|
||||
"""
|
||||
if retry_count > 1:
|
||||
if fail_silently:
|
||||
return None
|
||||
else:
|
||||
raise TldIOError
|
||||
|
||||
global tld_names
|
||||
_tld_names = tld_names
|
||||
# _tld_names = get_tld_names_container()
|
||||
|
||||
# If already loaded, return
|
||||
if (
|
||||
cls.local_path in _tld_names
|
||||
and _tld_names[cls.local_path] is not None
|
||||
):
|
||||
return _tld_names
|
||||
|
||||
try:
|
||||
# Load the TLD names file
|
||||
if isabs(cls.local_path):
|
||||
local_path = cls.local_path
|
||||
else:
|
||||
local_path = project_dir(cls.local_path)
|
||||
local_file = codecs_open(
|
||||
local_path,
|
||||
'r',
|
||||
encoding='utf8'
|
||||
)
|
||||
trie = Trie()
|
||||
trie_add = trie.add # Performance opt
|
||||
# Make a list of it all, strip all garbage
|
||||
private_section = False
|
||||
|
||||
for line in local_file:
|
||||
if '===BEGIN PRIVATE DOMAINS===' in line:
|
||||
private_section = True
|
||||
|
||||
# Puny code TLD names
|
||||
if '// xn--' in line:
|
||||
line = line.split()[1]
|
||||
|
||||
if line[0] in ('/', '\n'):
|
||||
continue
|
||||
|
||||
trie_add(
|
||||
f'{line.strip()}',
|
||||
private=private_section
|
||||
)
|
||||
|
||||
update_tld_names_container(cls.local_path, trie)
|
||||
|
||||
local_file.close()
|
||||
except IOError as err:
|
||||
# Grab the file
|
||||
cls.update_tld_names(
|
||||
fail_silently=fail_silently
|
||||
)
|
||||
# Increment ``retry_count`` in order to avoid infinite loops
|
||||
retry_count += 1
|
||||
# Run again
|
||||
return cls.get_tld_names(
|
||||
fail_silently=fail_silently,
|
||||
retry_count=retry_count
|
||||
)
|
||||
except Exception as err:
|
||||
if fail_silently:
|
||||
return None
|
||||
else:
|
||||
raise err
|
||||
finally:
|
||||
try:
|
||||
local_file.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return _tld_names
|
||||
|
||||
|
||||
class MozillaTLDSourceParser(BaseMozillaTLDSourceParser):
|
||||
"""Mozilla TLD source."""
|
||||
|
||||
uid: str = 'mozilla'
|
||||
source_url: str = 'https://publicsuffix.org/list/public_suffix_list.dat'
|
||||
local_path: str = 'res/effective_tld_names.dat.txt'
|
||||
|
||||
# **************************************************************************
|
||||
# **************************** Core functions ******************************
|
||||
# **************************************************************************
|
||||
|
||||
|
||||
def process_url(
|
||||
url: str,
|
||||
fail_silently: bool = False,
|
||||
fix_protocol: bool = False,
|
||||
search_public: bool = True,
|
||||
search_private: bool = True,
|
||||
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser
|
||||
) -> Union[Tuple[List[str], int, SplitResult], Tuple[None, None, SplitResult]]:
|
||||
"""Process URL.
|
||||
|
||||
:param parser_class:
|
||||
:param url:
|
||||
:param fail_silently:
|
||||
:param fix_protocol:
|
||||
:param search_public:
|
||||
:param search_private:
|
||||
:return:
|
||||
"""
|
||||
if not (search_public or search_private):
|
||||
raise TldImproperlyConfigured(
|
||||
"Either `search_public` or `search_private` (or both) shall be "
|
||||
"set to True."
|
||||
)
|
||||
|
||||
# Init
|
||||
_tld_names = get_tld_names(
|
||||
fail_silently=fail_silently,
|
||||
parser_class=parser_class
|
||||
)
|
||||
|
||||
if not isinstance(url, SplitResult):
|
||||
url = url.lower()
|
||||
|
||||
if (
|
||||
fix_protocol and not url.startswith(('//', 'http://', 'https://'))
|
||||
):
|
||||
url = f'https://{url}'
|
||||
|
||||
# Get parsed URL as we might need it later
|
||||
parsed_url = urlsplit(url)
|
||||
else:
|
||||
parsed_url = url
|
||||
|
||||
# Get (sub) domain name
|
||||
domain_name = parsed_url.hostname
|
||||
|
||||
if not domain_name:
|
||||
if fail_silently:
|
||||
return None, None, parsed_url
|
||||
else:
|
||||
raise TldBadUrl(url=url)
|
||||
|
||||
# This will correctly handle dots at the end of domain name in URLs like
|
||||
# https://github.com............/barseghyanartur/tld/
|
||||
if domain_name.endswith('.'):
|
||||
domain_name = domain_name.rstrip('.')
|
||||
|
||||
domain_parts = domain_name.split('.')
|
||||
tld_names_local_path = parser_class.local_path
|
||||
|
||||
# Now we query our Trie iterating on the domain parts in reverse order
|
||||
node = _tld_names[tld_names_local_path].root
|
||||
current_length = 0
|
||||
tld_length = 0
|
||||
match = None
|
||||
len_domain_parts = len(domain_parts)
|
||||
for i in range(len_domain_parts-1, -1, -1):
|
||||
part = domain_parts[i]
|
||||
|
||||
# Cannot go deeper
|
||||
if node.children is None:
|
||||
break
|
||||
|
||||
# Exception
|
||||
if part == node.exception:
|
||||
break
|
||||
|
||||
child = node.children.get(part)
|
||||
|
||||
# Wildcards
|
||||
if child is None:
|
||||
child = node.children.get('*')
|
||||
|
||||
# If the current part is not in current node's children, we can stop
|
||||
if child is None:
|
||||
break
|
||||
|
||||
# Else we move deeper and increment our tld offset
|
||||
current_length += 1
|
||||
node = child
|
||||
|
||||
if node.leaf:
|
||||
tld_length = current_length
|
||||
match = node
|
||||
|
||||
# Checking the node we finished on is a leaf and is one we allow
|
||||
if (
|
||||
(match is None) or
|
||||
(not match.leaf) or
|
||||
(not search_public and not match.private) or
|
||||
(not search_private and match.private)
|
||||
):
|
||||
if fail_silently:
|
||||
return None, None, parsed_url
|
||||
else:
|
||||
raise TldDomainNotFound(domain_name=domain_name)
|
||||
|
||||
if len_domain_parts == tld_length:
|
||||
non_zero_i = -1 # hostname = tld
|
||||
else:
|
||||
non_zero_i = max(1, len_domain_parts - tld_length)
|
||||
|
||||
return domain_parts, non_zero_i, parsed_url
|
||||
|
||||
|
||||
def get_fld(
|
||||
url: str,
|
||||
fail_silently: bool = False,
|
||||
fix_protocol: bool = False,
|
||||
search_public: bool = True,
|
||||
search_private: bool = True,
|
||||
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser,
|
||||
**kwargs
|
||||
) -> Optional[str]:
|
||||
"""Extract the first level domain.
|
||||
|
||||
Extract the top level domain based on the mozilla's effective TLD names
|
||||
dat file. Returns a string. May throw ``TldBadUrl`` or
|
||||
``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
|
||||
match found respectively.
|
||||
|
||||
:param url: URL to get top level domain from.
|
||||
:param fail_silently: If set to True, no exceptions are raised and None
|
||||
is returned on failure.
|
||||
:param fix_protocol: If set to True, missing or wrong protocol is
|
||||
ignored (https is appended instead).
|
||||
:param search_public: If set to True, search in public domains.
|
||||
:param search_private: If set to True, search in private domains.
|
||||
:param parser_class:
|
||||
:type url: str
|
||||
:type fail_silently: bool
|
||||
:type fix_protocol: bool
|
||||
:type search_public: bool
|
||||
:type search_private: bool
|
||||
:return: String with top level domain (if ``as_object`` argument
|
||||
is set to False) or a ``tld.utils.Result`` object (if ``as_object``
|
||||
argument is set to True); returns None on failure.
|
||||
:rtype: str
|
||||
"""
|
||||
if 'as_object' in kwargs:
|
||||
raise TldImproperlyConfigured(
|
||||
"`as_object` argument is deprecated for `get_fld`. Use `get_tld` "
|
||||
"instead."
|
||||
)
|
||||
|
||||
domain_parts, non_zero_i, parsed_url = process_url(
|
||||
url=url,
|
||||
fail_silently=fail_silently,
|
||||
fix_protocol=fix_protocol,
|
||||
search_public=search_public,
|
||||
search_private=search_private,
|
||||
parser_class=parser_class
|
||||
)
|
||||
|
||||
if domain_parts is None:
|
||||
return None
|
||||
|
||||
# This should be None when domain_parts is None
|
||||
# but mypy isn't quite smart enough to figure that out yet
|
||||
assert non_zero_i is not None
|
||||
if non_zero_i < 0:
|
||||
# hostname = tld
|
||||
return parsed_url.hostname
|
||||
|
||||
return ".".join(domain_parts[non_zero_i-1:])
|
||||
|
||||
|
||||
def get_tld(
|
||||
url: str,
|
||||
fail_silently: bool = False,
|
||||
as_object: bool = False,
|
||||
fix_protocol: bool = False,
|
||||
search_public: bool = True,
|
||||
search_private: bool = True,
|
||||
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser
|
||||
) -> Optional[Union[str, Result]]:
|
||||
"""Extract the top level domain.
|
||||
|
||||
Extract the top level domain based on the mozilla's effective TLD names
|
||||
dat file. Returns a string. May throw ``TldBadUrl`` or
|
||||
``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
|
||||
match found respectively.
|
||||
|
||||
:param url: URL to get top level domain from.
|
||||
:param fail_silently: If set to True, no exceptions are raised and None
|
||||
is returned on failure.
|
||||
:param as_object: If set to True, ``tld.utils.Result`` object is returned,
|
||||
``domain``, ``suffix`` and ``tld`` properties.
|
||||
:param fix_protocol: If set to True, missing or wrong protocol is
|
||||
ignored (https is appended instead).
|
||||
:param search_public: If set to True, search in public domains.
|
||||
:param search_private: If set to True, search in private domains.
|
||||
:param parser_class:
|
||||
:type url: str
|
||||
:type fail_silently: bool
|
||||
:type as_object: bool
|
||||
:type fix_protocol: bool
|
||||
:type search_public: bool
|
||||
:type search_private: bool
|
||||
:return: String with top level domain (if ``as_object`` argument
|
||||
is set to False) or a ``tld.utils.Result`` object (if ``as_object``
|
||||
argument is set to True); returns None on failure.
|
||||
:rtype: str
|
||||
"""
|
||||
domain_parts, non_zero_i, parsed_url = process_url(
|
||||
url=url,
|
||||
fail_silently=fail_silently,
|
||||
fix_protocol=fix_protocol,
|
||||
search_public=search_public,
|
||||
search_private=search_private,
|
||||
parser_class=parser_class
|
||||
)
|
||||
|
||||
if domain_parts is None:
|
||||
return None
|
||||
|
||||
# This should be None when domain_parts is None
|
||||
# but mypy isn't quite smart enough to figure that out yet
|
||||
assert non_zero_i is not None
|
||||
|
||||
if not as_object:
|
||||
if non_zero_i < 0:
|
||||
# hostname = tld
|
||||
return parsed_url.hostname
|
||||
return ".".join(domain_parts[non_zero_i:])
|
||||
|
||||
if non_zero_i < 0:
|
||||
# hostname = tld
|
||||
subdomain = ""
|
||||
domain = ""
|
||||
# This is checked in process_url but the type is ambiguous (Optional[str])
|
||||
# so this assertion is just to satisfy mypy
|
||||
assert parsed_url.hostname is not None, "No hostname in URL"
|
||||
_tld = parsed_url.hostname
|
||||
else:
|
||||
subdomain = ".".join(domain_parts[:non_zero_i-1])
|
||||
domain = ".".join(
|
||||
domain_parts[non_zero_i-1:non_zero_i]
|
||||
)
|
||||
_tld = ".".join(domain_parts[non_zero_i:])
|
||||
|
||||
return Result(
|
||||
subdomain=subdomain,
|
||||
domain=domain,
|
||||
tld=_tld,
|
||||
parsed_url=parsed_url
|
||||
)
|
||||
|
||||
|
||||
def parse_tld(
|
||||
url: str,
|
||||
fail_silently: bool = False,
|
||||
fix_protocol: bool = False,
|
||||
search_public: bool = True,
|
||||
search_private: bool = True,
|
||||
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser
|
||||
) -> Union[Tuple[None, None, None], Tuple[str, str, str]]:
|
||||
"""Parse TLD into parts.
|
||||
|
||||
:param url:
|
||||
:param fail_silently:
|
||||
:param fix_protocol:
|
||||
:param search_public:
|
||||
:param search_private:
|
||||
:param parser_class:
|
||||
:return: Tuple (tld, domain, subdomain)
|
||||
:rtype: tuple
|
||||
"""
|
||||
try:
|
||||
obj = get_tld(
|
||||
url,
|
||||
fail_silently=fail_silently,
|
||||
as_object=True,
|
||||
fix_protocol=fix_protocol,
|
||||
search_public=search_public,
|
||||
search_private=search_private,
|
||||
parser_class=parser_class
|
||||
)
|
||||
if obj is None:
|
||||
return None, None, None
|
||||
|
||||
return obj.tld, obj.domain, obj.subdomain # type: ignore
|
||||
|
||||
except (
|
||||
TldBadUrl,
|
||||
TldDomainNotFound,
|
||||
TldImproperlyConfigured,
|
||||
TldIOError
|
||||
):
|
||||
pass
|
||||
|
||||
return None, None, None
|
||||
|
||||
|
||||
def is_tld(
|
||||
value: str,
|
||||
search_public: bool = True,
|
||||
search_private: bool = True,
|
||||
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser
|
||||
) -> bool:
|
||||
"""Check if given URL is tld.
|
||||
|
||||
:param value: URL to get top level domain from.
|
||||
:param search_public: If set to True, search in public domains.
|
||||
:param search_private: If set to True, search in private domains.
|
||||
:param parser_class:
|
||||
:type value: str
|
||||
:type search_public: bool
|
||||
:type search_private: bool
|
||||
:return:
|
||||
:rtype: bool
|
||||
"""
|
||||
_tld = get_tld(
|
||||
url=value,
|
||||
fail_silently=True,
|
||||
fix_protocol=True,
|
||||
search_public=search_public,
|
||||
search_private=search_private,
|
||||
parser_class=parser_class
|
||||
)
|
||||
return value == _tld
|
||||
|
||||
|
||||
def reset_tld_names(tld_names_local_path: str = None) -> None:
|
||||
"""Reset the ``tld_names`` to empty value.
|
||||
|
||||
If ``tld_names_local_path`` is given, removes specified
|
||||
entry from ``tld_names`` instead.
|
||||
|
||||
:param tld_names_local_path:
|
||||
:type tld_names_local_path: str
|
||||
:return:
|
||||
"""
|
||||
|
||||
if tld_names_local_path:
|
||||
pop_tld_names_container(tld_names_local_path)
|
||||
else:
|
||||
global tld_names
|
||||
tld_names = {}
|
Before Width: | Height: | Size: 153 KiB |
Before Width: | Height: | Size: 142 KiB |
Before Width: | Height: | Size: 100 KiB |
Before Width: | Height: | Size: 98 KiB |
Before Width: | Height: | Size: 300 KiB |
Before Width: | Height: | Size: 2.8 MiB |
Before Width: | Height: | Size: 504 KiB |
Before Width: | Height: | Size: 656 KiB |
Before Width: | Height: | Size: 300 KiB |
Before Width: | Height: | Size: 278 KiB |
Before Width: | Height: | Size: 155 KiB |
Before Width: | Height: | Size: 285 KiB |
Before Width: | Height: | Size: 374 KiB |
Before Width: | Height: | Size: 347 KiB |
Before Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 358 KiB |
Before Width: | Height: | Size: 128 KiB |
Before Width: | Height: | Size: 129 KiB |
Before Width: | Height: | Size: 256 KiB |
Before Width: | Height: | Size: 237 KiB |
Before Width: | Height: | Size: 107 KiB |
Before Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 437 KiB |
After Width: | Height: | Size: 855 KiB |