File size: 7,690 Bytes
8247f19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | # Patch-overlapping Python context after the fix
## `mangadex_downloader/cli/validator.py` — `function:_try_read` (lines 34-39)
```python
def _try_read(path):
if not os.path.exists(path):
return None
with open(path, 'r') as o:
return o.read()
```
## `mangadex_downloader/cli/validator.py` — `function:validate_url` (lines 41-49)
```python
def validate_url(url):
urls = []
for _url in url.splitlines():
if not _url:
continue
urls.append((_validate(_url), _url))
return urls
```
## `mangadex_downloader/cli/validator.py` — `function:validate` (lines 200-405)
```python
def validate(parser, args):
urls = args.URL
if (
not args.search and
not args.fetch_library_manga and
not args.fetch_library_list and
not args.fetch_library_follows_list and
not args.random and
not args.fetch_group
):
# Parsing file path
if args.file:
result = urls.split(':')
file = result[1:]
file_path = ""
err_file = False
try:
file_path += file.pop(0)
except IndexError:
err_file = True
if not file_path:
err_file = True
if err_file:
parser.error("Syntax error: file path argument is empty")
# Because ":" was removed during .split()
# add it again
for f in file:
file_path += ':' + f
# web URL location support for "file:{location}" syntax
if file_path.startswith('http://') or file_path.startswith('https://'):
r = Net.requests.get(file_path)
try:
r.raise_for_status()
except requests.HTTPError:
raise MangaDexException(f"Failed to connect '{file_path}', status code = {r.status_code}")
file_path = r.text
# Because this is specified syntax for batch downloading
# If file doesn't exist, raise error
elif not os.path.exists(file_path):
parser.error(f"File \"{file_path}\" is not exist")
else:
file_content = _try_read(urls)
file_path = file_content if file_content is not None else urls
try:
args.URL = validate_url(file_path)
except argparse.ArgumentTypeError as e:
parser.error(str(e))
return
kwargs = {
'parser': parser
}
limit_items = None
if args.random:
def iter_random_manga():
ids = []
while True:
_, raw_cr = get_key_value(urls, sep=":")
content_ratings = split_comma_separated(raw_cr, single_value_to_list=True)
if not content_ratings[0]:
# Fallback to default values
content_ratings = [i.value for i in ContentRating]
else:
# Verify it
try:
content_ratings = [ContentRating(i).value for i in content_ratings]
except ValueError as e:
raise MangaDexException(e)
params = {
'includes[]': ['author', 'artist', 'cover_art'],
"contentRating[]": content_ratings
}
r = Net.mangadex.get(f'{base_url}/manga/random', params=params)
data = r.json()['data']
manga = Manga(data=data)
if manga.id not in ids:
# Make sure it's not duplicated manga
ids.append(manga.id)
yield manga
continue
iterator = iter_random_manga()
text = f"Found random manga"
on_empty_err = f"Unknown Error" # This should never happened
on_preview = preview_cover_manga
limit_items = 5
elif args.fetch_group:
# Getting group id
_, group_id = get_key_value(urls, sep=':')
if not group_id:
parser.error("group id or url are required")
group_id = __validate(group_id)
group = Group(group_id)
iterator = search(None, args.unsafe, group=group.id)
text = f'List manga from group "{group.name}"'
on_empty_err = f'Group "{group.name}" has no uploaded mangas'
on_preview = preview_cover_manga
elif args.search:
filter_kwargs = {}
filters = args.search_filter or []
for f in filters:
key, value = get_key_value(f)
try:
value_filter_kwargs = filter_kwargs[key]
except KeyError:
filter_kwargs[key] = split_comma_separated(value)
else:
# Found duplicate filter with different value
if isinstance(value_filter_kwargs, str):
new_values = [value_filter_kwargs]
else:
new_values = value_filter_kwargs
values = split_comma_separated(value, single_value_to_list=True)
new_values.extend(values)
filter_kwargs[key] = new_values
iterator = search(urls, args.unsafe, **filter_kwargs)
text = f"Search results for \"{urls}\""
on_empty_err = f"Search results \"{urls}\" are empty"
on_preview = preview_cover_manga
elif args.fetch_library_manga:
result = urls.split(':')
# Try to get filter status
try:
status = result[1]
except IndexError:
status = None
try:
iterator = get_manga_from_user_library(status, args.unsafe)
except MangaDexException as e:
parser.error(str(e))
user = Net.mangadex.user
text = f"Manga library from user \"{user.name}\""
on_empty_err = f"User \"{user.name}\" has no saved mangas"
on_preview = preview_cover_manga
elif args.fetch_library_list:
# Try to get user (if available)
_, user = get_key_value(urls, sep=':')
user_id = None
if user:
try:
user_id = __validate(user)
except InvalidURL as e:
parser.error(f"\"{user}\" is not a valid user")
try:
if user:
iterator = get_list_from_user(user_id)
else:
iterator = get_list_from_user_library()
except MangaDexException as e:
parser.error(str(e))
try:
user = iterator.user
except AttributeError:
# Logged in user
user = Net.mangadex.user
text = f"MangaDex List library from user \"{user.name}\""
on_empty_err = f"User \"{user.name}\" has no saved lists"
on_preview = lambda x: preview_list(args, x)
elif args.fetch_library_follows_list:
try:
iterator = get_followed_list_from_user_library()
except MangaDexException as e:
parser.error(str(e))
user = Net.mangadex.user
text = f"MangaDex followed List from user \"{user.name}\""
on_empty_err = f"User \"{user.name}\" has no followed lists"
on_preview = lambda x: preview_list(args, x)
kwargs.update({
'iterator': iterator,
'text': text,
'on_empty_err': on_empty_err,
'on_preview': on_preview
})
if limit_items:
kwargs.update(limit=limit_items)
result = _create_prompt_choices(**kwargs)
args.URL = validate_url(result.id)
```
|