-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangadownloader.py
125 lines (99 loc) · 2.82 KB
/
mangadownloader.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
import re
import os
import string
import zipfile
from subprocess import check_output as co
import mangafox
import kissmanga
# Save Home Directory
homeDirectory = os.getcwd()
# Get Manga
mangaURL = raw_input("Manga URL: ")
if(mangaURL != "GG"):
downloadManga(mangaURL)
def downloadManga(url, source="MF", zip=False, remove=False):
# Get Source
mangaSource = getMangaSource(source)
# Get Manga Info
mangaInfo = mangaSource.getMangaInfo(url)
# Create Manga Directory
title = getPossibleFileName(mangaInfo['title'])
if(not os.path.exists(title)):
os.mkdir(title)
# Change to Manga Directory
os.chdir(title)
chapters = []
for chapter in mangaInfo['chapters']:
chapters.insert(0, chapter)
for chapter in chapters:
# Create Chapter Directory
name = getPossibleFileName(chapter['name'])
if(not os.path.exists(name)):
os.mkdir(name)
# Change to Chapter Directory
os.chdir(name)
if(not os.path.exists('.mangadone')):
# Get Chapter Images URLs
chapterURL = chapter['url']
chapterImageURLs = mangaSource.getChapterImages(chapterURL)
# Download Images
downloadChapter(chapterImageURLs)
# Check number of Images
numberOfImages = len(chapterImageURLs)
numberOfDownloadedFiles = getNumberOfFiles()
if(numberOfImages == numberOfDownloadedFiles):
# Create Done File
open('.mangadone', 'a').close()
# Create Zip File
if(zip == True):
zipDirectory(name)
if(remove == True):
deleteImages()
# Return to Parent Directory
os.chdir("..")
# Return to Parent Directory
os.chdir("..")
def getNumberOfFiles():
numberOfFiles = len([filename for filename in os.listdir('.') if os.path.isfile(filename)])
if(os.path.exists(".DS_Store")):
numberOfFiles -= 1
return numberOfFiles
def getPossibleFileName(name):
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
# name = ''.join(c for c in name if c in valid_chars)
newName = ""
for c in name:
if c in valid_chars:
newName += c
else:
newName += '-'
return newName
def downloadChapter(imageURLs):
for line in imageURLs:
try:
co("curl -O -J \"" + line + "\"", shell=True)
except Exception, e:
print str(e)
pass
pass
def zipDirectory(name):
zf = zipfile.ZipFile("../" + name + ".zip", "w")
files = [filename for filename in os.listdir('.') if os.path.isfile(filename)]
for fileItem in files:
if(fileItem != ".DS_Store" and fileItem != ".mangadone"):
zf.write(fileItem)
zf.close()
def deleteImages():
filenames = [filename for filename in os.listdir('.') if os.path.isfile(filename)]
for filename in filenames:
if(filename != '.mangadone'):
os.remove(filename)
def goToHomeDirectory():
os.chdir(homeDirectory)
def getMangaSource(source):
mangaSource = kissmanga
if source == "MF":
mangaSource = mangafox
elif source == "KM":
mangaSource = kissmanga
return mangaSource