-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhdownloader.py
52 lines (40 loc) · 1.48 KB
/
mhdownloader.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
# -*- coding: utf-8 -*-
import os
import urllib.request ,urllib.error, urllib.parse, contextlib
from mhparser import *
from PIL import Image
import re
class MangaHostDownloader:
def __init__(self):
self._pattern = re.compile(".+\.jpg$")
def download_url(self, url, path=""):
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', MangaHostParser.HDR['User-Agent'])]
urllib.request.install_opener(opener)
file_path = path + self.get_filename(url)
if not os.path.exists(os.path.dirname(file_path)):
os.makedirs(os.path.dirname(file_path))
urllib.request.urlretrieve(url, file_path)
if not self._pattern.match(file_path):
self.convert_to_jpg(file_path)
def convert_to_jpg(self, file_path):
parser = MangaHostParser()
new_path_file = os.path.splitext(file_path)[0]
if not self._pattern.match(new_path_file):
new_path_file = new_path_file + '.jpg'
im = Image.open(file_path).convert('RGB')
try:
im = parser.remove_borders(im)
except(InvalidImage):
os.remove(file_path)
return None
im.save(file_path, "jpeg")
try:
os.rename(file_path, new_path_file)
except FileExistsError:
os.remove(file_path)
def get_filename(self, url):
splitted_url = url.split("/")
return splitted_url[-1]
def convert_images_to_jpg(self):
pass