Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit de0402e

Browse files
committed
Added download location option
1 parent 6422233 commit de0402e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

comic-scrapper.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,29 @@ def readcomics_extract_chapters(url):
3535
return chapters
3636

3737

38-
def readcomics_download_chapter(url, chapter_num):
38+
def readcomics_download_chapter(url, chapter_num, download_location):
3939
chapter_name = 'chapter-' + str(chapter_num)
40+
chapter_location = os.path.join(download_location, chapter_name)
4041
r = requests.get(url)
4142
soup = bsoup.BeautifulSoup(r.text, 'html.parser')
4243
images = [image.get('src') for image in soup.find_all(
4344
'img', attrs={'class': "chapter_img"})]
4445
filenames = [
45-
os.path.join(chapter_name, '%0.3d.jpg' % (i))
46+
os.path.join(chapter_location, '%0.3d.jpg' % (i))
4647
for i in range(len(images))]
4748
urls = zip(images, filenames)
4849
# Create chapter folder
49-
if not os.path.exists(chapter_name):
50-
os.makedirs(chapter_name)
50+
if not os.path.exists(chapter_location):
51+
os.makedirs(chapter_location)
5152
# Start downloading the urls
5253
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
5354
for image, filename in urls:
5455
executor.submit(download_image, image, filename)
5556
# Convert the folder to a comic book zip filename
5657
subprocess.check_output(
57-
['zip', '-r', chapter_name + '.cbz', chapter_name],
58+
['zip', '-r', chapter_location + '.cbz', chapter_location],
5859
stderr=subprocess.STDOUT)
59-
shutil.rmtree(chapter_name)
60+
shutil.rmtree(chapter_location)
6061
print(chapter_name + ': Downloaded')
6162

6263

@@ -73,17 +74,23 @@ def main():
7374

7475
parser.add_argument('urls', metavar='url', nargs='+',
7576
help='Comic urls to download')
77+
parser.add_argument(
78+
"-l", "--location", default=os.getcwd(), help="set download location")
7679

7780
args = parser.parse_args()
7881

7982
for url in args.urls:
80-
print('Downloading comic:' + url.split('/')[-1])
83+
comic = url.split('/')[-1]
84+
print('Downloading comic: ' + comic)
8185
if 'readcomics.tv' in url:
8286
chapters = readcomics_extract_chapters(url)
8387

8488
if 'readcomics.tv' in url:
8589
for k in chapters:
86-
readcomics_download_chapter(chapters[k], k)
90+
download_location = os.path.join(args.location, comic)
91+
if not os.path.exists(download_location):
92+
os.makedirs(download_location)
93+
readcomics_download_chapter(chapters[k], k, download_location)
8794

8895
print('Downloaded comic:' + url.split('/')[-1])
8996

0 commit comments

Comments
 (0)