Skip to content

Commit 78a394f

Browse files
authored
Merge pull request #9 from karl0ss/CreateCLITool
Create cli tool
2 parents 1b2738d + 2d1eda3 commit 78a394f

6 files changed

+182
-7
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ __pycache__/
55
build/
66
dist/
77
main.spec
8+
.vscode/launch.json
9+
app.log
10+
bookmarkList.json
11+
downloadHistory.json

GoGoDownloaderCLI.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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()

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ If you want to run from source, or are using Linux/Mac you can run directly from
5757

5858
## Usage
5959

60-
The anime name is separated by "-". You can either type it manually, or go to [gogoanime.film](https://gogoanime.film/) and search for the anime you want to download and copy the name from the URL.
60+
The anime name is separated by "-". You can either type it manually, or go to [gogoanime.gg](https://gogoanime.gg/) and search for the anime you want to download and copy the name from the URL.
6161

6262
### Examples
6363

6464
##### One word title
6565

66-
- https://gogoanime.film/category/bakemonogatari >> bakemonogatari
67-
- https://gogoanime.film/category/steinsgate >> steinsgate
66+
- https://gogoanime.gg/category/bakemonogatari >> bakemonogatari
67+
- https://gogoanime.gg/category/steinsgate >> steinsgate
6868

6969
##### Multiple word title
7070

71-
- https://gogoanime.film/category/shadows-house >> shadows-house
72-
- https://gogoanime.film/category/kono-subarashii-sekai-ni-shukufuku-wo- >> kono-subarashii-sekai-ni-shukufuku-wo-
71+
- https://gogoanime.gg/category/shadows-house >> shadows-house
72+
- https://gogoanime.gg/category/kono-subarashii-sekai-ni-shukufuku-wo- >> kono-subarashii-sekai-ni-shukufuku-wo-
73+
74+
75+
# GoGoDownloader CLI
76+
I have now also created the GoGoDownloader CLI, this tool can be used to run on a scheduled basis, it will login and get the latest episodes from your GoGoAnime bookmarks, and download the latest episode if it has not been downloaded yet.

backend.py

+36
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,42 @@ def file_downloader(self, file_list: dict, overwrite_downloads: bool = None):
213213
files = dl.download()
214214
return files
215215

216+
def get_show_from_bookmark(self):
217+
print(f"{IN}Loading shows from bookmarks")
218+
bookmarkList = []
219+
a = dict(auth=gogoanime.get_gogoanime_auth_cookie(self))
220+
resp = requests.get(
221+
f"https://gogoanime.{self.config['CurrentGoGoAnimeDomain']}/user/bookmark",
222+
cookies=a,
223+
)
224+
soup = BeautifulSoup(resp.text, "html.parser")
225+
table = soup.find("div", attrs={"class": "article_bookmark"})
226+
splitTableLines = table.text.split("Remove")
227+
for rows in splitTableLines:
228+
fullRow = " ".join(rows.split())
229+
if "Anime name" in fullRow:
230+
fullRow = fullRow.replace("Anime name Latest", "")
231+
splitRow = fullRow.split("Latest")
232+
elif fullRow == "Status":
233+
break
234+
else:
235+
fullRow = fullRow.replace("Status ", "")
236+
splitRow = fullRow.split("Latest")
237+
animeName = splitRow[0].strip().encode("ascii", "ignore").decode()
238+
animeName = re.sub("[^A-Za-z0-9 ]+", "", animeName)
239+
animeDownloadName = animeName.replace(" ", "-").lower()
240+
episodeNum = splitRow[-1].split()[-1]
241+
bookmarkList.append(
242+
{
243+
"showName": animeName,
244+
"latestEpisode": int(episodeNum),
245+
"downloadURL": f"https://gogoanime.{self.config['CurrentGoGoAnimeDomain']}/{animeDownloadName}-episode-{str(episodeNum)}",
246+
}
247+
)
248+
with open("bookmarkList.json", "w") as f:
249+
json.dump(bookmarkList, f)
250+
return bookmarkList
251+
216252

217253
@dataclass(init=True)
218254
class CustomMessage(Exception):

config.json.default

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"GoGoAnime_Password":"",
44
"MaxConcurrentDownloads": 4,
55
"CurrentGoGoAnimeDomain": "gg",
6-
"OverwriteDownloads": 0
6+
"OverwriteDownloads": 0,
7+
"CLIQuality":"720",
8+
"CLIDownloadLocation": "CLIOutput",
9+
"CleanUpFileName": false
710
}

version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.1
1+
3.2.0

0 commit comments

Comments
 (0)