Repository: iamLiquidX/MirrorX Branch: master Commit: 3a410904bf7f Files: 60 Total size: 213.4 KB Directory structure: gitextract_q9y7459o/ ├── .github/ │ └── workflows/ │ └── release.yml ├── Dockerfile ├── LICENSE ├── README.md ├── bin/ │ ├── MirrorX │ ├── extract │ └── pextract ├── bot/ │ ├── __init__.py │ ├── __main__.py │ ├── helper/ │ │ ├── __init__.py │ │ ├── ext_utils/ │ │ │ ├── __init__.py │ │ │ ├── bot_utils.py │ │ │ ├── exceptions.py │ │ │ └── fs_utils.py │ │ ├── mirror_utils/ │ │ │ ├── __init__.py │ │ │ ├── download_utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── aria2_download.py │ │ │ │ ├── direct_link_generator.py │ │ │ │ ├── direct_link_generator_license.md │ │ │ │ ├── download_helper.py │ │ │ │ ├── mega_download.py │ │ │ │ ├── telegram_downloader.py │ │ │ │ └── youtube_dl_download_helper.py │ │ │ ├── status_utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── aria_download_status.py │ │ │ │ ├── extract_status.py │ │ │ │ ├── gdownload_status.py │ │ │ │ ├── listeners.py │ │ │ │ ├── mega_status.py │ │ │ │ ├── status.py │ │ │ │ ├── tar_status.py │ │ │ │ ├── telegram_download_status.py │ │ │ │ ├── upload_status.py │ │ │ │ └── youtube_dl_download_status.py │ │ │ └── upload_utils/ │ │ │ ├── __init__.py │ │ │ └── gdriveTools.py │ │ └── telegram_helper/ │ │ ├── __init__.py │ │ ├── bot_commands.py │ │ ├── button_build.py │ │ ├── filters.py │ │ └── message_utils.py │ └── modules/ │ ├── __init__.py │ ├── authorize.py │ ├── cancel_mirror.py │ ├── clone.py │ ├── count.py │ ├── delete.py │ ├── list.py │ ├── mirror.py │ ├── mirror_status.py │ ├── speedtest.py │ └── watch.py ├── config_sample.env ├── docker-compose.yml ├── netrc ├── requirements.txt ├── setup.py └── utils/ ├── add_to_team_drive.py ├── gen_sa_accounts.py └── generate_drive_token.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/release.yml ================================================ name: MirrorX Release on: push: tags: - "v*.*.*" jobs: BuildWheel: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Setup Python uses: actions/setup-python@v1 with: python-version: 3.8.12 - name: Get the version id: get_version run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} - name: Install deps fro Pypi Release run: | pip -q install setuptools wheel twine pip -q install -r requirements.txt - name: Publish to pypi env: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} run: | python setup.py sdist bdist_wheel twine upload dist/* || exit 0 - name: Upload all the data files to github Release uses: softprops/action-gh-release@v1 with: name: MirrorX Release ${{ steps.get_version.outputs.VERSION }} files: | dist/* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Sleep run: sleep 60 - name: Set up QEMU uses: docker/setup-qemu-action@v1 with: platforms: arm64 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - name: Login to GitHub Container Registry uses: docker/login-action@v1 with: registry: ghcr.io username: token password: ${{ secrets.GH_TOKEN }} - name: Docker meta id: metaraw uses: docker/metadata-action@v3 with: images: ghcr.io/iamliquidx/mirrorx tags: | type=semver,pattern={{version}} - name: Build and push Docker images uses: docker/build-push-action@v2 with: context: . file: ./Dockerfile platforms: linux/amd64, linux/arm64/v8 push: true tags: ${{ steps.metaraw.outputs.tags }} labels: ${{ steps.metaraw.outputs.labels }} ================================================ FILE: Dockerfile ================================================ FROM python:3.9-slim WORKDIR / # Deps SHELL [ "/usr/bin/bash" , "-cel" ] RUN \ [[ ${valid_arch:-aarch64 amd64 x86_64} =~ ${HOST_CPU_ARCH:=$(uname -m)} ]] \ || echo 'unsupported cpu arch' && exit 1 RUN \ export HOST_CPU_ARCH=$(uname -m) \ sed -i 's/main/main non-free/g' /etc/apt/sources.list && \ apt-get -qq update && \ apt-get -qq install -y tzdata curl aria2 p7zip-full p7zip-rar wget xz-utils libmagic-dev gcc libffi-dev nscd && \ apt-get -y autoremove && rm -rf /var/lib/apt/lists/* && apt-get clean && \ wget -q https://github.com/yzop/gg/raw/main/ffmpeg-git-${HOST_CPU_ARCH}-static.tar.xz && \ tar -xf ff*.tar.xz && rm -rf *.tar.xz && \ mv ff*/ff* /usr/local/bin/ && rm -rf ff* && \ wget -q https://github.com/viswanathbalusu/megasdkrest/releases/latest/download/megasdkrest-${HOST_CPU_ARCH} -O /usr/local/bin/megasdkrest && \ chmod a+x /usr/local/bin/megasdkrest && mkdir /app/ && chmod 777 /app/ && \ pip3 install --no-cache-dir MirrorX && \ apt-get purge -yqq gcc && apt-get -y autoremove && rm -rf /var/lib/apt/lists/* && apt-get clean WORKDIR /app CMD ["MirrorX"] ### ================================================ FILE: LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. 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. Copyright (C) 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 . 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: Copyright (C) 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 . 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 . ================================================ FILE: README.md ================================================

Material Bread logo

# This Is A Telegram Bot Written In Python For Mirroring Files On The Internet To Our Beloved Google Drive.

