Commit 3ea063d037
Unsigned
Layout: unified · split
curd.py +68
| @@ -18,10 +18,78 @@ import argparse | ||
| 18 | 18 | import traceback |
| 19 | 19 | import requests |
| 20 | 20 | import csv |
| 21 | import math | |
| 21 | 22 | import socket |
| 23 | # pypresence is also imported later if user enabled it in the config | |
| 22 | 24 | |
| 23 | 25 | discord_client_id = "1287457464148820089" |
| 24 | 26 | |
| 27 | # ----------------------------------------------- AniSkip Functions ---------------------------------------- | |
| 28 | ||
| 29 | def get_aniskip_data(anime_id, episode): | |
| 30 | base_url = "https://api.aniskip.com/v1/skip-times" | |
| 31 | url = f"{base_url}/{anime_id}/{episode}?types=op&types=ed" | |
| 32 | ||
| 33 | try: | |
| 34 | response = requests.get(url) | |
| 35 | response.raise_for_status() # Raise an exception for bad status codes | |
| 36 | return response.text | |
| 37 | except requests.RequestException as e: | |
| 38 | print(f"Error fetching data from AniSkip API: {e}") | |
| 39 | return None | |
| 40 | ||
| 41 | def round_time(time_value, precision=0): | |
| 42 | """ | |
| 43 | Round a time value to a specified precision. | |
| 44 | precision=0 rounds to the nearest second. | |
| 45 | precision=1 rounds to the nearest tenth of a second, etc. | |
| 46 | """ | |
| 47 | multiplier = 10 ** precision | |
| 48 | return math.floor(time_value * multiplier + 0.5) / multiplier | |
| 49 | ||
| 50 | def parse_aniskip_response(response_text, time_precision=1): | |
| 51 | if response_text is None: | |
| 52 | return None | |
| 53 | ||
| 54 | data = json.loads(response_text) | |
| 55 | ||
| 56 | if not data['found']: | |
| 57 | return None | |
| 58 | ||
| 59 | skip_intervals = {} | |
| 60 | op = data['results'][0]['interval'] | |
| 61 | # print(op) | |
| 62 | skip_intervals['op'] = {'start_time': round_time(op['start_time'], time_precision), | |
| 63 | 'end_time': round_time(op['end_time'], time_precision), | |
| 64 | } | |
| 65 | ed = data['results'][-1]['interval'] | |
| 66 | skip_intervals['ed'] = {'start_time': round_time(ed['start_time'], time_precision), | |
| 67 | 'end_time': round_time(ed['end_time'], time_precision), | |
| 68 | } | |
| 69 | ||
| 70 | return skip_intervals | |
| 71 | ||
| 72 | def get_and_parse_aniskip_data(anime_id, episode, time_precision=1): | |
| 73 | response_text = get_aniskip_data(anime_id, episode) | |
| 74 | return parse_aniskip_response(response_text, time_precision) | |
| 75 | ||
| 76 | # Example usage | |
| 77 | # anime_id = 28223 | |
| 78 | # episode = 10 | |
| 79 | ||
| 80 | # skip_intervals = get_and_parse_aniskip_data(anime_id, episode) | |
| 81 | ||
| 82 | # print(skip_intervals) | |
| 83 | ||
| 84 | # exit(0) | |
| 85 | ||
| 86 | # if skip_intervals: | |
| 87 | # for interval in skip_intervals: | |
| 88 | # print(f"{interval['skip_type'].upper()} - Start: {interval['start_time']:.2f}, End: {interval['end_time']:.2f}") | |
| 89 | # else: | |
| 90 | # print("No skip intervals found.") | |
| 91 | ||
| 92 | ||
| 25 | 93 | # ----------------------------------------------- AniList Functions ---------------------------------------- |
| 26 | 94 | |
| 27 | 95 | def search_anime_anilist(query, token): |