This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
212 lines (164 loc) · 6.19 KB
/
main.py
File metadata and controls
212 lines (164 loc) · 6.19 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Importing necessary libraries
import requests
import ast
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
import math
from tkinter.filedialog import asksaveasfile
from ebooklib import epub
import re
from progress.bar import Bar
import time
import os
import shutil
import zipfile
import pdfkit
# Extracting cookie Id
session = requests.Session()
response = session.get('https://ranobes.net/')
cookieId = session.cookies.get_dict().get('__cf_bm')
headers = {'User-Agent': 'Mozilla/5.0',
'__cf_bm': '{cookieId}' .format(cookieId=cookieId)}
# Requesting homepage of novel
URL = input("Enter URL : ")
print()
req = Request(URL, headers=headers)
webpage = urlopen(req).read()
# Parsing using BeautifulSoup
soup = BeautifulSoup(webpage, "html.parser")
soup.style.decompose()
# Extracting chapter count
chapter_num = soup.find(
"li", title="Glossary + illustrations + division of chapters, etc.")
chapter_count = chapter_num.find("span", class_="grey").text
chapter_count = re.findall(r'\d+', chapter_count)
count = int(chapter_count[0])
print("Available chapters : ", count)
print()
# Extracting index page
redirect = soup.find("a", class_="uppercase bold")['href']
index_page = "https://ranobes.net" + redirect
# Extracting chapter list
i = 1
chapter_list = []
index_list = []
index_list.append(index_page)
#print("Extracting chapter list...")
bar = Bar('Extracting chapter list...', max=int(math.ceil(count / 25)))
while i <= int(math.ceil(count / 25)):
try:
page_num = index_page + "page/{num}/" .format(num=i)
index_list.append(page_num)
index_req = Request(page_num, headers=headers)
index_webpage = urlopen(index_req).read()
index_soup = BeautifulSoup(index_webpage, "html.parser")
chap_block = index_soup.find("div", id="dle-content")
chap_block = chap_block.find("script")
m = re.search(r"\"chapters\":(.*?),\"pages", chap_block.string)
lst = m.group(1).strip('][').split('},')
for item in range(0, len(lst)):
if item != len(lst) - 1:
lst[item] += '}'
data = ast.literal_eval(lst[item])
for key, value in data.items():
if key == 'link':
chapter_list.append(value)
bar.next()
except Exception as e:
print(e)
print("Retrying after 15 seconds...")
time.sleep(15)
continue
i += 1
bar.finish()
print()
print("Done")
print()
chapter_list = [x for x in chapter_list if x not in index_list]
chapter_list.reverse()
# Creating EpubBook
book = epub.EpubBook()
# Adding metadata
book.add_author(str(soup.find("span", class_="tag_list").text.strip()))
for span in soup("span"):
span.decompose()
# img_URL = "https://ranobes.net" + \
# soup.find("img", alt=str(soup.find(class_="poster").text.strip()))["src"]
# img_req = Request(img_URL, headers=headers)
f = open("cover.jpg", 'wb')
# f.write(urlopen(img_req).read())
f.close()
book.set_cover("image.jpg", open("cover.jpg", 'rb').read())
os.remove("cover.jpg")
book.set_title(str(soup.find(class_="title").text.strip()))
book.set_language('en')
chapter_epub = []
# Nunito Sans Font
font = "<link href='https://fonts.googleapis.com/css?family=Nunito Sans' rel='stylesheet'>"
# Extracting introduction to novel
descr = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='en')
descr_novel = soup.find("div", class_="cont-text showcont-h")
descr_para = '''{font}<style>h2{{font-family:'Nunito Sans'; padding:0 10px; line-height: 1.6}}</style>
<h2>Introduction</h2>''' .format(font=font)
descr_para += '''{font}<style>body{{font-family:'Nunito Sans'; padding:0 10px; line-height: 1.6}}</style>
<body>{descr_novel}</body>''' .format(font=font, descr_novel=descr_novel)
descr.content = descr_para
book.add_item(descr)
# Extracting chapters
start = input("Enter starting chapter number : ")
end = input("Enter ending chapter number : ")
print()
print("Extracting chapters...")
print()
i = int(start) - 1
while i < int(end):
try:
# Requesting chapter page
site = chapter_list[i]
site_req = Request(site, headers=headers)
site_page = urlopen(site_req).read()
site_soup = BeautifulSoup(site_page, "html.parser")
for span in site_soup("span"):
span.decompose()
site_soup.find("div", class_="category grey ellipses").decompose()
# Extracting chapter title
chapter_title = site_soup.find("h1", class_="h4 title")
file_name = 'ch{numb}.xhtml' .format(numb=str(i+1))
c = epub.EpubHtml(
title=str(chapter_title.text.strip()), file_name=file_name)
# Extracting chapter content
chapter_content = site_soup.find_all("p")
para = '''{font}<h2 style="font-family:'Nunito Sans'; padding:0 10px; line-height: 1.6">
{title_page}</h2>''' .format(font=font, title_page=str(chapter_title.text.strip()))
for j in chapter_content:
para += '''{font}<p style="font-family:'Nunito Sans'; padding:0 10px; line-height: 1.6">{element}
</p>''' .format(font=font, element=str(j.text.strip()))
c.content = para
# Adding chapters to book
book.add_item(c)
chapter_epub.append(c)
# Printing progress on terminal
print(chapter_title.text.strip())
except Exception as e:
print(e)
print("Retrying after 15 seconds...")
time.sleep(15)
continue
i += 1
print()
# Defining index of book
book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
(epub.Section('Chapters'),
(chapter_epub))
)
# Adding navigation files
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = chapter_epub
# Creating epub file
files = [('EPUB File', '*.epub')]
file_name = asksaveasfile(filetypes=files, defaultextension='*.epub')
os.chdir(os.path.dirname(file_name.name))
file_name = os.path.basename(file_name.name)
epub.write_epub(file_name, book, {})
print("Enjoy your read...")