now using an actual temporary directory

This commit is contained in:
nihilazo 2018-12-26 14:33:25 +00:00
parent c4a4d652e1
commit 6b4f1c1d01
1 changed files with 7 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import requests
import tempfile
import shutil
import zipfile
import os
@ -27,12 +28,13 @@ def get_chapters(manga_id, language, group, chapter_range):
def save_page(page):
print("Downloading page " + str(page['index'] + 1))
response = requests.get(page['url'], stream=True)
filetitle = "tmp/"+ str(page['index']) + "." + page['url'].split('.')[-1:][0]
filetitle = os.path.join(page['dir'], str(page['index']) + "." + page['url'].split('.')[-1:][0])
with open(filetitle, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
def save_chapter(chapter_id):
pages_dir = tempfile.mkdtemp()
api_url = "https://mangadex.org/api/chapter/" + chapter_id
r = requests.get(api_url)
data = r.json()
@ -46,14 +48,13 @@ def save_chapter(chapter_id):
page_url = data['server'] + data['hash'] + "/" + data['page_array'][page]
pages.append({
"url": page_url,
"index": page
"index": page,
"dir": pages_dir
})
if not os.path.exists("tmp"):
os.makedirs("tmp")
with ThreadPoolExecutor() as pool:
pool.map(save_page,pages)
filename = "Ch. {} - {}".format(data['chapter'],data['title'])
shutil.make_archive(filename, "zip", "tmp")
shutil.make_archive(filename, "zip", pages_dir)
shutil.move(filename + ".zip", filename + ".cbz")
shutil.rmtree("tmp")
shutil.rmtree(pages_dir)