Skip to content

Commit 761120e

Browse files
committed
more linting updates
1 parent 3f9eb36 commit 761120e

File tree

20 files changed

+292
-234
lines changed

20 files changed

+292
-234
lines changed

core/download.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import asyncio
22
from typing import List
3-
import cfscrape
43
import os
54
import re
6-
import requests
75
from concurrent.futures import ThreadPoolExecutor
6+
import requests
7+
import cfscrape
88

99
from models import Chapter, Extension
1010

@@ -25,7 +25,7 @@ def download_chapters(ext_active: Extension, valid_chapters: List[Chapter]):
2525
for chapter in valid_chapters:
2626
# runs the pre_download for the extension if needed
2727
# most likely used to retrieve page_urls for the chapter
28-
if chapter.pre_download == True:
28+
if chapter.pre_download is True:
2929
chapter = ext_active.pre_download(chapter)
3030

3131
loop = asyncio.get_event_loop()
@@ -52,7 +52,7 @@ async def download_chapter_async(chapter: Chapter):
5252
loop = asyncio.get_event_loop()
5353
tasks = []
5454

55-
for i in range(len(chapter.page_urls)):
55+
for i, page_url in enumerate(chapter.page_urls):
5656
dl_print = f"{chapter.manga_title}: Chapter {chapter.number} page {i} download complete"
5757

