├── .envrc ├── .gitignore ├── LICENSE ├── README.md ├── TODO.md ├── mcat_providers ├── __init__.py ├── providers │ ├── __init__.py │ └── rabbitstream │ │ └── __init__.py ├── sources │ ├── __init__.py │ └── flixhq │ │ └── __init__.py └── utils │ ├── __init__.py │ ├── decorators.py │ ├── exceptions.py │ └── types.py ├── mypy.ini ├── pyproject.toml ├── requirements.txt ├── setup.cfg ├── shell.nix └── test.py /.envrc: -------------------------------------------------------------------------------- 1 | use nix 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.ts 2 | *.js 3 | *.mjs 4 | .mcat 5 | embed-scripts/ 6 | 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | build/ 18 | develop-eggs/ 19 | dist/ 20 | downloads/ 21 | eggs/ 22 | .eggs/ 23 | lib/ 24 | lib64/ 25 | parts/ 26 | sdist/ 27 | var/ 28 | wheels/ 29 | share/python-wheels/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | MANIFEST 34 | 35 | # PyInstaller 36 | # Usually these files are written by a python script from a template 37 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 38 | *.manifest 39 | *.spec 40 | 41 | # Installer logs 42 | pip-log.txt 43 | pip-delete-this-directory.txt 44 | 45 | # Unit test / coverage reports 46 | htmlcov/ 47 | .tox/ 48 | .nox/ 49 | .coverage 50 | .coverage.* 51 | .cache 52 | nosetests.xml 53 | coverage.xml 54 | *.cover 55 | *.py,cover 56 | .hypothesis/ 57 | .pytest_cache/ 58 | cover/ 59 | 60 | # Translations 61 | *.mo 62 | *.pot 63 | 64 | # Django stuff: 65 | *.log 66 | local_settings.py 67 | db.sqlite3 68 | db.sqlite3-journal 69 | 70 | # Flask stuff: 71 | instance/ 72 | .webassets-cache 73 | 74 | # Scrapy stuff: 75 | .scrapy 76 | 77 | # Sphinx documentation 78 | docs/_build/ 79 | 80 | # PyBuilder 81 | .pybuilder/ 82 | target/ 83 | 84 | # Jupyter Notebook 85 | .ipynb_checkpoints 86 | 87 | # IPython 88 | profile_default/ 89 | ipython_config.py 90 | 91 | # pyenv 92 | # For a library or package, you might want to ignore these files since the code is 93 | # intended to run in multiple environments; otherwise, check them in: 94 | # .python-version 95 | 96 | # pipenv 97 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 98 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 99 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 100 | # install all needed dependencies. 101 | #Pipfile.lock 102 | 103 | # poetry 104 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 105 | # This is especially recommended for binary packages to ensure reproducibility, and is more 106 | # commonly ignored for libraries. 107 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 108 | #poetry.lock 109 | 110 | # pdm 111 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 112 | #pdm.lock 113 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 114 | # in version control. 115 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 116 | .pdm.toml 117 | .pdm-python 118 | .pdm-build/ 119 | 120 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 121 | __pypackages__/ 122 | 123 | # Celery stuff 124 | celerybeat-schedule 125 | celerybeat.pid 126 | 127 | # SageMath parsed files 128 | *.sage.py 129 | 130 | # Environments 131 | .env 132 | .venv 133 | env/ 134 | venv/ 135 | ENV/ 136 | env.bak/ 137 | venv.bak/ 138 | 139 | # Spyder project settings 140 | .spyderproject 141 | .spyproject 142 | 143 | # Rope project settings 144 | .ropeproject 145 | 146 | # mkdocs documentation 147 | /site 148 | 149 | # mypy 150 | .mypy_cache/ 151 | .dmypy.json 152 | dmypy.json 153 | 154 | # Pyre type checker 155 | .pyre/ 156 | 157 | # pytype static type analyzer 158 | .pytype/ 159 | 160 | # Cython debug symbols 161 | cython_debug/ 162 | 163 | # PyCharm 164 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 165 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 166 | # and can be added to the global gitignore or merged into this file. For a more nuclear 167 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 168 | #.idea/ 169 | .direnv/ 170 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mcat providers 2 | 3 | *Contains the logic for scraping the different sources used by movie-cat.* 4 | 5 | 6 | *I do not recommend using this in its current state.* 7 | 8 | Providers API - https://github.com/movie-cat/providers-api 9 | 10 | --- 11 | 12 | ### Installation 13 | > Install from source 14 | 15 | $ git clone https://github.com/movie-cat/providers.git mcat-providers 16 | $ cd mcat-providers 17 | $ pip install . 18 | 19 | --- 20 | 21 | ### Usage 22 | 23 | > CLI 24 | 25 | $ mcat-providers --src "flixhq" --tmdb 278 26 | $ mcat-providers --src "flixhq" --tmdb 278 > streams.json 27 | 28 | ***OR*** 29 | 30 | > Python Lib 31 | ```py 32 | import os 33 | import asyncio 34 | from rich.console import Console 35 | from mcat_providers.sources import flixhq 36 | 37 | def scrape_flix(loop): 38 | console = Console() 39 | source = flixhq.FlixHq() 40 | sources_list = loop.run_until_complete( 41 | source.scrape_all( 42 | tmdb="278", 43 | media_type="movie", 44 | ) 45 | ) 46 | console.log(sources_list.as_dict) 47 | 48 | first_source = sources_list[0] 49 | source = first_source.streams[0] 50 | english_subs = [sub for sub in first_source.subtitles if "english" in sub.language.lower()] 51 | os.system(f"mpv \"{source.url}\" --referrer=\"{source.headers.referrer}\" --user-agent=\"{source.headers.user_agent}\" --sub-file=\"{english_subs[0].url}\"") 52 | 53 | if __name__ == "__main__": 54 | from mcat_providers import rich_handle 55 | rich_handle.setLevel("NOTSET") # Will log everything for debugging, suggest using 20 (INFO) 56 | loop = asyncio.get_event_loop() 57 | scrape_flix(loop) 58 | ``` 59 | 60 | 61 | --- 62 | 63 | ### Status 64 | 65 | - **Sources** 66 | 67 | | Source | Status | 68 | | ------------- |:-------------:| 69 | | Flixhq | ✅ | 70 | 71 | - **Providers** 72 | 73 | | Provider | Status | 74 | | ------------- |:-------------:| 75 | | Rabbitstream | ✅ | 76 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | *In order or priority* 2 | 3 | - [x] Package everything 4 | - [ ] Add tmdb API support 5 | - [x] Add series support 6 | - [ ] Add livestream support 7 | -------------------------------------------------------------------------------- /mcat_providers/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import httpx 4 | import click 5 | import base64 6 | import asyncio 7 | import logging 8 | from pathlib import Path 9 | from dotenv import load_dotenv 10 | from rich.logging import RichHandler 11 | 12 | fh = logging.FileHandler("debug.log", mode="w") 13 | logging.basicConfig( 14 | level="NOTSET", 15 | format='%(asctime)s %(levelname)s | %(name)s | %(message)s', 16 | datefmt='[%H:%M:%S]', 17 | handlers=[fh] 18 | ) 19 | log = logging.getLogger() 20 | rich_handle = RichHandler(rich_tracebacks=True) 21 | rich_handle.setLevel(logging.CRITICAL) 22 | log.addHandler(rich_handle) 23 | 24 | env_path = os.path.join(os.getcwd(), ".mcat") 25 | if not os.path.exists(env_path): 26 | file_dir = Path(__file__).parent 27 | fallback_path = Path(os.path.join(file_dir, ".mcat")) 28 | if not os.path.exists(fallback_path): 29 | # fallback_path.touch() 30 | # TODO: REMOVE THIS VVV FOR TESTING ONLY 31 | tmdb_api_key = "" 32 | try: 33 | temp_key = input("Input TMDB API Read Access Token: ").strip() 34 | if temp_key and base64.b64decode(temp_key.split(".")[0] + "==") == b'{"alg":"HS256"}': 35 | tmdb_api_key = temp_key 36 | except Exception as e: 37 | log.error(e) 38 | with open(fallback_path, "w", encoding="utf-8") as f: 39 | f.write(f"TMDB_API_KEY = \"{tmdb_api_key}\"") 40 | env_path = fallback_path 41 | 42 | load_dotenv(env_path) 43 | loop = asyncio.get_event_loop() 44 | default_timeout = httpx.Timeout(999) 45 | default_ua = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0" 46 | client = httpx.AsyncClient(timeout=default_timeout) 47 | sync_client = httpx.Client(timeout=default_timeout) 48 | 49 | def handle_flixhq(tmdb: str, media_type: str, se: str, ep: str, **kwargs): 50 | from mcat_providers.sources import flixhq 51 | source = flixhq.FlixHq() 52 | sources_list = loop.run_until_complete( 53 | source.scrape_all( 54 | tmdb=tmdb, 55 | media_type=media_type, 56 | season=se, 57 | episode=ep 58 | ) 59 | ) 60 | return json.dumps(sources_list.as_dict) 61 | 62 | @click.command() 63 | @click.option("--src", required=True) 64 | @click.option("--tmdb", required=True) 65 | @click.option("--media-type", default="movie") 66 | @click.option("--se", default="0") 67 | @click.option("--ep", default="0") 68 | @click.option("--log-level", default=40, show_default=True) # logging.ERROR default 69 | def main(src: str, **kwargs): 70 | rich_handle.setLevel(kwargs.pop("log_level")) 71 | 72 | if src.lower() == "flixhq": 73 | data = handle_flixhq(**kwargs) 74 | print(data) 75 | return data 76 | 77 | raise ValueError(f"Unknown source: '{src}'") -------------------------------------------------------------------------------- /mcat_providers/providers/__init__.py: -------------------------------------------------------------------------------- 1 | import re 2 | import hashlib 3 | from pathlib import Path 4 | from typing import Optional, Union, List, Dict 5 | 6 | from mcat_providers import client, sync_client, default_ua, log 7 | from mcat_providers.utils.types import ProviderHeaders, Stream 8 | from mcat_providers.utils.exceptions import DisabledProviderError 9 | 10 | class BaseProvider: 11 | # Variables 12 | name: str 13 | base: str 14 | 15 | # Defaults 16 | logger = log 17 | client = client 18 | sync_client = sync_client 19 | default_headers: Dict = {"User-Agent": default_ua} 20 | 21 | # Internals 22 | _URL_PATTERN = re.compile(r"^(https:\/\/.+)$") 23 | _TARGET_PATTERNS = { 24 | "bandwith": re.compile(r"BANDWIDTH=(\d+)"), 25 | "quality": re.compile(r"RESOLUTION=(\d+x\d+)"), 26 | "codecs": re.compile(r"CODECS=(?:\"|\')([^\"\']+)"), 27 | "uri": re.compile(r"URI=(?:\"|\')([^\'\"]+)") 28 | } 29 | 30 | # def __init__(self): 31 | # if self.disabled: 32 | # print(f"'{self.__class__.__name__}' has been disabled!") 33 | # raise DisabledProviderError(f"'{self.__class__.__name__}' has been disabled!") 34 | 35 | @staticmethod 36 | def validate_working_dir(working_dir: Union[Path, str]) -> Path: 37 | working_dir = working_dir if isinstance(working_dir, Path) else Path(working_dir) 38 | return working_dir if working_dir.is_dir() else working_dir.parent 39 | 40 | @staticmethod 41 | def calculate_md5(input_bytes: bytes, _mode: str = "digest"): 42 | md5 = hashlib.md5(input_bytes) 43 | if _mode == "digest": 44 | return md5.digest() 45 | elif _mode == "hexdigest": 46 | return md5.hexdigest() 47 | else: 48 | raise ValueError("Unknown md5 formatting mode '{}'".format(_mode)) 49 | 50 | @classmethod 51 | def parse_m3u8(cls, headers: ProviderHeaders, m3u8_url: str, m3u8_data: str) -> List: 52 | ''' 53 | This is badly written. 54 | Oh well. 55 | ''' 56 | def get_provider_data(): 57 | return { 58 | "provider": cls.__name__, 59 | "headers": headers, 60 | "url": "", 61 | "ext": ".m3u8", 62 | "quality": "" 63 | } 64 | 65 | 66 | m3u8_data_split = m3u8_data.strip().split("\n") 67 | parsed_data = get_provider_data() 68 | m3u8_data_parsed = [] 69 | expect_url = False 70 | 71 | if not m3u8_data_split: 72 | print("No m3u8 data!") 73 | raise ValueError("No m3u8 data!") 74 | 75 | for item in m3u8_data_split: 76 | if expect_url: 77 | url_match = BaseProvider._URL_PATTERN.search(item) 78 | if not url_match: 79 | continue 80 | parsed_data.update({"url": url_match.group(1)}) 81 | m3u8_data_parsed.append(Stream(**parsed_data)) 82 | parsed_data = get_provider_data() 83 | expect_url = False 84 | continue 85 | data = {} 86 | for target, pattern in BaseProvider._TARGET_PATTERNS.items(): 87 | regex_match = pattern.search(item) 88 | if regex_match: 89 | data.update({target: regex_match.group(1)}) 90 | if data.get("uri"): 91 | uri = data.pop("uri") 92 | if not uri.startswith("/"): uri = f"/{uri}" 93 | url = f"{m3u8_url}{uri}" 94 | data.update({"url": url}) 95 | parsed_data.update(data) 96 | m3u8_data_parsed.append(Stream(**parsed_data)) 97 | parsed_data = get_provider_data() 98 | continue 99 | 100 | if data: 101 | expect_url = True 102 | parsed_data.update(data) 103 | 104 | return m3u8_data_parsed -------------------------------------------------------------------------------- /mcat_providers/providers/rabbitstream/__init__.py: -------------------------------------------------------------------------------- 1 | import re 2 | import json 3 | import httpx 4 | import base64 5 | import pythonmonkey 6 | 7 | from Cryptodome.Cipher import AES 8 | from Cryptodome.Util.Padding import unpad 9 | 10 | from pathlib import Path 11 | from typing import Optional, Dict, List 12 | from tenacity import retry, retry_if_exception_type, stop_after_attempt, RetryError 13 | 14 | from mcat_providers.providers import BaseProvider 15 | from mcat_providers.utils.exceptions import IntegrityError 16 | from mcat_providers.utils.types import ProviderHeaders, ProviderResponse, Subtitle 17 | from mcat_providers.utils.decorators import async_lru_cache, async_lru_cache_parameterless 18 | 19 | class Rabbitstream(BaseProvider): 20 | base = "https://rabbitstream.net" 21 | default_headers = { 22 | "Referer": "https://flixhq.to/", 23 | **BaseProvider.default_headers 24 | } 25 | embedded_file = { 26 | "name": "payload.js", 27 | "hash": "d587be31e78f245f63afd5331430d0d1", 28 | "url": "https://raw.githubusercontent.com/movie-cat/embed-scripts/main/rabbitstream/payload.js" 29 | } 30 | 31 | filename = embedded_file["name"] 32 | working_dir = BaseProvider.validate_working_dir(Path(__file__)) 33 | file_dir = working_dir.joinpath(filename) 34 | 35 | # We dont want to randomly execute files from the internet 36 | # So we calculate checksums before allowing the user to use anything 37 | # If this breaks then that likely means the target file has updated. 38 | # I will update the hash manually if I modify the file, so updating should fix the issue. 39 | # If updating doesnt fix the issue then you can fix this manually by updating the file_hash in __meta__ to the new MD5 hash 40 | # Only update the file hash if you are happy with the content of the file and have deemed it as safe 41 | if not file_dir.exists(): 42 | payload_url = embedded_file["url"] 43 | expected_hash = embedded_file["hash"] 44 | BaseProvider.logger.info(f"Attempting to download most recent version of '{filename}'") 45 | try: 46 | req = httpx.get(payload_url) 47 | except Exception as e: 48 | BaseProvider.logger.error(e) 49 | raise IntegrityError(f"Failed to retrieve '{filename}'") 50 | md5 = BaseProvider.calculate_md5(req.text.encode(), "hexdigest") 51 | BaseProvider.logger.info(f"Checksum = {md5}, Expected = {expected_hash}") 52 | if md5 != expected_hash: 53 | raise IntegrityError(f"Could not validate the checksum of '{filename}'...") 54 | with open(file_dir, "w", encoding="utf-8") as f: 55 | f.write(req.text) 56 | 57 | with open(file_dir, "r", encoding="utf-8") as f: 58 | payload = f.read() 59 | 60 | if not payload: 61 | raise IntegrityError(f"Could not find any content inside '{filename}' for the WASM bundle!") 62 | 63 | instantiate_and_decrypt = pythonmonkey.eval(payload) 64 | 65 | def __init__(self, **kwargs) -> None: 66 | self.client_headers = kwargs.get("headers") or {} 67 | self.client_headers.update(self.default_headers) 68 | 69 | @staticmethod 70 | def base64_to_bytearray(encoded_str) -> bytearray: 71 | return bytearray(base64.b64decode(encoded_str)) 72 | 73 | def format_wasm_key(self, keys: List, kversion: str) -> str: 74 | def convert_to_bytes(kversion: int) -> List[int]: 75 | return [ 76 | (4278190080 & kversion) >> 24, 77 | (16711680 & kversion) >> 16, 78 | (65280 & kversion) >> 8, 79 | 255 & kversion 80 | ] 81 | 82 | def xor_with_version(keys: List, kversion_bytes: List[int]) -> Optional[List]: 83 | try: 84 | for i in range(len(keys)): 85 | keys[i] ^= kversion_bytes[i % len(kversion_bytes)] 86 | return keys 87 | except Exception as e: 88 | self.logger.error(e) 89 | return None 90 | 91 | converted = convert_to_bytes(int(kversion)) 92 | processed_keys = xor_with_version(keys, converted) or keys 93 | return base64.b64encode(bytearray(processed_keys)).decode('utf-8') 94 | 95 | def generate_encryption_key(self, salt, secret) -> bytes: 96 | key = self.calculate_md5(secret + salt) 97 | current_key = key 98 | while len(current_key) < 48: 99 | key = self.calculate_md5(key + secret + salt) 100 | current_key += key 101 | return current_key 102 | 103 | def decrypt_aes_data(self, ciphertext, decryption_key) -> str: 104 | cipher_data = self.base64_to_bytearray(ciphertext) 105 | encrypted = cipher_data[16:] 106 | AES_CBC = AES.new( 107 | decryption_key[:32], AES.MODE_CBC, iv=decryption_key[32:] 108 | ) 109 | decrypted_data = unpad( 110 | AES_CBC.decrypt(encrypted), AES.block_size 111 | ) 112 | return decrypted_data.decode("utf-8") 113 | 114 | async def get_meta(self, xrax: str) -> Optional[str]: 115 | embed_req = await self.client.get(f"https://rabbitstream.net/v2/embed-4/{xrax}?z=", headers=self.client_headers) 116 | meta_match = re.search(r"name=\"fyq\"\s?content=\"(\w+)\"", embed_req.text) 117 | if not meta_match: 118 | self.logger.error("No meta could be retrived!") 119 | return None 120 | return meta_match.group(1) 121 | 122 | async def get_sources(self, xrax: str, keys: List, kversion: str, kid: str, browserid: str) -> Optional[Dict]: 123 | req = await self.client.get( 124 | f"https://rabbitstream.net/ajax/v2/embed-4/getSources", 125 | params={"id": xrax, "v": kversion, "h": kid, "b": browserid}, 126 | headers=self.client_headers 127 | ) 128 | if not req.is_success: 129 | self.logger.error( 130 | "Failed to fetch 'getSources' endpoint!\n\t" \ 131 | f"Keys: {keys}\n\tKversion: {kversion}\n\tKid: {kid}\n\tBrowserid: {browserid}" \ 132 | f"Url: {req.url}\n\tStatus: {req.status_code}\n\tBody:\n{req.text}" 133 | ) 134 | return None 135 | data = req.json() 136 | if not data: 137 | self.logger.error("No JSON data from getSources request!") 138 | return None 139 | return data 140 | 141 | @async_lru_cache_parameterless 142 | async def get_wasm(self) -> bytes: 143 | wasm_req = await self.client.get(f'https://rabbitstream.net/images/loading.png?v=0.6', headers=self.client_headers) 144 | return wasm_req.content 145 | 146 | @async_lru_cache(maxsize=128) 147 | @retry(retry=retry_if_exception_type(ValueError), stop=stop_after_attempt(3)) 148 | async def get_data(self, xrax: str) -> Dict: 149 | wasm = await self.get_wasm() 150 | meta = await self.get_meta(xrax) 151 | if not wasm or not meta: 152 | raise ValueError("Failed to retrieve wasm or meta!\n\tWasm Exists: {}\nMeta - {}".format(not not wasm, meta)) 153 | 154 | keys, kversion, kid, browserid = await self.instantiate_and_decrypt(xrax, meta, wasm) 155 | sources_data = await self.get_sources(xrax=xrax, keys=keys.tolist(), kversion=kversion, kid=kid, browserid=browserid) 156 | if not sources_data: 157 | self.logger.error("Could not retrieve encrypted sources!") 158 | raise ValueError("Could not retrieve encrypted sources!") 159 | 160 | ciphertext = sources_data.pop("sources") 161 | if not ciphertext: 162 | self.logger.error("Could not retrieve ciphertext from encrypted sources!") 163 | raise ValueError("Could not retrieve ciphertext from encrypted sources!") 164 | 165 | formatted_key = self.format_wasm_key( 166 | keys=keys.tolist(), 167 | kversion=kversion 168 | ) 169 | if not formatted_key: 170 | self.logger.error("No formatted key!") 171 | raise ValueError("No formatted key!") 172 | 173 | decryption_key = self.generate_encryption_key( 174 | salt=self.base64_to_bytearray(ciphertext)[8:16], 175 | secret=formatted_key.encode("utf-8") 176 | ) 177 | if not decryption_key: 178 | self.logger.error("No decryption key!") 179 | raise ValueError("No decryption key!") 180 | 181 | decrypted = self.decrypt_aes_data( 182 | ciphertext=ciphertext, 183 | decryption_key=decryption_key 184 | ) 185 | if not decrypted or "https://" not in decrypted: 186 | self.logger.error("Failed to decrypt AES data!") 187 | raise ValueError("Failed to decrypt AES data!") 188 | subtitles = [Subtitle(**{"language": subtitle.get("label"), "url": subtitle.get("file"), "ext": "." + subtitle.get("file").rpartition(".")[2]}) for subtitle in sources_data.pop("tracks")] 189 | sources_data.update({"subtitles": subtitles}) 190 | sources_data.update({"sources": json.loads(decrypted)}) 191 | return sources_data 192 | 193 | @async_lru_cache(maxsize=128) 194 | async def get_qualities(self, playlist: str, provider_headers=ProviderHeaders) -> List: 195 | req = await self.client.get(playlist, headers=provider_headers.headers) 196 | if not req.is_success: 197 | self.logger.error("Failed to request playlist!") 198 | raise ValueError("Failed to request playlist!") 199 | 200 | base = playlist.rpartition("/")[0] 201 | m3u8_data = self.parse_m3u8(headers=provider_headers, m3u8_url=base, m3u8_data=req.text) 202 | if not m3u8_data: 203 | self.logger.error("No result from parse_m3u8!") 204 | raise ValueError("No result from parse_m3u8!") 205 | return m3u8_data 206 | 207 | async def resolve(self, url: str) -> Optional[ProviderResponse]: 208 | # if self.disabled: 209 | # return None 210 | 211 | xrax = url.rpartition("?")[0].rpartition("/")[2] 212 | 213 | try: 214 | data = await self.get_data(xrax) 215 | except RetryError: 216 | self.logger.error("Ran out of retry attempts!") 217 | return None 218 | except Exception as e: 219 | self.logger.error(e) 220 | return None 221 | 222 | headers = ProviderHeaders( 223 | origin=self.base, 224 | referrer=self.base 225 | ) 226 | 227 | playlist = data.get("sources")[0] 228 | if not playlist or not playlist.get("file"): 229 | self.logger.error(f"Bad playlist data: {playlist}") 230 | return None 231 | 232 | qualities = await self.get_qualities(playlist=playlist.get("file"), provider_headers=headers) 233 | return ProviderResponse(provider=self.__class__.__name__, streams=qualities, subtitles=data.get("subtitles")) -------------------------------------------------------------------------------- /mcat_providers/sources/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | from typing import Optional, Dict 3 | 4 | from mcat_providers import client, sync_client, default_ua, log 5 | from mcat_providers.providers import BaseProvider 6 | from mcat_providers.utils.types import MediaType, MediaEnum 7 | from mcat_providers.utils.decorators import async_lru_cache 8 | from mcat_providers.utils.exceptions import DisabledSourceError 9 | 10 | class BaseSource: 11 | # Variables 12 | name: str 13 | base: str 14 | 15 | # Defaults 16 | logger = log 17 | client = client 18 | sync_client = sync_client 19 | tmdb_api_key = os.getenv("TMDB_API_KEY") 20 | default_headers = {"User-Agent": default_ua} 21 | 22 | @classmethod 23 | @async_lru_cache(maxsize=128) 24 | async def resolve_tmdb(cls, media: MediaType): 25 | """ 26 | Needs a TMDB_API_KEY in .mcat env in current state 27 | """ 28 | assert cls.tmdb_api_key, "No tmdb key set!" 29 | 30 | base_url = "https://api.themoviedb.org/3" 31 | headers = { 32 | "authorization": f"Bearer {cls.tmdb_api_key}", 33 | } 34 | headers.update(cls.default_headers) 35 | 36 | if media.media_type == "Movie": 37 | endpoint = f"/movie/{media.tmdb}" 38 | elif media.media_type == "Series": 39 | endpoint = f"/tv/{media.tmdb}" 40 | else: 41 | raise ValueError(f"Unsupported media type: {media.media_type}") 42 | 43 | url = f"{base_url}{endpoint}" 44 | 45 | try: 46 | response = await cls.client.get(url, headers=headers) 47 | response.raise_for_status() 48 | data = response.json() 49 | return { 50 | "title": data.get("title") or data.get("name"), 51 | "media_type": media.media_type, 52 | "duration": data.get("runtime", 0), 53 | "release": data.get("release_date") or data.get("first_air_date"), 54 | "genres": [item.get("name") for item in data.get("genres", [])], 55 | "episode_count": data.get("number_of_episodes", 0), 56 | "season_count": data.get("number_of_seasons", 0), 57 | "last_air_date": data.get("last_air_date") or data.get("first_air_date") or data.get("release_date"), 58 | "last_season_episode_count": data.get("seasons", [{}])[-1].get("episode_count", 0), 59 | "languages": [item.get("iso_639_1") for item in data.get("spoken_languages", [])] 60 | } 61 | except Exception as e: 62 | cls.logger.error(f"Failed to resolve TMDB data for {media.gmid}: {e}") 63 | raise -------------------------------------------------------------------------------- /mcat_providers/sources/flixhq/__init__.py: -------------------------------------------------------------------------------- 1 | import re 2 | import httpx 3 | import asyncio 4 | 5 | from datetime import datetime 6 | from typing import Optional, Union, List, Dict, Tuple 7 | 8 | from mcat_providers.sources import BaseSource 9 | from mcat_providers.utils.decorators import async_lru_cache 10 | from mcat_providers.providers.rabbitstream import Rabbitstream 11 | from mcat_providers.utils.types import ProviderResponse, SourceResponse, MediaType, MediaEnum 12 | 13 | class FlixHq(BaseSource): 14 | name = "FlixHq" 15 | base = "https://flixhq.to" 16 | default_headers = { 17 | "Referer": "https://flixhq.to/", 18 | **BaseSource.default_headers 19 | } 20 | entries_pattern = re.compile(r"
.+?(?=\"clearfix\")") 21 | 22 | def __init__(self, **kwargs) -> None: 23 | # self.client.cookies.update({"show_share": "true"}) 24 | self.client_headers = kwargs.get("headers") or {} 25 | self.client_headers.update(self.default_headers) 26 | self.providers = { 27 | "upcloud": Rabbitstream(**kwargs), 28 | "vidcloud": Rabbitstream(**kwargs), 29 | "upstream": None, 30 | "doodstream": None, 31 | "mixdrop": None, 32 | "voe": None, 33 | } 34 | 35 | @async_lru_cache(maxsize=128) 36 | async def query_flix(self, title: str): 37 | title = title.lower().strip() 38 | title = "-".join(title.split(" ")) 39 | tasks = [self.client.get(f"{self.base}/search/{title}", params={"page": i}) for i in range(1, 4)] 40 | responses = await asyncio.gather(*tasks) 41 | entries = [] 42 | for response in responses: 43 | data = self.entries_pattern.findall(response.text.replace("\n", "\\n")) 44 | entries.extend(data) 45 | results = [] 46 | for entry in entries: 47 | title = re.search(r"title=\"([^\"]+)\"", entry) 48 | href = re.search(r"href=\"([^\"]+)\"", entry) 49 | fdi_type = re.search(r"fdi-type\">([^<]+)", entry) 50 | if not all([title, href, fdi_type]): 51 | self.logger.warning(f"failed to gather all items in entry: {entry}") 52 | continue 53 | data_1, data_2 = re.findall(r"class=\"fdi-item(?:\sfdi-duration)?\">([^<]+)", entry) 54 | media_type = fdi_type.group(1).lower() 55 | if media_type not in ("movie", "tv"): 56 | raise ValueError(f"Bad fdi_type {media_type}") 57 | if data_2.strip() == "N/A": 58 | data_2 = "-1" 59 | results.append({ 60 | "title": title.group(1), 61 | "url": f"{self.base}{href.group(1)}", 62 | "type": MediaEnum.map_enum(media_type), 63 | "year": int(data_1) if media_type == "movie" else None, 64 | "duration": int(data_2.removesuffix("m")) if media_type == "movie" else 0, 65 | "season_count": int(data_1.split(" ")[-1] or -1) if media_type == "tv" else 0, 66 | "last_season_episode_count": int(data_2.split(" ")[-1] or -1) if media_type == "tv" else 0 67 | }) 68 | return results 69 | 70 | async def get_seasons(self, flixhq_id: str) -> Optional[Dict]: 71 | headers={"X-Requested-With": "XMLHttpRequest", **self.default_headers} 72 | req = await self.client.get(f"{self.base}/ajax/season/list/{flixhq_id}", headers=headers) 73 | if not req.is_success: 74 | self.logger.error("Could not retieve available seasons!") 75 | return None 76 | season_ids = re.findall(r" Optional[Dict]: 80 | headers={"X-Requested-With": "XMLHttpRequest", **self.default_headers} 81 | req = await self.client.get(f"{self.base}/ajax/season/episodes/{season_id}", headers=headers) 82 | if not req.is_success: 83 | self.logger.error("Could not retieve available episodes!") 84 | return None 85 | episode_ids = re.findall(r"data-id=\"(\w+)\"", req.text) 86 | return {str(i): ep for i, ep in enumerate(episode_ids, start=1)} 87 | 88 | async def get_sources(self, source_id: str, media_type: MediaType) -> Optional[List]: 89 | headers={"X-Requested-With": "XMLHttpRequest", **self.default_headers} 90 | req = await self.client.get(f"{self.base}/ajax/episode/{'list' if media_type == 'Movie' else 'servers'}/{source_id}", headers=headers) 91 | if not req.is_success: 92 | self.logger.error("Could not retieve available sources!") 93 | return None 94 | # Makes the assumption that titles and linkids are in order 95 | # And that the patterns will only match things we want 96 | # Could break in future. 97 | titles = [title.lower() for title in re.findall(r"title=\"(?:Server\s)?(\w+)\"", req.text)] 98 | link_ids = re.findall(r"data-(?:link)?id=\"(\d+)\"", req.text) 99 | return list(zip(titles, link_ids)) 100 | 101 | async def get_file(self, name: str, provider_id: int) -> Tuple[str, Optional[str]]: 102 | headers={"X-Requested-With": "XMLHttpRequest", **self.default_headers} 103 | req = await self.client.get(f"{self.base}/ajax/episode/sources/{provider_id}", headers=headers) 104 | if not req.is_success: 105 | self.logger.error(f"Could not retieve source: '{name}'") 106 | return name, None 107 | data = req.json() 108 | if not data: 109 | self.logger.error(f"Could not get data!") 110 | return name, None 111 | return name, data.get("link") 112 | 113 | async def resolve_source_id( 114 | self, 115 | title: str, 116 | media_type: MediaType, 117 | duration: int, 118 | release: str, 119 | genres: List, 120 | episode_count: int, 121 | season_count: int, 122 | last_air_date: str, 123 | last_season_episode_count: int, 124 | languages: List 125 | ) -> Optional[str]: 126 | results = await self.query_flix(title) 127 | title_year = int(release.split("-")[0]) 128 | last_aired_year = int(last_air_date.split("-")[0]) 129 | current_year = datetime.now().year 130 | filtered_results = [] 131 | 132 | for item in results: 133 | if item["type"] != media_type: 134 | continue 135 | if item["duration"] not in (duration, -1): 136 | continue 137 | if item["title"] != title: 138 | continue 139 | if media_type == "Movie": 140 | if item["year"] != title_year: 141 | continue 142 | elif media_type == "Series" and (current_year - last_aired_year) >= 1: 143 | if item["last_season_episode_count"] not in (last_season_episode_count, -1): 144 | continue 145 | if item["season_count"] not in (season_count, -1): 146 | continue 147 | filtered_results.append(item) 148 | 149 | if len(filtered_results) > 1: 150 | for item in filtered_results.copy(): 151 | if not filtered_results: 152 | break 153 | index = filtered_results.index(item) 154 | req = await self.client.get(item["url"]) 155 | full_date = re.search(r"Released:<\/span>\s+?(\d+-\d+-\d+)", req.text) 156 | if not full_date: 157 | filtered_results.pop(index) 158 | continue 159 | if full_date.group(1) != release: 160 | filtered_results.pop(index) 161 | continue 162 | start = req.text.find('Genre:') 163 | end = req.text.find('
', start) 164 | segment = req.text[start:end] 165 | page_genres = re.findall(r"href=\"\/genre\/[^\"]+\"\s+?title=\"([^\"]+)\"", segment) 166 | if not page_genres and genres: 167 | filtered_results.pop(index) 168 | for genre in page_genres: 169 | if genre in genres: 170 | continue 171 | filtered_results.pop(index) 172 | break 173 | 174 | if not filtered_results: 175 | return None 176 | 177 | result = filtered_results[0] 178 | source_id = result["url"].split("-")[-1] 179 | return source_id 180 | 181 | async def scrape_all( 182 | self, 183 | media_type: str, 184 | season: str = "0", 185 | episode: str = "0", 186 | source_id: Optional[str] = None, 187 | tmdb: Optional[str] = None, 188 | ) -> Optional[SourceResponse]: 189 | assert source_id or tmdb, "source_id or tmdb must be passed with call!" 190 | media = MediaType( 191 | tmdb=tmdb, 192 | source_id=source_id, 193 | media_type=media_type, 194 | episode=episode, 195 | season=season 196 | ) 197 | 198 | flixhq_id = source_id 199 | if not source_id: 200 | data = await self.resolve_tmdb(media) 201 | flixhq_id = await self.resolve_source_id(**data) 202 | 203 | if not flixhq_id: 204 | self.logger.error("No valid flixhq_id!") 205 | return None 206 | 207 | source_id = flixhq_id 208 | if media.media_type == "Series": 209 | seasons = await self.get_seasons(flixhq_id) 210 | season_id = seasons.get(season) 211 | if not season_id: 212 | self.logger.error(f"Season '{season}' does not exist in available seasons '{list(seasons.keys())}'") 213 | return None 214 | episodes = await self.get_episodes(season_id) 215 | episode_id = episodes.get(episode) 216 | if not episode_id: 217 | self.logger.error(f"Episode '{episode}' does not exist in available episodes '{list(episodes.keys())}'") 218 | return None 219 | source_id = episode_id 220 | 221 | sources = await self.get_sources(source_id, media.media_type) 222 | if not sources: 223 | print("Could not retrieve sources!") 224 | return None 225 | 226 | def remove_provider(name): 227 | for idx, data in enumerate(sources.copy()): 228 | value, _ = data 229 | if value == name: 230 | del sources[idx] 231 | 232 | for name, ctx in self.providers.items(): 233 | if not ctx: 234 | remove_provider(name) 235 | 236 | tasks = [self.get_file(name, provider_id) for name, provider_id in sources] 237 | seen_providers: List = [] 238 | sub_tasks: List = [] 239 | 240 | for task in asyncio.as_completed(tasks): 241 | name, file = await task 242 | if not file: 243 | continue 244 | resolver = self.providers.get(name, "unknown") 245 | name = resolver.__class__.__name__ 246 | if name in seen_providers: 247 | continue 248 | seen_providers.append(name) 249 | if not resolver: 250 | continue 251 | if resolver == "unknown": 252 | self.logger.warning(f"Unknown source '{name}'") 253 | continue 254 | sub_tasks.append(resolver.resolve(file)) 255 | 256 | if not sub_tasks: 257 | self.logger.error("No resolver tasks!") 258 | return None 259 | 260 | responses = await asyncio.gather(*sub_tasks) 261 | return SourceResponse(source=self.__class__.__name__, providers=responses) -------------------------------------------------------------------------------- /mcat_providers/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/movie-cat/providers/21893bc74319f55b13e627e75773b19892754731/mcat_providers/utils/__init__.py -------------------------------------------------------------------------------- /mcat_providers/utils/decorators.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import functools 3 | 4 | def async_lru_cache_parameterless(async_function): 5 | @functools.lru_cache 6 | def cached_async_function(*args, **kwargs): 7 | coroutine = async_function(*args, **kwargs) 8 | return asyncio.ensure_future(coroutine) 9 | return cached_async_function 10 | 11 | def async_lru_cache(*lru_cache_args, **lru_cache_kwargs): 12 | def async_lru_cache_decorator(async_function): 13 | @functools.lru_cache(*lru_cache_args, **lru_cache_kwargs) 14 | def cached_async_function(*args, **kwargs): 15 | coroutine = async_function(*args, **kwargs) 16 | return asyncio.ensure_future(coroutine) 17 | return cached_async_function 18 | return async_lru_cache_decorator -------------------------------------------------------------------------------- /mcat_providers/utils/exceptions.py: -------------------------------------------------------------------------------- 1 | class IntegrityError(ValueError): 2 | '''Generic Error for instances such as a checksum mismatch when hashing files.''' 3 | 4 | class DisabledError(ValueError): 5 | '''Generic Error for disabled providers/sources''' 6 | 7 | class DisabledProviderError(DisabledError): 8 | '''Generic Error for disabled provider''' 9 | 10 | class DisabledSourceError(DisabledError): 11 | '''Generic Error for disabled source''' -------------------------------------------------------------------------------- /mcat_providers/utils/types.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import re 4 | from enum import Enum 5 | from mcat_providers import default_ua 6 | from typing import Optional, Union, List, Dict 7 | 8 | from mcat_providers import log as logger 9 | 10 | # Enums 11 | class QualityEnum(str, Enum): 12 | P_144 = "144p" 13 | P_240 = "240p" 14 | P_360 = "360p" 15 | P_480 = "480p" 16 | P_720 = "720p" 17 | P_1080 = "1080p" 18 | P_1440 = "1440p" 19 | P_2160 = "2160p" 20 | P_4320 = "4320p" 21 | UNKNOWN = "UNKNOWN" 22 | 23 | @staticmethod 24 | def process_quality_str(quality_str: str) -> str: 25 | quality_str = quality_str.lower() 26 | quality_str = quality_str.strip() 27 | quality_str = re.sub(r"[^a-zA-Z0-9]", "", quality_str) 28 | return quality_str 29 | 30 | @classmethod 31 | def map_enum(cls, quality: str) -> QualityEnum: 32 | quality = cls.process_quality_str(quality) 33 | if quality in ["7680x4320", "4320p", "8k"]: 34 | return cls.P_4320 35 | if quality in ["4096x2160", "2160p", "ultrahd", "uhd", "4k"]: 36 | return cls.P_2160 37 | if quality in ["2560x1440", "1440p", "quadhd", "wqhd", "qhd"]: 38 | return cls.P_1440 39 | if quality in ["1920x1080", "1080p", "fullhd", "fhd"]: 40 | return cls.P_1080 41 | if quality in ["1280x720", "720p", "hd"]: 42 | return cls.P_720 43 | if quality in ["854x480", "480p"]: 44 | return cls.P_480 45 | if quality in ["640x360", "360p"]: 46 | return cls.P_360 47 | if quality in ["426x240", "240p"]: 48 | return cls.P_240 49 | if quality in ["144p"]: 50 | return cls.P_144 51 | logger.warn(f"Unknown quality: {quality}") 52 | return cls.UNKNOWN 53 | 54 | class MediaEnum(str, Enum): 55 | MOVIE = "Movie" 56 | SERIES = "Series" 57 | ANIME = "Anime" 58 | LIVE = "Live" 59 | UNKNOWN = "UNKNOWN" 60 | 61 | @staticmethod 62 | def process_media_type(media_type: str) -> str: 63 | media_type = media_type.lower() 64 | return media_type.strip() 65 | 66 | @classmethod 67 | def map_enum(cls, media_type: str) -> MediaEnum: 68 | media_type = cls.process_media_type(media_type) 69 | if any(keyword in media_type for keyword in ["movie"]): 70 | return cls.MOVIE 71 | if any(keyword in media_type for keyword in ["tv", "series"]): 72 | return cls.SERIES 73 | if any(keyword in media_type for keyword in ["anime"]): 74 | return cls.ANIME 75 | if any(keyword in media_type for keyword in ["live", "stream"]): 76 | return cls.LIVE 77 | logger.warn(f"Unknown media type: {media_type}") 78 | return cls.UNKNOWN 79 | 80 | @property 81 | def gmid_key(self) -> Optional[str]: 82 | if self == MediaEnum.MOVIE: 83 | return "M" 84 | elif self == MediaEnum.SERIES: 85 | return "S" 86 | return None 87 | 88 | # Types 89 | class MediaType: 90 | def __init__( 91 | self, 92 | media_type: Union[str, MediaEnum], 93 | season: Union[str, int] = "0", 94 | episode: Union[str, int] = "0", 95 | source_id: Optional[Union[str, int]] = None, 96 | tmdb: Optional[Union[str, int]] = None, 97 | ) -> None: 98 | assert tmdb or source_id, "Must pass source_id or tmdb to MediaType!" 99 | self.tmdb = str(tmdb) if tmdb else tmdb 100 | self.source_id = str(source_id) if source_id else source_id 101 | self.media_type = media_type if isinstance(media_type, MediaEnum) else MediaEnum.map_enum(media_type) 102 | self.season = str(season) 103 | self.episode = str(episode) 104 | 105 | @property 106 | def gmid(self) -> str: 107 | assert self.tmdb, "Cannot get gmid without tmdb existing!" 108 | gmid = f"{self.media_type.gmid_key}.{self.tmdb}" 109 | if self.media_type == MediaEnum.SERIES: 110 | gmid += f".{self.season}.{self.episode}" 111 | return gmid 112 | 113 | class ProviderHeaders: 114 | def __init__( 115 | self, 116 | origin: Optional[str] = None, 117 | referrer: Optional[str] = None, 118 | user_agent: Optional[str] = None, 119 | additional_headers: Optional[Dict] = None, 120 | **kwargs 121 | ) -> None: 122 | self._origin = origin 123 | self._referrer = referrer 124 | self._user_agent = user_agent or default_ua 125 | self._headers = { 126 | "Origin": self._origin, 127 | "Referrer": self._referrer, 128 | "User-Agent": self._user_agent, 129 | } 130 | if additional_headers: 131 | self._headers.update(additional_headers) 132 | self._headers.update(kwargs) 133 | 134 | @property 135 | def origin(self) -> str: 136 | if not self._origin: 137 | raise ValueError("Origin must be set first!") 138 | return self._origin 139 | 140 | @origin.setter 141 | def origin(self, value: str) -> None: 142 | self._origin = value 143 | self._headers.update({"Origin": self._origin}) 144 | 145 | @property 146 | def referrer(self) -> str: 147 | if not self._referrer: 148 | raise ValueError("Referrer must be set first!") 149 | return self._referrer 150 | 151 | @referrer.setter 152 | def referrer(self, value: str) -> None: 153 | self._referrer = value 154 | self._headers.update({"Referrer": self._referrer}) 155 | 156 | @property 157 | def user_agent(self) -> str: 158 | if not self._user_agent: 159 | raise ValueError("User agent must be set first!") 160 | return self._user_agent 161 | 162 | @user_agent.setter 163 | def user_agent(self, value: str) -> None: 164 | self._user_agent = value 165 | self._headers.update({"User-Agent": self._user_agent}) 166 | 167 | @property 168 | def headers(self) -> Dict: 169 | if not self._origin: 170 | raise ValueError("Origin must be set first!") 171 | if not self._referrer: 172 | raise ValueError("Referrer must be set first!") 173 | if not self._user_agent: 174 | raise ValueError("User agent must be set first!") 175 | return self._headers.copy() 176 | 177 | def add_header(self, key: str, value: str) -> None: 178 | self._headers[key] = value 179 | 180 | def remove_header(self, key: str) -> None: 181 | if key in self._headers: 182 | del self._headers[key] 183 | 184 | def __repr__(self) -> str: 185 | return f"ProviderHeaders(origin='{self._origin}', referrer='{self._referrer}', user_agent='{self._user_agent}')" 186 | 187 | class Subtitle: 188 | def __init__(self, language: str, url: str, ext: str): 189 | self.language = language 190 | self.url = url 191 | self.ext = ext 192 | 193 | @property 194 | def as_dict(self) -> Dict: 195 | return { 196 | "language": self.language, 197 | "url": self.url, 198 | "ext": self.ext 199 | } 200 | 201 | def __repr__(self) -> str: 202 | return f"Subtitle(language='{self.language}', url='{self.url}', ext='{self.ext}')" 203 | 204 | def __eq__(self, other) -> bool: 205 | if not isinstance(other, Subtitle): 206 | return False 207 | return self.language == other.language and self.url == other.url and self.ext == other.ext 208 | 209 | def __hash__(self) -> int: 210 | return hash((self.language, self.url, self.ext)) 211 | 212 | class Stream: 213 | def __init__( 214 | self, 215 | provider: str, 216 | headers: ProviderHeaders, 217 | url: str, 218 | ext: str, 219 | quality: Union[str, QualityEnum], 220 | codec: Optional[str] = None, 221 | bandwith: Optional[int] = None, 222 | audio_channels: Optional[int] = None, 223 | **kwargs 224 | ) -> None: 225 | self.provider = provider 226 | self.headers = headers 227 | self.url = url 228 | self.ext = ext 229 | self.quality = quality if isinstance(quality, QualityEnum) else QualityEnum.map_enum(quality) 230 | self.codec = codec 231 | self.bandwith = bandwith 232 | self.audio_channels = audio_channels # TODO 233 | 234 | @property 235 | def as_dict(self) -> Dict: 236 | return { 237 | "provider": self.provider, 238 | "headers": self.headers.headers, 239 | "url": self.url, 240 | "ext": self.ext, 241 | "quality": self.quality, 242 | "codec": self.codec, 243 | "bandwith": self.bandwith, 244 | "audio_channels": self.audio_channels 245 | } 246 | 247 | def __repr__(self) -> str: 248 | return f"Stream(provider='{self.provider}', headers={self.headers}, url='{self.url}', ext='{self.ext}'," \ 249 | f"quality={self.quality}, codec='{self.codec}', bandwith={self.bandwith}, audio_channels={self.audio_channels})" 250 | 251 | def __eq__(self, other) -> bool: 252 | if not isinstance(other, Stream): 253 | return False 254 | return self.url == other.url and self.quality == other.quality and \ 255 | self.provider == other.provider and self.headers == other.headers and\ 256 | self.ext == other.ext and self.codec == other.codec and self.bandwith == other.bandwith and\ 257 | self.audio_channels == other.audio_channels 258 | 259 | def __hash__(self) -> int: 260 | return hash((self.url, self.quality, self.provider, self.headers, self.ext, self.codec, self.bandwith, self.audio_channels)) 261 | 262 | class ProviderResponse: 263 | def __init__( 264 | self, 265 | provider: str, 266 | streams: List[Stream], 267 | subtitles: List[Subtitle], 268 | ) -> None: 269 | self.provider = provider 270 | self.streams = streams 271 | self.subtitles = subtitles 272 | 273 | @property 274 | def as_dict(self) -> Dict: 275 | return { 276 | "provider": self.provider, 277 | "streams": [stream.as_dict for stream in self.streams], 278 | "subtitles": [subtitle.as_dict for subtitle in self.subtitles] 279 | } 280 | 281 | def __repr__(self) -> str: 282 | return f"ProviderResponse(provider='{self.provider}', streams={self.streams}, subtitles={self.subtitles})" 283 | 284 | class SourceResponse: 285 | def __init__( 286 | self, 287 | source: str, 288 | providers: List[Optional[ProviderResponse]] 289 | ) -> None: 290 | self.source = source 291 | self.providers = providers 292 | 293 | @property 294 | def as_dict(self) -> Dict: 295 | return { 296 | "name": self.source, 297 | "providers": [provider.as_dict for provider in self.providers if provider] 298 | } 299 | 300 | def __getitem__(self, item) -> Optional[ProviderResponse]: 301 | result = list.__getitem__(self.providers, item) 302 | try: 303 | return result 304 | except TypeError: 305 | return result 306 | 307 | def __repr__(self) -> str: 308 | return f"SourceResponse(source='{self.source}', providers={self.providers})" -------------------------------------------------------------------------------- /mypy.ini: -------------------------------------------------------------------------------- 1 | [mypy-pythonmonkey] 2 | ignore_missing_imports = true 3 | 4 | [mypy-tenacity] 5 | ignore_missing_imports = true -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=42", "wheel"] 3 | build-backend = "setuptools.build_meta" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | httpx==0.27.0 2 | pycryptodomex==3.20.0 3 | pythonmonkey==0.8.0 4 | tenacity==9.0.0 5 | rich==13.7.1 6 | click==8.1.7 7 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = movie-cat-providers 3 | version = 0.0.0 4 | author = Ciarán 5 | description = Providers used by movie-cat. 6 | long_description = file: README.md 7 | long_description_content_type = text/markdown 8 | url = https://github.com/movie-cat/providers/tree/main 9 | classifiers = 10 | Programming Language :: Python :: 3 11 | License :: OSI Approved :: MIT License 12 | Operating System :: OS Independent 13 | 14 | [options] 15 | packages = find: 16 | install_requires = 17 | httpx==0.27.0 18 | pycryptodomex==3.20.0 19 | pythonmonkey==0.8.0 20 | tenacity==9.0.0 21 | rich==13.7.1 22 | click==8.1.7 23 | python_requires = >=3.6 24 | 25 | [options.entry_points] 26 | console_scripts = 27 | mcat-providers = mcat_providers.__init__:main -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import {} }: 2 | 3 | pkgs.mkShell { 4 | buildInputs = [ 5 | pkgs.python310 6 | pkgs.python310Packages.setuptools 7 | pkgs.python310Packages.wheel 8 | pkgs.python310Packages.pip 9 | pkgs.python310Packages.virtualenv 10 | ]; 11 | 12 | shellHook = '' 13 | if [ ! -d ".venv" ]; then 14 | python -m venv .venv 15 | fi 16 | 17 | source .venv/bin/activate 18 | 19 | pip install --upgrade pip 20 | pip install -r requirements.txt 21 | 22 | export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [ 23 | pkgs.stdenv.cc.cc 24 | ]} 25 | ''; 26 | } 27 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import asyncio 3 | from mcat_providers.sources import flixhq 4 | 5 | if __name__ == "__main__": 6 | src = flixhq.FlixHq() 7 | data = asyncio.run(src.scrape_all( 8 | tmdb=sys.argv[1], 9 | media_type="movie" 10 | )) 11 | print(data.as_dict) 12 | --------------------------------------------------------------------------------