Skip to content

Commit

Permalink
FIXED THE STGUPID FUCKING SPAM BUG FINALLY OMFG
Browse files Browse the repository at this point in the history
  • Loading branch information
reticivis-net committed Jan 13, 2021
1 parent 745092b commit cf03fb8
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 32 deletions.
21 changes: 17 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion captionfunctions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import io
import logging
import os
Expand All @@ -6,7 +7,9 @@
import subprocess
import sys
from PIL import Image
from improcessing import filetostring, imgkitstring, temp_file

import imgkit
from improcessing import filetostring, temp_file, options


# stolen code https://stackoverflow.com/questions/6116978/how-to-replace-multiple-substrings-of-a-string
Expand Down Expand Up @@ -105,3 +108,10 @@ def jpeg(image, params: list, tosavename=None):

def speed(media):
pass


def imgkitstring(torender, tosavename=None):
if tosavename is None:
tosavename = temp_file("png")
imgkit.from_string(torender, tosavename, options=options)
return tosavename
13 changes: 13 additions & 0 deletions imgkit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
"""
Wkhtmltopdf python wrapper to convert html to image using the webkit rendering engine and qt
"""

__author__ = 'jarrekk'
__contact__ = 'me@jarrekk.com'
__version__ = '1.0.2'
__homepage__ = 'https://github.com/jarrekk/imgkit'
__license__ = 'MIT'

from .imgkit import IMGKit
from .api import from_url, from_file, from_string, config
101 changes: 101 additions & 0 deletions imgkit/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
from .imgkit import IMGKit
from .config import Config


def from_url(url,
output_path,
options=None,
toc=None,
cover=None,
config=None,
cover_first=None):
"""
Convert URL/URLs to IMG file/files
:param url: URL or list of URLs to be saved
:param output_path: path to output PDF file/files. False means file will be returned as string
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: style of input
:param config: (optional) instance of imgkit.config.Config()
:param cover_first: (optional) if True, cover always precedes TOC
:return: True when success
"""
rtn = IMGKit(url,
'url',
options=options,
toc=toc, cover=cover,
config=config,
cover_first=cover_first)
return rtn.to_img(output_path)


def from_file(filename,
output_path,
options=None,
toc=None,
cover=None,
css=None,
config=None,
cover_first=None):
"""
Convert HTML file/files to IMG file/files
:param filename: path of HTML file or list with paths or file-like object
:param output_path: path to output PDF file/files. False means file will be returned as string
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: style of input
:param config: (optional) instance of imgkit.config.Config()
:param cover_first: (optional) if True, cover always precedes TOC
:return: True when success
"""
rtn = IMGKit(filename,
'file',
options=options,
toc=toc,
cover=cover,
css=css,
config=config,
cover_first=cover_first)
return rtn.to_img(output_path)


def from_string(string,
output_path,
options=None,
toc=None,
cover=None,
css=None,
config=None,
cover_first=None):
"""
Convert given string/strings to IMG file
:param string:
:param output_path: path to output PDF file/files. False means file will be returned as string
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: style of input
:param config: (optional) instance of imgkit.config.Config()
:param cover_first: (optional) if True, cover always precedes TOC
:return: True when success
"""
rtn = IMGKit(string, 'string', options=options, toc=toc, cover=cover, css=css,
config=config, cover_first=cover_first)
return rtn.to_img(output_path)


def config(**kwargs):
"""
Constructs and returns a :class:`Config` with given options
:param wkhtmltopdf: path to binary
:param meta_tag_prefix: the prefix for ``pdfkit`` specific meta tags
"""

return Config(**kwargs)
36 changes: 36 additions & 0 deletions imgkit/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import subprocess
import sys


class Config(object):
def __init__(self, wkhtmltoimage='', meta_tag_prefix='imgkit-'):
self.meta_tag_prefix = meta_tag_prefix

self.wkhtmltoimage = wkhtmltoimage

self.xvfb = ''

if not self.wkhtmltoimage:
if sys.platform == 'win32':
self.wkhtmltoimage = subprocess.Popen(['where', 'wkhtmltoimage'],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].strip()
else:
self.wkhtmltoimage = subprocess.Popen(['which', 'wkhtmltoimage'],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].strip()
if not self.xvfb:
if sys.platform == 'win32':
self.xvfb = subprocess.Popen(['where', 'xvfb-run'],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].strip()
else:
self.xvfb = subprocess.Popen(['which', 'xvfb-run'],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).communicate()[0].strip()

try:
with open(self.wkhtmltoimage):
pass
except IOError:
raise IOError('No wkhtmltoimage executable found: "{0}"\n'
'If this file exists please check that this process can '
'read it. Otherwise please install wkhtmltopdf - '
'http://wkhtmltopdf.org\n'.format(self.wkhtmltoimage))

0 comments on commit cf03fb8

Please sign in to comment.