|
| 1 | +import json |
| 2 | +import io |
| 3 | +import os |
| 4 | +import re |
| 5 | +from backend import * |
| 6 | + |
| 7 | + |
| 8 | +def renameFile(filename: str): |
| 9 | + """_summary_ |
| 10 | +
|
| 11 | + Args: |
| 12 | + filename (str): _description_ |
| 13 | +
|
| 14 | + Returns: |
| 15 | + _type_: _description_ |
| 16 | + """ |
| 17 | + newFileName = "".join(re.split("\(|\)|\[|\]", filename)[::2]) |
| 18 | + try: |
| 19 | + os.rename(filename, newFileName) |
| 20 | + return True |
| 21 | + except OSError as err: |
| 22 | + return err |
| 23 | + |
| 24 | + |
| 25 | +def loadDownloadHistory(): |
| 26 | + """Loads the downloadHistory.json, creates it if it doesn't exist |
| 27 | +
|
| 28 | + Returns: |
| 29 | + object: download history list |
| 30 | + """ |
| 31 | + if os.path.isfile("./downloadHistory.json") and os.access( |
| 32 | + "./downloadHistory.json", os.R_OK |
| 33 | + ): |
| 34 | + return json.load(open("./downloadHistory.json")) |
| 35 | + else: |
| 36 | + with io.open(os.path.join("./", "downloadHistory.json"), "w") as db_file: |
| 37 | + db_file.write(json.dumps([])) |
| 38 | + return json.load(open("./downloadHistory.json")) |
| 39 | + |
| 40 | + |
| 41 | +def writeShowToDownloadHistory(showName: str, downloadHistory: list): |
| 42 | + """Writes the showName and latestEpisode to the downloadHistory.json file |
| 43 | +
|
| 44 | + Args: |
| 45 | + showName (str): _description_ |
| 46 | + downloadHistory (list): _description_ |
| 47 | +
|
| 48 | + Returns: |
| 49 | + _type_: _description_ |
| 50 | + """ |
| 51 | + downloadHistory.append(showName) |
| 52 | + with io.open(os.path.join("./", "downloadHistory.json"), "w") as db_file: |
| 53 | + db_file.write(json.dumps(downloadHistory)) |
| 54 | + return json.load(open("./downloadHistory.json")) |
| 55 | + |
| 56 | + |
| 57 | +def readDownloadHistory(fileNameObject: object, downloadHistory: list): |
| 58 | + """Reads the downloadHistory.json and checks if the fileName is present |
| 59 | +
|
| 60 | + Args: |
| 61 | + fileNameObject (str): _description_ |
| 62 | + downloadHistory (list): _description_ |
| 63 | +
|
| 64 | + Returns: |
| 65 | + _type_: _description_ |
| 66 | + """ |
| 67 | + dhFileName = ( |
| 68 | + fileNameObject["showName"] + " - " + str(fileNameObject["latestEpisode"]) |
| 69 | + ) |
| 70 | + if dhFileName not in downloadHistory: |
| 71 | + writeShowToDownloadHistory(dhFileName, downloadHistory) |
| 72 | + return False |
| 73 | + else: |
| 74 | + return True |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + dh = loadDownloadHistory() |
| 79 | + config = config_check() |
| 80 | + downloader = gogoanime( |
| 81 | + config, |
| 82 | + 1, |
| 83 | + config["CLIQuality"], |
| 84 | + "a", |
| 85 | + 1, |
| 86 | + 1, |
| 87 | + 1, |
| 88 | + config["CLIDownloadLocation"], |
| 89 | + ) |
| 90 | + list = downloader.get_show_from_bookmark() |
| 91 | + dl_links = {} |
| 92 | + for ep in list: |
| 93 | + if readDownloadHistory(ep, dh): |
| 94 | + showName = ep["showName"] + " - " + str(ep["latestEpisode"]) |
| 95 | + print(f"{IN}{showName} already downloaded") |
| 96 | + else: |
| 97 | + print( |
| 98 | + f"{IN}Scraping DL for " |
| 99 | + + ep["showName"] |
| 100 | + + " Ep " |
| 101 | + + str(ep["latestEpisode"]) |
| 102 | + ) |
| 103 | + dl_links[downloader.get_download_link(ep["downloadURL"])] = ( |
| 104 | + ep["showName"], |
| 105 | + ep["latestEpisode"], |
| 106 | + ) |
| 107 | + result = downloader.file_downloader(dl_links) |
| 108 | + if config["CleanUpFileName"]: |
| 109 | + for file in result.data: |
| 110 | + renameFile(file) |
| 111 | + if len(result.errors) > 0: |
| 112 | + while len(result.errors) > 0: |
| 113 | + print(f"{ERR}{len(result.errors)} links failed retrying.") |
| 114 | + print(f"{IN}Re-Scraping Links") |
| 115 | + dl_links.clear() |
| 116 | + for ep in list: |
| 117 | + dl_links[downloader.get_download_link(ep["downloadURL"])] = ( |
| 118 | + ep["showName"], |
| 119 | + ep["latestEpisode"], |
| 120 | + ) |
| 121 | + result = downloader.file_downloader(dl_links, overwrite_downloads=0) |
| 122 | + if config["CleanUpFileName"]: |
| 123 | + for file in result.data: |
| 124 | + renameFile(file) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments