├── .gitignore ├── README.md ├── demo.mp4 ├── magatama.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Magatama 2 | A python CLI utility that can give you a random anime or manga from a selected genre, you can then watch the fetched anime straight from the terminal using `ani-cli`. Made using InquirerPy and the Jikan API to pull data from MyAnimeList. 3 | 4 | ## Demo 5 | https://github.com/tsukki9696/magatama/assets/127806743/98ab56cd-4fcb-44ce-a12d-79697402e6c7 6 | 7 | ## Dependencies 8 | - Python 3.x 9 | - [InquirerPy](https://github.com/kazhala/InquirerPy) 0.3.3 10 | - [JikanPy](https://github.com/abhinavk99/jikanpy) 1.0.0 11 | - [clipboard](https://pypi.org/project/clipboard/) 0.0.4 12 | - [ani-cli](https://github.com/pystardust/ani-cli) 4.6 (optional) 13 | 14 | ## Installation 15 | Clone this repository, then download the dependencies. 16 | ``` 17 | git clone https://github.com/tsukki9696/magatama.git 18 | cd magatama/ 19 | pip install -r requirements.txt 20 | ``` 21 | 22 | **TODO**: Package script to the AUR. 23 | 24 | ## Usage 25 | ``` 26 | python magatama.py 27 | ``` 28 | 29 | Move up and down genres with the arrow keys, confirm with Enter/Return. 30 | 31 | ## Limitations 32 | Currently, the script is quite slow, and since the API requests from Jikan are fairly limited, I can only do so much optimisation, but improving the fetching speed is in my plans. 33 | 34 | ## Credits 35 | - JikanPy wrapper and the Jikan API maintainers 36 | - InquirerPy maintainers 37 | - ani-cli maintainers 38 | - [Where I got this whole idea from](https://www.youtube.com/watch?v=_xf1TMs0ysk&t=194s&pp=ygUdcHl0aG9uIHByb2plY3RzIGZvciBiZWdpbm5lcnM%3D) 39 | -------------------------------------------------------------------------------- /demo.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deceptionfalls/magatama/0205bd9170319f4ad8133cfebe9baf0ebd77abea/demo.mp4 -------------------------------------------------------------------------------- /magatama.py: -------------------------------------------------------------------------------- 1 | import time 2 | import subprocess 3 | import os 4 | import random 5 | import pyperclip 6 | from InquirerPy import inquirer 7 | from jikanpy import Jikan 8 | 9 | api = Jikan() 10 | 11 | # Genre IDs from Jikan 12 | # Master dictionary, this is so we can have our selected genre's 13 | # name appear in InquirerPy when we use it later down in the prompts 14 | masterdict = { 15 | "Shounen": 27, 16 | "Fantasy": 10, 17 | "Drama": 8, 18 | "Sci-fi": 24, 19 | "Romance": 22, 20 | "Comedy": 4, 21 | "Seinen": 42, 22 | "Horror": 14, 23 | "Sports": 30, 24 | "Mecha": 18, 25 | } 26 | 27 | anime_set: dict[str, dict] = dict() 28 | manga_set = anime_set 29 | 30 | def get_random_animanga(media_type, genre_id): 31 | # Rate limits for the API 32 | requests_per_second = 2 33 | start_time = time.time() 34 | media_set = {} 35 | 36 | # This is a hack, since Jikan doesn't allow us to random search based on genre 37 | # we have to submit an empty query with a specified genre -- the genre_id parameter, 38 | # it'll be used down in the code when we select our genre -- then we fetch 39 | # a couple of anime based on that query, return just the title, and finally we 40 | # fetch a random anime from that anime pool we got 41 | for i in range(10): 42 | elapsed_time = time.time() - start_time 43 | 44 | # Rate limit delays so we don't bork the API 45 | if elapsed_time < 1.0 / requests_per_second: 46 | time.sleep(1.0 / requests_per_second - elapsed_time) 47 | 48 | response = api.search( 49 | search_type=media_type, query="", page=i + 1, parameters={"genres": genre_id} 50 | ) 51 | 52 | start_time = time.time() 53 | 54 | for media in response["data"]: 55 | media_set[media["title"]] = media 56 | 57 | random_media_title = random.choice(list(media_set.keys())) 58 | return random_media_title # return the random anime so we can use it down in the main func 59 | 60 | 61 | def random_animanga(): 62 | # Initial prompts 63 | choice = inquirer.select(message="Select one: ", choices=["Anime", "Manga"]).execute() 64 | os.system("clear") 65 | genre = inquirer.select(message="Choose a genre: ", choices=[item for item in masterdict]).execute() 66 | 67 | genre_id = masterdict[genre] # Selected genre is used as a parameter for our query 68 | 69 | time.sleep(1) 70 | os.system("clear") 71 | 72 | if choice == "Anime": 73 | media_type = "anime" 74 | print("Fetching anime...") 75 | else: 76 | media_type = "manga" 77 | print("Fetching manga...") 78 | 79 | media = get_random_animanga(media_type, genre_id) 80 | os.system("clear") 81 | 82 | print( 83 | f"You selected: \033[93m{genre}\033[0m.\nYour random {media_type} is \033[91m{media}\033[0m." # Colored output 84 | ) 85 | 86 | time.sleep(1) 87 | 88 | if choice == "Anime": 89 | prompt = inquirer.select( 90 | message="Want to watch it? (Requires ani-cli)", 91 | choices=["Yes", "No"], 92 | ).execute() 93 | 94 | if prompt == "Yes": 95 | subprocess.run(["ani-cli", media]) 96 | else: 97 | prompt = inquirer.select( 98 | message="Want to read it on Mangadex?", 99 | choices=["Yes", "No"], 100 | ).execute() 101 | 102 | if prompt == "Yes": 103 | pyperclip.copy(f"https://mangadex.org/search?q={media}") 104 | print("\033[93mLink copied to your clipboard!\033[0m") 105 | 106 | random_animanga() 107 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | inquirerpy 2 | jikanpy-v4 3 | clipboard 4 | --------------------------------------------------------------------------------