Commit a992c39690
Unsigned
Layout: unified · split
anilist.py deleted −307
| @@ -1,307 +0,0 @@ | ||
| 1 | import requests | |
| 2 | import json | |
| 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 | ||
| 47 | ||
| 48 | ||
| 49 | def get_anilist_user_id(token): | |
| 50 | url = "https://graphql.anilist.co" | |
| 51 | query = ''' | |
| 52 | query { | |
| 53 | Viewer { | |
| 54 | id | |
| 55 | name | |
| 56 | } | |
| 57 | } | |
| 58 | ''' | |
| 59 | ||
| 60 | headers = { | |
| 61 | "Authorization": f"Bearer {token}", | |
| 62 | "Content-Type": "application/json", | |
| 63 | "Accept": "application/json" | |
| 64 | } | |
| 65 | ||
| 66 | response = requests.post(url, json={"query": query}, headers=headers) | |
| 67 | ||
| 68 | if response.status_code == 200: | |
| 69 | data = response.json() | |
| 70 | user_id = data['data']['Viewer']['id'] | |
| 71 | user_name = data['data']['Viewer']['name'] | |
| 72 | return user_id, user_name | |
| 73 | else: | |
| 74 | raise Exception(f"Error: {response.status_code}, {response.text}") | |
| 75 | ||
| 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}") | |
| 103 | ||
| 104 | def get_anime_image(anilist_media_id): | |
| 105 | url = "https://graphql.anilist.co" | |
| 106 | query = ''' | |
| 107 | query ($id: Int) { | |
| 108 | Media(id: $id) { | |
| 109 | coverImage { | |
| 110 | large | |
| 111 | } | |
| 112 | } | |
| 113 | } | |
| 114 | ''' | |
| 115 | variables = { | |
| 116 | 'id': anilist_media_id | |
| 117 | } | |
| 118 | ||
| 119 | # Make the API request | |
| 120 | response = requests.post(url, json={'query': query, 'variables': variables}) | |
| 121 | ||
| 122 | if response.status_code == 200: | |
| 123 | data = response.json() | |
| 124 | image_url = data['data']['Media']['coverImage']['large'] | |
| 125 | return image_url | |
| 126 | else: | |
| 127 | raise Exception(f"Failed to retrieve data from AniList API. Status Code: {response.status_code}") | |
| 128 | ||
| 129 | def get_user_data(access_token, user_id): | |
| 130 | # Replace with your actual access token and user ID | |
| 131 | # access_token = | |
| 132 | # user_id = | |
| 133 | ||
| 134 | # GraphQL query | |
| 135 | query = """ | |
| 136 | { | |
| 137 | MediaListCollection(userId: %s, type: ANIME) { | |
| 138 | lists { | |
| 139 | entries { | |
| 140 | media { | |
| 141 | id | |
| 142 | episodes | |
| 143 | duration | |
| 144 | title { | |
| 145 | romaji | |
| 146 | english | |
| 147 | } | |
| 148 | } | |
| 149 | status | |
| 150 | score | |
| 151 | progress | |
| 152 | } | |
| 153 | } | |
| 154 | } | |
| 155 | } | |
| 156 | """ % user_id | |
| 157 | ||
| 158 | # Send the request | |
| 159 | response = requests.post( | |
| 160 | 'https://graphql.anilist.co', | |
| 161 | json={'query': query}, | |
| 162 | headers={'Authorization': f'Bearer {access_token}'} | |
| 163 | ) | |
| 164 | ||
| 165 | ||
| 166 | # Print the response | |
| 167 | return response.json() | |
| 168 | ||
| 169 | def load_json_file(file_path): | |
| 170 | with open(file_path, 'r', encoding='utf-8') as file: | |
| 171 | return json.load(file) | |
| 172 | ||
| 173 | def search_anime_by_title(json_data, search_title): | |
| 174 | results = [] | |
| 175 | if search_title=="1P": | |
| 176 | search_title = "ONE PIECE" | |
| 177 | for list_item in json_data['data']['MediaListCollection']['lists']: | |
| 178 | for entry in list_item['entries']: | |
| 179 | media = entry['media'] | |
| 180 | romaji_title = media['title']['romaji'] | |
| 181 | english_title = media['title']['english'] | |
| 182 | episodes = media['episodes'] | |
| 183 | duration = media['duration'] | |
| 184 | try: | |
| 185 | if search_title.lower() in romaji_title.lower() or search_title.lower() in english_title.lower(): | |
| 186 | results.append({ | |
| 187 | 'id': media['id'], | |
| 188 | 'progress': entry['progress'], | |
| 189 | 'romaji_title': romaji_title, | |
| 190 | 'english_title': english_title, | |
| 191 | 'episodes': episodes, | |
| 192 | 'duration': duration, | |
| 193 | }) | |
| 194 | except: | |
| 195 | pass | |
| 196 | ||
| 197 | return results | |
| 198 | ||
| 199 | def update_anime_progress(token: str, media_id: int, progress: int): | |
| 200 | url = "https://graphql.anilist.co" | |
| 201 | ||
| 202 | query = ''' | |
| 203 | mutation($mediaId: Int, $progress: Int) { | |
| 204 | SaveMediaListEntry(mediaId: $mediaId, progress: $progress) { | |
| 205 | id | |
| 206 | progress | |
| 207 | } | |
| 208 | } | |
| 209 | ''' | |
| 210 | ||
| 211 | variables = { | |
| 212 | "mediaId": media_id, # The AniList ID of the anime | |
| 213 | "progress": progress # The number of the latest episode you watched | |
| 214 | } | |
| 215 | ||
| 216 | headers = { | |
| 217 | "Authorization": f"Bearer {token}", | |
| 218 | "Content-Type": "application/json", | |
| 219 | "Accept": "application/json" | |
| 220 | } | |
| 221 | ||
| 222 | response = requests.post(url, json={"query": query, "variables": variables}, headers=headers) | |
| 223 | ||
| 224 | if response.status_code == 200: | |
| 225 | data = response.json() | |
| 226 | updated_progress = data['data']['SaveMediaListEntry']['progress'] | |
| 227 | print("updating progress..") | |
| 228 | print(f"Anime progress updated! Latest watched episode: {updated_progress}") | |
| 229 | else: | |
| 230 | print(f"Error {response.status_code}: {response.text}") | |
| 231 | ||
| 232 | def rate_anime(anilist_token, media_id, score): | |
| 233 | url = 'https://graphql.anilist.co' | |
| 234 | ||
| 235 | # GraphQL mutation to rate anime | |
| 236 | query = ''' | |
| 237 | mutation($mediaId: Int, $score: Float) { | |
| 238 | SaveMediaListEntry(mediaId: $mediaId, score: $score) { | |
| 239 | id | |
| 240 | mediaId | |
| 241 | score | |
| 242 | } | |
| 243 | } | |
| 244 | ''' | |
| 245 | ||
| 246 | # Variables for the mutation | |
| 247 | variables = { | |
| 248 | "mediaId": media_id, | |
| 249 | "score": float(score) | |
| 250 | } | |
| 251 | ||
| 252 | headers = { | |
| 253 | "Authorization": f"Bearer {anilist_token}", | |
| 254 | "Content-Type": "application/json", | |
| 255 | } | |
| 256 | ||
| 257 | response = requests.post(url, json={'query': query, 'variables': variables}, headers=headers) | |
| 258 | ||
| 259 | if response.status_code == 200: | |
| 260 | data = response.json() | |
| 261 | print(f"Successfully rated anime (mediaId: {media_id}) with score: {score}") | |
| 262 | return data | |
| 263 | else: | |
| 264 | print(f"Failed to rate anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 265 | return None | |
| 266 | ||
| 267 | def main(): | |
| 268 | # Load the JSON file | |
| 269 | json_file_path = 'response.json' # Replace with the path to your JSON file | |
| 270 | json_data = load_json_file(json_file_path) | |
| 271 | ||
| 272 | # Ask for the title to search | |
| 273 | # search_title = input("Enter the anime title to search (Romaji or English): ") | |
| 274 | ||
| 275 | search_title = "1P" | |
| 276 | ||
| 277 | # Search for the anime | |
| 278 | results = search_anime_by_title(json_data, search_title) | |
| 279 | ||
| 280 | # Print the results | |
| 281 | if results: | |
| 282 | for result in results: | |
| 283 | print(f"Anime ID: {result['id']}, Progress: {result['progress']}, " | |
| 284 | f"Romaji Title: {result['romaji_title']}, English Title: {result['english_title']}") | |
| 285 | else: | |
| 286 | print("Anime not found.") | |
| 287 | ||
| 288 | # if __name__ == "__main__": | |
| 289 | # main() | |
| 290 | ||
| 291 | ||
| 292 | # if __name__ == '__main__': | |
| 293 | # # Load JSON data from a file (replace 'your_file.json' with the actual file name) | |
| 294 | # with open('response.json', 'r') as file: | |
| 295 | # data = json.load(file) | |
| 296 | ||
| 297 | # # Define the title to search for | |
| 298 | # search_title = 'Fullmetal Alchemist: Brotherhood' # Replace with the actual title you're searching for | |
| 299 | ||
| 300 | # # Search for the anime and get its ID | |
| 301 | # anime_id = search_anime_by_title(data, search_title) | |
| 302 | ||
| 303 | # # Print the result | |
| 304 | # if anime_id: | |
| 305 | # print(f"Anime ID: {anime_id}") | |
| 306 | # else: | |
| 307 | # print("Anime not found.") | |
curd.py +618 −20
| @@ -13,19 +13,627 @@ import json | ||
| 13 | 13 | import random |
| 14 | 14 | import subprocess |
| 15 | 15 | import time |
| 16 | import curses | |
| 16 | 17 | import argparse |
| 17 | ||
| 18 | from anilist import get_anime_image, search_anime_by_title | |
| 19 | from anilist import get_user_data, get_anilist_user_id, update_anime_progress, rate_anime, search_anime_anilist, add_anime_to_watching_list | |
| 20 | from select_link import load_links | |
| 21 | from start_video import get_mpv_paused_status, start_video, send_command, get_percentage_watched, percentage_watched, get_mpv_playback_speed | |
| 22 | from select_anime import load_anime_data | |
| 23 | from select_anime import extract_anime_info | |
| 24 | from select_anime import select_anime | |
| 25 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime | |
| 18 | import requests | |
| 19 | import csv | |
| 20 | import socket | |
| 26 | 21 | |
| 27 | 22 | discord_client_id = "1287457464148820089" |
| 28 | 23 | |
| 24 | # ----------------------------------------------- AniList Functions ---------------------------------------- | |
| 25 | ||
| 26 | def search_anime_anilist(query, token): | |
| 27 | url = "https://graphql.anilist.co" | |
| 28 | ||
| 29 | query_string = ''' | |
| 30 | query ($search: String) { | |
| 31 | Page(page: 1, perPage: 10) { | |
| 32 | media(search: $search, type: ANIME) { | |
| 33 | id | |
| 34 | title { | |
| 35 | romaji | |
| 36 | english | |
| 37 | native | |
| 38 | } | |
| 39 | } | |
| 40 | } | |
| 41 | } | |
| 42 | ''' | |
| 43 | ||
| 44 | variables = { | |
| 45 | 'search': query | |
| 46 | } | |
| 47 | ||
| 48 | headers = { | |
| 49 | 'Authorization': f'Bearer {token}', | |
| 50 | 'Content-Type': 'application/json' | |
| 51 | } | |
| 52 | ||
| 53 | response = requests.post(url, json={'query': query_string, 'variables': variables}, headers=headers) | |
| 54 | ||
| 55 | if response.status_code == 200: | |
| 56 | anime_list = response.json()['data']['Page']['media'] | |
| 57 | anime_dict = {} | |
| 58 | for anime in anime_list: | |
| 59 | if anime['title']['english']==None: | |
| 60 | anime_dict[anime['title']['romaji']] = anime['id'] | |
| 61 | else: | |
| 62 | anime_dict[anime['title']['english']] = anime['id'] | |
| 63 | ||
| 64 | return anime_dict | |
| 65 | else: | |
| 66 | print(f"Failed to search for anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 67 | return {} | |
| 68 | ||
| 69 | def get_anilist_user_id(token): | |
| 70 | url = "https://graphql.anilist.co" | |
| 71 | query = ''' | |
| 72 | query { | |
| 73 | Viewer { | |
| 74 | id | |
| 75 | name | |
| 76 | } | |
| 77 | } | |
| 78 | ''' | |
| 79 | ||
| 80 | headers = { | |
| 81 | "Authorization": f"Bearer {token}", | |
| 82 | "Content-Type": "application/json", | |
| 83 | "Accept": "application/json" | |
| 84 | } | |
| 85 | ||
| 86 | response = requests.post(url, json={"query": query}, headers=headers) | |
| 87 | ||
| 88 | if response.status_code == 200: | |
| 89 | data = response.json() | |
| 90 | user_id = data['data']['Viewer']['id'] | |
| 91 | user_name = data['data']['Viewer']['name'] | |
| 92 | return user_id, user_name | |
| 93 | else: | |
| 94 | raise Exception(f"Error: {response.status_code}, {response.text}") | |
| 95 | ||
| 96 | def add_anime_to_watching_list(anime_id: int, token: str): | |
| 97 | url = "https://graphql.anilist.co" | |
| 98 | ||
| 99 | mutation = ''' | |
| 100 | mutation ($mediaId: Int) { | |
| 101 | SaveMediaListEntry (mediaId: $mediaId, status: CURRENT) { | |
| 102 | id | |
| 103 | status | |
| 104 | } | |
| 105 | } | |
| 106 | ''' | |
| 107 | ||
| 108 | variables = { | |
| 109 | 'mediaId': anime_id | |
| 110 | } | |
| 111 | ||
| 112 | headers = { | |
| 113 | 'Authorization': f'Bearer {token}', | |
| 114 | 'Content-Type': 'application/json' | |
| 115 | } | |
| 116 | ||
| 117 | response = requests.post(url, json={'query': mutation, 'variables': variables}, headers=headers) | |
| 118 | ||
| 119 | if response.status_code == 200: | |
| 120 | print(f"Anime with ID {anime_id} has been added to your watching list.") | |
| 121 | else: | |
| 122 | print(f"Failed to add anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 123 | ||
| 124 | def get_anime_image(anilist_media_id): | |
| 125 | url = "https://graphql.anilist.co" | |
| 126 | query = ''' | |
| 127 | query ($id: Int) { | |
| 128 | Media(id: $id) { | |
| 129 | coverImage { | |
| 130 | large | |
| 131 | } | |
| 132 | } | |
| 133 | } | |
| 134 | ''' | |
| 135 | variables = { | |
| 136 | 'id': anilist_media_id | |
| 137 | } | |
| 138 | ||
| 139 | # Make the API request | |
| 140 | response = requests.post(url, json={'query': query, 'variables': variables}) | |
| 141 | ||
| 142 | if response.status_code == 200: | |
| 143 | data = response.json() | |
| 144 | image_url = data['data']['Media']['coverImage']['large'] | |
| 145 | return image_url | |
| 146 | else: | |
| 147 | raise Exception(f"Failed to retrieve data from AniList API. Status Code: {response.status_code}") | |
| 148 | ||
| 149 | def get_user_data(access_token, user_id): | |
| 150 | query = """ | |
| 151 | { | |
| 152 | MediaListCollection(userId: %s, type: ANIME) { | |
| 153 | lists { | |
| 154 | entries { | |
| 155 | media { | |
| 156 | id | |
| 157 | episodes | |
| 158 | duration | |
| 159 | title { | |
| 160 | romaji | |
| 161 | english | |
| 162 | } | |
| 163 | } | |
| 164 | status | |
| 165 | score | |
| 166 | progress | |
| 167 | } | |
| 168 | } | |
| 169 | } | |
| 170 | } | |
| 171 | """ % user_id | |
| 172 | ||
| 173 | # Send the request | |
| 174 | response = requests.post( | |
| 175 | 'https://graphql.anilist.co', | |
| 176 | json={'query': query}, | |
| 177 | headers={'Authorization': f'Bearer {access_token}'} | |
| 178 | ) | |
| 179 | ||
| 180 | ||
| 181 | # Print the response | |
| 182 | return response.json() | |
| 183 | ||
| 184 | def load_json_file(file_path): | |
| 185 | with open(file_path, 'r', encoding='utf-8') as file: | |
| 186 | return json.load(file) | |
| 187 | ||
| 188 | def search_anime_by_title(json_data, search_title): | |
| 189 | results = [] | |
| 190 | if search_title=="1P": | |
| 191 | search_title = "ONE PIECE" | |
| 192 | for list_item in json_data['data']['MediaListCollection']['lists']: | |
| 193 | for entry in list_item['entries']: | |
| 194 | media = entry['media'] | |
| 195 | romaji_title = media['title']['romaji'] | |
| 196 | english_title = media['title']['english'] | |
| 197 | episodes = media['episodes'] | |
| 198 | duration = media['duration'] | |
| 199 | try: | |
| 200 | if search_title.lower() in romaji_title.lower() or search_title.lower() in english_title.lower(): | |
| 201 | results.append({ | |
| 202 | 'id': media['id'], | |
| 203 | 'progress': entry['progress'], | |
| 204 | 'romaji_title': romaji_title, | |
| 205 | 'english_title': english_title, | |
| 206 | 'episodes': episodes, | |
| 207 | 'duration': duration, | |
| 208 | }) | |
| 209 | except: | |
| 210 | pass | |
| 211 | ||
| 212 | return results | |
| 213 | ||
| 214 | def update_anime_progress(token: str, media_id: int, progress: int): | |
| 215 | url = "https://graphql.anilist.co" | |
| 216 | ||
| 217 | query = ''' | |
| 218 | mutation($mediaId: Int, $progress: Int) { | |
| 219 | SaveMediaListEntry(mediaId: $mediaId, progress: $progress) { | |
| 220 | id | |
| 221 | progress | |
| 222 | } | |
| 223 | } | |
| 224 | ''' | |
| 225 | ||
| 226 | variables = { | |
| 227 | "mediaId": media_id, # The AniList ID of the anime | |
| 228 | "progress": progress # The number of the latest episode you watched | |
| 229 | } | |
| 230 | ||
| 231 | headers = { | |
| 232 | "Authorization": f"Bearer {token}", | |
| 233 | "Content-Type": "application/json", | |
| 234 | "Accept": "application/json" | |
| 235 | } | |
| 236 | ||
| 237 | response = requests.post(url, json={"query": query, "variables": variables}, headers=headers) | |
| 238 | ||
| 239 | if response.status_code == 200: | |
| 240 | data = response.json() | |
| 241 | updated_progress = data['data']['SaveMediaListEntry']['progress'] | |
| 242 | print("updating progress..") | |
| 243 | print(f"Anime progress updated! Latest watched episode: {updated_progress}") | |
| 244 | else: | |
| 245 | print(f"Error {response.status_code}: {response.text}") | |
| 246 | ||
| 247 | def rate_anime(anilist_token, media_id, score): | |
| 248 | url = 'https://graphql.anilist.co' | |
| 249 | ||
| 250 | # GraphQL mutation to rate anime | |
| 251 | query = ''' | |
| 252 | mutation($mediaId: Int, $score: Float) { | |
| 253 | SaveMediaListEntry(mediaId: $mediaId, score: $score) { | |
| 254 | id | |
| 255 | mediaId | |
| 256 | score | |
| 257 | } | |
| 258 | } | |
| 259 | ''' | |
| 260 | ||
| 261 | # Variables for the mutation | |
| 262 | variables = { | |
| 263 | "mediaId": media_id, | |
| 264 | "score": float(score) | |
| 265 | } | |
| 266 | ||
| 267 | headers = { | |
| 268 | "Authorization": f"Bearer {anilist_token}", | |
| 269 | "Content-Type": "application/json", | |
| 270 | } | |
| 271 | ||
| 272 | response = requests.post(url, json={'query': query, 'variables': variables}, headers=headers) | |
| 273 | ||
| 274 | if response.status_code == 200: | |
| 275 | data = response.json() | |
| 276 | print(f"Successfully rated anime (mediaId: {media_id}) with score: {score}") | |
| 277 | return data | |
| 278 | else: | |
| 279 | print(f"Failed to rate anime. Status Code: {response.status_code}, Response: {response.text}") | |
| 280 | return None | |
| 281 | ||
| 282 | # ----------------------------------------------- Select Anime Functions ---------------------------------------- | |
| 283 | ||
| 284 | def load_anime_data(filename): | |
| 285 | anime_list = {} | |
| 286 | with open(filename, "r") as file: | |
| 287 | data = file.read().strip() | |
| 288 | entries = data.split("NEWLINEFROMHERE") | |
| 289 | for entry in entries: | |
| 290 | if entry.strip(): | |
| 291 | parts = entry.split(maxsplit=1) | |
| 292 | if len(parts) == 2: | |
| 293 | anime_id, anime_name = parts | |
| 294 | anime_list[anime_name] = anime_id | |
| 295 | return anime_list | |
| 296 | ||
| 297 | def extract_anime_info(json_data): | |
| 298 | anime_info_list = [] | |
| 299 | anime_info_dict = {} | |
| 300 | ||
| 301 | # Traverse the JSON structure to get to the list of entries | |
| 302 | for list_entry in json_data['data']['MediaListCollection']['lists']: | |
| 303 | for entry in list_entry['entries']: | |
| 304 | anime = entry['media'] | |
| 305 | anime_info = { | |
| 306 | 'id': anime['id'], | |
| 307 | 'romaji_name': anime['title']['romaji'], | |
| 308 | 'english_name': anime['title']['english'], | |
| 309 | 'status': entry['status'], | |
| 310 | 'episodes': anime['episodes'], | |
| 311 | } | |
| 312 | ||
| 313 | if anime['title']['english'] == None: | |
| 314 | anime['title']['english'] = anime['title']['romaji'] | |
| 315 | # if anime['title'][''] == None: | |
| 316 | # anime['title']['english'] = anime['title']['romanji'] | |
| 317 | ||
| 318 | anime_info_dict[anime['title']['english']] = str(anime['id']) # {anime_name: anime_id, ...} | |
| 319 | anime_info_list.append(anime_info) # [{"id": anime_id, "romaji_name": anime_name_romnaji, "english_name": anime_name_english, status: anime_status}, ...] | |
| 320 | ||
| 321 | return [anime_info_dict, anime_info_list] | |
| 322 | ||
| 323 | def select_anime(anime_list): | |
| 324 | def main(stdscr): | |
| 325 | # Clear screen | |
| 326 | curses.curs_set(1) | |
| 327 | stdscr.clear() | |
| 328 | stdscr.refresh() | |
| 329 | ||
| 330 | # Initial values | |
| 331 | current_input = "" | |
| 332 | filtered_list = list(anime_list.keys()) | |
| 333 | selected_index = 0 | |
| 334 | scroll_position = 0 # Position to start displaying the list from | |
| 335 | ||
| 336 | try: | |
| 337 | while True: | |
| 338 | stdscr.clear() | |
| 339 | ||
| 340 | # Get terminal dimensions | |
| 341 | height, width = stdscr.getmaxyx() | |
| 342 | list_height = height - 2 # Leave space for input and status lines | |
| 343 | ||
| 344 | # Handle edge case where terminal window is too small | |
| 345 | if height < 5 or width < len("Search Anime: ") + len(current_input): | |
| 346 | stdscr.addstr(0, 0, "Terminal window too small!") | |
| 347 | stdscr.refresh() | |
| 348 | continue | |
| 349 | ||
| 350 | # Display search prompt | |
| 351 | stdscr.addstr(0, 0, "Search Anime: " + current_input) | |
| 352 | ||
| 353 | # Calculate scrolling parameters | |
| 354 | if selected_index < scroll_position: | |
| 355 | scroll_position = selected_index | |
| 356 | elif selected_index >= scroll_position + list_height: | |
| 357 | scroll_position = selected_index - list_height + 1 | |
| 358 | ||
| 359 | # Display filtered results with highlighting and scrolling | |
| 360 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): | |
| 361 | anime = filtered_list[idx] | |
| 362 | display_idx = idx - scroll_position + 1 | |
| 363 | if idx == selected_index: | |
| 364 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) | |
| 365 | else: | |
| 366 | stdscr.addstr(display_idx, 0, anime) | |
| 367 | ||
| 368 | # Get user input | |
| 369 | key = stdscr.getch() | |
| 370 | ||
| 371 | # Handle key inputs | |
| 372 | if key in [curses.KEY_BACKSPACE, 127, 8]: | |
| 373 | current_input = current_input[:-1] | |
| 374 | selected_index = 0 | |
| 375 | elif key == curses.KEY_DOWN: | |
| 376 | if selected_index < len(filtered_list) - 1: | |
| 377 | selected_index += 1 | |
| 378 | elif key == curses.KEY_UP: | |
| 379 | if selected_index > 0: | |
| 380 | selected_index -= 1 | |
| 381 | elif key == curses.KEY_ENTER or key in [10, 13]: # Enter key | |
| 382 | if filtered_list: | |
| 383 | selected_anime = filtered_list[selected_index] | |
| 384 | selected_id = anime_list[selected_anime] | |
| 385 | break | |
| 386 | elif key == ord('q'): # Exit if 'q' is pressed | |
| 387 | return | |
| 388 | else: | |
| 389 | try: | |
| 390 | current_input += chr(key) | |
| 391 | selected_index = 0 | |
| 392 | except: | |
| 393 | pass | |
| 394 | ||
| 395 | # Filter the anime list based on current input | |
| 396 | filtered_list = [anime for anime in anime_list.keys() if current_input.lower() in anime.lower()] | |
| 397 | ||
| 398 | # Ensure selected index is within bounds after filtering | |
| 399 | if selected_index >= len(filtered_list): | |
| 400 | selected_index = len(filtered_list) - 1 | |
| 401 | ||
| 402 | # Refresh screen | |
| 403 | stdscr.refresh() | |
| 404 | ||
| 405 | except Exception as e: | |
| 406 | # If an exception occurs, print it after exiting curses | |
| 407 | curses.endwin() | |
| 408 | print(f"An error occurred: {e}") | |
| 409 | finally: | |
| 410 | # Ensure curses always exits cleanly | |
| 411 | curses.endwin() | |
| 412 | ||
| 413 | # Print selected anime and its ID after exiting curses mode | |
| 414 | # print(f"Selected Anime: {selected_anime}, ID: {selected_id}") | |
| 415 | with open(f"/tmp/curd/curd_anime", "w") as anime_name: | |
| 416 | anime_name.write(selected_anime) | |
| 417 | ||
| 418 | with open(f"/tmp/curd/curd_id", "w") as id_file: | |
| 419 | id_file.write(str(selected_id)) | |
| 420 | ||
| 421 | return True | |
| 422 | ||
| 423 | # Initialize curses | |
| 424 | try: | |
| 425 | curses.wrapper(main) | |
| 426 | except: | |
| 427 | pass | |
| 428 | ||
| 429 | # ----------------------------------------------- Select Link Functions ---------------------------------------- | |
| 430 | ||
| 431 | ||
| 432 | def load_links(filename): | |
| 433 | links = [] | |
| 434 | with open(filename, 'r') as file: | |
| 435 | content = file.read() | |
| 436 | # Regex to match the name and URL pairs | |
| 437 | matches = re.findall(r'(\S+)\s*>\s*(https?://\S+)', content) | |
| 438 | links = [(name, url) for name, url in matches] | |
| 439 | return links | |
| 440 | ||
| 441 | def display_links(stdscr, links): | |
| 442 | curses.curs_set(0) # Hide the cursor | |
| 443 | stdscr.clear() | |
| 444 | h, w = stdscr.getmaxyx() | |
| 445 | max_height = h - 1 | |
| 446 | start = 0 | |
| 447 | selected_index = 0 | |
| 448 | ||
| 449 | while True: | |
| 450 | stdscr.clear() | |
| 451 | for i in range(start, min(start + max_height, len(links))): | |
| 452 | if i == selected_index: | |
| 453 | stdscr.attron(curses.A_REVERSE) # Highlight the selected item | |
| 454 | stdscr.addstr(i - start, 0, links[i][0]) | |
| 455 | if i == selected_index: | |
| 456 | stdscr.attroff(curses.A_REVERSE) # Remove highlight after the line | |
| 457 | stdscr.addstr(max_height, 0, "Use arrow keys to scroll, Enter to select, q to quit") | |
| 458 | stdscr.refresh() | |
| 459 | ||
| 460 | key = stdscr.getch() | |
| 461 | ||
| 462 | if key == curses.KEY_DOWN: | |
| 463 | if selected_index < len(links) - 1: | |
| 464 | selected_index += 1 | |
| 465 | if selected_index >= start + max_height: | |
| 466 | start += 1 | |
| 467 | elif key == curses.KEY_UP: | |
| 468 | if selected_index > 0: | |
| 469 | selected_index -= 1 | |
| 470 | if selected_index < start: | |
| 471 | start -= 1 | |
| 472 | elif key == curses.KEY_ENTER or key == 10: # Enter key | |
| 473 | stdscr.clear() | |
| 474 | stdscr.addstr(0, 0, f"Selected URL: {links[selected_index][1]}") | |
| 475 | # print("done") | |
| 476 | with open(f"/tmp/curd/curd_link", "w") as temp: | |
| 477 | temp.write(links[selected_index][1]) | |
| 478 | stdscr.refresh() | |
| 479 | # stdscr.getch() # Wait for any key press to exit | |
| 480 | return links[selected_index][1] | |
| 481 | break | |
| 482 | elif key == ord('q'): | |
| 483 | break | |
| 484 | ||
| 485 | # ----------------------------------------------- Start Video Functions ---------------------------------------- | |
| 486 | ||
| 487 | def start_video(link, salt:str, args:list=[]): | |
| 488 | # print(f"SALT IS {salt}") | |
| 489 | ||
| 490 | args_str = ' '.join(args) | |
| 491 | ||
| 492 | # Build the complete command string | |
| 493 | command = f"mpv {args_str} --no-terminal --really-quiet --input-ipc-server=/tmp/curd/curd_mpvsocket{salt} {link}" | |
| 494 | ||
| 495 | subprocess.Popen(command, shell=True) | |
| 496 | ||
| 497 | def send_command(ipc_socket_path, command): | |
| 498 | """ | |
| 499 | Sends a command to the MPV IPC socket and returns the response. | |
| 500 | """ | |
| 501 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: | |
| 502 | s.connect(ipc_socket_path) | |
| 503 | command_str = json.dumps({"command": command}) + "\n" | |
| 504 | s.sendall(command_str.encode()) | |
| 505 | response = s.recv(4096).decode() | |
| 506 | ||
| 507 | try: | |
| 508 | response_data = json.loads(response) | |
| 509 | if 'data' in response_data: | |
| 510 | return response_data['data'] | |
| 511 | except json.JSONDecodeError: | |
| 512 | return None | |
| 513 | return None | |
| 514 | ||
| 515 | def get_mpv_paused_status(ipc_socket_path): | |
| 516 | status = send_command(ipc_socket_path, ["get_property", "pause"]) | |
| 517 | if status is not None: | |
| 518 | return status | |
| 519 | else: | |
| 520 | return False | |
| 521 | ||
| 522 | def get_mpv_playback_speed(ipc_socket_path): | |
| 523 | current_speed = send_command(ipc_socket_path, ["get_property", "speed"]) | |
| 524 | if current_speed is not None: | |
| 525 | return current_speed | |
| 526 | else: | |
| 527 | print("Failed to get playback speed.") | |
| 528 | ||
| 529 | def get_percentage_watched(ipc_socket_path): | |
| 530 | """ | |
| 531 | Calculates the percentage watched of the currently playing video. | |
| 532 | """ | |
| 533 | # Get current playback time and total duration | |
| 534 | current_time = send_command(ipc_socket_path, ["get_property", "time-pos"]) | |
| 535 | duration = send_command(ipc_socket_path, ["get_property", "duration"]) | |
| 536 | ||
| 537 | if current_time is not None and duration is not None and duration > 0: | |
| 538 | percentage_watched = (current_time / duration) * 100 | |
| 539 | return percentage_watched | |
| 540 | return None | |
| 541 | ||
| 542 | def percentage_watched(playback_time:int, duration:int): | |
| 543 | if playback_time is not None and duration is not None and duration > 0: | |
| 544 | video_percentage_watched = (playback_time/duration) * 100 | |
| 545 | return video_percentage_watched | |
| 546 | return None | |
| 547 | ||
| 548 | # ----------------------------------------------- Track Anime Functions ---------------------------------------- | |
| 549 | def add_anime(database_file, anilist_id, allanime_id, episode, time, duration, name): | |
| 550 | with open(database_file, mode='a', newline='') as file: | |
| 551 | writer = csv.writer(file) | |
| 552 | writer.writerow([anilist_id, allanime_id, episode, time, duration, name]) | |
| 553 | print("Written to file") | |
| 554 | ||
| 555 | # Function to delete an anime entry by Anilist ID and Allanime ID | |
| 556 | def delete_anime(database_file, anilist_id, allanime_id): | |
| 557 | anime_list = [] | |
| 558 | with open(database_file, mode='r', newline='') as file: | |
| 559 | reader = csv.reader(file) | |
| 560 | for row in reader: | |
| 561 | # Keep only entries that don't match the Anilist ID and Allanime ID | |
| 562 | if row[0] != anilist_id or row[1] != allanime_id: | |
| 563 | anime_list.append(row) | |
| 564 | ||
| 565 | # Rewrite the database without the deleted entry | |
| 566 | with open(database_file, mode='w', newline='') as file: | |
| 567 | writer = csv.writer(file) | |
| 568 | writer.writerows(anime_list) | |
| 569 | ||
| 570 | # Function to retrieve all anime entries | |
| 571 | def get_all_anime(database_file): | |
| 572 | anime_list = [] | |
| 573 | with open(database_file, mode='r', newline='') as file: | |
| 574 | reader = csv.reader(file) | |
| 575 | for row in reader: | |
| 576 | anime_list.append({ | |
| 577 | 'anilist_id': row[0], | |
| 578 | 'allanime_id': row[1], | |
| 579 | 'episode': row[2], | |
| 580 | 'time': row[3], | |
| 581 | 'duration': row[4], | |
| 582 | 'name': row[5] | |
| 583 | }) | |
| 584 | return anime_list | |
| 585 | ||
| 586 | # Function to update or add a new anime entry | |
| 587 | def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:str, time:str, duration:str, name:str): | |
| 588 | anime_list = get_all_anime(database_file) | |
| 589 | updated = False | |
| 590 | ||
| 591 | # Create a new list to store updated entries | |
| 592 | updated_anime_list = [] | |
| 593 | ||
| 594 | # Check if the anime entry exists and update it | |
| 595 | for anime in anime_list: | |
| 596 | if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id: | |
| 597 | updated_anime_list.append({ | |
| 598 | 'anilist_id': anilist_id, | |
| 599 | 'allanime_id': allanime_id, | |
| 600 | 'episode': episode, | |
| 601 | 'time': time, | |
| 602 | 'duration': duration, | |
| 603 | 'name': name | |
| 604 | }) | |
| 605 | updated = True | |
| 606 | else: | |
| 607 | updated_anime_list.append(anime) | |
| 608 | ||
| 609 | # If no entry was updated, add new entry | |
| 610 | if not updated: | |
| 611 | updated_anime_list.append({ | |
| 612 | 'anilist_id': anilist_id, | |
| 613 | 'allanime_id': allanime_id, | |
| 614 | 'episode': episode, | |
| 615 | 'time': time, | |
| 616 | 'duration': duration, | |
| 617 | 'name': name | |
| 618 | }) | |
| 619 | ||
| 620 | # Write all entries back to the file | |
| 621 | with open(database_file, mode='w', newline='') as file: | |
| 622 | writer = csv.writer(file) | |
| 623 | for anime in updated_anime_list: | |
| 624 | writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) | |
| 625 | ||
| 626 | # print("Updated the file") | |
| 627 | ||
| 628 | def find_anime(anime_list, anilist_id=-1, allanime_id=-1): | |
| 629 | for anime in anime_list: | |
| 630 | if anime['anilist_id'] == anilist_id or anime['allanime_id'] == allanime_id: | |
| 631 | return anime | |
| 632 | ||
| 633 | return False | |
| 634 | ||
| 635 | # ----------------------------------------------- Curd Functions ---------------------------------------- | |
| 636 | ||
| 29 | 637 | def get_contents_of(tmp_file_name): |
| 30 | 638 | with open(f"/tmp/curd/curd_{tmp_file_name}", "r") as temp_file: |
| 31 | 639 | return temp_file.read() |
| @@ -59,16 +667,10 @@ def read_tmp(tmp_filename:str): | ||
| 59 | 667 | print("fuck") |
| 60 | 668 | return False |
| 61 | 669 | |
| 62 | # write_to_tmp("id", "this") | |
| 63 | # print(read_tmp("id")) | |
| 64 | # exit(0) | |
| 65 | ||
| 66 | 670 | def download_anilist_data(access_token, user_id): |
| 67 | 671 | ''' dowlnoad anilist user data''' |
| 68 | 672 | # print("downloading user data") |
| 69 | 673 | anilist_user_data = get_user_data(access_token, user_id) |
| 70 | with open("response.json", "w") as response: | |
| 71 | response.write(str(anilist_user_data)) | |
| 72 | 674 | try: |
| 73 | 675 | if anilist_user_data['data'] == None: |
| 74 | 676 | print("Cannot process user data.") |
| @@ -182,11 +784,8 @@ def load_config() -> dict: | ||
| 182 | 784 | |
| 183 | 785 | return config_dict |
| 184 | 786 | |
| 185 | # else: | |
| 186 | # with open(config_file_path, "r") as config_file: | |
| 187 | # return config_file.read() | |
| 787 | # ----------------------------------------------- Start of script ---------------------------------------- | |
| 188 | 788 | |
| 189 | # START OF SCRIPT | |
| 190 | 789 | current_dir = Path(__file__).parent |
| 191 | 790 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 192 | 791 | # print(current_dir) |
| @@ -238,7 +837,6 @@ if args.new: | ||
| 238 | 837 | |
| 239 | 838 | user_id, user_name = get_anilist_user_id(access_token) |
| 240 | 839 | mark_episode_as_completed_at = get_userconfig_value(user_config, "percentage_to_mark_complete") |
| 241 | print(mark_episode_as_completed_at) | |
| 242 | 840 | |
| 243 | 841 | anilist_user_data = download_anilist_data(access_token, user_id) |
| 244 | 842 | anime_dict = extract_anime_info(anilist_user_data)[0] |
select_anime.py deleted −150
| @@ -1,150 +0,0 @@ | ||
| 1 | import curses | |
| 2 | import os | |
| 3 | ||
| 4 | # Sample dictionary of anime names and their IDs | |
| 5 | current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 6 | ||
| 7 | def load_anime_data(filename): | |
| 8 | anime_list = {} | |
| 9 | with open(filename, "r") as file: | |
| 10 | data = file.read().strip() | |
| 11 | entries = data.split("NEWLINEFROMHERE") | |
| 12 | for entry in entries: | |
| 13 | if entry.strip(): | |
| 14 | parts = entry.split(maxsplit=1) | |
| 15 | if len(parts) == 2: | |
| 16 | anime_id, anime_name = parts | |
| 17 | anime_list[anime_name] = anime_id | |
| 18 | return anime_list | |
| 19 | ||
| 20 | def extract_anime_info(json_data): | |
| 21 | anime_info_list = [] | |
| 22 | anime_info_dict = {} | |
| 23 | ||
| 24 | # Traverse the JSON structure to get to the list of entries | |
| 25 | for list_entry in json_data['data']['MediaListCollection']['lists']: | |
| 26 | for entry in list_entry['entries']: | |
| 27 | anime = entry['media'] | |
| 28 | anime_info = { | |
| 29 | 'id': anime['id'], | |
| 30 | 'romaji_name': anime['title']['romaji'], | |
| 31 | 'english_name': anime['title']['english'], | |
| 32 | 'status': entry['status'], | |
| 33 | 'episodes': anime['episodes'], | |
| 34 | } | |
| 35 | ||
| 36 | if anime['title']['english'] == None: | |
| 37 | anime['title']['english'] = anime['title']['romaji'] | |
| 38 | # if anime['title'][''] == None: | |
| 39 | # anime['title']['english'] = anime['title']['romanji'] | |
| 40 | ||
| 41 | anime_info_dict[anime['title']['english']] = str(anime['id']) # {anime_name: anime_id, ...} | |
| 42 | anime_info_list.append(anime_info) # [{"id": anime_id, "romaji_name": anime_name_romnaji, "english_name": anime_name_english, status: anime_status}, ...] | |
| 43 | ||
| 44 | return [anime_info_dict, anime_info_list] | |
| 45 | ||
| 46 | def select_anime(anime_list): | |
| 47 | def main(stdscr): | |
| 48 | # Clear screen | |
| 49 | curses.curs_set(1) | |
| 50 | stdscr.clear() | |
| 51 | stdscr.refresh() | |
| 52 | ||
| 53 | # Initial values | |
| 54 | current_input = "" | |
| 55 | filtered_list = list(anime_list.keys()) | |
| 56 | selected_index = 0 | |
| 57 | scroll_position = 0 # Position to start displaying the list from | |
| 58 | ||
| 59 | try: | |
| 60 | while True: | |
| 61 | stdscr.clear() | |
| 62 | ||
| 63 | # Get terminal dimensions | |
| 64 | height, width = stdscr.getmaxyx() | |
| 65 | list_height = height - 2 # Leave space for input and status lines | |
| 66 | ||
| 67 | # Handle edge case where terminal window is too small | |
| 68 | if height < 5 or width < len("Search Anime: ") + len(current_input): | |
| 69 | stdscr.addstr(0, 0, "Terminal window too small!") | |
| 70 | stdscr.refresh() | |
| 71 | continue | |
| 72 | ||
| 73 | # Display search prompt | |
| 74 | stdscr.addstr(0, 0, "Search Anime: " + current_input) | |
| 75 | ||
| 76 | # Calculate scrolling parameters | |
| 77 | if selected_index < scroll_position: | |
| 78 | scroll_position = selected_index | |
| 79 | elif selected_index >= scroll_position + list_height: | |
| 80 | scroll_position = selected_index - list_height + 1 | |
| 81 | ||
| 82 | # Display filtered results with highlighting and scrolling | |
| 83 | for idx in range(scroll_position, min(len(filtered_list), scroll_position + list_height)): | |
| 84 | anime = filtered_list[idx] | |
| 85 | display_idx = idx - scroll_position + 1 | |
| 86 | if idx == selected_index: | |
| 87 | stdscr.addstr(display_idx, 0, anime, curses.A_REVERSE) | |
| 88 | else: | |
| 89 | stdscr.addstr(display_idx, 0, anime) | |
| 90 | ||
| 91 | # Get user input | |
| 92 | key = stdscr.getch() | |
| 93 | ||
| 94 | # Handle key inputs | |
| 95 | if key in [curses.KEY_BACKSPACE, 127, 8]: | |
| 96 | current_input = current_input[:-1] | |
| 97 | selected_index = 0 | |
| 98 | elif key == curses.KEY_DOWN: | |
| 99 | if selected_index < len(filtered_list) - 1: | |
| 100 | selected_index += 1 | |
| 101 | elif key == curses.KEY_UP: | |
| 102 | if selected_index > 0: | |
| 103 | selected_index -= 1 | |
| 104 | elif key == curses.KEY_ENTER or key in [10, 13]: # Enter key | |
| 105 | if filtered_list: | |
| 106 | selected_anime = filtered_list[selected_index] | |
| 107 | selected_id = anime_list[selected_anime] | |
| 108 | break | |
| 109 | elif key == ord('q'): # Exit if 'q' is pressed | |
| 110 | return | |
| 111 | else: | |
| 112 | try: | |
| 113 | current_input += chr(key) | |
| 114 | selected_index = 0 | |
| 115 | except: | |
| 116 | pass | |
| 117 | ||
| 118 | # Filter the anime list based on current input | |
| 119 | filtered_list = [anime for anime in anime_list.keys() if current_input.lower() in anime.lower()] | |
| 120 | ||
| 121 | # Ensure selected index is within bounds after filtering | |
| 122 | if selected_index >= len(filtered_list): | |
| 123 | selected_index = len(filtered_list) - 1 | |
| 124 | ||
| 125 | # Refresh screen | |
| 126 | stdscr.refresh() | |
| 127 | ||
| 128 | except Exception as e: | |
| 129 | # If an exception occurs, print it after exiting curses | |
| 130 | curses.endwin() | |
| 131 | print(f"An error occurred: {e}") | |
| 132 | finally: | |
| 133 | # Ensure curses always exits cleanly | |
| 134 | curses.endwin() | |
| 135 | ||
| 136 | # Print selected anime and its ID after exiting curses mode | |
| 137 | # print(f"Selected Anime: {selected_anime}, ID: {selected_id}") | |
| 138 | with open(f"/tmp/curd/curd_anime", "w") as anime_name: | |
| 139 | anime_name.write(selected_anime) | |
| 140 | ||
| 141 | with open(f"/tmp/curd/curd_id", "w") as id_file: | |
| 142 | id_file.write(str(selected_id)) | |
| 143 | ||
| 144 | return True | |
| 145 | ||
| 146 | # Initialize curses | |
| 147 | try: | |
| 148 | curses.wrapper(main) | |
| 149 | except: | |
| 150 | pass | |
select_link.py deleted −71
| @@ -1,71 +0,0 @@ | ||
| 1 | import curses | |
| 2 | import re | |
| 3 | import os | |
| 4 | ||
| 5 | current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 6 | ||
| 7 | def load_links(filename): | |
| 8 | links = [] | |
| 9 | with open(filename, 'r') as file: | |
| 10 | content = file.read() | |
| 11 | # Regex to match the name and URL pairs | |
| 12 | matches = re.findall(r'(\S+)\s*>\s*(https?://\S+)', content) | |
| 13 | links = [(name, url) for name, url in matches] | |
| 14 | return links | |
| 15 | ||
| 16 | def display_links(stdscr, links): | |
| 17 | curses.curs_set(0) # Hide the cursor | |
| 18 | stdscr.clear() | |
| 19 | h, w = stdscr.getmaxyx() | |
| 20 | max_height = h - 1 | |
| 21 | start = 0 | |
| 22 | selected_index = 0 | |
| 23 | ||
| 24 | while True: | |
| 25 | stdscr.clear() | |
| 26 | for i in range(start, min(start + max_height, len(links))): | |
| 27 | if i == selected_index: | |
| 28 | stdscr.attron(curses.A_REVERSE) # Highlight the selected item | |
| 29 | stdscr.addstr(i - start, 0, links[i][0]) | |
| 30 | if i == selected_index: | |
| 31 | stdscr.attroff(curses.A_REVERSE) # Remove highlight after the line | |
| 32 | stdscr.addstr(max_height, 0, "Use arrow keys to scroll, Enter to select, q to quit") | |
| 33 | stdscr.refresh() | |
| 34 | ||
| 35 | key = stdscr.getch() | |
| 36 | ||
| 37 | if key == curses.KEY_DOWN: | |
| 38 | if selected_index < len(links) - 1: | |
| 39 | selected_index += 1 | |
| 40 | if selected_index >= start + max_height: | |
| 41 | start += 1 | |
| 42 | elif key == curses.KEY_UP: | |
| 43 | if selected_index > 0: | |
| 44 | selected_index -= 1 | |
| 45 | if selected_index < start: | |
| 46 | start -= 1 | |
| 47 | elif key == curses.KEY_ENTER or key == 10: # Enter key | |
| 48 | stdscr.clear() | |
| 49 | stdscr.addstr(0, 0, f"Selected URL: {links[selected_index][1]}") | |
| 50 | # print("done") | |
| 51 | with open(f"/tmp/curd/curd_link", "w") as temp: | |
| 52 | temp.write(links[selected_index][1]) | |
| 53 | stdscr.refresh() | |
| 54 | # stdscr.getch() # Wait for any key press to exit | |
| 55 | return links[selected_index][1] | |
| 56 | break | |
| 57 | elif key == ord('q'): | |
| 58 | break | |
| 59 | ||
| 60 | def main(stdscr): | |
| 61 | filename = f"/tmp/curd/curd_links" # Replace with your file name | |
| 62 | links = load_links(filename) | |
| 63 | print("printed", display_links(stdscr, links)) | |
| 64 | print("done") | |
| 65 | ||
| 66 | # print(display_links(stdscr,links)) | |
| 67 | if __name__ == "__main__": | |
| 68 | try: | |
| 69 | curses.wrapper(main) | |
| 70 | except: | |
| 71 | pass | |
| \ No newline at end of file | ||
start_video.py deleted −74
| @@ -1,74 +0,0 @@ | ||
| 1 | import os | |
| 2 | import json | |
| 3 | import socket | |
| 4 | import subprocess | |
| 5 | ||
| 6 | def start_video(link, salt:str, args:list=[]): | |
| 7 | # print(f"SALT IS {salt}") | |
| 8 | ||
| 9 | args_str = ' '.join(args) | |
| 10 | ||
| 11 | # Build the complete command string | |
| 12 | command = f"mpv {args_str} --no-terminal --really-quiet --input-ipc-server=/tmp/curd/curd_mpvsocket{salt} {link}" | |
| 13 | ||
| 14 | subprocess.Popen(command, shell=True) | |
| 15 | # os.system() | |
| 16 | # Path to the MPV socket | |
| 17 | # socket_path = f'/tmp/curd/mpvsocket{salt}' | |
| 18 | # Connect to the MPV IPC socket | |
| 19 | # client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
| 20 | # client.connect(socket_path) | |
| 21 | ||
| 22 | def send_command(ipc_socket_path, command): | |
| 23 | """ | |
| 24 | Sends a command to the MPV IPC socket and returns the response. | |
| 25 | """ | |
| 26 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: | |
| 27 | s.connect(ipc_socket_path) | |
| 28 | command_str = json.dumps({"command": command}) + "\n" | |
| 29 | s.sendall(command_str.encode()) | |
| 30 | response = s.recv(4096).decode() | |
| 31 | ||
| 32 | try: | |
| 33 | response_data = json.loads(response) | |
| 34 | if 'data' in response_data: | |
| 35 | return response_data['data'] | |
| 36 | except json.JSONDecodeError: | |
| 37 | return None | |
| 38 | return None | |
| 39 | ||
| 40 | def get_mpv_paused_status(ipc_socket_path): | |
| 41 | status = send_command(ipc_socket_path, ["get_property", "pause"]) | |
| 42 | if status is not None: | |
| 43 | return status | |
| 44 | else: | |
| 45 | return False | |
| 46 | ||
| 47 | def get_mpv_playback_speed(ipc_socket_path): | |
| 48 | current_speed = send_command(ipc_socket_path, ["get_property", "speed"]) | |
| 49 | if current_speed is not None: | |
| 50 | return current_speed | |
| 51 | else: | |
| 52 | print("Failed to get playback speed.") | |
| 53 | ||
| 54 | ||
| 55 | def get_percentage_watched(ipc_socket_path): | |
| 56 | """ | |
| 57 | Calculates the percentage watched of the currently playing video. | |
| 58 | """ | |
| 59 | # Get current playback time and total duration | |
| 60 | current_time = send_command(ipc_socket_path, ["get_property", "time-pos"]) | |
| 61 | duration = send_command(ipc_socket_path, ["get_property", "duration"]) | |
| 62 | ||
| 63 | if current_time is not None and duration is not None and duration > 0: | |
| 64 | percentage_watched = (current_time / duration) * 100 | |
| 65 | return percentage_watched | |
| 66 | return None | |
| 67 | ||
| 68 | def percentage_watched(playback_time:int, duration:int): | |
| 69 | if playback_time is not None and duration is not None and duration > 0: | |
| 70 | video_percentage_watched = (playback_time/duration) * 100 | |
| 71 | return video_percentage_watched | |
| 72 | return None | |
| 73 | ||
| 74 | # start_video("https://video.wixstatic.com/video/36bbae_bef5207b465447f19c0f9366f3ee1c27/720p/mp4/file.mp4", "10", ['--start=100']) | |
| \ No newline at end of file | ||
track_anime.py deleted −106
| @@ -1,106 +0,0 @@ | ||
| 1 | import csv | |
| 2 | ||
| 3 | # Path to your text database file | |
| 4 | # database_file = 'curd_history.txt' | |
| 5 | ||
| 6 | # Function to add a new anime entry | |
| 7 | def add_anime(database_file, anilist_id, allanime_id, episode, time, duration, name): | |
| 8 | with open(database_file, mode='a', newline='') as file: | |
| 9 | writer = csv.writer(file) | |
| 10 | writer.writerow([anilist_id, allanime_id, episode, time, duration, name]) | |
| 11 | print("Written to file") | |
| 12 | ||
| 13 | # Function to delete an anime entry by Anilist ID and Allanime ID | |
| 14 | def delete_anime(database_file, anilist_id, allanime_id): | |
| 15 | anime_list = [] | |
| 16 | with open(database_file, mode='r', newline='') as file: | |
| 17 | reader = csv.reader(file) | |
| 18 | for row in reader: | |
| 19 | # Keep only entries that don't match the Anilist ID and Allanime ID | |
| 20 | if row[0] != anilist_id or row[1] != allanime_id: | |
| 21 | anime_list.append(row) | |
| 22 | ||
| 23 | # Rewrite the database without the deleted entry | |
| 24 | with open(database_file, mode='w', newline='') as file: | |
| 25 | writer = csv.writer(file) | |
| 26 | writer.writerows(anime_list) | |
| 27 | ||
| 28 | # Function to retrieve all anime entries | |
| 29 | def get_all_anime(database_file): | |
| 30 | anime_list = [] | |
| 31 | with open(database_file, mode='r', newline='') as file: | |
| 32 | reader = csv.reader(file) | |
| 33 | for row in reader: | |
| 34 | anime_list.append({ | |
| 35 | 'anilist_id': row[0], | |
| 36 | 'allanime_id': row[1], | |
| 37 | 'episode': row[2], | |
| 38 | 'time': row[3], | |
| 39 | 'duration': row[4], | |
| 40 | 'name': row[5] | |
| 41 | }) | |
| 42 | return anime_list | |
| 43 | ||
| 44 | # Function to update or add a new anime entry | |
| 45 | def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:str, time:str, duration:str, name:str): | |
| 46 | anime_list = get_all_anime(database_file) | |
| 47 | updated = False | |
| 48 | ||
| 49 | # Create a new list to store updated entries | |
| 50 | updated_anime_list = [] | |
| 51 | ||
| 52 | # Check if the anime entry exists and update it | |
| 53 | for anime in anime_list: | |
| 54 | if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id: | |
| 55 | updated_anime_list.append({ | |
| 56 | 'anilist_id': anilist_id, | |
| 57 | 'allanime_id': allanime_id, | |
| 58 | 'episode': episode, | |
| 59 | 'time': time, | |
| 60 | 'duration': duration, | |
| 61 | 'name': name | |
| 62 | }) | |
| 63 | updated = True | |
| 64 | else: | |
| 65 | updated_anime_list.append(anime) | |
| 66 | ||
| 67 | # If no entry was updated, add new entry | |
| 68 | if not updated: | |
| 69 | updated_anime_list.append({ | |
| 70 | 'anilist_id': anilist_id, | |
| 71 | 'allanime_id': allanime_id, | |
| 72 | 'episode': episode, | |
| 73 | 'time': time, | |
| 74 | 'duration': duration, | |
| 75 | 'name': name | |
| 76 | }) | |
| 77 | ||
| 78 | # Write all entries back to the file | |
| 79 | with open(database_file, mode='w', newline='') as file: | |
| 80 | writer = csv.writer(file) | |
| 81 | for anime in updated_anime_list: | |
| 82 | writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) | |
| 83 | ||
| 84 | # print("Updated the file") | |
| 85 | ||
| 86 | def find_anime(anime_list, anilist_id=-1, allanime_id=-1): | |
| 87 | for anime in anime_list: | |
| 88 | if anime['anilist_id'] == anilist_id or anime['allanime_id'] == allanime_id: | |
| 89 | return anime | |
| 90 | ||
| 91 | return False | |
| 92 | ||
| 93 | # Example usage | |
| 94 | # database_name = "/home/wraient/.local/share/curd/curd_history.txt" | |
| 95 | # add_anime(database_name, '54321', '09876', '3', '100', '24', 'My Hero Academia') | |
| 96 | ||
| 97 | # # Print all anime entries | |
| 98 | # update_anime(database_name, '21', 'ReooPAxPMsHM4KPMY', '1110', '10', 'ONE PIECE') | |
| 99 | # print(find_anime(get_all_anime(database_name), allanime_id="21")) | |
| 100 | # update_anime(database_name, '21', 'ReooPAxPMsHM4KPMY', '1110', '101', 'ONE PIECE') | |
| 101 | ||
| 102 | # # Update an entry | |
| 103 | # update_anime(database_name, '12345', '67890', '13', '120', 'Attack on Titan') | |
| 104 | ||
| 105 | # # Print all anime entries after update | |
| 106 | # print(get_all_anime(database_name)) | |