mirror of
https://github.com/openzim/zimit.git
synced 2025-09-22 11:22:23 -04:00
Send an email when the download is ready
This commit is contained in:
parent
146bd32f63
commit
8277e3f883
3
setup.py
3
setup.py
@ -24,7 +24,8 @@ setup(name='zimit',
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
zip_safe=False,
|
||||
install_requires=['cornice', 'waitress', 'rq', 'colander'],
|
||||
install_requires=['cornice', 'waitress', 'rq', 'colander',
|
||||
'python-slugify', 'pyramid_mailer'],
|
||||
entry_points="""\
|
||||
[paste.app_factory]
|
||||
main=zimit:main
|
||||
|
16
zimit.ini
16
zimit.ini
@ -1,15 +1,15 @@
|
||||
[app:main]
|
||||
use = egg:zimit
|
||||
|
||||
pyramid.reload_templates = true
|
||||
pyramid.debug_authorization = false
|
||||
pyramid.debug_notfound = false
|
||||
pyramid.debug_routematch = false
|
||||
pyramid.debug_templates = true
|
||||
pyramid.default_locale_name = en
|
||||
|
||||
zimit.zimwriterfs_bin = /home/rhubscher/git/openzim/zimwriterfs/zimwriterfs
|
||||
zimit.zimwriterfs_bin = /home/alexis/dev/openzim/zimwriterfs/zimwriterfs
|
||||
zimit.httrack_bin = /usr/bin/httrack
|
||||
mail.host = localhost
|
||||
mail.port = 2525
|
||||
mail.default_sender = Zim It!
|
||||
|
||||
pyramid.includes =
|
||||
pyramid_mailer
|
||||
pyramid_mailer.debug
|
||||
|
||||
[server:main]
|
||||
use = egg:waitress#main
|
||||
|
@ -3,6 +3,8 @@ from pyramid.events import NewRequest
|
||||
from redis import Redis
|
||||
from rq import Queue
|
||||
|
||||
from worker import ZimCreator
|
||||
|
||||
|
||||
def main(global_config, **settings):
|
||||
config = Configurator(settings=settings)
|
||||
@ -10,8 +12,11 @@ def main(global_config, **settings):
|
||||
|
||||
def attach_objects_to_request(event):
|
||||
event.request.queue = config.registry.queue
|
||||
event.request.client = ZimCreator(event.request.registry.settings)
|
||||
|
||||
config.add_subscriber(attach_objects_to_request, NewRequest)
|
||||
|
||||
config.include("cornice")
|
||||
config.include('pyramid_mailer')
|
||||
config.scan("zimit.views")
|
||||
return config.make_wsgi_app()
|
||||
|
15
zimit/messages.py
Normal file
15
zimit/messages.py
Normal file
@ -0,0 +1,15 @@
|
||||
from pyramid_mailer.message import Attachment, Message
|
||||
|
||||
|
||||
class ZimReadyMessage(Message):
|
||||
def __init__(self, email, zim_link):
|
||||
subject = "[ZimIt!] Your zimfile is ready!"
|
||||
|
||||
bdata = "{zim_link}".format(zim_link=zim_link)
|
||||
hdata = bdata
|
||||
|
||||
body = Attachment(data=bdata, transfer_encoding="quoted-printable")
|
||||
html = Attachment(data=hdata, transfer_encoding="quoted-printable")
|
||||
|
||||
super(ZimReadyMessage, self).__init__(
|
||||
subject=subject, body=body, html=html, recipients=[email])
|
@ -1,59 +1,7 @@
|
||||
import os
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from cornice import Service
|
||||
from colander import MappingSchema, SchemaNode, String
|
||||
|
||||
HTTRACK_BIN = "/usr/bin/httrack"
|
||||
DEFAULT_AUTHOR = "BSF"
|
||||
|
||||
|
||||
def spawn(cmd):
|
||||
print cmd
|
||||
return subprocess.Popen(shlex.split(cmd))
|
||||
|
||||
|
||||
def zim_it(config, settings):
|
||||
location = download_website(config['url'], settings)
|
||||
create_zim(location, config)
|
||||
|
||||
|
||||
def download_website(url, settings):
|
||||
httrack_bin = settings.get('zimit.httrack_bin', HTTRACK_BIN)
|
||||
|
||||
if not os.path.exists(httrack_bin):
|
||||
raise OSError('%s does not exist.' % httrack_bin)
|
||||
|
||||
path = tempfile.mkdtemp("website")
|
||||
p = spawn("%s --path %s %s" % (httrack_bin, path, url))
|
||||
p.wait()
|
||||
shutil.copy('./favicon.ico', path)
|
||||
|
||||
return path
|
||||
|
||||
|
||||
def create_zim(location, config, settings):
|
||||
if 'zimit.zimwriterfs_bin' not in settings:
|
||||
raise ValueError('Please define zimit.zimwriterfs_bin config.')
|
||||
|
||||
if not os.path.exists(settings['zimit.zimwriterfs_bin']):
|
||||
raise OSError('%s does not exist.' % settings['zimit.zimwriterfs_bin'])
|
||||
|
||||
config.update({
|
||||
'bin': settings['zimit.zimwriterfs_bin'],
|
||||
'location': location,
|
||||
'output': 'test.zim',
|
||||
'icon': 'favicon.ico',
|
||||
'publisher': settings.get('zimit.default_author', DEFAULT_AUTHOR),
|
||||
})
|
||||
# Spawn zimwriterfs with the correct options.
|
||||
p = spawn(('{bin} -w "{welcome}" -l "{language}" -t "{title}"'
|
||||
' -d "{description}" -f {icon} -c "{author}"'
|
||||
' -p "{publisher}" {location} {output}').format(**config))
|
||||
p.wait()
|
||||
webpage = Service(name='website', path='/website')
|
||||
|
||||
|
||||
class WebSiteSchema(MappingSchema):
|
||||
@ -70,12 +18,11 @@ class WebSiteSchema(MappingSchema):
|
||||
location="body", type='str')
|
||||
|
||||
|
||||
webpage = Service(name='website', path='/website')
|
||||
|
||||
|
||||
@webpage.post(schema=WebSiteSchema)
|
||||
def crawl_new_website(request):
|
||||
request.queue.enqueue(zim_it, request.validated,
|
||||
request.registry.settings, timeout=1800)
|
||||
request.queue.enqueue(
|
||||
request.client.create_zim_from_website,
|
||||
request.validated,
|
||||
timeout=1800)
|
||||
request.response.status_code = 201
|
||||
return {'success': True}
|
||||
|
72
zimit/worker.py
Normal file
72
zimit/worker.py
Normal file
@ -0,0 +1,72 @@
|
||||
import os
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pyramid_mailer import Mailer
|
||||
from slugify import slugify
|
||||
|
||||
from zimit.messages import ZimReadyMessage
|
||||
|
||||
|
||||
HTTRACK_BIN = "/usr/bin/httrack"
|
||||
DEFAULT_AUTHOR = "BSF"
|
||||
|
||||
|
||||
def spawn(cmd):
|
||||
"""Quick shortcut to spawn a command on the filesystem"""
|
||||
return subprocess.Popen(shlex.split(cmd))
|
||||
|
||||
|
||||
class ZimCreator(object):
|
||||
|
||||
def __init__(self, settings):
|
||||
if 'zimit.zimwriterfs_bin' not in settings:
|
||||
raise ValueError('Please define zimit.zimwriterfs_bin config.')
|
||||
|
||||
if not os.path.exists(settings['zimit.zimwriterfs_bin']):
|
||||
msg = '%s does not exist.' % settings['zimit.zimwriterfs_bin']
|
||||
raise OSError(msg)
|
||||
|
||||
httrack_bin = settings.get('zimit.httrack_bin', HTTRACK_BIN)
|
||||
if not os.path.exists(httrack_bin):
|
||||
raise OSError('%s does not exist.' % httrack_bin)
|
||||
|
||||
self.zimwriterfs_bin = settings.get('zimit.zimwriterfs_bin')
|
||||
self.httrack_bin = httrack_bin
|
||||
self.author = settings.get('zimit.default_author', DEFAULT_AUTHOR)
|
||||
self.settings = settings
|
||||
|
||||
def download_website(self, url):
|
||||
path = tempfile.mkdtemp("website")
|
||||
p = spawn("%s --path %s %s" % (self.httrack_bin, path, url))
|
||||
p.wait()
|
||||
shutil.copy('./favicon.ico', path)
|
||||
return path
|
||||
|
||||
def create_zim(self, html_location, config):
|
||||
zim_file = "{slug}.zim".format(slug=slugify(config['url']))
|
||||
config.update({
|
||||
'bin': self.zimwriterfs_bin,
|
||||
'location': html_location,
|
||||
'output': zim_file,
|
||||
'icon': 'favicon.ico',
|
||||
'publisher': self.author,
|
||||
})
|
||||
|
||||
# Spawn zimwriterfs with the correct options.
|
||||
p = spawn(('{bin} -w "{welcome}" -l "{language}" -t "{title}"'
|
||||
' -d "{description}" -f {icon} -c "{author}"'
|
||||
' -p "{publisher}" {location} {output}').format(**config))
|
||||
p.wait()
|
||||
return zim_file
|
||||
|
||||
def send_email(self, email, zim_file):
|
||||
mailer = Mailer.from_settings(self.settings)
|
||||
msg = ZimReadyMessage(email, zim_file)
|
||||
mailer.send_immediately(msg)
|
||||
|
||||
def create_zim_from_website(self, config):
|
||||
location = self.download_website(config['url'])
|
||||
zim_file = self.create_zim(location, config)
|
||||
self.send_email(config['email'], zim_file)
|
Loading…
x
Reference in New Issue
Block a user