forked from techwizrd/MangaFox-Download-Script
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfdl.py
executable file
·175 lines (152 loc) · 6.83 KB
/
mfdl.py
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
#!/usr/bin/env python
# encoding: utf-8
"""Mangafox Download Script by Kunal Sarkhel <[email protected]>"""
import sys
import os
import urllib
import glob
import shutil
import re
from zipfile import ZipFile
try:
from bs4 import BeautifulSoup
except ImportError:
from BeautifulSoup import BeautifulSoup
from contextlib import closing
from collections import OrderedDict
from itertools import islice
URL_BASE = "http://mangafox.me/"
def get_page_soup(url):
"""Download a page and return a BeautifulSoup object of the html"""
with closing(urllib.urlopen(url)) as html_file:
return BeautifulSoup(html_file.read())
def get_chapter_urls(manga_name):
"""Get the chapter list for a manga"""
replace = lambda s, k: s.replace(k, '_')
manga_url = reduce(replace, [' ', '-'], manga_name.lower())
url = '{0}manga/{1}'.format(URL_BASE, manga_url)
print('Url: ' + url)
soup = get_page_soup(url)
manga_does_not_exist = soup.find('form', {'id': 'searchform'})
if manga_does_not_exist:
search_sort_options = 'sort=views&order=za'
url = '{0}/search.php?name={1}&{2}'.format(URL_BASE,
manga_url,
search_sort_options)
soup = get_page_soup(url)
results = soup.findAll('a', {'class': 'series_preview'})
error_text = 'Error: Manga \'{0}\' does not exist'.format(manga_name)
error_text += '\nDid you meant one of the following?\n * '
error_text += '\n * '.join([manga.text for manga in results][:10])
sys.exit(error_text)
warning = soup.find('div', {'class': 'warning'})
if warning and 'licensed' in warning.text:
sys.exit('Error: ' + warning.text)
chapters = OrderedDict()
links = soup.findAll('a', {'class': 'tips'})
if(len(links) == 0):
sys.exit('Error: Manga either does not exist or has no chapters')
replace_manga_name = re.compile(re.escape(manga_name.replace('_', ' ')),
re.IGNORECASE)
for link in links:
chapters[replace_manga_name.sub('', link.text).strip()] = link['href']
return chapters
def get_page_numbers(soup):
"""Return the list of page numbers from the parsed page"""
raw = soup.findAll('select', {'class': 'm'})[0]
return (html['value'] for html in raw.findAll('option'))
def get_chapter_image_urls(url_fragment):
"""Find all image urls of a chapter and return them"""
print('Getting chapter urls')
url_fragment = os.path.dirname(url_fragment) + '/'
chapter_url = url_fragment
chapter = get_page_soup(chapter_url)
pages = get_page_numbers(chapter)
image_urls = []
print('Getting image urls...')
for page in pages:
print('url_fragment: {0}'.format(url_fragment))
print('page: {0}'.format(page))
print('Getting image url from {0}{1}.html'.format(url_fragment, page))
page_soup = get_page_soup(chapter_url + page + '.html')
images = page_soup.findAll('img', {'id': 'image'})
if images:
image_urls.append(images[0]['src'])
return image_urls
def get_chapter_number(url_fragment):
"""Parse the url fragment and return the chapter number."""
return ''.join(url_fragment.rsplit("/")[5:-1])
def download_urls(image_urls, manga_name, chapter_number):
"""Download all images from a list"""
download_dir = '{0}/{1}/'.format(manga_name, chapter_number)
if os.path.exists(download_dir):
shutil.rmtree(download_dir)
os.makedirs(download_dir)
for i, url in enumerate(image_urls):
filename = './{0}/{1}/{2:03}.jpg'.format(manga_name, chapter_number, i)
print('Downloading {0} to {1}'.format(url, filename))
urllib.urlretrieve(url, filename)
def make_cbz(dirname):
"""Create CBZ files for all JPEG image files in a directory."""
zipname = dirname + '.cbz'
images = glob.glob(os.path.abspath(dirname) + '/*.jpg')
with closing(ZipFile(zipname, 'w')) as zipfile:
for filename in images:
print('writing {0} to {1}'.format(filename, zipname))
zipfile.write(filename)
def download_manga_range(manga_name, range_start, range_end):
"""Download a range of a chapters"""
print('Getting chapter urls')
chapter_urls = get_chapter_urls(manga_name)
iend = chapter_urls.keys().index(range_start) + 1
istart = chapter_urls.keys().index(range_end)
for url_fragment in islice(chapter_urls.itervalues(), istart, iend):
chapter_number = get_chapter_number(url_fragment)
print('===============================================')
print('Chapter ' + chapter_number)
print('===============================================')
image_urls = get_chapter_image_urls(url_fragment)
download_urls(image_urls, manga_name, chapter_number)
download_dir = './{0}/{1}'.format(manga_name, chapter_number)
make_cbz(download_dir)
shutil.rmtree(download_dir)
def download_manga(manga_name, chapter_number=None):
"""Download all chapters of a manga"""
chapter_urls = get_chapter_urls(manga_name)
if chapter_number:
if chapter_number in chapter_urls:
url_fragment = chapter_urls[chapter_number]
else:
error_text = 'Error: Chapter {0} does not exist'
sys.exit(error_text.format(chapter_number))
chapter_number = get_chapter_number(url_fragment)
print('===============================================')
print('Chapter ' + chapter_number)
print('===============================================')
image_urls = get_chapter_image_urls(url_fragment)
download_urls(image_urls, manga_name, chapter_number)
download_dir = './{0}/{1}'.format(manga_name, chapter_number)
make_cbz(download_dir)
shutil.rmtree(download_dir)
else:
for chapter_number, url_fragment in chapter_urls.iteritems():
chapter_number = get_chapter_number(url_fragment)
print('===============================================')
print('Chapter ' + chapter_number)
print('===============================================')
image_urls = get_chapter_image_urls(url_fragment)
download_urls(image_urls, manga_name, chapter_number)
download_dir = './{0}/{1}'.format(manga_name, chapter_number)
make_cbz(download_dir)
shutil.rmtree(download_dir)
if __name__ == '__main__':
if len(sys.argv) == 4:
download_manga_range(sys.argv[1], sys.argv[2], sys.argv[3])
elif len(sys.argv) == 3:
download_manga(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 2:
download_manga(sys.argv[1])
else:
print('USAGE: mfdl.py [MANGA_NAME]')
print(' mfdl.py [MANGA_NAME] [CHAPTER_NUMBER]')
print(' mfdl.py [MANGA_NAME] [RANGE_START] [RANGE_END]')