Here Are Some Things To Get You Started.👇 ## 👉[All The Feature Of This Bot Or What This Bot Can Do For You.](https://github.com/iamLiquidX/MirrorX/wiki/Feature-Or-What-This-Bot-Can-Do) ## 👉[How To Deploy](https://github.com/iamLiquidX/MirrorX/wiki/How-To-Deploy) ## 👉[Commands To Use The Bot](https://github.com/iamLiquidX/MirrorX/wiki/Commands-To-Use-This-Bot) ## 👉[Modification Guide](https://github.com/iamLiquidX/MirrorX/wiki/Modification) For The Most Recent Changes, Please Check The Changelog.👇 ## 👉[Changelog](https://github.com/iamLiquidX/MirrorX/wiki/Changelog) # Credits 👇 1. [Shivam Jha aka lzzy12](https://github.com/lzzy12) & [JaskaranSM aka Zero Cool](https://github.com/jaskaranSM) - They Built This Bot From Scratch. 2. [Sreeraj V R](https://github.com/SVR666)- Added Inline Button, Added Support For Deleting File/Folders From GDrive, Search Results On Telegra.ph. 3. [Archie](https://github.com/archie9211) - Added Support For Extraction Of Archives, Fixed SSL Handshake Error, Update Trackers Dynamically. 4. [Magneto](https://github.com/magneto261290) - Added Alot Of Customization, Support For Custom File Names, Support For Password Protected Archives, Quality Selection Option In YTDL And Much More. 5. [KenHV](https://github.com/KenHV) - Many Fixes And Imporovements. 6. [Anos](https://github.com/destiny6520) - Modification/Customization Guide. 7. [Viswanath](https://github.com/nenokkadine) - Fixes & Improvements, Dockerfile Clean Up, DHT Support In Aria. 8. [breakdowns](https://github.com/breakdowns) - Source Code For Count,Zip/Unzip GDrive Links & Fembed. ================================================ FILE: bin/MirrorX ================================================ #!/bin/bash pip3 install -U MirrorX MirrorXBot ================================================ FILE: bin/extract ================================================ #!/bin/bash if [ $# -lt 1 ]; then echo "Usage: $(basename $0) FILES" exit 1 fi extract() { arg="$1" cd "$(dirname "$arg")" || exit case "$arg" in *.tar.bz2) tar xjf "$arg" --one-top-level local code=$? ;; *.tar.gz) tar xzf "$arg" --one-top-level local code=$? ;; *.bz2) bunzip2 "$arg" local code=$? ;; *.gz) gunzip "$arg" local code=$? ;; *.tar) tar xf "$arg" --one-top-level local code=$? ;; *.tbz2) (tar xjf "$arg" --one-top-level) local code=$? ;; *.tgz) tar xzf "$arg" --one-top-level local code=$? ;; *.zip) a_dir=$(expr "$arg" : '\(.*\).zip') 7z x "$arg" -o"$a_dir" local code=$? ;; *.7z) a_dir=$(expr "$arg" : '\(.*\).7z') 7z x "$arg" -o"$a_dir" local code=$? ;; *.Z) uncompress "$arg" local code=$? ;; *.rar) a_dir=$(expr "$arg" : '\(.*\).rar') mkdir "$a_dir" 7z x "$arg" -o"$a_dir" local code=$? ;; *.iso) a_dir=$(expr "$arg" : '\(.*\).iso') 7z x "$arg" -o"$a_dir" local code=$? ;; *.wim) a_dir=$(expr "$arg" : '\(.*\).wim') 7z x "$arg" -o"$a_dir" local code=$? ;; *.cab) a_dir=$(expr "$arg" : '\(.*\).cab') 7z x "$arg" -o"$a_dir" local code=$? ;; *.apm) a_dir=$(expr "$arg" : '\(.*\).apm') 7z x "$arg" -o"$a_dir" local code=$? ;; *.arj) a_dir=$(expr "$arg" : '\(.*\).arj') 7z x "$arg" -o"$a_dir" local code=$? ;; *.chm) a_dir=$(expr "$arg" : '\(.*\).chm') 7z x "$arg" -o"$a_dir" local code=$? ;; *.cpio) a_dir=$(expr "$arg" : '\(.*\).cpio') 7z x "$arg" -o"$a_dir" local code=$? ;; *.cramfs) a_dir=$(expr "$arg" : '\(.*\).cramfs') 7z x "$arg" -o"$a_dir" local code=$? ;; *.deb) a_dir=$(expr "$arg" : '\(.*\).deb') 7z x "$arg" -o"$a_dir" local code=$? ;; *.dmg) a_dir=$(expr "$arg" : '\(.*\).dmg') 7z x "$arg" -o"$a_dir" local code=$? ;; *.fat) a_dir=$(expr "$arg" : '\(.*\).fat') 7z x "$arg" -o"$a_dir" local code=$? ;; *.hfs) a_dir=$(expr "$arg" : '\(.*\).hfs') 7z x "$arg" -o"$a_dir" local code=$? ;; *.lzh) a_dir=$(expr "$arg" : '\(.*\).lzh') 7z x "$arg" -o"$a_dir" local code=$? ;; *.lzma) a_dir=$(expr "$arg" : '\(.*\).lzma') 7z x "$arg" -o"$a_dir" local code=$? ;; *.lzma2) a_dir=$(expr "$arg" : '\(.*\).lzma2') 7z x "$arg" -o"$a_dir" local code=$? ;; *.mbr) a_dir=$(expr "$arg" : '\(.*\).mbr') 7z x "$arg" -o"$a_dir" local code=$? ;; *.msi) a_dir=$(expr "$arg" : '\(.*\).msi') 7z x "$arg" -o"$a_dir" local code=$? ;; *.mslz) a_dir=$(expr "$arg" : '\(.*\).mslz') 7z x "$arg" -o"$a_dir" local code=$? ;; *.nsis) a_dir=$(expr "$arg" : '\(.*\).nsis') 7z x "$arg" -o"$a_dir" local code=$? ;; *.ntfs) a_dir=$(expr "$arg" : '\(.*\).ntfs') 7z x "$arg" -o"$a_dir" local code=$? ;; *.rpm) a_dir=$(expr "$arg" : '\(.*\).rpm') 7z x "$arg" -o"$a_dir" local code=$? ;; *.squashfs) a_dir=$(expr "$arg" : '\(.*\).squashfs') 7z x "$arg" -o"$a_dir" local code=$? ;; *.udf) a_dir=$(expr "$arg" : '\(.*\).udf') 7z x "$arg" -o"$a_dir" local code=$? ;; *.vhd) a_dir=$(expr "$arg" : '\(.*\).vhd') 7z x "$arg" -o"$a_dir" local code=$? ;; *.xar) a_dir=$(expr "$arg" : '\(.*\).xar') 7z x "$arg" -o"$a_dir" local code=$? ;; *) echo "'$arg' cannot be extracted via extract()" 1>&2 exit 1 ;; esac cd - || exit $? exit $code } extract "$1" ================================================ FILE: bin/pextract ================================================ #!/bin/bash if [ $# -lt 1 ]; then echo "Usage: $(basename $0) FILES" exit 1 fi extract() { arg="$1" pswd="$2" cd "$(dirname "$arg")" || exit case "$arg" in *.tar.bz2) tar xjf "$arg" --one-top-level local code=$? ;; *.tar.gz) tar xzf "$arg" --one-top-level local code=$? ;; *.bz2) bunzip2 "$arg" local code=$? ;; *.gz) gunzip "$arg" local code=$? ;; *.tar) tar xf "$arg" --one-top-level local code=$? ;; *.tbz2) (tar xjf "$arg" --one-top-level) local code=$? ;; *.tgz) tar xzf "$arg" --one-top-level local code=$? ;; *.zip) a_dir=$(expr "$arg" : '\(.*\).zip') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.7z) a_dir=$(expr "$arg" : '\(.*\).7z') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.Z) uncompress "$arg" local code=$? ;; *.rar) a_dir=$(expr "$arg" : '\(.*\).rar') mkdir "$a_dir" 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.iso) a_dir=$(expr "$arg" : '\(.*\).iso') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.wim) a_dir=$(expr "$arg" : '\(.*\).wim') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.cab) a_dir=$(expr "$arg" : '\(.*\).cab') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.apm) a_dir=$(expr "$arg" : '\(.*\).apm') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.arj) a_dir=$(expr "$arg" : '\(.*\).arj') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.chm) a_dir=$(expr "$arg" : '\(.*\).chm') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.cpio) a_dir=$(expr "$arg" : '\(.*\).cpio') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.cramfs) a_dir=$(expr "$arg" : '\(.*\).cramfs') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.deb) a_dir=$(expr "$arg" : '\(.*\).deb') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.dmg) a_dir=$(expr "$arg" : '\(.*\).dmg') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.fat) a_dir=$(expr "$arg" : '\(.*\).fat') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.hfs) a_dir=$(expr "$arg" : '\(.*\).hfs') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.lzh) a_dir=$(expr "$arg" : '\(.*\).lzh') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.lzma) a_dir=$(expr "$arg" : '\(.*\).lzma') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.lzma2) a_dir=$(expr "$arg" : '\(.*\).lzma2') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.mbr) a_dir=$(expr "$arg" : '\(.*\).mbr') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.msi) a_dir=$(expr "$arg" : '\(.*\).msi') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.mslz) a_dir=$(expr "$arg" : '\(.*\).mslz') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.nsis) a_dir=$(expr "$arg" : '\(.*\).nsis') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.ntfs) a_dir=$(expr "$arg" : '\(.*\).ntfs') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.rpm) a_dir=$(expr "$arg" : '\(.*\).rpm') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.squashfs) a_dir=$(expr "$arg" : '\(.*\).squashfs') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.udf) a_dir=$(expr "$arg" : '\(.*\).udf') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.vhd) a_dir=$(expr "$arg" : '\(.*\).vhd') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *.xar) a_dir=$(expr "$arg" : '\(.*\).xar') 7z x "$arg" -o"$a_dir" -p"$pswd" local code=$? ;; *) echo "'$arg' cannot be extracted via extract()" 1>&2 exit 1 ;; esac cd - || exit $? exit $code } extract "$1" "$2" ================================================ FILE: bot/__init__.py ================================================ import logging import os import threading import time import random import string import subprocess import pkgutil import pathlib import sys import aria2p import requests import telegram.ext as tg from dotenv import load_dotenv from pyrogram import Client from telegraph import Telegraph from megasdkrestclient import MegaSdkRestClient, errors as mega_err import socket import faulthandler faulthandler.enable() socket.setdefaulttimeout(600) botStartTime = time.time() if os.path.exists('log.txt'): with open('log.txt', 'r+') as f: f.truncate(0) logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[logging.FileHandler('log.txt'), logging.StreamHandler()], level=logging.INFO) load_dotenv('config.env') Interval = [] def getConfig(name: str): return os.environ[name] LOGGER = logging.getLogger(__name__) try: if bool(getConfig('_____REMOVE_THIS_LINE_____')): logging.error('The README.md file there to be read! Exiting now!') exit() except KeyError: pass CWD = os.getcwd() ariaconfig = pkgutil.get_data("bot", "data/aria.conf").decode() dhtfile = pkgutil.get_data("bot", "data/dht.dat") dht6file = pkgutil.get_data("bot", "data/dht6.dat") with open("dht.dat", "wb+") as dht: dht.write(dhtfile) with open("dht6.dat", "wb+") as dht6: dht6.write(dhtfile) ariaconfig = ariaconfig.replace("/currentwd", str(CWD)) try: max_dl = getConfig("MAX_CONCURRENT_DOWNLOADS") except KeyError: max_dl = "4" tracker_list = requests.get("https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/all_aria2.txt").text ariaconfig += f"\nmax-concurrent-downloads={max_dl}\nbt-tracker={tracker_list}" with open("aria.conf", "w+") as ariaconf: ariaconf.write(ariaconfig) ARIA_CHILD_PROC = None try: ARIA_CHILD_PROC = subprocess.Popen(["aria2c", f"--conf-path={CWD}/aria.conf"]) except FileNotFoundError: LOGGER.error("Please install Aria2c, Exiting..") sys.exit(0) except OSError: LOGGER.error("Aria2c Binary might have got damaged, Please Check and reinstall..") sys.exit(0) time.sleep(1) aria2 = aria2p.API( aria2p.Client( host="http://localhost", port=6800, secret="", ) ) DOWNLOAD_DIR = None BOT_TOKEN = None download_dict_lock = threading.Lock() status_reply_dict_lock = threading.Lock() # Key: update.effective_chat.id # Value: telegram.Message status_reply_dict = {} # Key: update.message.message_id # Value: An object of Status download_dict = {} # Stores list of users and chats the bot is authorized to use in AUTHORIZED_CHATS = set() if os.path.exists('authorized_chats.txt'): with open('authorized_chats.txt', 'r+') as f: lines = f.readlines() for line in lines: # LOGGER.info(line.split()) AUTHORIZED_CHATS.add(int(line.split()[0])) try: achats = getConfig('AUTHORIZED_CHATS') achats = achats.split(" ") for chats in achats: AUTHORIZED_CHATS.add(int(chats)) except: pass try: BOT_TOKEN = getConfig('BOT_TOKEN') parent_id = getConfig('GDRIVE_FOLDER_ID') DOWNLOAD_DIR = getConfig('DOWNLOAD_DIR') if not DOWNLOAD_DIR.endswith("/"): DOWNLOAD_DIR = DOWNLOAD_DIR + '/' if not os.path.exists(DOWNLOAD_DIR): os.makedirs(DOWNLOAD_DIR, 0o777) DOWNLOAD_STATUS_UPDATE_INTERVAL = int(getConfig('DOWNLOAD_STATUS_UPDATE_INTERVAL')) OWNER_ID = int(getConfig('OWNER_ID')) AUTO_DELETE_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_MESSAGE_DURATION')) TELEGRAM_API = getConfig('TELEGRAM_API') TELEGRAM_HASH = getConfig('TELEGRAM_HASH') except KeyError as e: LOGGER.error("One or more env variables missing! Exiting now") sys.exit() # exit() LOGGER.info("Generating USER_SESSION_STRING") app = Client(':memory:', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) #Generate Telegraph Token sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8)) LOGGER.info("Generating Telegraph Token using '" + sname + "' name") telegraph = Telegraph() telegraph.create_account(short_name=sname) telegraph_token = telegraph.get_access_token() LOGGER.info("Telegraph Token Generated: '" + telegraph_token + "'") try: UPTOBOX_TOKEN = getConfig('UPTOBOX_TOKEN') except KeyError: logging.warning('UPTOBOX_TOKEN not provided!') UPTOBOX_TOKEN = None try: MEGA_KEY = getConfig('MEGA_KEY') except KeyError: MEGA_KEY = None LOGGER.info('MEGA API KEY NOT AVAILABLE') MEGA_CHILD_PROC = None if MEGA_KEY is not None: try: MEGA_CHILD_PROC = subprocess.Popen(["megasdkrest", "--apikey", MEGA_KEY]) except FileNotFoundError: LOGGER.error("Please install Megasdkrest Binary, Exiting..") sys.exit(0) except OSError: LOGGER.error("Megasdkrest Binary might have got damaged, Please Check ..") sys.exit(0) time.sleep(3) mega_client = MegaSdkRestClient('http://localhost:6090') try: MEGA_USERNAME = getConfig('MEGA_USERNAME') MEGA_PASSWORD = getConfig('MEGA_PASSWORD') if len(MEGA_USERNAME) > 0 and len(MEGA_PASSWORD) > 0: try: mega_client.login(MEGA_USERNAME, MEGA_PASSWORD) except mega_err.MegaSdkRestClientException as e: logging.error(e.message['message']) exit(0) else: LOGGER.info("Mega API KEY provided but credentials not provided. Starting mega in anonymous mode!") MEGA_USERNAME = None MEGA_PASSWORD = None except KeyError: LOGGER.info("Mega API KEY provided but credentials not provided. Starting mega in anonymous mode!") MEGA_USERNAME = None MEGA_PASSWORD = None else: MEGA_USERNAME = None MEGA_PASSWORD = None try: INDEX_URL = getConfig('INDEX_URL') if len(INDEX_URL) == 0: INDEX_URL = None except KeyError: INDEX_URL = None try: BUTTON_THREE_NAME = getConfig('BUTTON_THREE_NAME') BUTTON_THREE_URL = getConfig('BUTTON_THREE_URL') if len(BUTTON_THREE_NAME) == 0 or len(BUTTON_THREE_URL) == 0: raise KeyError except KeyError: BUTTON_THREE_NAME = None BUTTON_THREE_URL = None try: BUTTON_FOUR_NAME = getConfig('BUTTON_FOUR_NAME') BUTTON_FOUR_URL = getConfig('BUTTON_FOUR_URL') if len(BUTTON_FOUR_NAME) == 0 or len(BUTTON_FOUR_URL) == 0: raise KeyError except KeyError: BUTTON_FOUR_NAME = None BUTTON_FOUR_URL = None try: BUTTON_FIVE_NAME = getConfig('BUTTON_FIVE_NAME') BUTTON_FIVE_URL = getConfig('BUTTON_FIVE_URL') if len(BUTTON_FIVE_NAME) == 0 or len(BUTTON_FIVE_URL) == 0: raise KeyError except KeyError: BUTTON_FIVE_NAME = None BUTTON_FIVE_URL = None try: STOP_DUPLICATE_MIRROR = getConfig('STOP_DUPLICATE_MIRROR') if STOP_DUPLICATE_MIRROR.lower() == 'true': STOP_DUPLICATE_MIRROR = True else: STOP_DUPLICATE_MIRROR = False except KeyError: STOP_DUPLICATE_MIRROR = False try: IS_TEAM_DRIVE = getConfig('IS_TEAM_DRIVE') if IS_TEAM_DRIVE.lower() == 'true': IS_TEAM_DRIVE = True else: IS_TEAM_DRIVE = False except KeyError: IS_TEAM_DRIVE = False try: USE_SERVICE_ACCOUNTS = getConfig('USE_SERVICE_ACCOUNTS') if USE_SERVICE_ACCOUNTS.lower() == 'true': USE_SERVICE_ACCOUNTS = True else: USE_SERVICE_ACCOUNTS = False except KeyError: USE_SERVICE_ACCOUNTS = False try: BLOCK_MEGA_LINKS = getConfig('BLOCK_MEGA_LINKS') if BLOCK_MEGA_LINKS.lower() == 'true': BLOCK_MEGA_LINKS = True else: BLOCK_MEGA_LINKS = False except KeyError: BLOCK_MEGA_LINKS = False try: SHORTENER = getConfig('SHORTENER') SHORTENER_API = getConfig('SHORTENER_API') if len(SHORTENER) == 0 or len(SHORTENER_API) == 0: raise KeyError except KeyError: SHORTENER = None SHORTENER_API = None updater = tg.Updater(token=BOT_TOKEN) bot = updater.bot dispatcher = updater.dispatcher ================================================ FILE: bot/__main__.py ================================================ import os import shutil, psutil import signal from sys import executable import time from telegram.ext import CommandHandler from bot import bot, dispatcher, updater, botStartTime from bot.helper.ext_utils import fs_utils from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.message_utils import * from .helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time from .helper.telegram_helper.filters import CustomFilters from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, delete, speedtest, count from pyrogram import idle from bot import app def stats(update, context): currentTime = get_readable_time(time.time() - botStartTime) total, used, free = shutil.disk_usage('.') total = get_readable_file_size(total) used = get_readable_file_size(used) free = get_readable_file_size(free) sent = get_readable_file_size(psutil.net_io_counters().bytes_sent) recv = get_readable_file_size(psutil.net_io_counters().bytes_recv) cpuUsage = psutil.cpu_percent(interval=0.5) memory = psutil.virtual_memory().percent disk = psutil.disk_usage('/').percent stats = f'Bot Uptime:- {currentTime}\n' \ f'Total Disk Space:- {total}\n' \ f'Used:- {used} ' \ f'Free:- {free}\n\n' \ f'Data Usage\nUp:- {sent}\n' \ f'Down:- {recv}\n\n' \ f'CPU: {cpuUsage}% ' \ f'RAM: {memory}% ' \ f'Disk: {disk}%' sendMessage(stats, context.bot, update) def start(update, context): start_string = f''' This is a bot which can mirror all your links to Google drive! Type /{BotCommands.HelpCommand} to get a list of available commands ''' sendMessage(start_string, context.bot, update) def restart(update, context): restart_message = sendMessage("Restarting, Please wait!", context.bot, update) # Save restart message ID and chat ID in order to edit it after restarting with open(".restartmsg", "w") as f: f.truncate(0) f.write(f"{restart_message.chat.id}\n{restart_message.message_id}\n") fs_utils.clean_all() os.execl(executable, executable, "-m", "bot") def ping(update, context): start_time = int(round(time.time() * 1000)) reply = sendMessage("Starting Ping", context.bot, update) end_time = int(round(time.time() * 1000)) editMessage(f'{end_time - start_time} ms', reply) def log(update, context): sendLogFile(context.bot, update) def bot_help(update, context): help_string = f''' /{BotCommands.HelpCommand}: To get this message /{BotCommands.MirrorCommand} [download_url][magnet_link]: Start mirroring the link to google drive.\n /{BotCommands.UnzipMirrorCommand} [download_url][magnet_link] : starts mirroring and if downloaded file is any archive , extracts it to google drive /{BotCommands.CountCommand}: Count files/folders of G-Drive Links /{BotCommands.TarMirrorCommand} [download_url][magnet_link]: start mirroring and upload the archived (.tar) version of the download /{BotCommands.WatchCommand} [youtube-dl supported link]: Mirror through youtube-dl. Click /{BotCommands.WatchCommand} for more help. /{BotCommands.TarWatchCommand} [youtube-dl supported link]: Mirror through youtube-dl and tar before uploading /{BotCommands.CancelMirror} : Reply to the message by which the download was initiated and that download will be cancelled /{BotCommands.StatusCommand}: Shows a status of all the downloads /{BotCommands.ListCommand} [search term]: Searches the search term in the Google drive, if found replies with the link /{BotCommands.StatsCommand}: Show Stats of the machine the bot is hosted on /{BotCommands.AuthorizeCommand}: Authorize a chat or a user to use the bot (Can only be invoked by owner of the bot) /{BotCommands.LogCommand}: Get a log file of the bot. Handy for getting crash reports /{BotCommands.SpeedCommand} : Check Internet Speed Of The Host ''' sendMessage(help_string, context.bot, update) def main(): fs_utils.start_cleanup() # Check if the bot is restarting if os.path.isfile(".restartmsg"): with open(".restartmsg") as f: chat_id, msg_id = map(int, f) bot.edit_message_text("Restarted successfully!", chat_id, msg_id) os.remove(".restartmsg") start_handler = CommandHandler(BotCommands.StartCommand, start, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) ping_handler = CommandHandler(BotCommands.PingCommand, ping, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) restart_handler = CommandHandler(BotCommands.RestartCommand, restart, filters=CustomFilters.owner_filter| CustomFilters.authorized_user, run_async=True) help_handler = CommandHandler(BotCommands.HelpCommand, bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) stats_handler = CommandHandler(BotCommands.StatsCommand, stats, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(start_handler) dispatcher.add_handler(ping_handler) dispatcher.add_handler(restart_handler) dispatcher.add_handler(help_handler) dispatcher.add_handler(stats_handler) dispatcher.add_handler(log_handler) updater.start_polling() LOGGER.info("Bot Started!") signal.signal(signal.SIGINT, fs_utils.exit_clean_up) app.start() main() idle() ================================================ FILE: bot/helper/__init__.py ================================================ ================================================ FILE: bot/helper/ext_utils/__init__.py ================================================ ================================================ FILE: bot/helper/ext_utils/bot_utils.py ================================================ import logging import re import threading import time from bot.helper.telegram_helper.bot_commands import BotCommands from bot import download_dict, download_dict_lock LOGGER = logging.getLogger(__name__) MAGNET_REGEX = r"magnet:\?xt=urn:btih:[a-zA-Z0-9]*" URL_REGEX = r"(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+" class MirrorStatus: STATUS_UPLOADING = "Uploading" STATUS_DOWNLOADING = "Downloading" STATUS_WAITING = "Queued" STATUS_FAILED = "Failed.Cleaning download" STATUS_CANCELLED = "Cancelled " STATUS_ARCHIVING = "Archiving" STATUS_EXTRACTING = "Extracting" PROGRESS_MAX_SIZE = 100 // 8 PROGRESS_INCOMPLETE = ['█', '█', '█', '█', '█', '█', '█'] SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] class setInterval: def __init__(self, interval, action): self.interval = interval self.action = action self.stopEvent = threading.Event() thread = threading.Thread(target=self.__setInterval) thread.start() def __setInterval(self): nextTime = time.time() + self.interval while not self.stopEvent.wait(nextTime - time.time()): nextTime += self.interval self.action() def cancel(self): self.stopEvent.set() def get_readable_file_size(size_in_bytes) -> str: if size_in_bytes is None: return '0B' index = 0 while size_in_bytes >= 1024: size_in_bytes /= 1024 index += 1 try: return f'{round(size_in_bytes, 2)}{SIZE_UNITS[index]}' except IndexError: return 'File too large' def getDownloadByGid(gid): with download_dict_lock: for dl in download_dict.values(): status = dl.status() if status != MirrorStatus.STATUS_UPLOADING and status != MirrorStatus.STATUS_ARCHIVING \ and status != MirrorStatus.STATUS_EXTRACTING: if dl.gid() == gid: return dl return None def get_progress_bar_string(status): completed = status.processed_bytes() / 8 total = status.size_raw() / 8 if total == 0: p = 0 else: p = round(completed * 100 / total) p = min(max(p, 0), 100) cFull = p // 8 cPart = p % 8 - 1 p_str = '█' * cFull if cPart >= 0: p_str += PROGRESS_INCOMPLETE[cPart] p_str += '░' * (PROGRESS_MAX_SIZE - cFull) p_str = f"[{p_str}]" return p_str def get_readable_message(): with download_dict_lock: msg = "" for download in list(download_dict.values()): msg += f"Name:- {download.name()}" msg += f"\nStatus:- {download.status()}" if download.status() != MirrorStatus.STATUS_ARCHIVING and download.status() != MirrorStatus.STATUS_EXTRACTING: msg += f"\n{get_progress_bar_string(download)} {download.progress()}" if download.status() == MirrorStatus.STATUS_DOWNLOADING: msg += f"\nDownloaded:- {get_readable_file_size(download.processed_bytes())} of {download.size()}" else: msg += f"\nUploaded:- {get_readable_file_size(download.processed_bytes())} of {download.size()}" msg += f"\nSpeed:- {download.speed()}, \nETA:- {download.eta()} " # if hasattr(download, 'is_torrent'): try: msg += f"\nInfo: Seeders:- {download.aria_download().num_seeders}" \ f" & Peers:- {download.aria_download().connections}" except: pass if download.status() == MirrorStatus.STATUS_DOWNLOADING: msg += f"\nTo Stop:- /{BotCommands.CancelMirror} {download.gid()}" msg += "\n\n" return msg def get_readable_time(seconds: int) -> str: result = '' (days, remainder) = divmod(seconds, 86400) days = int(days) if days != 0: result += f'{days}d' (hours, remainder) = divmod(remainder, 3600) hours = int(hours) if hours != 0: result += f'{hours}h' (minutes, seconds) = divmod(remainder, 60) minutes = int(minutes) if minutes != 0: result += f'{minutes}m' seconds = int(seconds) result += f'{seconds}s' return result def is_url(url: str): url = re.findall(URL_REGEX, url) if url: return True return False def is_magnet(url: str): magnet = re.findall(MAGNET_REGEX, url) if magnet: return True return False def is_gdrive_link(url: str): return "drive.google.com" in url def is_mega_link(url: str): return "mega.nz" in url def get_mega_link_type(url: str): if "folder" in url: return "folder" elif "file" in url: return "file" elif "/#F!" in url: return "folder" return "file" def new_thread(fn): """To use as decorator to make a function call threaded. Needs import from threading import Thread""" def wrapper(*args, **kwargs): thread = threading.Thread(target=fn, args=args, kwargs=kwargs) thread.start() return thread return wrapper ================================================ FILE: bot/helper/ext_utils/exceptions.py ================================================ class DirectDownloadLinkException(Exception): """Not method found for extracting direct download link from the http link""" pass class NotSupportedExtractionArchive(Exception): """The archive format use is trying to extract is not supported""" pass ================================================ FILE: bot/helper/ext_utils/fs_utils.py ================================================ import sys from bot import aria2, LOGGER, DOWNLOAD_DIR, ARIA_CHILD_PROC, MEGA_CHILD_PROC import shutil import os import pathlib import magic import tarfile from .exceptions import NotSupportedExtractionArchive def clean_download(path: str): if os.path.exists(path): LOGGER.info(f"Cleaning download: {path}") shutil.rmtree(path) def start_cleanup(): try: shutil.rmtree(DOWNLOAD_DIR) except FileNotFoundError: pass def clean_all(): aria2.remove_all(True) try: shutil.rmtree(DOWNLOAD_DIR) except FileNotFoundError: pass def exit_clean_up(signal, frame): try: LOGGER.info("Please wait, while we clean up the downloads and stop running downloads") clean_all() ARIA_CHILD_PROC.kill() MEGA_CHILD_PROC.kill() sys.exit(0) except KeyboardInterrupt: LOGGER.warning("Force Exiting before the cleanup finishes!") ARIA_CHILD_PROC.kill() MEGA_CHILD_PROC.kill() sys.exit(1) def get_path_size(path): if os.path.isfile(path): return os.path.getsize(path) total_size = 0 for root, dirs, files in os.walk(path): for f in files: abs_path = os.path.join(root, f) total_size += os.path.getsize(abs_path) return total_size def tar(org_path): tar_path = org_path + ".tar" path = pathlib.PurePath(org_path) LOGGER.info(f'Tar: orig_path: {org_path}, tar_path: {tar_path}') tar = tarfile.open(tar_path, "w") tar.add(org_path, arcname=path.name) tar.close() return tar_path def get_base_name(orig_path: str): if orig_path.endswith(".tar.bz2"): return orig_path.replace(".tar.bz2", "") elif orig_path.endswith(".tar.gz"): return orig_path.replace(".tar.gz", "") elif orig_path.endswith(".bz2"): return orig_path.replace(".bz2", "") elif orig_path.endswith(".gz"): return orig_path.replace(".gz", "") elif orig_path.endswith(".tar"): return orig_path.replace(".tar", "") elif orig_path.endswith(".tbz2"): return orig_path.replace("tbz2", "") elif orig_path.endswith(".tgz"): return orig_path.replace(".tgz", "") elif orig_path.endswith(".zip"): return orig_path.replace(".zip", "") elif orig_path.endswith(".7z"): return orig_path.replace(".7z", "") elif orig_path.endswith(".Z"): return orig_path.replace(".Z", "") elif orig_path.endswith(".rar"): return orig_path.replace(".rar", "") elif orig_path.endswith(".iso"): return orig_path.replace(".iso", "") elif orig_path.endswith(".wim"): return orig_path.replace(".wim", "") elif orig_path.endswith(".cab"): return orig_path.replace(".cab", "") elif orig_path.endswith(".apm"): return orig_path.replace(".apm", "") elif orig_path.endswith(".arj"): return orig_path.replace(".arj", "") elif orig_path.endswith(".chm"): return orig_path.replace(".chm", "") elif orig_path.endswith(".cpio"): return orig_path.replace(".cpio", "") elif orig_path.endswith(".cramfs"): return orig_path.replace(".cramfs", "") elif orig_path.endswith(".deb"): return orig_path.replace(".deb", "") elif orig_path.endswith(".dmg"): return orig_path.replace(".dmg", "") elif orig_path.endswith(".fat"): return orig_path.replace(".fat", "") elif orig_path.endswith(".hfs"): return orig_path.replace(".hfs", "") elif orig_path.endswith(".lzh"): return orig_path.replace(".lzh", "") elif orig_path.endswith(".lzma"): return orig_path.replace(".lzma", "") elif orig_path.endswith(".lzma2"): return orig_path.replace(".lzma2", "") elif orig_path.endswith(".mbr"): return orig_path.replace(".mbr", "") elif orig_path.endswith(".msi"): return orig_path.replace(".msi", "") elif orig_path.endswith(".mslz"): return orig_path.replace(".mslz", "") elif orig_path.endswith(".nsis"): return orig_path.replace(".nsis", "") elif orig_path.endswith(".ntfs"): return orig_path.replace(".ntfs", "") elif orig_path.endswith(".rpm"): return orig_path.replace(".rpm", "") elif orig_path.endswith(".squashfs"): return orig_path.replace(".squashfs", "") elif orig_path.endswith(".udf"): return orig_path.replace(".udf", "") elif orig_path.endswith(".vhd"): return orig_path.replace(".vhd", "") elif orig_path.endswith(".xar"): return orig_path.replace(".xar", "") else: raise NotSupportedExtractionArchive('File format not supported for extraction') def get_mime_type(file_path): mime = magic.Magic(mime=True) mime_type = mime.from_file(file_path) mime_type = mime_type if mime_type else "text/plain" return mime_type ================================================ FILE: bot/helper/mirror_utils/__init__.py ================================================ ================================================ FILE: bot/helper/mirror_utils/download_utils/__init__.py ================================================ ================================================ FILE: bot/helper/mirror_utils/download_utils/aria2_download.py ================================================ from bot import aria2, download_dict_lock, STOP_DUPLICATE_MIRROR from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot.helper.ext_utils.bot_utils import * from .download_helper import DownloadHelper from bot.helper.mirror_utils.status_utils.aria_download_status import AriaDownloadStatus from bot.helper.telegram_helper.message_utils import * import threading from aria2p import API from time import sleep class AriaDownloadHelper(DownloadHelper): def __init__(self): super().__init__() @new_thread def __onDownloadStarted(self, api, gid): sleep(1) LOGGER.info(f"onDownloadStart: {gid}") dl = getDownloadByGid(gid) download = api.get_download(gid) self.name = download.name sname = download.name if STOP_DUPLICATE_MIRROR: if dl.getListener().isTar == True: sname = sname + ".tar" if dl.getListener().extract == True: smsg = None else: gdrive = GoogleDriveHelper(None) smsg, button = gdrive.drive_list(sname) if smsg: dl.getListener().onDownloadError(f'File is already available in drive.This download has been stopped.\n\n') sendMarkup("Here are the search results:", dl.getListener().bot, dl.getListener().update, button) aria2.remove([download]) return update_all_messages() def __onDownloadComplete(self, api: API, gid): LOGGER.info(f"onDownloadComplete: {gid}") dl = getDownloadByGid(gid) download = api.get_download(gid) if download.followed_by_ids: new_gid = download.followed_by_ids[0] new_download = api.get_download(new_gid) with download_dict_lock: download_dict[dl.uid()] = AriaDownloadStatus(new_gid, dl.getListener()) if new_download.is_torrent: download_dict[dl.uid()].is_torrent = True update_all_messages() LOGGER.info(f'Changed gid from {gid} to {new_gid}') else: if dl: threading.Thread(target=dl.getListener().onDownloadComplete).start() @new_thread def __onDownloadPause(self, api, gid): LOGGER.info(f"onDownloadPause: {gid}") dl = getDownloadByGid(gid) dl.getListener().onDownloadError('Download stopped by user!') @new_thread def __onDownloadStopped(self, api, gid): LOGGER.info(f"onDownloadStop: {gid}") dl = getDownloadByGid(gid) if dl: dl.getListener().onDownloadError('Your torrent has no seeds.Download stopped automatically.') @new_thread def __onDownloadError(self, api, gid): sleep(0.5) #sleep for split second to ensure proper dl gid update from onDownloadComplete LOGGER.info(f"onDownloadError: {gid}") dl = getDownloadByGid(gid) download = api.get_download(gid) error = download.error_message LOGGER.info(f"Download Error: {error}") if dl: dl.getListener().onDownloadError(error) def start_listener(self): aria2.listen_to_notifications(threaded=True, on_download_start=self.__onDownloadStarted, on_download_error=self.__onDownloadError, on_download_pause=self.__onDownloadPause, on_download_stop=self.__onDownloadStopped, on_download_complete=self.__onDownloadComplete) def add_download(self, link: str, path, listener, filename): if is_magnet(link): download = aria2.add_magnet(link, {'dir': path, 'out': filename}) else: download = aria2.add_uris([link], {'dir': path, 'out': filename}) if download.error_message: #no need to proceed further at this point listener.onDownloadError(download.error_message) return with download_dict_lock: download_dict[listener.uid] = AriaDownloadStatus(download.gid,listener) LOGGER.info(f"Started: {download.gid} DIR:{download.dir} ") ================================================ FILE: bot/helper/mirror_utils/download_utils/direct_link_generator.py ================================================ # Copyright (C) 2019 The Raphielscape Company LLC. # # Licensed under the Raphielscape Public License, Version 1.c (the "License"); # you may not use this file except in compliance with the License. # """ Helper Module containing various sites direct links generators. This module is copied and modified as per need from https://github.com/AvinashReddy3108/PaperplaneExtended . I hereby take no credit of the following code other than the modifications. See https://github.com/AvinashReddy3108/PaperplaneExtended/commits/master/userbot/modules/direct_links.py for original authorship. """ import json import re import math import urllib.parse from os import popen from random import choice from urllib.parse import urlparse import lk21 import requests import logging from bot import UPTOBOX_TOKEN from bs4 import BeautifulSoup from lk21.extractors.bypasser import Bypass from base64 import standard_b64encode from js2py import EvalJs from bot.helper.ext_utils.exceptions import DirectDownloadLinkException def direct_link_generator(link: str): """ direct links generator """ if not link: raise DirectDownloadLinkException("`No links found!`") elif 'zippyshare.com' in link: return zippy_share(link) elif 'yadi.sk' in link: return yandex_disk(link) elif 'cloud.mail.ru' in link: return cm_ru(link) elif 'mediafire.com' in link: return mediafire(link) elif 'uptobox.com' in link: return uptobox(link) elif 'osdn.net' in link: return osdn(link) elif 'github.com' in link: return github(link) elif 'fembed.com' in link: return fembed(link) elif 'femax20.com' in link: return fembed(link) elif 'feurl.com' in link: return fembed(link) else: raise DirectDownloadLinkException(f'No Direct link function found for {link}') def zippy_share(url: str) -> str: link = re.findall("https:/.(.*?).zippyshare", url)[0] response_content = (requests.get(url)).content bs_obj = BeautifulSoup(response_content, "lxml") try: js_script = bs_obj.find("div", {"class": "center",}).find_all( "script" )[1] except: js_script = bs_obj.find("div", {"class": "right",}).find_all( "script" )[0] js_content = re.findall(r'\.href.=."/(.*?)";', str(js_script)) js_content = 'var x = "/' + js_content[0] + '"' evaljs = EvalJs() setattr(evaljs, "x", None) evaljs.execute(js_content) js_content = getattr(evaljs, "x") return f"https://{link}.zippyshare.com{js_content}" def yandex_disk(url: str) -> str: """ Yandex.Disk direct links generator Based on https://github.com/wldhx/yadisk-direct""" try: link = re.findall(r'\bhttps?://.*yadi\.sk\S+', url)[0] except IndexError: reply = "`No Yandex.Disk links found`\n" return reply api = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}' try: dl_url = requests.get(api.format(link)).json()['href'] return dl_url except KeyError: raise DirectDownloadLinkException("`Error: File not found / Download limit reached`\n") def cm_ru(url: str) -> str: """ cloud.mail.ru direct links generator Using https://github.com/JrMasterModelBuilder/cmrudl.py""" reply = '' try: link = re.findall(r'\bhttps?://.*cloud\.mail\.ru\S+', url)[0] except IndexError: raise DirectDownloadLinkException("`No cloud.mail.ru links found`\n") command = f'vendor/cmrudl.py/cmrudl -s {link}' result = popen(command).read() result = result.splitlines()[-1] try: data = json.loads(result) except json.decoder.JSONDecodeError: raise DirectDownloadLinkException("`Error: Can't extract the link`\n") dl_url = data['download'] return dl_url def mediafire(url: str) -> str: """ MediaFire direct links generator """ try: link = re.findall(r'\bhttps?://.*mediafire\.com\S+', url)[0] except IndexError: raise DirectDownloadLinkException("`No MediaFire links found`\n") page = BeautifulSoup(requests.get(link).content, 'lxml') info = page.find('a', {'aria-label': 'Download file'}) dl_url = info.get('href') return dl_url def uptobox(url: str) -> str: try: link = re.findall(r'\bhttps?://.*uptobox\.com\S+', url)[0] except IndexError: raise DirectDownloadLinkException("`No Uptobox links found`\n") if UPTOBOX_TOKEN is None: logging.error('UPTOBOX_TOKEN not provided!') else: check = 'https://uptobox.com/api/user/me?token=%s' % (UPTOBOX_TOKEN) request = requests.get(check) info = request.json() premium = info["data"]["premium"] try: link = re.findall(r'\bhttp?://.*uptobox\.com/dl\S+', url)[0] logging.info('Uptobox direct link') dl_url = url except: if premium == 1: file_id = re.findall(r'\bhttps?://.*uptobox\.com/(\w+)', url)[0] file_link = 'https://uptobox.com/api/link?token=%s&file_code=%s' % (UPTOBOX_TOKEN, file_id) req = requests.get(file_link) result = req.json() dl_url = result['data']['dlLink'] else: file_id = re.findall(r'\bhttps?://.*uptobox\.com/(\w+)', url)[0] file_link = 'https://uptobox.com/api/link?token=%s&file_code=%s' % (UPTOBOX_TOKEN, file_id) req = requests.get(file_link) result = req.json() waiting_time = result["data"]["waiting"] + 1 waiting_token = result["data"]["waitingToken"] _countdown(waiting_time) file_link = 'https://uptobox.com/api/link?token=%s&file_code=%s&waitingToken=%s' % (UPTOBOX_TOKEN, file_id, waiting_token) req = requests.get(file_link) result = req.json() dl_url = result['data']['dlLink'] return dl_url def osdn(url: str) -> str: """ OSDN direct links generator """ osdn_link = 'https://osdn.net' try: link = re.findall(r'\bhttps?://.*osdn\.net\S+', url)[0] except IndexError: raise DirectDownloadLinkException("`No OSDN links found`\n") page = BeautifulSoup( requests.get(link, allow_redirects=True).content, 'lxml') info = page.find('a', {'class': 'mirror_link'}) link = urllib.parse.unquote(osdn_link + info['href']) mirrors = page.find('form', {'id': 'mirror-select-form'}).findAll('tr') urls = [] for data in mirrors[1:]: mirror = data.find('input')['value'] urls.append(re.sub(r'm=(.*)&f', f'm={mirror}&f', link)) return urls[0] def github(url: str) -> str: """ GitHub direct links generator """ try: re.findall(r'\bhttps?://.*github\.com.*releases\S+', url)[0] except IndexError: raise DirectDownloadLinkException("`No GitHub Releases links found`\n") download = requests.get(url, stream=True, allow_redirects=False) try: dl_url = download.headers["location"] return dl_url except KeyError: raise DirectDownloadLinkException("`Error: Can't extract the link`\n") def useragent(): """ useragent random setter """ useragents = BeautifulSoup( requests.get( 'https://developers.whatismybrowser.com/' 'useragents/explore/operating_system_name/android/').content, 'lxml').findAll('td', {'class': 'useragent'}) user_agent = choice(useragents) return user_agent.text def fembed(link: str) -> str: """ Fembed direct link generator Based on https://github.com/breakdowns/slam-mirrorbot """ bypasser = lk21.Bypass() dl_url=bypasser.bypass_fembed(link) lst_link = [] count = len(dl_url) for i in dl_url: lst_link.append(dl_url[i]) return lst_link[count-1] ================================================ FILE: bot/helper/mirror_utils/download_utils/direct_link_generator_license.md ================================================ RAPHIELSCAPE PUBLIC LICENSE Version 1.c, June 2019 Copyright (C) 2019 Raphielscape LLC. Copyright (C) 2019 Devscapes Open Source Holding GmbH. Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. RAPHIELSCAPE PUBLIC LICENSE A-1. DEFINITIONS 0. “This License” refers to version 1.c of the Raphielscape Public License. 1. “Copyright” also means copyright-like laws that apply to other kinds of works. 2. “The Work" refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations. 3. 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. 4. Source Form. The “source form” 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. 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. 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. 5. "The author" refers to "author" of the code, which is the one that made the particular code which exists inside of the Corresponding Source. 6. "Owner" refers to any parties which is made the early form of the Corresponding Source. A-2. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You must give any other recipients of the Work or Derivative Works a copy of this License; and 1. You must cause any modified files to carry prominent notices stating that You changed the files; and 2. You must retain, in the Source form of any Derivative Works that You distribute, this license, all copyright, patent, trademark, authorships and attribution notices from the Source form of the Work; and 3. Respecting the author and owner of works that are distributed in any way. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. B. DISCLAIMER OF WARRANTY THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. C. REVISED VERSION OF THIS LICENSE The Devscapes Open Source Holding GmbH. may publish revised and/or new versions of the Raphielscape 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 Raphielscape 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 Devscapes Open Source Holding GmbH. If the Program does not specify a version number of the Raphielscape Public License, you may choose any version ever published by the Devscapes Open Source Holding GmbH. END OF LICENSE ================================================ FILE: bot/helper/mirror_utils/download_utils/download_helper.py ================================================ # An abstract class which will be inherited by the tool specific classes like aria2_helper or mega_download_helper import threading class MethodNotImplementedError(NotImplementedError): def __init__(self): super(self, 'Not implemented method') class DownloadHelper: def __init__(self): self.__name = '' # Name of the download; empty string if no download has been started self.__size = 0.0 # Size of the download self.downloaded_bytes = 0.0 # Bytes downloaded self.speed = 0.0 # Download speed in bytes per second self.progress = 0.0 self.progress_string = '0.00%' self.eta = 0 # Estimated time of download complete self.eta_string = '0s' # A listener class which have event callbacks self._resource_lock = threading.Lock() def add_download(self, link: str, path): raise MethodNotImplementedError def cancel_download(self): # Returns None if successfully cancelled, else error string raise MethodNotImplementedError ================================================ FILE: bot/helper/mirror_utils/download_utils/mega_download.py ================================================ import threading from bot import LOGGER, download_dict, download_dict_lock from .download_helper import DownloadHelper from ..status_utils.mega_status import MegaDownloadStatus from megasdkrestclient import MegaSdkRestClient, constants from bot.helper.ext_utils.bot_utils import setInterval from pathlib import Path class MegaDownloader: POLLING_INTERVAL = 2 def __init__(self, listener): super().__init__() self.__listener = listener self.__name = "" self.__gid = '' self.__resource_lock = threading.Lock() self.__mega_client = MegaSdkRestClient('http://localhost:6090') self.__periodic = None self.__downloaded_bytes = 0 self.__progress = 0 self.__size = 0 @property def progress(self): with self.__resource_lock: return self.__progress @property def downloaded_bytes(self): with self.__resource_lock: return self.__downloaded_bytes @property def size(self): with self.__resource_lock: return self.__size @property def gid(self): with self.__resource_lock: return self.__gid @property def name(self): with self.__resource_lock: return self.__name @property def download_speed(self): if self.gid is not None: return self.__mega_client.getDownloadInfo(self.gid)['speed'] def __onDownloadStart(self, name, size, gid): self.__periodic = setInterval(self.POLLING_INTERVAL, self.__onInterval) with download_dict_lock: download_dict[self.__listener.uid] = MegaDownloadStatus(self, self.__listener) with self.__resource_lock: self.__name = name self.__size = size self.__gid = gid self.__listener.onDownloadStarted() def __onInterval(self): dlInfo = self.__mega_client.getDownloadInfo(self.gid) if (dlInfo['state'] == constants.State.TYPE_STATE_COMPLETED or dlInfo[ 'state'] == constants.State.TYPE_STATE_CANCELED or dlInfo[ 'state'] == constants.State.TYPE_STATE_FAILED) and self.__periodic is not None: self.__periodic.cancel() if dlInfo['state'] == constants.State.TYPE_STATE_COMPLETED: self.__onDownloadComplete() return if dlInfo['state'] == constants.State.TYPE_STATE_CANCELED: self.__onDownloadError('Cancelled by user') return if dlInfo['state'] == constants.State.TYPE_STATE_FAILED: self.__onDownloadError(dlInfo['error_string']) return self.__onDownloadProgress(dlInfo['completed_length'], dlInfo['total_length']) def __onDownloadProgress(self, current, total): with self.__resource_lock: self.__downloaded_bytes = current try: self.__progress = current / total * 100 except ZeroDivisionError: self.__progress = 0 def __onDownloadError(self, error): self.__listener.onDownloadError(error) def __onDownloadComplete(self): self.__listener.onDownloadComplete() def add_download(self, link, path): Path(path).mkdir(parents=True, exist_ok=True) dl = self.__mega_client.addDl(link, path) gid = dl['gid'] info = self.__mega_client.getDownloadInfo(gid) file_name = info['name'] file_size = info['total_length'] self.__onDownloadStart(file_name, file_size, gid) LOGGER.info(f'Started mega download with gid: {gid}') def cancel_download(self): LOGGER.info(f'Cancelling download on user request: {self.gid}') self.__mega_client.cancelDl(self.gid) ================================================ FILE: bot/helper/mirror_utils/download_utils/telegram_downloader.py ================================================ import logging import threading import time from bot import LOGGER, download_dict, download_dict_lock, app from .download_helper import DownloadHelper from ..status_utils.telegram_download_status import TelegramDownloadStatus global_lock = threading.Lock() GLOBAL_GID = set() logging.getLogger("pyrogram").setLevel(logging.WARNING) class TelegramDownloadHelper(DownloadHelper): def __init__(self, listener): super().__init__() self.__listener = listener self.__resource_lock = threading.RLock() self.__name = "" self.__gid = '' self.__start_time = time.time() self.__user_bot = app self.__is_cancelled = False @property def gid(self): with self.__resource_lock: return self.__gid @property def download_speed(self): with self.__resource_lock: return self.downloaded_bytes / (time.time() - self.__start_time) def __onDownloadStart(self, name, size, file_id): with download_dict_lock: download_dict[self.__listener.uid] = TelegramDownloadStatus(self, self.__listener) with global_lock: GLOBAL_GID.add(file_id) with self.__resource_lock: self.name = name self.size = size self.__gid = file_id self.__listener.onDownloadStarted() def __onDownloadProgress(self, current, total): if self.__is_cancelled: self.__onDownloadError('Cancelled by user!') self.__user_bot.stop_transmission() return with self.__resource_lock: self.downloaded_bytes = current try: self.progress = current / self.size * 100 except ZeroDivisionError: self.progress = 0 def __onDownloadError(self, error): with global_lock: try: GLOBAL_GID.remove(self.gid) except KeyError: pass self.__listener.onDownloadError(error) def __onDownloadComplete(self): with global_lock: GLOBAL_GID.remove(self.gid) self.__listener.onDownloadComplete() def __download(self, message, path): download = self.__user_bot.download_media(message, progress=self.__onDownloadProgress, file_name=path) if download is not None: self.__onDownloadComplete() else: if not self.__is_cancelled: self.__onDownloadError('Internal error occurred') def add_download(self, message, path, filename): _message = self.__user_bot.get_messages(message.chat.id, message.message_id) media = None media_array = [_message.document, _message.video, _message.audio] for i in media_array: if i is not None: media = i break if media is not None: with global_lock: # For avoiding locking the thread lock for long time unnecessarily download = media.file_id not in GLOBAL_GID if filename == "": name = media.file_name else: name = filename path = path + name if download: self.__onDownloadStart(name, media.file_size, media.file_id) LOGGER.info(f'Downloading telegram file with id: {media.file_id}') threading.Thread(target=self.__download, args=(_message, path)).start() else: self.__onDownloadError('File already being downloaded!') else: self.__onDownloadError('No document in the replied message') def cancel_download(self): LOGGER.info(f'Cancelling download on user request: {self.gid}') self.__is_cancelled = True ================================================ FILE: bot/helper/mirror_utils/download_utils/youtube_dl_download_helper.py ================================================ from .download_helper import DownloadHelper import time from yt_dlp import YoutubeDL, DownloadError from bot import download_dict_lock, download_dict from ..status_utils.youtube_dl_download_status import YoutubeDLDownloadStatus import logging import re import threading LOGGER = logging.getLogger(__name__) class MyLogger: def __init__(self, obj): self.obj = obj def debug(self, msg): LOGGER.debug(msg) # Hack to fix changing changing extension match = re.search(r'.ffmpeg..Merging formats into..(.*?).$', msg) if match and not self.obj.is_playlist: newname = match.group(1) newname = newname.split("/") newname = newname[-1] self.obj.name = newname @staticmethod def warning(msg): LOGGER.warning(msg) @staticmethod def error(msg): LOGGER.error(msg) class YoutubeDLHelper(DownloadHelper): def __init__(self, listener): super().__init__() self.name = "" self.__start_time = time.time() self.__listener = listener self.__gid = "" self.opts = { 'progress_hooks': [self.__onDownloadProgress], 'logger': MyLogger(self), 'usenetrc': True } self.__download_speed = 0 self.download_speed_readable = '' self.downloaded_bytes = 0 self.size = 0 self.is_playlist = False self.last_downloaded = 0 self.is_cancelled = False self.vid_id = '' self.__resource_lock = threading.RLock() @property def download_speed(self): with self.__resource_lock: return self.__download_speed @property def gid(self): with self.__resource_lock: return self.__gid def __onDownloadProgress(self, d): if self.is_cancelled: raise ValueError("Cancelling Download..") if d['status'] == "finished": if self.is_playlist: self.last_downloaded = 0 elif d['status'] == "downloading": with self.__resource_lock: self.__download_speed = d['speed'] try: tbyte = d['total_bytes'] except KeyError: tbyte = d['total_bytes_estimate'] if self.is_playlist: progress = d['downloaded_bytes'] / tbyte chunk_size = d['downloaded_bytes'] - self.last_downloaded self.last_downloaded = tbyte * progress self.downloaded_bytes += chunk_size try: self.progress = (self.downloaded_bytes / self.size) * 100 except ZeroDivisionError: pass else: self.download_speed_readable = d['_speed_str'] self.downloaded_bytes = d['downloaded_bytes'] def __onDownloadStart(self): with download_dict_lock: download_dict[self.__listener.uid] = YoutubeDLDownloadStatus(self, self.__listener) def __onDownloadComplete(self): self.__listener.onDownloadComplete() def onDownloadError(self, error): self.__listener.onDownloadError(error) def extractMetaData(self, link, qual, name): if "hotstar" in link or "sonyliv" in link: self.opts['geo_bypass_country'] = 'IN' with YoutubeDL(self.opts) as ydl: try: result = ydl.extract_info(link, download=False) if name == "": name = ydl.prepare_filename(result) else: name = name # noobway hack for changing extension after converting to mp3 if qual == "audio": name = name.replace(".mp4", ".mp3").replace(".webm", ".mp3") except DownloadError as e: self.onDownloadError(str(e)) return if result.get('direct'): return None if 'entries' in result: video = result['entries'][0] for v in result['entries']: if v and v.get('filesize'): self.size += float(v['filesize']) # For playlists, ydl.prepare-filename returns the following format: -.NA self.name = name.split(f"-{result['id']}")[0] self.vid_id = video.get('id') self.is_playlist = True else: video = result if video.get('filesize'): self.size = float(video.get('filesize')) self.name = name self.vid_id = video.get('id') return video def __download(self, link): try: with YoutubeDL(self.opts) as ydl: try: ydl.download([link]) except DownloadError as e: self.onDownloadError(str(e)) return self.__onDownloadComplete() except ValueError: LOGGER.info("Download Cancelled by User!") self.onDownloadError("Download Cancelled by User!") def add_download(self, link, path, qual, name): pattern = '^.*(youtu\.be\/|youtube.com\/)(playlist?)' if re.match(pattern, link): self.opts['ignoreerrors'] = True self.__onDownloadStart() self.extractMetaData(link, qual, name) LOGGER.info(f"Downloading with YT-DL: {link}") self.__gid = f"{self.vid_id}{self.__listener.uid}" if qual == "audio": self.opts['format'] = 'bestaudio/best' self.opts['postprocessors'] = [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192',}] else: self.opts['format'] = qual if not self.is_playlist: self.opts['outtmpl'] = f"{path}/{self.name}" else: self.opts['outtmpl'] = f"{path}/{self.name}/%(title)s.%(ext)s" self.__download(link) def cancel_download(self): self.is_cancelled = True ================================================ FILE: bot/helper/mirror_utils/status_utils/__init__.py ================================================ ================================================ FILE: bot/helper/mirror_utils/status_utils/aria_download_status.py ================================================ from bot import aria2, DOWNLOAD_DIR, LOGGER from bot.helper.ext_utils.bot_utils import MirrorStatus from .status import Status def get_download(gid): return aria2.get_download(gid) class AriaDownloadStatus(Status): def __init__(self, gid, listener): super().__init__() self.upload_name = None self.is_archiving = False self.__gid = gid self.__download = get_download(self.__gid) self.__uid = listener.uid self.__listener = listener self.message = listener.message self.last = None self.is_waiting = False self.is_extracting = False def __update(self): self.__download = get_download(self.__gid) def progress(self): """ Calculates the progress of the mirror (upload or download) :return: returns progress in percentage """ self.__update() return self.__download.progress_string() def size_raw(self): """ Gets total size of the mirror file/folder :return: total size of mirror """ return self.aria_download().total_length def processed_bytes(self): return self.aria_download().completed_length def speed(self): return self.aria_download().download_speed_string() def name(self): return self.aria_download().name def path(self): return f"{DOWNLOAD_DIR}{self.__uid}" def size(self): return self.aria_download().total_length_string() def eta(self): return self.aria_download().eta_string() def status(self): download = self.aria_download() if download.is_waiting: status = MirrorStatus.STATUS_WAITING elif download.is_paused: status = MirrorStatus.STATUS_CANCELLED elif download.has_failed: status = MirrorStatus.STATUS_FAILED else: status = MirrorStatus.STATUS_DOWNLOADING return status def aria_download(self): self.__update() return self.__download def download(self): return self def updateName(self,name): self.__name = name def updateGid(self,gid): self.__gid = gid def getListener(self): return self.__listener def uid(self): return self.__uid def gid(self): self.__update() return self.__gid def cancel_download(self): LOGGER.info(f"Cancelling Download: {self.name()}") download = self.aria_download() if download.is_waiting: aria2.remove([download]) self.__listener.onDownloadError("Cancelled by user") return if len(download.followed_by_ids) != 0: downloads = aria2.get_downloads(download.followed_by_ids) aria2.pause(downloads) aria2.pause([download]) ================================================ FILE: bot/helper/mirror_utils/status_utils/extract_status.py ================================================ from .status import Status from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus class ExtractStatus(Status): def __init__(self, name, path, size): self.__name = name self.__path = path self.__size = size # The progress of extract function cannot be tracked. So we just return dummy values. # If this is possible in future,we should implement it def progress(self): return '0' def speed(self): return '0' def name(self): return self.__name def path(self): return self.__path def size(self): return get_readable_file_size(self.__size) def eta(self): return '0s' def status(self): return MirrorStatus.STATUS_EXTRACTING def processed_bytes(self): return 0 ================================================ FILE: bot/helper/mirror_utils/status_utils/gdownload_status.py ================================================ import bot from .status import Status from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from bot import DOWNLOAD_DIR class DownloadStatus(Status): def __init__(self, obj, size, listener, gid): self.dobj = obj self.__dsize = size self.uid = listener.uid self.message = listener.message self.__dgid = gid def path(self): return f"{DOWNLOAD_DIR}{self.uid}" def processed_bytes(self): return self.dobj.downloaded_bytes def size_raw(self): return self.__dsize def size(self): return get_readable_file_size(self.__dsize) def status(self): return MirrorStatus.STATUS_DOWNLOADING def name(self): return self.dobj.name def gid(self) -> str: return self.__dgid def progress_raw(self): try: return self.dobj.downloaded_bytes / self.__dsize * 100 except ZeroDivisionError: return 0 def progress(self): return f'{round(self.progress_raw(), 2)}%' def speed_raw(self): """ :return: Download speed in Bytes/Seconds """ return self.dobj.dspeed() def speed(self): return f'{get_readable_file_size(self.speed_raw())}/s' def eta(self): try: seconds = (self.__dsize - self.dobj.downloaded_bytes) / self.speed_raw() return f'{get_readable_time(seconds)}' except ZeroDivisionError: return '-' def download(self): return self.dobj ================================================ FILE: bot/helper/mirror_utils/status_utils/listeners.py ================================================ class MirrorListeners: def __init__(self, context, update): self.bot = context self.update = update self.message = update.message self.uid = self.message.message_id def onDownloadStarted(self): raise NotImplementedError def onDownloadProgress(self): raise NotImplementedError def onDownloadComplete(self): raise NotImplementedError def onDownloadError(self, error: str): raise NotImplementedError def onUploadStarted(self): raise NotImplementedError def onUploadProgress(self): raise NotImplementedError def onUploadComplete(self, link: str): raise NotImplementedError def onUploadError(self, error: str): raise NotImplementedError ================================================ FILE: bot/helper/mirror_utils/status_utils/mega_status.py ================================================ from bot import DOWNLOAD_DIR from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from .status import Status class MegaDownloadStatus(Status): def __init__(self, obj, listener): self.obj = obj self.uid = listener.uid self.message = listener.message def gid(self): return self.obj.gid def path(self): return f"{DOWNLOAD_DIR}{self.uid}" def processed_bytes(self): return self.obj.downloaded_bytes def size_raw(self): return self.obj.size def size(self): return get_readable_file_size(self.size_raw()) def status(self): return MirrorStatus.STATUS_DOWNLOADING def name(self): return self.obj.name def progress_raw(self): return self.obj.progress def progress(self): return f'{round(self.progress_raw(), 2)}%' def speed_raw(self): """ :return: Download speed in Bytes/Seconds """ return self.obj.download_speed def speed(self): return f'{get_readable_file_size(self.speed_raw())}/s' def eta(self): try: seconds = (self.size_raw() - self.processed_bytes()) / self.speed_raw() return f'{get_readable_time(seconds)}' except ZeroDivisionError: return '-' def download(self): return self.obj ================================================ FILE: bot/helper/mirror_utils/status_utils/status.py ================================================ # Generic status class. All other status classes must inherit this class class Status: def progress(self): """ Calculates the progress of the mirror (upload or download) :return: progress in percentage """ raise NotImplementedError def speed(self): """:return: speed in bytes per second""" raise NotImplementedError def name(self): """:return name of file/directory being processed""" raise NotImplementedError def path(self): """:return path of the file/directory""" raise NotImplementedError def size(self): """:return Size of file folder""" raise NotImplementedError def eta(self): """:return ETA of the process to complete""" raise NotImplementedError def status(self): """:return String describing what is the object of this class will be tracking (upload/download/something else) """ raise NotImplementedError def processed_bytes(self): """:return The size of file that has been processed (downloaded/uploaded/archived)""" raise NotImplementedError ================================================ FILE: bot/helper/mirror_utils/status_utils/tar_status.py ================================================ from .status import Status from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus class TarStatus(Status): def __init__(self, name, path, size): self.__name = name self.__path = path self.__size = size # The progress of Tar function cannot be tracked. So we just return dummy values. # If this is possible in future,we should implement it def progress(self): return '0' def speed(self): return '0' def name(self): return self.__name def path(self): return self.__path def size(self): return get_readable_file_size(self.__size) def eta(self): return '0s' def status(self): return MirrorStatus.STATUS_ARCHIVING def processed_bytes(self): return 0 ================================================ FILE: bot/helper/mirror_utils/status_utils/telegram_download_status.py ================================================ from bot import DOWNLOAD_DIR from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from .status import Status class TelegramDownloadStatus(Status): def __init__(self, obj, listener): self.obj = obj self.uid = listener.uid self.message = listener.message def gid(self): return self.obj.gid def path(self): return f"{DOWNLOAD_DIR}{self.uid}" def processed_bytes(self): return self.obj.downloaded_bytes def size_raw(self): return self.obj.size def size(self): return get_readable_file_size(self.size_raw()) def status(self): return MirrorStatus.STATUS_DOWNLOADING def name(self): return self.obj.name def progress_raw(self): return self.obj.progress def progress(self): return f'{round(self.progress_raw(), 2)}%' def speed_raw(self): """ :return: Download speed in Bytes/Seconds """ return self.obj.download_speed def speed(self): return f'{get_readable_file_size(self.speed_raw())}/s' def eta(self): try: seconds = (self.size_raw() - self.processed_bytes()) / self.speed_raw() return f'{get_readable_time(seconds)}' except ZeroDivisionError: return '-' def download(self): return self.obj ================================================ FILE: bot/helper/mirror_utils/status_utils/upload_status.py ================================================ from .status import Status from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from bot import DOWNLOAD_DIR class UploadStatus(Status): def __init__(self, obj, size, listener): self.obj = obj self.__size = size self.uid = listener.uid self.message = listener.message def path(self): return f"{DOWNLOAD_DIR}{self.uid}" def processed_bytes(self): return self.obj.uploaded_bytes def size_raw(self): return self.__size def size(self): return get_readable_file_size(self.__size) def status(self): return MirrorStatus.STATUS_UPLOADING def name(self): return self.obj.name def progress_raw(self): try: return self.obj.uploaded_bytes / self.__size * 100 except ZeroDivisionError: return 0 def progress(self): return f'{round(self.progress_raw(), 2)}%' def speed_raw(self): """ :return: Upload speed in Bytes/Seconds """ return self.obj.speed() def speed(self): return f'{get_readable_file_size(self.speed_raw())}/s' def eta(self): try: seconds = (self.__size - self.obj.uploaded_bytes) / self.speed_raw() return f'{get_readable_time(seconds)}' except ZeroDivisionError: return '-' ================================================ FILE: bot/helper/mirror_utils/status_utils/youtube_dl_download_status.py ================================================ from bot import DOWNLOAD_DIR from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from .status import Status from bot.helper.ext_utils.fs_utils import get_path_size class YoutubeDLDownloadStatus(Status): def __init__(self, obj, listener): self.obj = obj self.uid = listener.uid self.message = listener.message def gid(self): return self.obj.gid def path(self): return f"{DOWNLOAD_DIR}{self.uid}" def processed_bytes(self): if self.obj.downloaded_bytes != 0: return self.obj.downloaded_bytes else: return get_path_size(f"{DOWNLOAD_DIR}{self.uid}") def size_raw(self): return self.obj.size def size(self): return get_readable_file_size(self.size_raw()) def status(self): return MirrorStatus.STATUS_DOWNLOADING def name(self): return self.obj.name def progress_raw(self): return self.obj.progress def progress(self): return f'{round(self.progress_raw(), 2)}%' def speed_raw(self): """ :return: Download speed in Bytes/Seconds """ return self.obj.download_speed def speed(self): return f'{get_readable_file_size(self.speed_raw())}/s' def eta(self): try: seconds = (self.size_raw() - self.processed_bytes()) / self.speed_raw() return f'{get_readable_time(seconds)}' except: return '-' def download(self): return self.obj ================================================ FILE: bot/helper/mirror_utils/upload_utils/__init__.py ================================================ ================================================ FILE: bot/helper/mirror_utils/upload_utils/gdriveTools.py ================================================ import os import io import pickle import urllib.parse as urlparse from urllib.parse import parse_qs import re import json import requests import logging from random import randrange from google.auth.transport.requests import Request from google.oauth2 import service_account from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload from tenacity import * from telegram import InlineKeyboardMarkup from bot.helper.telegram_helper import button_build from telegraph import Telegraph from bot import parent_id, DOWNLOAD_DIR, IS_TEAM_DRIVE, INDEX_URL, \ USE_SERVICE_ACCOUNTS, download_dict, telegraph_token, BUTTON_THREE_NAME, BUTTON_THREE_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, SHORTENER, SHORTENER_API from bot.helper.ext_utils.bot_utils import * from bot.helper.ext_utils.fs_utils import get_mime_type, get_path_size LOGGER = logging.getLogger(__name__) logging.getLogger('googleapiclient.discovery').setLevel(logging.ERROR) if USE_SERVICE_ACCOUNTS: SERVICE_ACCOUNT_INDEX = randrange(len(os.listdir("accounts"))) TELEGRAPHLIMIT = 95 class GoogleDriveHelper: def __init__(self, name=None, listener=None): self.__G_DRIVE_TOKEN_FILE = "token.pickle" # Check https://developers.google.com/drive/scopes for all available scopes self.__OAUTH_SCOPE = ['https://www.googleapis.com/auth/drive'] # Redirect URI for installed apps, can be left as is self.__REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob" self.__G_DRIVE_DIR_MIME_TYPE = "application/vnd.google-apps.folder" self.__G_DRIVE_BASE_DOWNLOAD_URL = "https://drive.google.com/uc?id={}&export=download" self.__G_DRIVE_DIR_BASE_DOWNLOAD_URL = "https://drive.google.com/drive/folders/{}" self.__listener = listener self.__service = self.authorize() self.__listener = listener self._file_uploaded_bytes = 0 self._file_downloaded_bytes = 0 self.uploaded_bytes = 0 self.downloaded_bytes = 0 self.UPDATE_INTERVAL = 5 self.start_time = 0 self.total_time = 0 self.dtotal_time = 0 self._should_update = True self.is_uploading = True self.is_cancelled = False self.status = None self.dstatus = None self.updater = None self.name = name self.update_interval = 3 self.telegraph_content = [] self.path = [] self.total_bytes = 0 self.total_files = 0 self.total_folders = 0 def cancel(self): self.is_cancelled = True self.is_uploading = False def speed(self): """ It calculates the average upload speed and returns it in bytes/seconds unit :return: Upload speed in bytes/second """ try: return self.uploaded_bytes / self.total_time except ZeroDivisionError: return 0 def dspeed(self): try: return self.downloaded_bytes / self.dtotal_time except ZeroDivisionError: return 0 @staticmethod def getIdFromUrl(link: str): if "folders" in link or "file" in link: regex = r"https://drive\.google\.com/(drive)?/?u?/?\d?/?(mobile)?/?(file)?(folders)?/?d?/([-\w]+)[?+]?/?(w+)?" res = re.search(regex,link) if res is None: raise IndexError("GDrive ID not found.") return res.group(5) parsed = urlparse.urlparse(link) return parse_qs(parsed.query)['id'][0] @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def _on_upload_progress(self): if self.status is not None: chunk_size = self.status.total_size * self.status.progress() - self._file_uploaded_bytes self._file_uploaded_bytes = self.status.total_size * self.status.progress() LOGGER.debug(f'Uploading {self.name}, chunk size: {get_readable_file_size(chunk_size)}') self.uploaded_bytes += chunk_size self.total_time += self.update_interval def __upload_empty_file(self, path, file_name, mime_type, parent_id=None): media_body = MediaFileUpload(path, mimetype=mime_type, resumable=False) file_metadata = { 'name': file_name, 'description': 'mirrored', 'mimeType': mime_type, } if parent_id is not None: file_metadata['parents'] = [parent_id] return self.__service.files().create(supportsTeamDrives=True, body=file_metadata, media_body=media_body).execute() def switchServiceAccount(self): global SERVICE_ACCOUNT_INDEX service_account_count = len(os.listdir("accounts")) if SERVICE_ACCOUNT_INDEX == service_account_count - 1: SERVICE_ACCOUNT_INDEX = 0 SERVICE_ACCOUNT_INDEX += 1 LOGGER.info(f"Switching to {SERVICE_ACCOUNT_INDEX}.json service account") self.__service = self.authorize() @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def __set_permission(self, drive_id): permissions = { 'role': 'reader', 'type': 'anyone', 'value': None, 'withLink': True } return self.__service.permissions().create(supportsTeamDrives=True, fileId=drive_id, body=permissions).execute() @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def upload_file(self, file_path, file_name, mime_type, parent_id): # File body description file_metadata = { 'name': file_name, 'description': 'mirror', 'mimeType': mime_type, } if parent_id is not None: file_metadata['parents'] = [parent_id] if os.path.getsize(file_path) == 0: media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=False) response = self.__service.files().create(supportsTeamDrives=True, body=file_metadata, media_body=media_body).execute() if not IS_TEAM_DRIVE: self.__set_permission(response['id']) drive_file = self.__service.files().get(supportsTeamDrives=True, fileId=response['id']).execute() download_url = self.__G_DRIVE_BASE_DOWNLOAD_URL.format(drive_file.get('id')) return download_url media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True, chunksize=50 * 1024 * 1024) # Insert a file drive_file = self.__service.files().create(supportsTeamDrives=True, body=file_metadata, media_body=media_body) response = None while response is None: if self.is_cancelled: return None try: self.status, response = drive_file.next_chunk() except HttpError as err: if err.resp.get('content-type', '').startswith('application/json'): reason = json.loads(err.content).get('error').get('errors')[0].get('reason') if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded': if USE_SERVICE_ACCOUNTS: self.switchServiceAccount() LOGGER.info(f"Got: {reason}, Trying Again.") return self.upload_file(file_path, file_name, mime_type, parent_id) else: raise err self._file_uploaded_bytes = 0 # Insert new permissions if not IS_TEAM_DRIVE: self.__set_permission(response['id']) # Define file instance and get url for download drive_file = self.__service.files().get(supportsTeamDrives=True, fileId=response['id']).execute() download_url = self.__G_DRIVE_BASE_DOWNLOAD_URL.format(drive_file.get('id')) return download_url def deletefile(self, link: str): try: file_id = self.getIdFromUrl(link) except (KeyError,IndexError): msg = "Google drive ID could not be found in the provided link" return msg msg = '' try: res = self.__service.files().delete(fileId=file_id, supportsTeamDrives=IS_TEAM_DRIVE).execute() msg = "Successfully deleted" except HttpError as err: LOGGER.error(str(err)) if "File not found" in str(err): msg = "No such file exist" else: msg = "Something went wrong check log" finally: return msg def upload(self, file_name: str): if USE_SERVICE_ACCOUNTS: self.service_account_count = len(os.listdir("accounts")) self.__listener.onUploadStarted() file_dir = f"{DOWNLOAD_DIR}{self.__listener.message.message_id}" file_path = f"{file_dir}/{file_name}" size = get_readable_file_size(get_path_size(file_path)) LOGGER.info("Uploading File: " + file_path) self.start_time = time.time() self.updater = setInterval(self.update_interval, self._on_upload_progress) if os.path.isfile(file_path): try: mime_type = get_mime_type(file_path) link = self.upload_file(file_path, file_name, mime_type, parent_id) if link is None: raise Exception('Upload has been manually cancelled') LOGGER.info("Uploaded To G-Drive:- " + file_path) except Exception as e: if isinstance(e, RetryError): LOGGER.info(f"Total Attempts: {e.last_attempt.attempt_number}") err = e.last_attempt.exception() else: err = e LOGGER.error(err) self.__listener.onUploadError(str(err)) return finally: self.updater.cancel() else: try: dir_id = self.create_directory(os.path.basename(os.path.abspath(file_name)), parent_id) result = self.upload_dir(file_path, dir_id) if result is None: raise Exception('Upload has been manually cancelled!') LOGGER.info("Uploaded To G-Drive: " + file_name) link = f"https://drive.google.com/folderview?id={dir_id}" except Exception as e: if isinstance(e, RetryError): LOGGER.info(f"Total Attempts: {e.last_attempt.attempt_number}") err = e.last_attempt.exception() else: err = e LOGGER.error(err) self.__listener.onUploadError(str(err)) return finally: self.updater.cancel() LOGGER.info(download_dict) self.__listener.onUploadComplete(link, size) LOGGER.info("Deleting downloaded file/folder..") return link @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def copyFile(self, file_id, dest_id): body = { 'parents': [dest_id] } try: res = self.__service.files().copy(supportsAllDrives=True,fileId=file_id,body=body).execute() return res except HttpError as err: if err.resp.get('content-type', '').startswith('application/json'): reason = json.loads(err.content).get('error').get('errors')[0].get('reason') if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded': if USE_SERVICE_ACCOUNTS: self.switchServiceAccount() LOGGER.info(f"Got: {reason}, Trying Again.") return self.copyFile(file_id,dest_id) else: raise err @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def getFileMetadata(self,file_id): return self.__service.files().get(supportsAllDrives=True, fileId=file_id, fields="name,id,mimeType,size").execute() @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def getFilesByFolderId(self,folder_id): page_token = None q = f"'{folder_id}' in parents" files = [] while True: response = self.__service.files().list(supportsTeamDrives=True, includeTeamDriveItems=True, q=q, spaces='drive', pageSize=200, fields='nextPageToken, files(id, name, mimeType,size)', pageToken=page_token).execute() for file in response.get('files', []): files.append(file) page_token = response.get('nextPageToken', None) if page_token is None: break return files def clone(self, link): self.transferred_size = 0 self.total_files = 0 self.total_folders = 0 try: file_id = self.getIdFromUrl(link) except (KeyError,IndexError): msg = "Google drive ID could not be found in the provided link" return msg, "" msg = "" LOGGER.info(f"File ID: {file_id}") try: meta = self.getFileMetadata(file_id) if meta.get("mimeType") == self.__G_DRIVE_DIR_MIME_TYPE: dir_id = self.create_directory(meta.get('name'), parent_id) result = self.cloneFolder(meta.get('name'), meta.get('name'), meta.get('id'), dir_id) msg += f'Name:- {meta.get("name")}\nSize:- {get_readable_file_size(self.transferred_size)}' durl = self.__G_DRIVE_DIR_BASE_DOWNLOAD_URL.format(dir_id) buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={durl}&format=text').text buttons.buildbutton("Drive Link", surl) else: buttons.buildbutton("Drive Link", durl) if INDEX_URL is not None: url_path = requests.utils.quote(f'{meta.get("name")}') url = f'{INDEX_URL}/{url_path}/' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={url}&format=text').text buttons.buildbutton("Index Link", siurl) else: buttons.buildbutton("Index Link", url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: buttons.buildbutton(f"{BUTTON_FOUR_NAME}", f"{BUTTON_FOUR_URL}") if BUTTON_FIVE_NAME is not None and BUTTON_FIVE_URL is not None: buttons.buildbutton(f"{BUTTON_FIVE_NAME}", f"{BUTTON_FIVE_URL}") else: file = self.copyFile(meta.get('id'), parent_id) msg += f'Name:- {file.get("name")}' durl = self.__G_DRIVE_BASE_DOWNLOAD_URL.format(file.get("id")) buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={durl}&format=text').text buttons.buildbutton("Drive Link", surl) else: buttons.buildbutton("Drive Link", durl) try: msg += f'\nSize:- {get_readable_file_size(int(meta.get("size")))}' except TypeError: pass if INDEX_URL is not None: url_path = requests.utils.quote(f'{file.get("name")}') url = f'{INDEX_URL}/{url_path}' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={url}&format=text').text buttons.buildbutton("Index Link", siurl) else: buttons.buildbutton("Index Link", url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: buttons.buildbutton(f"{BUTTON_FOUR_NAME}", f"{BUTTON_FOUR_URL}") if BUTTON_FIVE_NAME is not None and BUTTON_FIVE_URL is not None: buttons.buildbutton(f"{BUTTON_FIVE_NAME}", f"{BUTTON_FIVE_URL}") except Exception as err: if isinstance(err, RetryError): LOGGER.info(f"Total Attempts:- {err.last_attempt.attempt_number}") err = err.last_attempt.exception() err = str(err).replace('>', '').replace('<', '') LOGGER.error(err) return err, "" return msg, InlineKeyboardMarkup(buttons.build_menu(2)) def cloneFolder(self, name, local_path, folder_id, parent_id): LOGGER.info(f"Syncing: {local_path}") files = self.getFilesByFolderId(folder_id) new_id = None if len(files) == 0: return parent_id for file in files: if file.get('mimeType') == self.__G_DRIVE_DIR_MIME_TYPE: file_path = os.path.join(local_path, file.get('name')) current_dir_id = self.create_directory(file.get('name'), parent_id) new_id = self.cloneFolder(file.get('name'), file_path, file.get('id'), current_dir_id) else: try: self.transferred_size += int(file.get('size')) except TypeError: pass try: self.copyFile(file.get('id'), parent_id) new_id = parent_id except Exception as e: if isinstance(e, RetryError): LOGGER.info(f"Total Attempts: {e.last_attempt.attempt_number}") err = e.last_attempt.exception() else: err = e LOGGER.error(err) return new_id @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def create_directory(self, directory_name, parent_id): file_metadata = { "name": directory_name, "mimeType": self.__G_DRIVE_DIR_MIME_TYPE } if parent_id is not None: file_metadata["parents"] = [parent_id] file = self.__service.files().create(supportsTeamDrives=True, body=file_metadata).execute() file_id = file.get("id") if not IS_TEAM_DRIVE: self.__set_permission(file_id) LOGGER.info("Created Google-Drive Folder:\nName: {}\nID: {} ".format(file.get("name"), file_id)) return file_id def upload_dir(self, input_directory, parent_id): list_dirs = os.listdir(input_directory) if len(list_dirs) == 0: return parent_id new_id = None for item in list_dirs: current_file_name = os.path.join(input_directory, item) if self.is_cancelled: return None if os.path.isdir(current_file_name): current_dir_id = self.create_directory(item, parent_id) new_id = self.upload_dir(current_file_name, current_dir_id) else: mime_type = get_mime_type(current_file_name) file_name = current_file_name.split("/")[-1] # current_file_name will have the full path self.upload_file(current_file_name, file_name, mime_type, parent_id) new_id = parent_id return new_id def authorize(self): # Get credentials credentials = None if not USE_SERVICE_ACCOUNTS: if os.path.exists(self.__G_DRIVE_TOKEN_FILE): with open(self.__G_DRIVE_TOKEN_FILE, 'rb') as f: credentials = pickle.load(f) if credentials is None or not credentials.valid: if credentials and credentials.expired and credentials.refresh_token: credentials.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', self.__OAUTH_SCOPE) LOGGER.info(flow) credentials = flow.run_console(port=0) # Save the credentials for the next run with open(self.__G_DRIVE_TOKEN_FILE, 'wb') as token: pickle.dump(credentials, token) else: LOGGER.info(f"Authorizing with {SERVICE_ACCOUNT_INDEX}.json service account") credentials = service_account.Credentials.from_service_account_file( f'accounts/{SERVICE_ACCOUNT_INDEX}.json', scopes=self.__OAUTH_SCOPE) return build('drive', 'v3', credentials=credentials, cache_discovery=False) def edit_telegraph(self): nxt_page = 1 prev_page = 0 for content in self.telegraph_content : if nxt_page == 1 : content += f'Next' nxt_page += 1 else : if prev_page <= self.num_of_path: content += f'Prev' prev_page += 1 if nxt_page < self.num_of_path: content += f' | Next' nxt_page += 1 Telegraph(access_token=telegraph_token).edit_page(path = self.path[prev_page], title = 'MirrorX Search', author_name='MirrorX', author_url='https://github.com/iamLiquidX', html_content=content) return def escapes(self, str): chars = ['\\', "'", '"', r'\a', r'\b', r'\f', r'\n', r'\r', r'\t'] for char in chars: str = str.replace(char, '\\'+char) return str def drive_list(self, fileName): msg = "" fileName = self.escapes(str(fileName)) # Create Search Query for API request. query = f"'{parent_id}' in parents and (name contains '{fileName}')" response = self.__service.files().list(supportsTeamDrives=True, includeTeamDriveItems=True, q=query, spaces='drive', pageSize=200, fields='files(id, name, mimeType, size)', orderBy='modifiedTime desc').execute() content_count = 0 if response["files"]: msg += f'

