Commit f4eb076c83
Unsigned
Layout: unified · split
anilist.py +70 −2
| @@ -1,8 +1,50 @@ | ||
| 1 | 1 | import requests |
| 2 | 2 | import json |
| 3 | 3 | |
| 4 | def search_anime_anilist(query, token): | |
| 5 | url = "https://graphql.anilist.co" | |
| 6 | ||
| 7 | query_string = ''' | |
| 8 | query ($search: String) { | |
| 9 | Page(page: 1, perPage: 10) { | |
| 10 | media(search: $search, type: ANIME) { | |
| 11 | id | |
| 12 | title { | |
| 13 | romaji | |
| 14 | english | |
| 15 | native | |
| 16 | } | |
| 17 | } | |
| 18 | } | |
| 19 | } | |
| 20 | ''' | |
| 21 | ||
| 22 | variables = { | |
| 23 | 'search': query | |
| 24 | } | |
| 25 | ||
| 26 | headers = { | |
| 27 | 'Authorization': f'Bearer {token}', | |
| 28 | 'Content-Type': 'application/json' | |
| 29 | } | |
| 30 | ||
| 31 | response = requests.post(url, json={'query': query_string, 'variables': variables}, headers=headers) | |
| 32 | ||
| 33 | if response.status_code == 200: | |
| 34 | anime_list = response.json()['data']['Page']['media'] | |
| 35 | anime_dict = {} | |
| 36 | for anime in anime_list: | |
| 37 | if anime['title']['english']==None: | |
| 38 | anime_dict[anime['title']['romaji']] = anime['id'] | |
| 39 | else: | |
| 40 | anime_dict[anime['title']['english']] = anime['id'] | |
| 41 | ||
| 42 | return anime_dict | |
| 43 | else: | |
| 44 | print(f"Failed to search for anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 45 | return {} | |
| 46 | ||
| 4 | 47 | |
| 5 | import requests | |
| 6 | 48 | |
| 7 | 49 | def get_anilist_user_id(token): |
| 8 | 50 | url = "https://graphql.anilist.co" |
| @@ -31,7 +73,33 @@ def get_anilist_user_id(token): | ||
| 31 | 73 | else: |
| 32 | 74 | raise Exception(f"Error: {response.status_code}, {response.text}") |
| 33 | 75 | |
| 34 | # Replace 'your_token' with your actual AniList token | |
| 76 | def add_anime_to_watching_list(anime_id: int, token: str): | |
| 77 | url = "https://graphql.anilist.co" | |
| 78 | ||
| 79 | mutation = ''' | |
| 80 | mutation ($mediaId: Int) { | |
| 81 | SaveMediaListEntry (mediaId: $mediaId, status: CURRENT) { | |
| 82 | id | |
| 83 | status | |
| 84 | } | |
| 85 | } | |
| 86 | ''' | |
| 87 | ||
| 88 | variables = { | |
| 89 | 'mediaId': anime_id | |
| 90 | } | |
| 91 | ||
| 92 | headers = { | |
| 93 | 'Authorization': f'Bearer {token}', | |
| 94 | 'Content-Type': 'application/json' | |
| 95 | } | |
| 96 | ||
| 97 | response = requests.post(url, json={'query': mutation, 'variables': variables}, headers=headers) | |
| 98 | ||
| 99 | if response.status_code == 200: | |
| 100 | print(f"Anime with ID {anime_id} has been added to your watching list.") | |
| 101 | else: | |
| 102 | print(f"Failed to add anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 35 | 103 | |
| 36 | 104 | |
| 37 | 105 | def get_user_data(access_token, user_id): |
curd.py +25 −14
| @@ -11,9 +11,10 @@ import json | ||
| 11 | 11 | import random |
| 12 | 12 | import subprocess |
| 13 | 13 | import time |
| 14 | import argparse | |
| 14 | 15 | |
| 15 | 16 | from anilist import search_anime_by_title |
| 16 | from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime | |
| 17 | from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list | |
| 17 | 18 | from select_link import load_links |
| 18 | 19 | from start_video import start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed |
| 19 | 20 | from select_anime import load_anime_data |
| @@ -24,17 +25,6 @@ from track_anime import add_anime, update_anime, get_all_anime, delete_anime, fi | ||
| 24 | 25 | |
| 25 | 26 | mark_episode_as_completed_at = 85 |
| 26 | 27 | |
| 27 | access_token = os.environ.get('ANILIST_ACCESS_TOKEN') | |
| 28 | user_id, user_name = get_anilist_user_id(access_token) | |
| 29 | # user_id = os.environ.get('ANILIST_USER_ID') | |
| 30 | ||
| 31 | if access_token == None: | |
| 32 | print("No Access_token provided.") | |
| 33 | exit(1) | |
| 34 | ||
| 35 | # print(access_token) | |
| 36 | # print(user_id) | |
| 37 | ||
| 38 | 28 | def get_contents_of(tmp_file_name): |
| 39 | 29 | with open(f"scripts/tmp/{tmp_file_name}", "r") as temp_file: |
| 40 | 30 | return temp_file.read() |
| @@ -80,8 +70,8 @@ default_config = { | ||
| 80 | 70 | "history_file":"$HOME/.local/share/curd/curd_history.txt", |
| 81 | 71 | "subs_language":"english", |
| 82 | 72 | "sub_or_dub":"sub", |
| 83 | "next_episode_prompt":False, | |
| 84 | "score_on_completion":False, | |
| 73 | "next_episode_prompt": False, | |
| 74 | "score_on_completion": True, | |
| 85 | 75 | "save_mpv_speed": True, |
| 86 | 76 | "discord_presence": True, |
| 87 | 77 | "presence_script_path":"curddiscordpresence.py" |
| @@ -169,6 +159,27 @@ def load_config() -> dict: | ||
| 169 | 159 | # return config_file.read() |
| 170 | 160 | |
| 171 | 161 | # START OF SCRIPT |
| 162 | access_token = os.environ.get('ANILIST_ACCESS_TOKEN') | |
| 163 | ||
| 164 | parser = argparse.ArgumentParser(description="Print a greeting message.") | |
| 165 | # parser.add_argument("--name", type=str, required=True, help="Your name") | |
| 166 | parser.add_argument("-new", action='store_true', help="Add new anime to watching (optional)") | |
| 167 | args = parser.parse_args() | |
| 168 | ||
| 169 | if args.new: | |
| 170 | new_anime_name_ = input("Enter anime name: ") | |
| 171 | temp__ = search_anime_anilist(new_anime_name_, access_token) | |
| 172 | if temp__ : | |
| 173 | select_anime(temp__) | |
| 174 | add_anime_to_watching_list(read_tmp('id'), access_token) | |
| 175 | else: | |
| 176 | print("No anime found") | |
| 177 | ||
| 178 | user_id, user_name = get_anilist_user_id(access_token) | |
| 179 | ||
| 180 | if access_token == None: | |
| 181 | print("No Access_token provided.") | |
| 182 | exit(1) | |
| 172 | 183 | |
| 173 | 184 | user_config = load_config() # python dictionary containing all the configs as key value pairs |
| 174 | 185 | anilist_user_data = download_anilist_data(access_token, user_id) |
select_anime.py +4 −2
| @@ -137,8 +137,10 @@ def select_anime(anime_list): | ||
| 137 | 137 | with open("scripts/tmp/anime", "w") as anime_name: |
| 138 | 138 | anime_name.write(selected_anime) |
| 139 | 139 | |
| 140 | with open("scripts/tmp/id", "w") as id: | |
| 141 | id.write(selected_id) | |
| 140 | with open("scripts/tmp/id", "w") as id_file: | |
| 141 | id_file.write(str(selected_id)) | |
| 142 | ||
| 143 | return True | |
| 142 | 144 | |
| 143 | 145 | # Initialize curses |
| 144 | 146 | try: |