Skip to content

Commit

Permalink
Merge pull request #34 from theslavicbear/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
theslavicbear authored Nov 30, 2019
2 parents 312423b + 59c0faf commit 4692fda
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
21 changes: 20 additions & 1 deletion Ebook-Publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import os
import threading
import queue

import shutil
from zipfile import ZipFile

#Master dict of supported sites
sites={
Expand All @@ -33,6 +34,11 @@ def MakeText(site):
published.write(site.story)
published.close()

def GetImage(url):
req = urllib.request.Request(url, headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'})
return urllib.request.urlopen(req).read()


#This function is basically all space magic from the docs of ebooklib
def MakeEpub(site):
book=epub.EpubBook()
Expand Down Expand Up @@ -61,6 +67,7 @@ def MakeEpub(site):
c[i].content='<h2>\n'+site.chapters[i]+'\n</h2>\n'+site.rawstoryhtml[i].prettify()
book.add_item(c[i])
toc=toc+(c[i],)

book.toc=toc
book.spine.append('nav')

Expand All @@ -77,6 +84,15 @@ def MakeEpub(site):
book.spine.append(i)
epub.write_epub(wd+site.title+'.epub', book, {})

if type(site) is Chyoa.Chyoa:
if site.hasimages == True:
with ZipFile(wd+site.title+'.epub', 'a') as myfile:
i=1
for url in site.images:
with myfile.open('EPUB/img'+str(i)+'.jpg', 'w') as myimg:
myimg.write(GetImage(url))
i=i+1


def MakeClass(url):
#getting url
Expand All @@ -94,12 +110,15 @@ def MakeClass(url):
parser.add_argument('-d','--directory', help="Directory to place output files. Default ./")
parser.add_argument('-q','--quiet', help="Turns off most terminal output", action='store_true')
parser.add_argument('-t', help="Turns on multithreading mode. Recommend also enabling --quiet", action='store_true')
parser.add_argument('-i', '--insert-images', help="Downloads and inserts images for Chyoa stories", action='store_true')
args=parser.parse_args()

if args.quiet:
Common.quiet=True
#sys.stdout=open(os.devnull, 'w')
#print('quiet enabled')
if args.insert_images:
Common.images=True

stdin=False
if not sys.stdin.isatty():
Expand Down
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A Python tool for converting online stories into portable formats
* fanfiction.net
* fictionpress.com
* literotica.com
* classicreader.com
* classicreader.com (The site does not appear to be working as of 11/30/19)
* chyoa.com (rudimentary support: Input the last page you wish to include, and the code will work backwards towards the beginning of the story. You will be asked to input customizable names if they are found)
* wattpad.com

Expand All @@ -25,25 +25,26 @@ Both external libraries can be installed with pip `pip3 install beautifulsoup4 &

To run Ebook-Publisher, use the terminal or command prompt to execute Python3 and pass in Ebook-Publisher.py and the URL for the story you want. You can add several other arguments. Try `python3 Ebook-Publisher.py --help` for the detailed readout, or see below:


usage: Ebook-Publisher.py [-h] [-o {txt,epub}] [-f] [-d DIRECTORY] [url]

positional arguments:
url The URL of the story you want

optional arguments:
-h, --help show this help message and exit
-o {txt,epub}, --output-type {txt,epub}
The file type you want
-f, --file Use text file containing a list of URLs instead of
single URL
-d DIRECTORY, --directory DIRECTORY
Directory to place output files. Default ./
-q, --quiet Turns off most terminal output
-t Turns on multithreading mode (one thread for each story). Recommend also enabling
--quiet

```
usage: ebook-publisher [-h] [-o {txt,epub}] [-f] [-d DIRECTORY] [-q] [-t] [-i]
[url]
positional arguments:
url The URL of the story you want
optional arguments:
-h, --help show this help message and exit
-o {txt,epub}, --output-type {txt,epub}
The file type you want
-f, --file Use text file containing a list of URLs instead of
single URL
-d DIRECTORY, --directory DIRECTORY
Directory to place output files. Default ./
-q, --quiet Turns off most terminal output
-t Turns on multithreading mode. Recommend also enabling
--quiet
-i, --insert-images Downloads and inserts images for Chyoa stories
```

### Sample Usage:
Expand Down
26 changes: 25 additions & 1 deletion Site/Chyoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(self, url):
self.length=1
self.pbar=None
self.url=url
self.images=[] #testing images
self.hasimages = False
try:
page=requests.get(self.url)
except:
Expand All @@ -41,7 +43,7 @@ def __init__(self, url):
self.authors.insert(0,soup.find_all('a')[7].get_text())
self.chapters.insert(0, soup.find('h1').get_text())
self.summary=soup.find('p', attrs={'class': 'synopsis'}).get_text()

tmp=soup.find('p', attrs={'class': 'meta'}).get_text()
t=[s for s in tmp.split() if s.isdigit()]
self.length=int(t[0])
Expand Down Expand Up @@ -79,9 +81,20 @@ def __init__(self, url):
#for name in self.renames:


if Common.images:
if soup.find('div', attrs={'class': 'chapter-content'}).find('img'):
for simg in soup.find('div', attrs={'class': 'chapter-content'}).find_all('img'):
self.images.append(simg.get('src'))
simg['src']='img'+str(len(self.images))+'.jpg'
self.hasimages = True


temp=str(soup.find('div', attrs={'class': 'chapter-content'}))





self.questions.insert(0, soup.find_all('h2')[1].get_text())
temp+='<h2>'+self.questions[0]+'</h2>'
self.temp.insert(0, temp)
Expand Down Expand Up @@ -154,6 +167,14 @@ def AddNextPage(self, url):
soup=BeautifulSoup(page.content, 'html.parser')
self.authors.insert(0,soup.find_all('a')[7].get_text())
self.chapters.insert(0, soup.find('h1').get_text())

if Common.images:
if soup.find('div', attrs={'class': 'chapter-content'}).find('img'):
for simg in soup.find('div', attrs={'class': 'chapter-content'}).find_all('img'):
self.images.append(simg.get('src'))
simg['src']='img'+str(len(self.images))+'.jpg'
self.hasimages = True

temp=str(soup.find('div', attrs={'class': 'chapter-content'}))
self.questions.insert(0, soup.find_all('h2')[1].get_text())
temp+='<h2>'+self.questions[0]+'</h2>'
Expand All @@ -162,6 +183,9 @@ def AddNextPage(self, url):
#self.temp[0]=self.temp[0].replace(self.oldnames[i], self.renames[i])
#self.temp[0]=self.temp[0].replace('\n <span class="js-immersion-receiver-c'+str(i)+'">\n '+self.oldnames[i]+'\n </span>\n ',' '+self.renames[i])
#self.rawstoryhtml.insert(0, BeautifulSoup(temp, 'html.parser'))



self.pbar.Update()
for i in soup.find_all('a'):
if i.text.strip()=='Previous Chapter':
Expand Down
2 changes: 2 additions & 0 deletions Site/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

quiet = False

images = False

def prnt(out, f=False):
if not quiet and not f:
print(out)
Expand Down

0 comments on commit 4692fda

Please sign in to comment.