diff --git a/README.md b/README.md
index e8ee97b..b4e95b1 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,6 @@
![Python Version][python-shield]
[![MIT License][license-shield]][license-url]
+[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=quantrancse_hako2epub&metric=alert_status)](https://sonarcloud.io/dashboard?id=quantrancse_hako2epub)
diff --git a/hako2epub.py b/hako2epub.py
index dc768a4..1dfbb64 100644
--- a/hako2epub.py
+++ b/hako2epub.py
@@ -12,6 +12,8 @@
HEADERS = {
'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36')}
+bs4_html_parser = 'html.parser'
+
class Utils():
@@ -26,78 +28,89 @@ def re_url(self, ln_url, url):
def format_text(self, text):
return text.strip().replace('\n', '')
- def getImage(self, image_url):
- if 'imgur.com' in image_url:
- if '.' not in image_url[-5:]:
- image_url += '.jpg'
- return Image.open(requests.get(image_url, headers=HEADERS, stream=True, timeout=10).raw).convert('RGB')
+ def get_image(self, image_url):
+ if 'imgur.com' in image_url and '.' not in image_url[-5:]:
+ image_url += '.jpg'
+ try:
+ image = Image.open(requests.get(
+ image_url, headers=HEADERS, stream=True, timeout=10).raw).convert('RGB')
+ except BaseException as e: # NOSONAR
+ print("Can not get image: " + image_url)
+ return image
class UpdateLN():
- def checkUpdate(self, ln_url='all', mode=''):
+ def __init__(self):
+ self.ln_info_json_file = 'ln_info.json'
+
+ def check_update(self, ln_url='all', mode=''):
try:
- if isfile('ln_info.json'):
+ if isfile(self.ln_info_json_file):
- with open('ln_info.json', 'r', encoding='utf-8') as read_file:
+ with open(self.ln_info_json_file, 'r', encoding='utf-8') as read_file:
save_file = json.load(read_file)
for old_ln in save_file.get('ln_list'):
- if ln_url == 'all':
- print('Checking update: ' + old_ln.get('ln_name'))
- self.checkUpdateLN(old_ln, mode)
- print('Done...\n')
- elif ln_url == old_ln.get('ln_url'):
- print('Checking update: ' + old_ln.get('ln_name'))
- self.checkUpdateLN(old_ln, mode)
- print('Done...\n')
+ if ln_url == 'all' or ln_url == old_ln.get('ln_url'):
+ self.check_update_ln(old_ln, mode)
else:
print('Can not find ln_info.json file!')
- except:
+ except BaseException as e: # NOSONAR
print('Error: Can not process ln_info.json!')
+ raise e
- def checkUpdateLN(self, old_ln, mode):
+ def check_update_ln(self, old_ln, mode):
+ print('Checking update: ' + old_ln.get('ln_name'))
old_ln_url = old_ln.get('ln_url')
try:
request = requests.get(old_ln_url, headers=HEADERS, timeout=10)
- soup = BeautifulSoup(request.text, 'html.parser')
+ soup = BeautifulSoup(request.text, bs4_html_parser)
new_ln = LNInfo()
- new_ln = new_ln.getLNInfo(old_ln_url, soup, 'default')
+ new_ln = new_ln.get_ln_info(old_ln_url, soup, 'default')
if mode == 'updatevol':
- volume_titles = [vol_item.get('vol_name')
- for vol_item in old_ln.get('vol_list')]
+ self.updatevol_ln(old_ln, new_ln)
+ else:
+ self.update_ln(old_ln, new_ln)
- print('Select a volume to update:\n')
- for i, volume_title in enumerate(volume_titles):
- print(str(i) + ': ' + volume_title + '\n')
+ print('Done...\n')
+ except BaseException as e: # NOSONAR
+ print('Error: Can not check ln info!')
- try:
- selected_volume = int(input('Enter volume number: '))
- for volume in new_ln.volume_list:
- if volume.name == old_ln.get('vol_list')[selected_volume].get('vol_name'):
- self.updateNewChapter(new_ln, volume, old_ln)
- except:
- print('Invalid input number.')
+ def updatevol_ln(self, old_ln, new_ln):
+ volume_titles = [vol_item.get('vol_name')
+ for vol_item in old_ln.get('vol_list')]
- else:
- old_ln_vol_list = [vol.get('vol_name')
- for vol in old_ln.get('vol_list')]
+ print('Select a volume to update:\n')
+ for i, volume_title in enumerate(volume_titles):
+ print(str(i) + ': ' + volume_title + '\n')
- for volume in new_ln.volume_list:
- if volume.name not in old_ln_vol_list:
- self.updateNewVolume(new_ln, volume)
- else:
- self.updateNewChapter(new_ln, volume, old_ln)
- except:
- print('Error: Can not check ln info!')
+ try:
+ selected_volume = int(input('Enter volume number: '))
+ for volume in new_ln.volume_list:
+ if volume.name == old_ln.get('vol_list')[selected_volume].get('vol_name'):
+ self.update_new_chapter(new_ln, volume, old_ln)
+ except BaseException as e:
+ print('Invalid input number.')
+ raise e
+
+ def update_ln(self, old_ln, new_ln):
+ old_ln_vol_list = [vol.get('vol_name')
+ for vol in old_ln.get('vol_list')]
+
+ for volume in new_ln.volume_list:
+ if volume.name not in old_ln_vol_list:
+ self.update_new_volume(new_ln, volume)
+ else:
+ self.update_new_chapter(new_ln, volume, old_ln)
- def updateNewVolume(self, new_ln, volume):
+ def update_new_volume(self, new_ln, volume):
new_ln.volume_list = [volume]
epub_engine = EpubEngine()
- epub_engine.createEpub(new_ln)
+ epub_engine.create_epub(new_ln)
- def updateNewChapter(self, new_ln, volume, old_ln):
+ def update_new_chapter(self, new_ln, volume, old_ln):
for vol in old_ln.get('vol_list'):
if volume.name == vol.get('vol_name'):
print('Checking volume: ' + volume.name)
@@ -108,12 +121,12 @@ def updateNewChapter(self, new_ln, volume, old_ln):
if volume.chapter_list:
print('Updating volume: ' + volume.name)
epub_engine = EpubEngine()
- epub_engine.updateEpub(new_ln, volume)
+ epub_engine.update_epub(new_ln, volume)
- def updateJson(self, ln):
+ def update_json(self, ln): # NOSONAR
try:
print('Updating ln_info.json...')
- with open('ln_info.json', 'r', encoding='utf-8') as read_file:
+ with open(self.ln_info_json_file, 'r', encoding='utf-8') as read_file:
save_file = json.load(read_file)
ln_url_list = [ln_item.get('ln_url')
@@ -158,12 +171,13 @@ def updateJson(self, ln):
save_file['ln_list'][i]['vol_list'][j]['chapter_list'].append(
chapter)
- with open('ln_info.json', 'w', encoding='utf-8') as outfile:
+ with open(self.ln_info_json_file, 'w', encoding='utf-8') as outfile:
json.dump(save_file, outfile, indent=4, ensure_ascii=False)
- except:
+ except BaseException as e:
print('Error: Can not update ln_info.json!')
+ raise e
- def createJson(self, ln):
+ def create_json(self, ln):
try:
print('Creating ln_info.json...')
ln_list = {}
@@ -185,34 +199,38 @@ def createJson(self, ln):
ln_list['ln_list'].append(current_ln)
- with open('ln_info.json', 'w', encoding='utf-8') as outfile:
+ with open(self.ln_info_json_file, 'w', encoding='utf-8') as outfile:
json.dump(ln_list, outfile, indent=4, ensure_ascii=False)
- except:
+ except BaseException as e:
print('Error: Can not create ln_info.json!')
+ raise e
class EpubEngine():
- def makeCoverImage(self):
+ def __init__(self):
+ self.ln_info_json_file = 'ln_info.json'
+
+ def make_cover_image(self):
try:
print('Making cover image...')
- img = Utils().getImage(self.volume.cover_img)
+ img = Utils().get_image(self.volume.cover_img)
b = BytesIO()
img.save(b, 'jpeg')
b_img = b.getvalue()
cover_image = epub.EpubItem(
file_name='cover_image.jpeg', media_type='image/jpeg', content=b_img)
return cover_image
- except:
+ except BaseException as e: # NOSONAR
print('Error: Can not get cover image!')
return None
- def setMetadata(self, title, author, lang='vi'):
+ def set_metadata(self, title, author, lang='vi'):
self.book.set_title(title)
self.book.set_language(lang)
self.book.add_author(author)
- def makeIntroPage(self):
+ def make_intro_page(self):
print('Making intro page...')
source_url = self.volume.url
github_url = 'https://github.com/quantrancse/hako2epub'
@@ -221,7 +239,7 @@ def makeIntroPage(self):
'text-align: center'
])
- cover_image = self.makeCoverImage()
+ cover_image = self.make_cover_image()
self.book.add_item(cover_image)
intro_html += '' % (
@@ -259,21 +277,21 @@ def makeIntroPage(self):
content=intro_html,
)
- def makeChapter(self, i=0):
+ def make_chapter(self, i=0):
try:
print('Making chapter contents...')
for i, chapter in enumerate(self.volume.chapter_list.keys(), i):
chapter_url = self.volume.chapter_list[chapter]
request = requests.get(
chapter_url, headers=HEADERS, timeout=10)
- soup = BeautifulSoup(request.text, 'html.parser')
+ soup = BeautifulSoup(request.text, bs4_html_parser)
xhtml_file = 'chap_%s.xhtml' % str(i + 1)
chapter_title = soup.find('div', 'title-top').find('h4').text
chapter_content = '''