Skip to content

Commit

Permalink
[Http] properly detect offline URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
GammaC0de committed Jan 22, 2024
1 parent bb8716b commit ed850e8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/pyload/core/network/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def http_download(
disposition=disposition,
)
name = self.dl.download(chunks, resume)
self.http.code = self.dl.code
self._size = self.dl.size

self.dl = None
Expand Down
6 changes: 4 additions & 2 deletions src/pyload/core/network/http/http_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def remove(self):
def get_count(self):
return len(self.chunks)

def get_chunk_name(self, index):
def get_chunk_filename(self, index):
return self.chunks[index][0]

def get_chunk_range(self, index):
Expand All @@ -122,6 +122,8 @@ def __init__(self, id, parent, range=None, resume=False):
self.arrived = 0
self.last_url = self.p.referer

self.code = 0 #: last http code, set by parent

self.aborted = False # indicates that the chunk aborted gracefully

self.c = pycurl.Curl()
Expand Down Expand Up @@ -178,7 +180,7 @@ def get_handle(self):
# request all bytes, since some servers in russia seems to have a defect
# arithmetic unit

fs_name = self.p.info.get_chunk_name(self.id)
fs_name = self.p.info.get_chunk_filename(self.id)
if self.resume:
self.fp = open(fs_name, mode="ab")
self.arrived = self.fp.tell()
Expand Down
18 changes: 12 additions & 6 deletions src/pyload/core/network/http/http_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def __init__(
self.disposition = disposition
#: all arguments

self.code = 0 #: last http code, would be set from the first chunk

self.abort = False
self.size = size
self.name_disposition = None #: will be parsed from content disposition
Expand Down Expand Up @@ -84,7 +86,7 @@ def percent(self):
return (self.arrived * 100) // self.size

def _copy_chunks(self):
init = self.info.get_chunk_name(0) #: initial chunk name
init = self.info.get_chunk_filename(0) #: initial chunk name

if self.info.get_count() > 1:
with open(init, mode="rb+") as fo: #: first chunk file
Expand Down Expand Up @@ -178,7 +180,7 @@ def _download(self, chunks, resume):
self.chunk_support = True

while True:
#: need to create chunks
#: do we need to create chunks?
if (
not chunks_created and self.chunk_support and self.size
): #: will be set later by first chunk
Expand Down Expand Up @@ -223,9 +225,10 @@ def _download(self, chunks, resume):
for c in ok_list:
chunk = self.find_chunk(c)
try: #: check if the header implies success, else add it to failed list
chunk.verify_header()
chunk.code = chunk.verify_header()
except BadHeader as exc:
self.log.debug(f"Chunk {chunk.id + 1} failed: {exc}")
chunk.code = exc.code
failed.append(chunk)
ex = exc
else:
Expand All @@ -242,15 +245,17 @@ def _download(self, chunks, resume):
continue

try: #: check if the header implies success, else add it to failed list
chunk.verify_header()
chunk.code = chunk.verify_header()
except BadHeader as exc:
self.log.debug(f"Chunk {chunk.id + 1} failed: {exc}")
chunk.code = exc.code
failed.append(chunk)
ex = exc
else:
self.log.debug(f"Chunk {chunk.id + 1} download finished")
chunks_done.add(c)
if not num_queued: #: no more infos to get

if num_queued == 0: #: no more infos to get

#: check if init is not finished, so we reset download connections
#: note that other chunks are closed and downloaded with init too
Expand All @@ -264,7 +269,7 @@ def _download(self, chunks, resume):
for chunk in to_clean:
self.close_chunk(chunk)
self.chunks.remove(chunk)
os.remove(self.info.get_chunk_name(chunk.id))
os.remove(self.info.get_chunk_filename(chunk.id))

#: let first chunk load the rest and update the info file
init.reset_range()
Expand Down Expand Up @@ -313,6 +318,7 @@ def _download(self, chunks, resume):

self._copy_chunks()

self.code = init.code
self.size = self.arrived #: set size to actual downloaded size

def update_progress(self):
Expand Down
11 changes: 7 additions & 4 deletions src/pyload/plugins/base/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class BaseDownloader(BaseHoster):
__name__ = "BaseDownloader"
__type__ = "downloader"
__version__ = "0.83"
__version__ = "0.84"
__status__ = "stable"

__pattern__ = r"^unmatchable$"
Expand Down Expand Up @@ -261,9 +261,12 @@ def _download(

else:
if self.req.code in (404, 410):
bad_file = os.path.join(os.path.dirname(filename), newname)
if self.remove(bad_file):
return ""
if newname:
bad_file = os.path.join(os.path.dirname(filename), newname)
else:
bad_file = filename
self.remove(bad_file)
return ""
else:
self.log_info(self._("File saved"))

Expand Down
5 changes: 4 additions & 1 deletion src/pyload/plugins/downloaders/Http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Http(BaseDownloader):
__name__ = "Http"
__type__ = "downloader"
__version__ = "0.14"
__version__ = "0.15"
__status__ = "testing"

__pattern__ = r"(?:jd|pys?)://.+"
Expand Down Expand Up @@ -80,6 +80,9 @@ def process(self, pyfile):
else:
raise

if self.req.code in (404, 410):
self.offline()

self.check_download()

def check_download(self):
Expand Down

0 comments on commit ed850e8

Please sign in to comment.