Skip to content

Commit

Permalink
added different file and exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chpoit committed Dec 3, 2018
1 parent 484377f commit 096948d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 50 deletions.
11 changes: 7 additions & 4 deletions getify.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def clean(file_name_in, file_name_out, start):
for a in content.find_all("a"):
a.decompose()
raw.close()
file = open(file_name_out + ".xhtml", "w", encoding = "utf8")
file = open(file_name_out, "w", encoding = "utf8")
file.write('<html xmlns="http://www.w3.org/1999/xhtml">')
file.write("\n<head>")
file.write("\n<title>" + chapter_title + "</title>")
Expand Down Expand Up @@ -119,7 +119,7 @@ def cover_generator(src, starting, ending):
Sidenote: Will take a lot of time."""


def generate(html_files, novelname, author, chaptername, chapter_s, chapter_e):
def generate(html_files, novelname, author, chaptername, chapter_s, chapter_e, cleanup=True):
epub = zipfile.ZipFile(novelname + "_" + chapter_s + "-" + chapter_e + ".epub", "w")

# The first file must be named "mimetype"
Expand Down Expand Up @@ -212,5 +212,8 @@ def generate(html_files, novelname, author, chaptername, chapter_s, chapter_e):


#removes all the temporary files
for x in html_files:
os.remove(x)
if cleanup:
print("Cleaning up...")
for html_file in os.listdir(novelname):
os.remove(os.path.join(novelname, html_file))
os.rmdir(novelname)
113 changes: 67 additions & 46 deletions wuxiaworld2ebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import tkinter as tk
from tkinter import ttk
from urllib.error import HTTPError, URLError
import os
from os import path

#Initializing Stuff

main = tk.Tk()
main.title("Wuxiaworld-2-eBook")
main.geometry("375x150")
main.geometry("400x190")
main.resizable(True, True)
app = tk.Frame(main)
app.grid()
Expand All @@ -26,54 +28,66 @@ def on_field_change(index, value, op):
pass

def button_press():
#Getting Information
generate_button.configure(state = "disabled")
s_chapter = starting_chapter.get()
reset = str(s_chapter)
e_chapter = ending_chapter.get()
booknr = book_number.get()
name = novel.get()

#Getting relevant novel Information
raw_info = []
for i in db:
if name in i[0]:
raw_info.append(i)
raw_info = raw_info[0]
if raw_info[5] == 0:
link = raw_info[1]
else:
link = raw_info[1] + str(booknr) + "-chapter-"

#Generating list with download links
bulk_list = []
for s_chapter in range(s_chapter, e_chapter + 1):
bulk_list.append(link + str(s_chapter))
s_chapter = reset

getify.cover_generator(raw_info[4], s_chapter, str(e_chapter))

#Calls function's for downloading, cleanup and managing
#a list of file name's for cleanup, ToC and packing
y = int(s_chapter)
file_list = []
for x in range(len(bulk_list)):
try:
getify.download(bulk_list[x], str(s_chapter) + ".xhtml")
except HTTPError as e:
# Return code error (e.g. 404, 501, ...)
print('URL: {}, HTTPError: {} - {}'.format(bulk_list[x], e.code, e.reason))
except URLError as e:
# Not an HTTP-specific error (e.g. connection refused)
print('URL: {}, URLError: {}'.format(bulk_list[x], e.reason))
try:
#Getting Information
generate_button.configure(state = "disabled")
s_chapter = starting_chapter.get()
reset = str(s_chapter)
e_chapter = ending_chapter.get()
cleanup = delete_chapters.get()
booknr = book_number.get()
name = novel.get()

#Getting relevant novel Information
raw_info = []
for i in db:
if name in i[0]:
raw_info.append(i)
raw_info = raw_info[0]
if raw_info[5] == 0:
link = raw_info[1]
else:
getify.clean(str(s_chapter) + ".xhtml", raw_info[2] + str(s_chapter), name)
file_list.append(raw_info[2] + str(s_chapter) + ".xhtml")
link = raw_info[1] + str(booknr) + "-chapter-"

#Generating list with download links
bulk_list = []
for s_chapter in range(s_chapter, e_chapter + 1):
bulk_list.append(link + str(s_chapter))
s_chapter = reset

getify.cover_generator(raw_info[4], s_chapter, str(e_chapter))

if not path.exists(raw_info[0]):
os.makedirs(raw_info[0])

#Calls function's for downloading, cleanup and managing
#a list of file name's for cleanup, ToC and packing
y = int(s_chapter)
file_list = []
for x in range(len(bulk_list)):
if path.exists(_get_xhtml_path(raw_info, s_chapter)):
print(_get_xhtml_path(raw_info, s_chapter), " already exists")
else:
try:
getify.download(bulk_list[x], str(s_chapter) + ".xhtml")
except HTTPError as e:
# Return code error (e.g. 404, 501, ...)
print('URL: {}, HTTPError: {} - {}'.format(bulk_list[x], e.code, e.reason))
except URLError as e:
# Not an HTTP-specific error (e.g. connection refused)
print('URL: {}, URLError: {}'.format(bulk_list[x], e.reason))
else:
getify.clean(str(s_chapter) + ".xhtml", _get_xhtml_path(raw_info, s_chapter), name)
file_list.append(_get_xhtml_path(raw_info, s_chapter))
s_chapter = int(s_chapter) + 1

getify.generate(file_list, raw_info[0], raw_info[3], raw_info[2], reset, str(e_chapter))
generate_button.configure(state = "enabled")
getify.generate(file_list, raw_info[0], raw_info[3], raw_info[2], reset, str(e_chapter), cleanup=cleanup)

finally:
generate_button.configure(state = "enabled")

def _get_xhtml_path(raw_info, s_chapter, extension=".xhtml"):
return path.join(raw_info[0], raw_info[2] + str(s_chapter) + extension)

#Getting information from Database
c.execute("SELECT * FROM 'Information'")
Expand Down Expand Up @@ -117,9 +131,16 @@ def button_press():
ending_chapter_chosen = ttk.Entry(app, width = 5, textvariable = ending_chapter)
ending_chapter_chosen.grid(column = 1, row = 3, sticky = "W")

#Code for delete chapters
label5 = ttk.Label(app, text = "Cleanup when done: ")
label5.grid(column = 0, row = 4, pady = 10, sticky = "W")
delete_chapters = tk.BooleanVar()
delete_chapters_chosen = ttk.Checkbutton(app, variable = delete_chapters, offvalue=False, onvalue=True)
delete_chapters_chosen.grid(column = 1, row = 4, sticky = "W")

#Code for "Generate" button
generate_button = ttk.Button(app, text = "Generate", command = button_press)
generate_button.grid(column = 1, row = 4, sticky = "E")
generate_button.grid(column = 1, row = 5, sticky = "E")



Expand Down

0 comments on commit 096948d

Please sign in to comment.