Results : {fileName}



' for file in response.get('files', []): if file.get('mimeType') == "application/vnd.google-apps.folder": # Detect Whether Current Entity is a Folder or File. furl = f"https://drive.google.com/drive/folders/{file.get('id')}" msg += f"⁍{file.get('name')}
(folder)

" if SHORTENER is not None and SHORTENER_API is not None: sfurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={furl}&format=text').text msg += f"Drive Link" else: msg += f"Drive Link" if INDEX_URL is not None: url_path = requests.utils.quote(f'{file.get("name")}') url = f'{INDEX_URL}/{url_path}/' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={url}&format=text').text msg += f' | Index Link' else: msg += f' | Index Link' else: furl = f"https://drive.google.com/uc?id={file.get('id')}&export=download" msg += f"⁍{file.get('name')}
({get_readable_file_size(int(file.get('size')))})

" if SHORTENER is not None and SHORTENER_API is not None: sfurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={furl}&format=text').text msg += f"Drive Link" else: msg += f"Drive Link" if INDEX_URL is not None: url_path = requests.utils.quote(f'{file.get("name")}') url = f'{INDEX_URL}/{url_path}' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={url}&format=text').text msg += f' | Index Link' else: msg += f' | Index Link' msg += '

' content_count += 1 if content_count == TELEGRAPHLIMIT : self.telegraph_content.append(msg) msg = "" content_count = 0 if msg != '': self.telegraph_content.append(msg) if len(self.telegraph_content) == 0: return "No Result Found :(", None for content in self.telegraph_content : self.path.append(Telegraph(access_token=telegraph_token).create_page( title = 'MirrorX Search', author_name='MirrorX', author_url='https://github.com/iamLiquidX', html_content=content )['path']) self.num_of_path = len(self.path) if self.num_of_path > 1: self.edit_telegraph() msg = f"Search Results For {fileName} 👇" buttons = button_build.ButtonMaker() buttons.buildbutton("HERE", f"https://telegra.ph/{self.path[0]}") return msg, InlineKeyboardMarkup(buttons.build_menu(1)) else : return '', '' def count(self, link): self.total_bytes = 0 self.total_files = 0 self.total_folders = 0 try: file_id = self.getIdFromUrl(link) except (KeyError,IndexError): msg = "Google drive ID could not be found in the provided link" return msg msg = "" LOGGER.info(f"File ID: {file_id}") try: drive_file = self.__service.files().get(fileId=file_id, fields="id, name, mimeType, size", supportsTeamDrives=True).execute() name = drive_file['name'] LOGGER.info(f"Counting: {name}") if drive_file['mimeType'] == self.__G_DRIVE_DIR_MIME_TYPE: self.gDrive_directory(**drive_file) msg += f'Name:- {name}' msg += f'\nSize:- {get_readable_file_size(self.total_bytes)}' msg += f"\nType:- Folder" msg += f"\nSubFolders:- {self.total_folders}" msg += f"\nFiles:- {self.total_files}" else: msg += f'Name:- {name}' try: typee = drive_file['mimeType'] except: typee = 'File' try: self.total_files += 1 self.gDrive_file(**drive_file) msg += f'\nSize:- {get_readable_file_size(self.total_bytes)}' msg += f"\nType:- {typee}" msg += f"\nFiles:- {self.total_files}" except TypeError: pass except Exception as err: if isinstance(err, RetryError): LOGGER.info(f"Total Attempts: {err.last_attempt.attempt_number}") err = err.last_attempt.exception() err = str(err).replace('>', '').replace('<', '') LOGGER.error(err) return err return msg def gDrive_file(self, **kwargs): try: size = int(kwargs['size']) except: size = 0 self.total_bytes += size def gDrive_directory(self, **kwargs) -> None: files = self.getFilesByFolderId(kwargs['id']) if len(files) == 0: return for file_ in files: if file_['mimeType'] == self.__G_DRIVE_DIR_MIME_TYPE: self.total_folders += 1 self.gDrive_directory(**file_) else: self.total_files += 1 self.gDrive_file(**file_) def clonehelper(self, link): try: file_id = self.getIdFromUrl(link) except (KeyError,IndexError): msg = "Google drive ID could not be found in the provided link" return msg, "", "" LOGGER.info(f"File ID: {file_id}") try: drive_file = self.__service.files().get(fileId=file_id, fields="id, name, mimeType, size", supportsTeamDrives=True).execute() name = drive_file['name'] LOGGER.info(f"Checking: {name}") if drive_file['mimeType'] == self.__G_DRIVE_DIR_MIME_TYPE: self.gDrive_directory(**drive_file) else: try: self.total_files += 1 self.gDrive_file(**drive_file) except TypeError: pass clonesize = self.total_bytes except Exception as err: if isinstance(err, RetryError): LOGGER.info(f"Total Attempts: {err.last_attempt.attempt_number}") err = err.last_attempt.exception() err = str(err).replace('>', '').replace('<', '') LOGGER.error(err) if "File not found" in str(err): msg = "File not found." else: msg = f"Error.\n{err}" return msg, "", "" return "", clonesize, name def download(self, link): self.is_downloading = True file_id = self.getIdFromUrl(link) if USE_SERVICE_ACCOUNTS: self.service_account_count = len(os.listdir("accounts")) self.start_time = time.time() self.updater = setInterval(self.update_interval, self._on_download_progress) try: meta = self.getFileMetadata(file_id) path = f"{DOWNLOAD_DIR}{self.__listener.uid}/" if meta.get("mimeType") == self.__G_DRIVE_DIR_MIME_TYPE: self.download_folder(file_id, path, meta.get('name')) else: os.makedirs(path) self.download_file(file_id, path, meta.get('name'), meta.get('mimeType')) except Exception as err: if isinstance(err, RetryError): LOGGER.info(f"Total Attempts: {err.last_attempt.attempt_number}") err = err.last_attempt.exception() err = str(err).replace('>', '').replace('<', '') LOGGER.error(err) self.is_cancelled = True self.__listener.onDownloadError(err) return finally: self.updater.cancel() if self.is_cancelled: return self.__listener.onDownloadComplete() def download_folder(self, folder_id, path, folder_name): if not os.path.exists(path + folder_name): os.makedirs(path + folder_name) path += folder_name + '/' result = [] page_token = None while True: files = self.__service.files().list( supportsTeamDrives=True, includeTeamDriveItems=True, q=f"'{folder_id}' in parents", fields='nextPageToken, files(id, name, mimeType, size, shortcutDetails)', pageToken=page_token, pageSize=1000).execute() result.extend(files['files']) page_token = files.get("nextPageToken") if not page_token: break result = sorted(result, key=lambda k: k['name']) for item in result: file_id = item['id'] filename = item['name'] mime_type = item['mimeType'] shortcut_details = item.get('shortcutDetails', None) if shortcut_details != None: file_id = shortcut_details['targetId'] mime_type = shortcut_details['targetMimeType'] if mime_type == 'application/vnd.google-apps.folder': self.download_folder(file_id, path, filename) elif not os.path.isfile(path + filename): self.download_file(file_id, path, filename, mime_type) if self.is_cancelled: break return def download_file(self, file_id, path, filename, mime_type): request = self.__service.files().get_media(fileId=file_id) fh = io.FileIO('{}{}'.format(path, filename), 'wb') downloader = MediaIoBaseDownload(fh, request, chunksize = 65 * 1024 * 1024) done = False while done is False: if self.is_cancelled: fh.close() break return try: self.dstatus, done = downloader.next_chunk() except HttpError as err: if err.resp.get('content-type', '').startswith('application/json'): reason = json.loads(err.content).get('error').get('errors')[0].get('reason') if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded': if USE_SERVICE_ACCOUNTS: if not self.switchServiceAccount(): raise err LOGGER.info(f"Got: {reason}, Trying Again...") return self.download_file(file_id, path, filename, mime_type) else: raise err else: raise err self._file_downloaded_bytes = 0 def _on_download_progress(self): if self.dstatus is not None: chunk_size = self.dstatus.total_size * self.dstatus.progress() - self._file_downloaded_bytes self._file_downloaded_bytes = self.dstatus.total_size * self.dstatus.progress() self.downloaded_bytes += chunk_size self.dtotal_time += self.update_interval def cancel_download(self): self.is_cancelled = True self.__listener.onDownloadError('Download stopped by user!') ================================================ FILE: bot/helper/telegram_helper/__init__.py ================================================ ================================================ FILE: bot/helper/telegram_helper/bot_commands.py ================================================ import os def getCommand(name: str, command: str): try: if len(os.environ[name]) == 0: raise KeyError return os.environ[name] except KeyError: return command class _BotCommands: def __init__(self): self.StartCommand = getCommand('START_COMMAND', 'start') self.MirrorCommand = getCommand('MIRROR_COMMAND', 'mirror') self.UnzipMirrorCommand = getCommand('UNZIP_COMMAND', 'unzipmirror') self.TarMirrorCommand = getCommand('TAR_COMMAND', 'tarmirror') self.CancelMirror = getCommand('CANCEL_COMMAND', 'cancel') self.CancelAllCommand = getCommand('CANCELALL_COMMAND', 'cancelall') self.ListCommand = getCommand('LIST_COMMAND', 'list') self.SpeedCommand = getCommand('SPEED_COMMAND', 'speedtest') self.CountCommand = getCommand('COUNT_COMMAND', 'count') self.StatusCommand = getCommand('STATUS_COMMAND', 'status') self.AuthorizeCommand = getCommand('AUTH_COMMAND', 'authorize') self.UnAuthorizeCommand = getCommand('UNAUTH_COMMAND', 'unauthorize') self.PingCommand = getCommand('PING_COMMAND', 'ping') self.RestartCommand = getCommand('RESTART_COMMAND', 'restart') self.StatsCommand = getCommand('STATS_COMMAND', 'stats') self.HelpCommand = getCommand('HELP_COMMAND', 'help') self.LogCommand = getCommand('LOG_COMMAND', 'log') self.CloneCommand = getCommand('CLONE_COMMAND', 'clone') self.WatchCommand = getCommand('WATCH_COMMAND', 'watch') self.TarWatchCommand = getCommand('TARWATCH_COMMAND', 'tarwatch') self.deleteCommand = getCommand('DELETE_COMMAND', 'del') BotCommands = _BotCommands() ================================================ FILE: bot/helper/telegram_helper/button_build.py ================================================ from telegram import InlineKeyboardButton class ButtonMaker: def __init__(self): self.button = [] def buildbutton(self, key, link): self.button.append(InlineKeyboardButton(text = key, url = link)) def build_menu(self, n_cols, footer_buttons=None, header_buttons=None): menu = [self.button[i:i + n_cols] for i in range(0, len(self.button), n_cols)] if header_buttons: menu.insert(0, header_buttons) if footer_buttons: menu.append(footer_buttons) return menu ================================================ FILE: bot/helper/telegram_helper/filters.py ================================================ from telegram.ext import MessageFilter from telegram import Message from bot import AUTHORIZED_CHATS, OWNER_ID, download_dict, download_dict_lock class CustomFilters: class _OwnerFilter(MessageFilter): def filter(self, message): return bool(message.from_user.id == OWNER_ID) owner_filter = _OwnerFilter() class _AuthorizedUserFilter(MessageFilter): def filter(self, message): id = message.from_user.id return bool(id in AUTHORIZED_CHATS or id == OWNER_ID) authorized_user = _AuthorizedUserFilter() class _AuthorizedChat(MessageFilter): def filter(self, message): return bool(message.chat.id in AUTHORIZED_CHATS) authorized_chat = _AuthorizedChat() class _MirrorOwner(MessageFilter): def filter(self, message: Message): user_id = message.from_user.id if user_id == OWNER_ID: return True args = str(message.text).split(' ') if len(args) > 1: # Cancelling by gid with download_dict_lock: for message_id, status in download_dict.items(): if status.gid() == args[1] and status.message.from_user.id == user_id: return True else: return False # Cancelling by replying to original mirror message reply_user = message.reply_to_message.from_user.id return bool(reply_user == user_id) mirror_owner_filter = _MirrorOwner() ================================================ FILE: bot/helper/telegram_helper/message_utils.py ================================================ from telegram import InlineKeyboardMarkup from telegram.message import Message from telegram.update import Update import time import psutil from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \ status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, MirrorStatus from telegram.error import TimedOut, BadRequest def sendMessage(text: str, bot, update: Update): try: return bot.send_message(update.message.chat_id, reply_to_message_id=update.message.message_id, text=text, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e)) def sendMarkup(text: str, bot, update: Update, reply_markup: InlineKeyboardMarkup): try: return bot.send_message(update.message.chat_id, reply_to_message_id=update.message.message_id, text=text, reply_markup=reply_markup, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e)) def editMessage(text: str, message: Message, reply_markup=None): try: bot.edit_message_text(text=text, message_id=message.message_id, chat_id=message.chat.id,reply_markup=reply_markup, parse_mode='HTMl') except Exception as e: LOGGER.error(str(e)) def deleteMessage(bot, message: Message): try: bot.delete_message(chat_id=message.chat.id, message_id=message.message_id) except Exception as e: LOGGER.error(str(e)) def sendLogFile(bot, update: Update): with open('log.txt', 'rb') as f: bot.send_document(document=f, filename=f.name, reply_to_message_id=update.message.message_id, chat_id=update.message.chat_id) def auto_delete_message(bot, cmd_message: Message, bot_message: Message): if AUTO_DELETE_MESSAGE_DURATION != -1: time.sleep(AUTO_DELETE_MESSAGE_DURATION) try: # Skip if None is passed meaning we don't want to delete bot xor cmd message deleteMessage(bot, cmd_message) deleteMessage(bot, bot_message) except AttributeError: pass def delete_all_messages(): with status_reply_dict_lock: for message in list(status_reply_dict.values()): try: deleteMessage(bot, message) del status_reply_dict[message.chat.id] except Exception as e: LOGGER.error(str(e)) def update_all_messages(): msg = get_readable_message() msg += f"CPU:- {psutil.cpu_percent()}%" \ f" DISK:- {psutil.disk_usage('/').percent}%" \ f" RAM:- {psutil.virtual_memory().percent}%" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 for download in list(download_dict.values()): speedy = download.speed() if download.status() == MirrorStatus.STATUS_DOWNLOADING: if 'KiB/s' in speedy: dlspeed_bytes += float(speedy.split('K')[0]) * 1024 elif 'MiB/s' in speedy: dlspeed_bytes += float(speedy.split('M')[0]) * 1048576 if download.status() == MirrorStatus.STATUS_UPLOADING: if 'KB/s' in speedy: uldl_bytes += float(speedy.split('K')[0]) * 1024 elif 'MB/s' in speedy: uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) msg += f"\nDL:{dlspeed}ps | UL:{ulspeed}ps \n" with status_reply_dict_lock: for chat_id in list(status_reply_dict.keys()): if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text: if len(msg) == 0: msg = "Starting DL" try: editMessage(msg, status_reply_dict[chat_id]) except Exception as e: LOGGER.error(str(e)) status_reply_dict[chat_id].text = msg def sendStatusMessage(msg, bot): progress = get_readable_message() progress += f"CPU: {psutil.cpu_percent()}%" \ f" DISK: {psutil.disk_usage('/').percent}%" \ f" RAM: {psutil.virtual_memory().percent}%" with download_dict_lock: dlspeed_bytes = 0 uldl_bytes = 0 for download in list(download_dict.values()): speedy = download.speed() if download.status() == MirrorStatus.STATUS_DOWNLOADING: if 'KiB/s' in speedy: dlspeed_bytes += float(speedy.split('K')[0]) * 1024 elif 'MiB/s' in speedy: dlspeed_bytes += float(speedy.split('M')[0]) * 1048576 if download.status() == MirrorStatus.STATUS_UPLOADING: if 'KB/s' in speedy: uldl_bytes += float(speedy.split('K')[0]) * 1024 elif 'MB/s' in speedy: uldl_bytes += float(speedy.split('M')[0]) * 1048576 dlspeed = get_readable_file_size(dlspeed_bytes) ulspeed = get_readable_file_size(uldl_bytes) progress += f"\nDL:{dlspeed}ps | UL:{ulspeed}ps \n" with status_reply_dict_lock: if msg.message.chat.id in list(status_reply_dict.keys()): try: message = status_reply_dict[msg.message.chat.id] deleteMessage(bot, message) del status_reply_dict[msg.message.chat.id] except Exception as e: LOGGER.error(str(e)) del status_reply_dict[msg.message.chat.id] if len(progress) == 0: progress = "Starting DL" message = sendMessage(progress, bot, msg) status_reply_dict[msg.message.chat.id] = message ================================================ FILE: bot/modules/__init__.py ================================================ ================================================ FILE: bot/modules/authorize.py ================================================ from bot.helper.telegram_helper.message_utils import sendMessage from bot import AUTHORIZED_CHATS, dispatcher from telegram.ext import CommandHandler from bot.helper.telegram_helper.filters import CustomFilters from telegram.ext import Filters from telegram import Update from bot.helper.telegram_helper.bot_commands import BotCommands def authorize(update,context): reply_message = update.message.reply_to_message msg = '' with open('authorized_chats.txt', 'a') as file: if reply_message is None: # Trying to authorize a chat chat_id = update.effective_chat.id if chat_id not in AUTHORIZED_CHATS: file.write(f'{chat_id}\n') AUTHORIZED_CHATS.add(chat_id) msg = 'Chat authorized' else: msg = 'Already authorized chat' else: # Trying to authorize someone in specific user_id = reply_message.from_user.id if user_id not in AUTHORIZED_CHATS: file.write(f'{user_id}\n') AUTHORIZED_CHATS.add(user_id) msg = 'Person Authorized to use the bot!' else: msg = 'Person already authorized' sendMessage(msg, context.bot, update) def unauthorize(update,context): reply_message = update.message.reply_to_message if reply_message is None: # Trying to unauthorize a chat chat_id = update.effective_chat.id if chat_id in AUTHORIZED_CHATS: AUTHORIZED_CHATS.remove(chat_id) msg = 'Chat unauthorized' else: msg = 'Already unauthorized chat' else: # Trying to authorize someone in specific user_id = reply_message.from_user.id if user_id in AUTHORIZED_CHATS: AUTHORIZED_CHATS.remove(user_id) msg = 'Person unauthorized to use the bot!' else: msg = 'Person already unauthorized!' with open('authorized_chats.txt', 'a') as file: file.truncate(0) for i in AUTHORIZED_CHATS: file.write(f'{i}\n') sendMessage(msg, context.bot, update) authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize, filters=CustomFilters.owner_filter & Filters.group, run_async=True) unauthorize_handler = CommandHandler(command=BotCommands.UnAuthorizeCommand, callback=unauthorize, filters=CustomFilters.owner_filter & Filters.group, run_async=True) dispatcher.add_handler(authorize_handler) dispatcher.add_handler(unauthorize_handler) ================================================ FILE: bot/modules/cancel_mirror.py ================================================ from telegram.ext import CommandHandler from bot import download_dict, dispatcher, download_dict_lock, DOWNLOAD_DIR from bot.helper.ext_utils.fs_utils import clean_download from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.message_utils import * from time import sleep from bot.helper.ext_utils.bot_utils import getDownloadByGid, MirrorStatus def cancel_mirror(update, context): args = update.message.text.split(" ", maxsplit=1) mirror_message = None if len(args) > 1: gid = args[1] dl = getDownloadByGid(gid) if not dl: sendMessage(f"GID: {gid} not found.", context.bot, update) return with download_dict_lock: keys = list(download_dict.keys()) mirror_message = dl.message elif update.message.reply_to_message: mirror_message = update.message.reply_to_message with download_dict_lock: keys = list(download_dict.keys()) dl = download_dict[mirror_message.message_id] if len(args) == 1: if mirror_message is None or mirror_message.message_id not in keys: if BotCommands.MirrorCommand in mirror_message.text or \ BotCommands.TarMirrorCommand in mirror_message.text: msg = "Mirror already have been cancelled" sendMessage(msg, context.bot, update) return else: msg = "Please reply to the /mirror message which was used to start the download or /cancel gid to cancel it!" sendMessage(msg, context.bot, update) return if dl.status() == "Uploading": sendMessage("Upload in Progress, Don't Cancel it.", context.bot, update) return elif dl.status() == "Archiving": sendMessage("Archival in Progress, Don't Cancel it.", context.bot, update) return else: dl.download().cancel_download() sleep(1) # Wait a Second For Aria2 To free Resources. clean_download(f'{DOWNLOAD_DIR}{mirror_message.message_id}/') def cancel_all(update, context): with download_dict_lock: count = 0 for dlDetails in list(download_dict.values()): if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING \ or dlDetails.status() == MirrorStatus.STATUS_WAITING: dlDetails.download().cancel_download() count += 1 delete_all_messages() sendMessage(f'Cancelled {count} downloads!', context.bot, update) cancel_mirror_handler = CommandHandler(BotCommands.CancelMirror, cancel_mirror, filters=(CustomFilters.authorized_chat | CustomFilters.authorized_user) & CustomFilters.mirror_owner_filter, run_async=True) cancel_all_handler = CommandHandler(BotCommands.CancelAllCommand, cancel_all, filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(cancel_all_handler) dispatcher.add_handler(cancel_mirror_handler) ================================================ FILE: bot/modules/clone.py ================================================ from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot.helper.telegram_helper.message_utils import * from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.ext_utils.bot_utils import new_thread from bot import dispatcher def cloneNode(update,context): args = update.message.text.split(" ",maxsplit=1) if len(args) > 1: link = args[1] msg = sendMessage(f"Cloning: {link}",context.bot,update) gd = GoogleDriveHelper() result, button = gd.clone(link) deleteMessage(context.bot,msg) if button == "": sendMessage(result,context.bot,update) else: if update.message.from_user.username: uname = f'@{update.message.from_user.username}' else: uname = f'{update.message.from_user.first_name}' if uname is not None: cc = f'\n\nReq. By: {uname}' sendMarkup(result + cc, context.bot, update, button) else: sendMessage("Provide G-Drive Shareable Link to Clone.",context.bot,update) clone_handler = CommandHandler(BotCommands.CloneCommand,cloneNode,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(clone_handler) ================================================ FILE: bot/modules/count.py ================================================ import bot from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot.helper.telegram_helper.message_utils import deleteMessage, sendMessage from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands from bot import dispatcher, AUTHORIZED_CHATS def countNode(update,context): args = update.message.text.split(" ",maxsplit=1) if len(args) > 1: link = args[1] msg = sendMessage(f"Counting: {link}",context.bot,update) gd = GoogleDriveHelper() result = gd.count(link) deleteMessage(context.bot,msg) if update.message.from_user.username: uname = f'@{update.message.from_user.username}' else: uname = f'{update.message.from_user.first_name}' if uname is not None: cc = f'\n\nReq. By: {uname}' sendMessage(result + cc, context.bot, update) else: sendMessage("Provide G-Drive Shareable Link to Count.",context.bot,update) count_handler = CommandHandler(BotCommands.CountCommand,countNode,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(count_handler) ================================================ FILE: bot/modules/delete.py ================================================ from telegram.ext import CommandHandler import threading from telegram import Update from bot import dispatcher, LOGGER from bot.helper.telegram_helper.message_utils import auto_delete_message, sendMessage from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.mirror_utils.upload_utils import gdriveTools def deletefile(update, context): msg_args = update.message.text.split(None, 1) msg = '' try: link = msg_args[1] LOGGER.info(msg_args[1]) except IndexError: msg = 'send a link along with command' if msg == '' : drive = gdriveTools.GoogleDriveHelper() msg = drive.deletefile(link) LOGGER.info(f"this is msg : {msg}") reply_message = sendMessage(msg, context.bot, update) threading.Thread(target=auto_delete_message, args=(context.bot, update.message, reply_message)).start() delete_handler = CommandHandler(command=BotCommands.deleteCommand, callback=deletefile, filters=CustomFilters.owner_filter, run_async=True) dispatcher.add_handler(delete_handler) ================================================ FILE: bot/modules/list.py ================================================ from telegram.ext import CommandHandler from bot.helper.mirror_utils.upload_utils.gdriveTools import GoogleDriveHelper from bot import LOGGER, dispatcher from bot.helper.telegram_helper.message_utils import sendMessage, sendMarkup, editMessage from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands def list_drive(update,context): try: search = update.message.text.split(' ',maxsplit=1)[1] LOGGER.info(f"Searching: {search}") reply = sendMessage('Searching..... Please wait!', context.bot, update) gdrive = GoogleDriveHelper(None) msg, button = gdrive.drive_list(search) if button: editMessage(msg, reply, button) else: editMessage('No result found', reply, button) except IndexError: sendMessage('send a search key along with command', context.bot, update) except AttributeError: pass list_handler = CommandHandler(BotCommands.ListCommand, list_drive,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(list_handler) ================================================ FILE: bot/modules/mirror.py ================================================ import requests from telegram.ext import CommandHandler from telegram import InlineKeyboardMarkup from bot import Interval, INDEX_URL, BUTTON_THREE_NAME, BUTTON_THREE_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BLOCK_MEGA_LINKS, MEGA_KEY from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock, SHORTENER, SHORTENER_API from bot.helper.ext_utils import fs_utils, bot_utils from bot.helper.ext_utils.bot_utils import setInterval from bot.helper.ext_utils.exceptions import DirectDownloadLinkException, NotSupportedExtractionArchive from bot.helper.mirror_utils.download_utils.aria2_download import AriaDownloadHelper from bot.helper.mirror_utils.download_utils.mega_download import MegaDownloader from bot.helper.mirror_utils.download_utils.direct_link_generator import direct_link_generator from bot.helper.mirror_utils.download_utils.telegram_downloader import TelegramDownloadHelper from bot.helper.mirror_utils.status_utils import listeners from bot.helper.mirror_utils.status_utils.extract_status import ExtractStatus from bot.helper.mirror_utils.status_utils.tar_status import TarStatus from bot.helper.mirror_utils.status_utils.upload_status import UploadStatus from bot.helper.mirror_utils.status_utils.gdownload_status import DownloadStatus from bot.helper.mirror_utils.upload_utils import gdriveTools from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.message_utils import * from bot.helper.telegram_helper import button_build import urllib import pathlib import os import subprocess import threading import re import random import string ariaDlManager = AriaDownloadHelper() ariaDlManager.start_listener() class MirrorListener(listeners.MirrorListeners): def __init__(self, bot, update, pswd, isTar=False, tag=None, extract=False): super().__init__(bot, update) self.isTar = isTar self.tag = tag self.extract = extract self.pswd = pswd def onDownloadStarted(self): pass def onDownloadProgress(self): # We are handling this on our own! pass def clean(self): try: Interval[0].cancel() del Interval[0] delete_all_messages() except IndexError: pass def onDownloadComplete(self): with download_dict_lock: LOGGER.info(f"Download completed: {download_dict[self.uid].name()}") download = download_dict[self.uid] name = download.name() size = download.size_raw() if name is None: # when pyrogram's media.file_name is of NoneType name = os.listdir(f'{DOWNLOAD_DIR}{self.uid}')[0] m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}' if self.isTar: download.is_archiving = True try: with download_dict_lock: download_dict[self.uid] = TarStatus(name, m_path, size) path = fs_utils.tar(m_path) except FileNotFoundError: LOGGER.info('File to archive not found!') self.onUploadError('Internal error occurred!!') return elif self.extract: download.is_extracting = True try: path = fs_utils.get_base_name(m_path) LOGGER.info( f"Extracting : {name} " ) with download_dict_lock: download_dict[self.uid] = ExtractStatus(name, m_path, size) pswd = self.pswd if pswd is not None: archive_result = subprocess.run(["pextract", m_path, pswd]) else: archive_result = subprocess.run(["extract", m_path]) if archive_result.returncode == 0: threading.Thread(target=os.remove, args=(m_path,)).start() LOGGER.info(f"Deleting archive : {m_path}") else: LOGGER.warning('Unable to extract archive! Uploading anyway') path = f'{DOWNLOAD_DIR}{self.uid}/{name}' LOGGER.info( f'got path : {path}' ) except NotSupportedExtractionArchive: LOGGER.info("Not any valid archive, uploading file as it is.") path = f'{DOWNLOAD_DIR}{self.uid}/{name}' else: path = f'{DOWNLOAD_DIR}{self.uid}/{name}' up_name = pathlib.PurePath(path).name if up_name == "None": up_name = "".join(os.listdir(f'{DOWNLOAD_DIR}{self.uid}/')) up_path = f'{DOWNLOAD_DIR}{self.uid}/{up_name}' LOGGER.info(f"Upload Name : {up_name}") drive = gdriveTools.GoogleDriveHelper(up_name, self) size = fs_utils.get_path_size(up_path) upload_status = UploadStatus(drive, size, self) with download_dict_lock: download_dict[self.uid] = upload_status update_all_messages() drive.upload(up_name) def onDownloadError(self, error): error = error.replace('<', ' ') error = error.replace('>', ' ') LOGGER.info(self.update.effective_chat.id) with download_dict_lock: try: download = download_dict[self.uid] del download_dict[self.uid] LOGGER.info(f"Deleting folder: {download.path()}") fs_utils.clean_download(download.path()) LOGGER.info(str(download_dict)) except Exception as e: LOGGER.error(str(e)) pass count = len(download_dict) if self.message.from_user.username: uname = f"@{self.message.from_user.username}" else: uname = f'{self.message.from_user.first_name}' msg = f"{uname} your download has been stopped due to: {error}" sendMessage(msg, self.bot, self.update) if count == 0: self.clean() else: update_all_messages() def onUploadStarted(self): pass def onUploadProgress(self): pass def onUploadComplete(self, link: str, size): with download_dict_lock: msg = f'Name:- {download_dict[self.uid].name()}\nSize:- {size}' buttons = button_build.ButtonMaker() if SHORTENER is not None and SHORTENER_API is not None: surl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={link}&format=text').text buttons.buildbutton("Drive Link", surl) else: buttons.buildbutton("Drive Link", link) LOGGER.info(f'Done Uploading {download_dict[self.uid].name()}') if INDEX_URL is not None: url_path = requests.utils.quote(f'{download_dict[self.uid].name()}') share_url = f'{INDEX_URL}/{url_path}' if os.path.isdir(f'{DOWNLOAD_DIR}/{self.uid}/{download_dict[self.uid].name()}'): share_url += '/' if SHORTENER is not None and SHORTENER_API is not None: siurl = requests.get(f'https://{SHORTENER}/api?api={SHORTENER_API}&url={share_url}&format=text').text buttons.buildbutton("Index Link", siurl) else: buttons.buildbutton("Index Link", share_url) if BUTTON_THREE_NAME is not None and BUTTON_THREE_URL is not None: buttons.buildbutton(f"{BUTTON_THREE_NAME}", f"{BUTTON_THREE_URL}") if BUTTON_FOUR_NAME is not None and BUTTON_FOUR_URL is not None: buttons.buildbutton(f"{BUTTON_FOUR_NAME}", f"{BUTTON_FOUR_URL}") if BUTTON_FIVE_NAME is not None and BUTTON_FIVE_URL is not None: buttons.buildbutton(f"{BUTTON_FIVE_NAME}", f"{BUTTON_FIVE_URL}") if self.message.from_user.username: uname = f"@{self.message.from_user.username}" else: uname = f'{self.message.from_user.first_name}' if uname is not None: msg += f'\n\nReq. By:- {uname}' try: fs_utils.clean_download(download_dict[self.uid].path()) except FileNotFoundError: pass del download_dict[self.uid] count = len(download_dict) sendMarkup(msg, self.bot, self.update, InlineKeyboardMarkup(buttons.build_menu(2))) if count == 0: self.clean() else: update_all_messages() def onUploadError(self, error): e_str = error.replace('<', '').replace('>', '') with download_dict_lock: try: fs_utils.clean_download(download_dict[self.uid].path()) except FileNotFoundError: pass del download_dict[self.message.message_id] count = len(download_dict) sendMessage(e_str, self.bot, self.update) if count == 0: self.clean() else: update_all_messages() def _mirror(bot, update, isTar=False, extract=False): mesg = update.message.text.split('\n') message_args = mesg[0].split(' ') name_args = mesg[0].split('|') try: link = message_args[1] print(link) if link.startswith("|") or link.startswith("pswd: "): link = '' except IndexError: link = '' try: name = name_args[1] name = name.strip() if name.startswith("pswd: "): name = '' except IndexError: name = '' try: ussr = urllib.parse.quote(mesg[1], safe='') pssw = urllib.parse.quote(mesg[2], safe='') except: ussr = '' pssw = '' if ussr != '' and pssw != '': link = link.split("://", maxsplit=1) link = f'{link[0]}://{ussr}:{pssw}@{link[1]}' pswd = re.search('(?<=pswd: )(.*)', update.message.text) if pswd is not None: pswd = pswd.groups() pswd = " ".join(pswd) LOGGER.info(link) link = link.strip() reply_to = update.message.reply_to_message if reply_to is not None: file = None tag = reply_to.from_user.username media_array = [reply_to.document, reply_to.video, reply_to.audio] for i in media_array: if i is not None: file = i break if not bot_utils.is_url(link) and not bot_utils.is_magnet(link) or len(link) == 0: if file is not None: if file.mime_type != "application/x-bittorrent": listener = MirrorListener(bot, update, pswd, isTar, tag, extract) tg_downloader = TelegramDownloadHelper(listener) tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/', name) sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) return else: link = file.get_file().file_path else: tag = None if not bot_utils.is_url(link) and not bot_utils.is_magnet(link): sendMessage('No download source provided', bot, update) return try: link = direct_link_generator(link) except DirectDownloadLinkException as e: LOGGER.info(f'{link}: {e}') listener = MirrorListener(bot, update, pswd, isTar, tag, extract) if bot_utils.is_gdrive_link(link): if not isTar and not extract: sendMessage(f"Use /{BotCommands.CloneCommand} To Copy File/Folder", bot, update) return res, size, name = gdriveTools.GoogleDriveHelper().clonehelper(link) if res != "": sendMessage(res, bot, update) return LOGGER.info(f"Download Name : {name}") drive = gdriveTools.GoogleDriveHelper(name, listener) gid = ''.join(random.SystemRandom().choices(string.ascii_letters + string.digits, k=12)) download_status = DownloadStatus(drive, size, listener, gid) with download_dict_lock: download_dict[listener.uid] = download_status if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) sendStatusMessage(update, bot) drive.download(link) elif bot_utils.is_mega_link(link) and MEGA_KEY is not None: if BLOCK_MEGA_LINKS: sendMessage("Mega Links Are Blocked.", bot, update) else: mega_dl = MegaDownloader(listener) mega_dl.add_download(link, f'{DOWNLOAD_DIR}{listener.uid}/') sendStatusMessage(update, bot) else: ariaDlManager.add_download(link, f'{DOWNLOAD_DIR}{listener.uid}/', listener, name) sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) def mirror(update, context): _mirror(context.bot, update) def tar_mirror(update, context): _mirror(context.bot, update, True) def unzip_mirror(update, context): _mirror(context.bot, update, extract=True) mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) tar_mirror_handler = CommandHandler(BotCommands.TarMirrorCommand, tar_mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) unzip_mirror_handler = CommandHandler(BotCommands.UnzipMirrorCommand, unzip_mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler) dispatcher.add_handler(unzip_mirror_handler) ================================================ FILE: bot/modules/mirror_status.py ================================================ from telegram.ext import CommandHandler from bot import dispatcher, status_reply_dict, DOWNLOAD_STATUS_UPDATE_INTERVAL, status_reply_dict_lock from bot.helper.telegram_helper.message_utils import * from time import sleep from bot.helper.ext_utils.bot_utils import get_readable_message from telegram.error import BadRequest from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands import threading def mirror_status(update,context): message = get_readable_message() if len(message) == 0: message = "No active downloads" reply_message = sendMessage(message, context.bot, update) threading.Thread(target=auto_delete_message, args=(bot, update.message, reply_message)).start() return index = update.effective_chat.id with status_reply_dict_lock: if index in status_reply_dict.keys(): deleteMessage(bot, status_reply_dict[index]) del status_reply_dict[index] sendStatusMessage(update,context.bot) deleteMessage(context.bot,update.message) mirror_status_handler = CommandHandler(BotCommands.StatusCommand, mirror_status, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_status_handler) ================================================ FILE: bot/modules/speedtest.py ================================================ import speedtest import bot from bot.helper.telegram_helper.filters import CustomFilters from bot import dispatcher, AUTHORIZED_CHATS from bot.helper.telegram_helper.bot_commands import BotCommands from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ParseMode from telegram.ext import CallbackContext, Filters, CommandHandler def speedtst(update, context): message = update.effective_message ed_msg = message.reply_text("Running Speed Test . . . 💨") test = speedtest.Speedtest() test.get_best_server() test.download() test.upload() test.results.share() result = test.results.dict() context.bot.editMessageText( "🔻 Download Speed : " f"{speed_convert(result['download'])}\n" "🔺 Upload Speed : " f"{speed_convert(result['upload'])}\n" "📶 Ping : " f"{result['ping']}\n" "🏬 ISP : " f"{result['client']['isp']}", update.effective_chat.id, ed_msg.message_id, ) def speed_convert(size): """Hi human, you can't read bytes?""" power = 2 ** 10 zero = 0 units = {0: "", 1: "Kb/s", 2: "Mb/s", 3: "Gb/s", 4: "Tb/s"} while size > power: size /= power zero += 1 return f"{round(size, 2)} {units[zero]}" SPEED_HANDLER = CommandHandler(BotCommands.SpeedCommand, speedtst, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(SPEED_HANDLER) ================================================ FILE: bot/modules/watch.py ================================================ from telegram.ext import CommandHandler from telegram import Bot, Update from bot import Interval, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, dispatcher, LOGGER from bot.helper.ext_utils.bot_utils import setInterval from bot.helper.telegram_helper.message_utils import update_all_messages, sendMessage, sendStatusMessage from .mirror import MirrorListener from bot.helper.mirror_utils.download_utils.youtube_dl_download_helper import YoutubeDLHelper from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.filters import CustomFilters import threading def _watch(bot: Bot, update, isTar=False): mssg = update.message.text message_args = mssg.split(' ') name_args = mssg.split('|') try: link = message_args[1] except IndexError: msg = f"/{BotCommands.WatchCommand} [yt_dl supported link] [quality] |[CustomName] to mirror with youtube_dl.\n\n" msg += "Note :- Quality and custom name are optional\n\nExample of quality :- audio, 144, 240, 360, 480, 720, 1080, 2160." msg += "\n\nIf you want to use custom filename, plz enter it after |" msg += f"\n\nExample :-\n/{BotCommands.WatchCommand} https://youtu.be/ocX2FN1nguA 720 |My video bro\n\n" msg += "This file will be downloaded in 720p quality and it's name will be My video bro" sendMessage(msg, bot, update) return try: if "|" in mssg: mssg = mssg.split("|") qual = mssg[0].split(" ")[2] if qual == "": raise IndexError else: qual = message_args[2] if qual != "audio": qual = f'bestvideo[height<={qual}]+bestaudio/best[height<={qual}]' except IndexError: qual = "bestvideo+bestaudio/best" try: name = name_args[1] except IndexError: name = "" reply_to = update.message.reply_to_message if reply_to is not None: tag = reply_to.from_user.username else: tag = None pswd = "" listener = MirrorListener(bot, update, pswd, isTar, tag) ydl = YoutubeDLHelper(listener) threading.Thread(target=ydl.add_download,args=(link, f'{DOWNLOAD_DIR}{listener.uid}', qual, name)).start() sendStatusMessage(update, bot) if len(Interval) == 0: Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages)) def watchTar(update, context): _watch(context.bot, update, True) def watch(update, context): _watch(context.bot, update) mirror_handler = CommandHandler(BotCommands.WatchCommand, watch, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) tar_mirror_handler = CommandHandler(BotCommands.TarWatchCommand, watchTar, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler) ================================================ FILE: config_sample.env ================================================ #Remove this line before deploying _____REMOVE_THIS_LINE_____=True ## Telegram BOT_TOKEN = "" # ENTER BOT TOKEN (Get your BOT_TOKEN by talking to @botfather) OWNER_ID = DOWNLOAD_STATUS_UPDATE_INTERVAL = 5 AUTO_DELETE_MESSAGE_DURATION = 20 TELEGRAM_API = TELEGRAM_HASH = "" AUTHORIZED_CHATS = "" #Separated by space MAX_CONCURRENT_DOWNLOADS = 4 ## Drive GDRIVE_FOLDER_ID = "" IS_TEAM_DRIVE = "" INDEX_URL = "" DOWNLOAD_DIR = "/app/downloads" # Dont change this if you are using Docker compose USE_SERVICE_ACCOUNTS = "" ## Optional config UPTOBOX_TOKEN = "" MEGA_KEY = "" # Get this from https://mega.nz/sdk MEGA_USERNAME = "" MEGA_PASSWORD = "" BLOCK_MEGA_LINKS = "" STOP_DUPLICATE_MIRROR = "" SHORTENER = "" SHORTENER_API = "" # Add more buttons (two buttons are already added of file link and index link, you can add extra buttons too, these are optional) # If you don't know what are below entries, simply leave them, Don't fill anything in them. BUTTON_THREE_NAME = "" BUTTON_THREE_URL = "" BUTTON_FOUR_NAME = "" BUTTON_FOUR_URL = "" BUTTON_FIVE_NAME = "" BUTTON_FIVE_URL = "" # Commands Customization (Completely Optional) # If you want to change START_COMMAND = "" MIRROR_COMMAND = "" UNZIP_COMMAND = "" TAR_COMMAND = "" CANCEL_COMMAND = "" CANCELALL_COMMAND = "" LIST_COMMAND = "" SPEED_COMMAND = "" COUNT_COMMAND = "" STATUS_COMMAND = "" AUTH_COMMAND = "" UNAUTH_COMMAND = "" PING_COMMAND = "" RESTART_COMMAND = "" STATS_COMMAND = "" HELP_COMMAND = "" LOG_COMMAND = "" CLONE_COMMAND = "" WATCH_COMMAND = "" TARWATCH_COMMAND = "" DELETE_COMMAND = "" ================================================ FILE: docker-compose.yml ================================================ version: "3.9" services: mirrorx: image: ghcr.io/iamliquidx/mirrorx:latest container_name: mirrorx env_file: # Should be filled up and should be kept in the same directory - config.env volumes: # Make a folder in the host OS and keep SA(accounts folder) or token there # syntax "host OS path":"where it should be mounted inside container" - /home/someuser/mirrorx/accounts:/app/accounts # for Service Accounts ex: /home/someuser/mirrorx/accounts:/app/accounts # for token and credential.json # - /home/someuser/mirrorx/token.pickle:/app/token.pickle # - /home/someuser/mirrorx/credential.json:/app/credentials.json # Incase you want to add a netrc file to youtube-dl # - /home/someuser/mirrorx/netrc:/root/.netrc # Dont edit the Container Directory (i.e Right Side one) as the bot runs in the same directory restart: unless-stopped # Restart policy, if container exits due to some error it will restart again ================================================ FILE: netrc ================================================ ================================================ FILE: requirements.txt ================================================ requests==2.26.0 appdirs==1.4.4 aria2p==0.10.4 progress==1.6 psutil==5.8.0 python-telegram-bot==13.7 google-api-python-client==2.11.0 google-auth-httplib2==0.1.0 google-auth-oauthlib==0.4.5 js2py==0.71 python-dotenv==0.19.0 tenacity==8.0.1 python-magic==0.4.24 beautifulsoup4==4.9.3 Pyrogram TgCrypto yt-dlp lxml==4.6.3 telegraph==1.4.1 speedtest-cli==2.1.3 messages==0.5.0 pybase64==1.1.4 lk21==1.6.0 megasdkrestclient ================================================ FILE: setup.py ================================================ from setuptools import setup, find_packages import pathlib CWD = pathlib.Path(__file__).parent README = (CWD / "README.md").read_text() setup( name='MirrorX', version='6.0.13', packages=find_packages(), long_description=README, long_description_content_type="text/markdown", url='https://github.com/iamliquidx/mirrorx', license='GPL3.0', author='', author_email='', include_package_data=True, description='Telegram Mirror Bot', platforms="any", install_requires=[ "requests==2.26.0", "appdirs==1.4.4", "aria2p==0.10.4", "progress==1.6", "psutil==5.8.0", "python-telegram-bot==13.7", "google-api-python-client==2.11.0", "google-auth-httplib2==0.1.0", "google-auth-oauthlib==0.4.5", "js2py==0.71", "python-dotenv==0.19.0", "tenacity==8.0.1", "python-magic==0.4.24", "beautifulsoup4==4.9.3", "Pyrogram", "TgCrypto", "yt-dlp", "lxml==4.6.3", "telegraph==1.4.1", "speedtest-cli==2.1.3", "messages==0.5.0", "pybase64==1.1.4", "lk21==1.6.0", "megasdkrestclient" ], classifiers=[ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "License :: OSI Approved :: GNU General Public License (GPL)", "Operating System :: POSIX :: Linux", "Development Status :: 5 - Production/Stable" ], python_requires=">=3.7", entry_points={ "console_scripts":[ "MirrorXBot = bot.__main__:main" ] }, package_data={ "": ["data/*.dat", "data/aria.conf"], }, scripts=['bin/extract', 'bin/pextract', 'bin/MirrorX'], ) ================================================ FILE: utils/add_to_team_drive.py ================================================ from google.oauth2.service_account import Credentials import googleapiclient.discovery, json, progress.bar, glob, sys, argparse, time from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import os, pickle stt = time.time() parse = argparse.ArgumentParser( description='A tool to add service accounts to a shared drive from a folder containing credential files.') parse.add_argument('--path', '-p', default='accounts', help='Specify an alternative path to the service accounts folder.') parse.add_argument('--credentials', '-c', default='./credentials.json', help='Specify the relative path for the credentials file.') parse.add_argument('--yes', '-y', default=False, action='store_true', help='Skips the sanity prompt.') parsereq = parse.add_argument_group('required arguments') parsereq.add_argument('--drive-id', '-d', help='The ID of the Shared Drive.', required=True) args = parse.parse_args() acc_dir = args.path did = args.drive_id credentials = glob.glob(args.credentials) try: open(credentials[0]) print('>> Found credentials.') except IndexError: print('>> No credentials found.') sys.exit(0) if not args.yes: # input('Make sure the following client id is added to the shared drive as Manager:\n' + json.loads((open( # credentials[0],'r').read()))['installed']['client_id']) input('>> Make sure the **Google account** that has generated credentials.json\n is added into your Team Drive ' '(shared drive) as Manager\n>> (Press any key to continue)') creds = None if os.path.exists('token_sa.pickle'): with open('token_sa.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file(credentials[0], scopes=[ 'https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.group.member' ]) # creds = flow.run_local_server(port=0) creds = flow.run_console() # Save the credentials for the next run with open('token_sa.pickle', 'wb') as token: pickle.dump(creds, token) drive = googleapiclient.discovery.build("drive", "v3", credentials=creds) batch = drive.new_batch_http_request() aa = glob.glob('%s/*.json' % acc_dir) pbar = progress.bar.Bar("Readying accounts", max=len(aa)) for i in aa: ce = json.loads(open(i).read())['client_email'] batch.add(drive.permissions().create(fileId=did, supportsAllDrives=True, body={ "role": "fileOrganizer", "type": "user", "emailAddress": ce })) pbar.next() pbar.finish() print('Adding...') batch.execute() print('Complete.') hours, rem = divmod((time.time() - stt), 3600) minutes, sec = divmod(rem, 60) print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) ================================================ FILE: utils/gen_sa_accounts.py ================================================ import errno import os import pickle import sys from argparse import ArgumentParser from base64 import b64decode from glob import glob from json import loads from random import choice from time import sleep from google.auth.transport.requests import Request from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/iam'] project_create_ops = [] current_key_dump = [] sleep_time = 30 # Create count SAs in project def _create_accounts(service, project, count): batch = service.new_batch_http_request(callback=_def_batch_resp) for i in range(count): aid = _generate_id('mfc-') batch.add(service.projects().serviceAccounts().create(name='projects/' + project, body={'accountId': aid, 'serviceAccount': { 'displayName': aid}})) batch.execute() # Create accounts needed to fill project def _create_remaining_accounts(iam, project): print('Creating accounts in %s' % project) sa_count = len(_list_sas(iam, project)) while sa_count != 100: _create_accounts(iam, project, 100 - sa_count) sa_count = len(_list_sas(iam, project)) # Generate a random id def _generate_id(prefix='saf-'): chars = '-abcdefghijklmnopqrstuvwxyz1234567890' return prefix + ''.join(choice(chars) for _ in range(25)) + choice(chars[1:]) # List projects using service def _get_projects(service): return [i['projectId'] for i in service.projects().list().execute()['projects']] # Default batch callback handler def _def_batch_resp(id, resp, exception): if exception is not None: if str(exception).startswith(' 0: current_count = len(_get_projects(cloud)) if current_count + create_projects <= max_projects: print('Creating %d projects' % (create_projects)) nprjs = _create_projects(cloud, create_projects) selected_projects = nprjs else: sys.exit('No, you cannot create %d new project (s).\n' 'Please reduce value of --quick-setup.\n' 'Remember that you can totally create %d projects (%d already).\n' 'Please do not delete existing projects unless you know what you are doing' % ( create_projects, max_projects, current_count)) else: print('Will overwrite all service accounts in existing projects.\n' 'So make sure you have some projects already.') input("Press Enter to continue...") if enable_services: ste = [] ste.append(enable_services) if enable_services == '~': ste = selected_projects elif enable_services == '*': ste = _get_projects(cloud) services = [i + '.googleapis.com' for i in services] print('Enabling services') _enable_services(serviceusage, ste, services) if create_sas: stc = [] stc.append(create_sas) if create_sas == '~': stc = selected_projects elif create_sas == '*': stc = _get_projects(cloud) for i in stc: _create_remaining_accounts(iam, i) if download_keys: try: os.mkdir(path) except OSError as e: if e.errno == errno.EEXIST: pass else: raise std = [] std.append(download_keys) if download_keys == '~': std = selected_projects elif download_keys == '*': std = _get_projects(cloud) _create_sa_keys(iam, std, path) if delete_sas: std = [] std.append(delete_sas) if delete_sas == '~': std = selected_projects elif delete_sas == '*': std = _get_projects(cloud) for i in std: print('Deleting service accounts in %s' % i) _delete_sas(iam, i) if __name__ == '__main__': parse = ArgumentParser(description='A tool to create Google service accounts.') parse.add_argument('--path', '-p', default='accounts', help='Specify an alternate directory to output the credential files.') parse.add_argument('--token', default='token_sa.pickle', help='Specify the pickle token file path.') parse.add_argument('--credentials', default='credentials.json', help='Specify the credentials file path.') parse.add_argument('--list-projects', default=False, action='store_true', help='List projects viewable by the user.') parse.add_argument('--list-sas', default=False, help='List service accounts in a project.') parse.add_argument('--create-projects', type=int, default=None, help='Creates up to N projects.') parse.add_argument('--max-projects', type=int, default=12, help='Max amount of project allowed. Default: 12') parse.add_argument('--enable-services', default=None, help='Enables services on the project. Default: IAM and Drive') parse.add_argument('--services', nargs='+', default=['iam', 'drive'], help='Specify a different set of services to enable. Overrides the default.') parse.add_argument('--create-sas', default=None, help='Create service accounts in a project.') parse.add_argument('--delete-sas', default=None, help='Delete service accounts in a project.') parse.add_argument('--download-keys', default=None, help='Download keys for all the service accounts in a project.') parse.add_argument('--quick-setup', default=None, type=int, help='Create projects, enable services, create service accounts and download keys. ') parse.add_argument('--new-only', default=False, action='store_true', help='Do not use exisiting projects.') args = parse.parse_args() # If credentials file is invalid, search for one. if not os.path.exists(args.credentials): options = glob('*.json') print('No credentials found at %s. Please enable the Drive API in:\n' 'https://developers.google.com/drive/api/v3/quickstart/python\n' 'and save the json file as credentials.json' % args.credentials) if len(options) < 1: exit(-1) else: i = 0 print('Select a credentials file below.') inp_options = [str(i) for i in list(range(1, len(options) + 1))] + options while i < len(options): print(' %d) %s' % (i + 1, options[i])) i += 1 inp = None while True: inp = input('> ') if inp in inp_options: break if inp in options: args.credentials = inp else: args.credentials = options[int(inp) - 1] print('Use --credentials %s next time to use this credentials file.' % args.credentials) if args.quick_setup: opt = '*' if args.new_only: opt = '~' args.services = ['iam', 'drive'] args.create_projects = args.quick_setup args.enable_services = opt args.create_sas = opt args.download_keys = opt resp = serviceaccountfactory( path=args.path, token=args.token, credentials=args.credentials, list_projects=args.list_projects, list_sas=args.list_sas, create_projects=args.create_projects, max_projects=args.max_projects, create_sas=args.create_sas, delete_sas=args.delete_sas, enable_services=args.enable_services, services=args.services, download_keys=args.download_keys ) if resp is not None: if args.list_projects: if resp: print('Projects (%d):' % len(resp)) for i in resp: print(' ' + i) else: print('No projects.') elif args.list_sas: if resp: print('Service accounts in %s (%d):' % (args.list_sas, len(resp))) for i in resp: print(' {} ({})'.format(i['email'], i['uniqueId'])) else: print('No service accounts.') ================================================ FILE: utils/generate_drive_token.py ================================================ import pickle import os from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request credentials = None __G_DRIVE_TOKEN_FILE = "token.pickle" __OAUTH_SCOPE = ["https://www.googleapis.com/auth/drive"] if os.path.exists(__G_DRIVE_TOKEN_FILE): with open(__G_DRIVE_TOKEN_FILE, 'rb') as f: credentials = pickle.load(f) if credentials is None or not credentials.valid: if credentials and credentials.expired and credentials.refresh_token: credentials.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', __OAUTH_SCOPE) credentials = flow.run_console(port=0) # Save the credentials for the next run with open(__G_DRIVE_TOKEN_FILE, 'wb') as token: pickle.dump(credentials, token)