Compare commits

...
3 Commits
3 changed files with 19 additions and 16 deletions

No files matched your search

+5 -5
View File
@@ -1,6 +1,6 @@
# mlwerke2pdf # mlwerke2pdf
Python module to convert texts on http://www.mlwerke.de into Portable Document Format files Python module to convert texts on http://www.mlwerke.de or https://www.marxists.org into Portable Document Format files
mlwerke2pdf fetches the linked subsites from the given table of contents and concats them to a single pdf mlwerke2pdf fetches the linked subsites from the given table of contents and concats them to a single pdf
@@ -9,19 +9,19 @@ converting them to pdf internal links would be possible but rather complex
# dependencies # dependencies
as pdfkit uses wkhtmltopdf you will need it. See https://pypi.org/project/pdfkit/ for instructions uses python modules requests, BeautifulSoup4, urllib and weasyprint
# Usage as commandline util # Usage as commandline util
python mlwerke2pdf_shutil.py -u URL -o PDF_OUTPUT_PATH python mlwerke2pdf_shutil.py -u URL -o PDF_OUTPUT_PATH [--dry True optional for not writing any files]
use the base / table of contents of a text like http://www.mlwerke.de/me/me04/me04_459.htm as URL use the base / table of contents of a text like https://www.marxists.org/deutsch/archiv/marx-engels/1848/manifest/index.htm as URL
# Usage as Python module # Usage as Python module
## create MLWerke Object: ## create MLWerke Object:
mlwerk = mlwerke2pdf.MLWerk(URL) mlwerk = mlwerke2pdf.MLWerk(URL)
use the base / table of contents of a text like http://www.mlwerke.de/me/me04/me04_459.htm as URL use the base / table of contents of a text like https://www.marxists.org/deutsch/archiv/marx-engels/1848/manifest/index.htm as URL
## generate a pdf ## generate a pdf
mlwerk.text2pdf(PDF_OUTPUT_PATH) mlwerk.text2pdf(PDF_OUTPUT_PATH)
+12 -10
View File
@@ -1,7 +1,7 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import urllib.parse import urllib.parse
import pdfkit from weasyprint import HTML
class MLWerk: class MLWerk:
class SubSite: class SubSite:
@@ -31,22 +31,24 @@ class MLWerk:
if limit != None and i > limit: if limit != None and i > limit:
break break
link = link.get('href') link = link.get('href')
if not (link == None and ("#" in link or "/" in link)): if not link == None and not ("#" in link or "/" in link):
self.subsites.append(self.SubSite(urllib.parse.urljoin(self.baseurl, link))) self.subsites.append(self.SubSite(urllib.parse.urljoin(self.baseurl, link)))
i += 1 i += 1
def concatsubsites(self): def concatsubsites(self):
for subsite in self.subsites: for subsite in self.subsites:
for element in subsite.soup.body: for tag in subsite.soup.body:
self.soup.body.append(element) if (tag != "\n" and tag.name != "a"):
self.soup.body.append(tag)
def genpdf(self, path): def genpdf(self, path):
pdfkit.from_string(str(self.soup), path, options={"enable-local-file-access": ""}) HTML(string=str(self.soup)).write_pdf(path)
def text2pdf(self, path): def text2pdf(self, path, dry=False):
print("gettings subsites") print("gettings subsites")
self.getsubsites self.getsubsites()
print("concatting subsites") print("concatting subsites")
self.concatsubsites self.concatsubsites()
print("generating pdf") if not dry:
self.genpdf(path) print("generating pdf")
self.genpdf(path)
+2 -1
View File
@@ -7,7 +7,8 @@ parser = argparse.ArgumentParser(
epilog='published under GPL-3 at https://git.roteserver.de/humorhenker/mlwerke2pdf') epilog='published under GPL-3 at https://git.roteserver.de/humorhenker/mlwerke2pdf')
parser.add_argument('-u', '--url', dest='url', help='base / table of contents of a text like http://www.mlwerke.de/me/me04/me04_459.htm', required=True) parser.add_argument('-u', '--url', dest='url', help='base / table of contents of a text like http://www.mlwerke.de/me/me04/me04_459.htm', required=True)
parser.add_argument('-o', '--pdfpath', dest='pdfpath', help='path for the output pdf', required=True) parser.add_argument('-o', '--pdfpath', dest='pdfpath', help='path for the output pdf', required=True)
parser.add_argument('--dry', dest='dryrun', default=False, help="dont store any files just dry fetch")
args = parser.parse_args() args = parser.parse_args()
mlwerke2pdf.MLWerk(args.url).text2pdf(args.pdfpath) mlwerke2pdf.MLWerk(args.url).text2pdf(args.pdfpath, args.dryrun)