5858
if not os.path.exists(chapter_path):
@@ -63,7 +63,7 @@ async def download_chapter_async(chapter: Chapter):
6363
executor,
6464
download_page,
6565
*(
66-
chapter.page_urls[i],
66+
page_url,
6767
chapter_path,
6868
i + 1,
6969
dl_print,
@@ -81,7 +81,7 @@ def download_page(
8181
page_num: int,
8282
dl_print: str = "",
8383
cloudflare: bool = False,
84-
headers: dict = {},
84+
headers: dict = None,
8585
):
8686
"""Downloads manga image from URL
8787
@@ -90,14 +90,18 @@ def download_page(
9090
chapter_path (str): Directory path to save chapter
9191
page_num (int): Page number of image
9292
dl_print (str): String to print upon download completion
93-
cloudflare (bool, optional): Boolean flag to indicate whether cloudflare bypass is required. Defaults to False.
93+
cloudflare (bool, optional): Boolean flag to indicate whether cloudflare bypass is required.
94+
Defaults to False.
9495
headers (dict, optional): Dict of http headers for cloudflare bypass. Defaults to {}.
9596
"""
9697

9798
# if somehow folder hasn't been made yet
9899
if not os.path.exists(chapter_path):
99100
os.makedirs(chapter_path)
100101

102+
# in case url has spaces
103+
url = re.sub(r"\s", "", url)
104+
101105
# Use either cloudflare bypass or regular http requests depending on extension requirements
102106
if cloudflare:
103107
res = cf_scraper.get(url, headers=headers)

core/manga_info.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def get_manga_info(ext_active: Extension, manga: Manga) -> List[Chapter]:
2020
# if no available chapters, exit
2121
if len(manga_info.chapters) == 0:
2222
print("There are no chapters for this manga in english")
23-
return
23+
return None
2424

2525
# if only 1 available chapter, no need to process
2626
if len(manga_info.chapters) == 1:
@@ -31,7 +31,8 @@ def get_manga_info(ext_active: Extension, manga: Manga) -> List[Chapter]:
3131

3232
for chapter in manga_info.chapters:
3333
scanlator = f"[{chapter.scanlator}] " if chapter.scanlator else ""
34-
foldername = f"{scanlator}Ch.{chapter.number}{'' if chapter.title == '' else ' - '}{chapter.title}"
34+
foldername = f"{scanlator}Ch.{chapter.number}{'' if chapter.title == '' else ' - '}\
35+
{chapter.title}"
3536

3637
chapter_float = round(float(chapter.number), 3)
3738
chapter.foldername = foldername
@@ -49,12 +50,10 @@ def get_manga_info(ext_active: Extension, manga: Manga) -> List[Chapter]:
4950
to_download = input(query_str).lower().strip() or "q"
5051

5152
if to_download == "q":
52-
return
53+
return None
5354

5455
# keeps asking for chapters until a valid input is given
55-
if is_valid_download_range(to_download):
56-
break
57-
else:
56+
if not is_valid_download_range(to_download):
5857
print("Error with input, please try again.")
5958

6059
to_download = parse_to_download(to_download, valid_chapters)
@@ -73,7 +72,8 @@ def parse_to_download(to_download: str, valid_chapters: List[Chapter]) -> List[C
7372
valid_chapters (List[Chapter]): List of models.Chapter objects
7473
7574
Returns:
76-
List[Chapter]: List of models.Chapter objects with chapter numbers in range of 'to_download' parameter
75+
List[Chapter]: List of models.Chapter objects with chapter numbers
76+
in range of 'to_download' parameter
7777
"""
7878

7979
new_to_download = []

core/misc.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def check_pickle(ext: str):
1414

1515
# creates pickle file if it doesn't exist and stores an empty dict in it
1616
if not os.path.exists(DATA_FILE):
17-
f = open(DATA_FILE, "wb")
18-
pickle.dump({}, f)
19-
f.close()
17+
with open(DATA_FILE, "wb") as f:
18+
pickle.dump({}, f)
19+
f.close()
2020

2121
with open(DATA_FILE, "rb") as f:
2222
data = pickle.load(f)
@@ -52,7 +52,7 @@ def write_pickle(ext: str, key: str, value):
5252
f.close()
5353

5454

55-
def read_pickle(ext: str, key: str):
55+
def read_pickle(ext: str, key: str) -> str:
5656
"""Reads pickle data for extension
5757
5858
Args:
@@ -72,8 +72,7 @@ def read_pickle(ext: str, key: str):
7272
if key in data[ext]:
7373
return data[ext][key]
7474

75-
else:
76-
return None
75+
return None
7776

7877

7978
def delete_pickle(ext: str, key: str = "") -> bool:
@@ -100,7 +99,7 @@ def delete_pickle(ext: str, key: str = "") -> bool:
10099
else:
101100
del data[ext][key]
102101

103-
except:
102+
except KeyError:
104103
return False
105104

106105
with open(DATA_FILE, "wb") as f:
@@ -130,7 +129,7 @@ def is_url(url: str) -> bool:
130129
re.IGNORECASE,
131130
)
132131

133-
return not re.match(regex, url) == None
132+
return re.match(regex, url) is not None
134133

135134

136135
def is_valid_download_range(to_download: str) -> bool:
@@ -143,12 +142,12 @@ def is_valid_download_range(to_download: str) -> bool:
143142
bool: _description_
144143
"""
145144
res = True
146-
pattern = re.compile("^\d+(\.?\d+)?$")
145+
pattern = re.compile(r"^\d+(\.?\d+)?$")
147146
dl_spl = to_download.split(",")
148147
num_list = []
149148

150-
for i in range(len(dl_spl)):
151-
num_list += dl_spl[i].split("-")
149+
for i, spl in enumerate(dl_spl):
150+
num_list += spl[i].split("-")
152151

153152
for number in num_list:
154153
if not bool(re.search(pattern, number.strip())):

core/random_manga.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def random_manga(ext_active: Extension) -> Manga:
2020
dl = input(query) or "Y"
2121

2222
if dl.upper() == "N":
23-
return
23+
return None
2424

25-
elif dl.upper() == "Y":
25+
if dl.upper() == "Y":
2626
break
2727

2828
return manga

core/search.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def search(ext_active: Extension, query: str) -> Manga:
1717
search_page = 1
1818
page_index_in = -1
1919

20-
while manga == None:
20+
while manga is None:
2121
# first retrieves list of manga from search query
2222
search_res = ext_active.search(query, search_page)
2323

2424
if len(search_res.manga_list) == 0:
2525
print("There are 0 results for your search query. Exiting...")
26-
return
26+
return None
2727

2828
print(f"Search results for '{query}' page {search_page}:")
2929

@@ -35,11 +35,12 @@ def search(ext_active: Extension, query: str) -> Manga:
3535
# keep asking until a manga selection is made, flagged by reinput
3636
while reinput:
3737
max_num = len(search_res.manga_list)
38-
query_str = f"Which manga do you wish to download (1-{max_num}, < or > to move search page, q to quit): "
38+
query_str = f"Which manga do you wish to download (1-{max_num}, \
39+
< or > to move search page, q to quit): "
3940
page_index_in = (input(query_str) or "q").strip()
4041

4142
if page_index_in == "q":
42-
return
43+
return None
4344

4445
print("")
4546

@@ -52,15 +53,14 @@ def search(ext_active: Extension, query: str) -> Manga:
5253
continue
5354

5455
# incrementing page when last page is reached
55-
elif search_res.last_page and page_index_in == ">":
56+
if search_res.last_page and page_index_in == ">":
5657
page_index_in = -1
5758
print("You can't go to a next page")
5859
continue
5960

6061
# increment/decrement as per usual
61-
else:
62-
search_page += -1 if page_index_in == "<" else 1
63-
reinput = False
62+
search_page += -1 if page_index_in == "<" else 1
63+
reinput = False
6464

6565
page_index_in = -1
6666

@@ -78,7 +78,7 @@ def search(ext_active: Extension, query: str) -> Manga:
7878
print("Invalid input")
7979

8080
# only choose manga if valid index is input via user
81-
if page_index_in >= 0 and page_index_in < len(search_res.manga_list):
81+
if 0 <= page_index_in < len(search_res.manga_list):
8282
manga = search_res.manga_list[page_index_in]
8383
print(f"You chose: '{manga.title}'")
8484

extensions/hitomi/ext.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
import re
2+
3+
24
from typing import List
5+
6+
37
import requests
48

9+
510
# local files
11+
12+
613
from models import Chapter, Extension, Manga, Tag, SearchResult, ParseResult
714
import core
815

16+
917
import extensions.hitomi.parse as parse
1018

19+
1120
NAME = "hitomi"
1221

1322

1423
class Hitomi(Extension):
1524
session = requests.Session()
25+
1626
always_webp = True
1727

1828
def __init__(self):
1929
super().__init__()
30+
2031
res = self.session.get("https://ltn.hitomi.la/gg.js")
2132
res.close()
33+
2234
self.gg = res.text
2335

24-
def parse_url(self, query: str) -> dict:
36+
def parse_url(self, url: str) -> dict:
2537
pattern = r"https:\/\/hitomi.la\/(gamecg|cg|manga|doujinshi){1}\/"
26-
matches = re.search(pattern, query)
38+
39+
matches = re.search(pattern, url)
2740

2841
if matches == None:
2942
return None
3043

31-
chapter = parse.parse_gallery(self, query)
32-
return ParseResult(ParseResult._CHAPTER, chapter)
44+
chapter = parse.parse_gallery(self, url)
45+
46+
return ParseResult(ParseResult.CHAPTER, chapter)
3347

3448
def search(self, query: str, page: int, cover: bool = False) -> SearchResult:
3549
return SearchResult(None, None)

extensions/mangadex/account.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from datetime import datetime
22
import json
33
import requests
4-
5-
import core
4+
from core.misc import read_pickle, write_pickle
65

76
API_URL = "https://api.mangadex.org"
87

@@ -43,7 +42,7 @@ def login(
4342
if data["result"] == "ok":
4443
update_login_session(session, data)
4544
print("Successfully logged in as", username)
46-
mark_on_dl_store = False
45+
mark_on_dl_store = mark_on_dl
4746

4847
if mark_on_dl == "":
4948
mark_on_dl = input(
@@ -58,7 +57,7 @@ def login(
5857
print("Invalid input, defaulting to no")
5958
mark_on_dl_store = False
6059

61-
core.write_pickle("mangadex", "mark_on_dl", str(mark_on_dl_store))
60+
write_pickle("mangadex", "mark_on_dl", str(mark_on_dl_store))
6261

6362

6463
def check_login_session(session: requests.Session):
@@ -101,14 +100,14 @@ def update_login_session(session: requests.Session, data: dict):
101100
session.headers.update({"Authorization": f"Bearer {data['token']['session']}"})
102101

103102
# saving current session into a pickle
104-
core.write_pickle("mangadex", "session", session)
103+
write_pickle("mangadex", "session", session)
105104

106105

107106
def toggle_data_saver():
108107
"""Toggles data setting for MangaDex"""
109-
data_saver = core.read_pickle("mangadex", "data_saver")
108+
data_saver = read_pickle("mangadex", "data_saver")
110109
data_saver = not data_saver
111-
core.write_pickle("mangadex", "data_saver", data_saver)
110+
write_pickle("mangadex", "data_saver", data_saver)
112111

113112
print(f"Data saver set to: {data_saver}")
114113

@@ -120,7 +119,7 @@ def set_language(language: str):
120119
language (str): Language code to set to
121120
"""
122121

123-
core.write_pickle("mangadex", "language", language)
122+
write_pickle("mangadex", "language", language)
124123
print(f"Language set to: {language}")
125124

126125

0 commit comments

Comments
 (0)