-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
156 lines (141 loc) · 4.74 KB
/
main.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
from selenium import webdriver
from fpdf import FPDF
import unidecode
import os
# driver.close() = close current tab
# driver.quit() = close browser
# These are the inputs for the novel and chapter selection
selected = False
title = input("1: Overgeared \n2: Trash of the Count's Family\n" + "Choose a Novel: ")
while selected == False:
if title == "1" or title.lower() == "overgeared":
link = 'https://www.wuxiaworld.com/novel/overgeared/og-chapter-'
title = "Overgeared"
selected = True
os.system('cls')
elif title == "2" or title.lower() == "trash of the count's family" or title.lower() == "trash":
link = 'https://www.wuxiaworld.com/novel/trash-of-the-counts-family/tcf-chapter-'
title = "TCF"
selected = True
os.system('cls')
else:
os.system('cls')
title = input("Invalid attempt\n" + "1: Overgeared \n2: Trash of the Count's Family\n" + "Choose a Novel: ")
# This portion starts selenium
chromedriver = 'C:\Program Files (x86)\chromedriver_win32\chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=chromedriver, options=options)
x = input("Which chapters do you want to download\n" + "from: ")
x2 = input("to: ")
for i in range(int(x), int((x2)) + 1):
i = str(i)
print("Compiling Chapter %s"%i)
driver.get(link + i)
if i != x:
content = content + "\n\n\n" + driver.find_element_by_id("chapter-outer").text.encode('utf8').decode('latin1')
elif i == x:
content = driver.find_element_by_id("chapter-outer").text.encode('utf8').decode('latin1')
os.system('cls')
driver.quit()
# This portion uses the fpdf library
print("Compiling chapter together")
class PDF(FPDF):
def chapter_body(self, name):
# Read text file
ay = content.encode('utf8').decode('latin1')
ay = unidecode.unidecode(ay)
ay = ay.replace("AC/AA", "'")
# ay = ay.replace ("Chapter %s"%x, "")
# Times 12
self.set_font('Times', '', 12)
# Output justified text
self.multi_cell(0, 5, ay)
# Line break
self.ln()
# Mention in italics
self.set_font('', 'I')
self.cell(0, 5, '(end of chapter %s)' % i)
def print_chapter(self, num, name):
self.add_page()
self.chapter_body(name)
pdf = PDF()
pdf.print_chapter(i, content)
pdf.output(r'C:\Users\kinga\Google Drive\Novels\%s' % title + " Chapters %s to %s.pdf" %(x,x2))
print("Done!")
print(r'Saved as: C:\Users\kinga\Google Drive\Novels\%s' % title + " Chapters %s to %s.pdf" %(x,x2))
print ("use this link to convert to epub: https://www.zamzar.com/convert/txt-to-epub/")
# - Things to do:
# Add bookmarks
# coding=utf-8
# from ebooklib import epub
#
# if __name__ == '__main__':
# book = epub.EpubBook()
#
# # add metadata
# book.set_identifier('sample123456')
# book.set_title('Sample book')
# book.set_language('en')
#
# book.add_author('Aleksandar Erkalovic')
#
# # intro chapter
# c1 = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='en')
# c1.content = u'<html><head></head><body><h1>Introduction</h1><p>Introduction paragraph where i explain what is happening.</p></body></html>'
#
# # about chapter
# c2 = epub.EpubHtml(title='About this book', file_name='about.xhtml')
# c2.content = '<h1>About this book</h1><p>Helou, this is my book! There are many books, but this one is mine.</p>'
#
# # add chapters to the book
# book.add_item(c1)
# book.add_item(c2)
#
# # create table of contents
# # - add section
# # - add auto created links to chapters
#
# book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
# (epub.Section('Languages'),
# (c1, c2))
# )
#
# # add navigation files
# book.add_item(epub.EpubNcx())
# book.add_item(epub.EpubNav())
#
# # define css style
# style = '''
# @namespace epub "http://www.idpf.org/2007/ops";
# body {
# font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif;
# }
# h2 {
# text-align: left;
# text-transform: uppercase;
# font-weight: 200;
# }
# ol {
# list-style-type: none;
# }
# ol > li:first-child {
# margin-top: 0.3em;
# }
# nav[epub|type~='toc'] > ol > li > ol {
# list-style-type:square;
# }
# nav[epub|type~='toc'] > ol > li > ol > li {
# margin-top: 0.3em;
# }
# '''
#
# # add css file
# nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
# book.add_item(nav_css)
#
# # create spine
# book.spine = ['nav', c1, c2]
#
# # create epub file
# epub.write_epub('test.epub', book, {})