GithubHelp home page GithubHelp logo

makinacorpus / django-screamshot Goto Github PK

View Code? Open in Web Editor NEW
106.0 38.0 35.0 173 KB

Web pages capture using Django & CasperJS

License: GNU Lesser General Public License v3.0

Python 88.23% JavaScript 11.22% HTML 0.54%

django-screamshot's Introduction

django-screamshot

Build Status

Latest PyPI version

Number of PyPI downloads

Number of Git Hub downloads

Format

License

django-screamshot is a very naive implementation of Web pages capture with CasperJS (aaAAaah!, phantomjs:))

(See the issues pages for more details about what remains to be done.)

Checkout screamshotter, the simplest Django project powered by django-screamshot.

INSTALL

First make sure you have either the casperjs or phantomjs command in your PATH, using related installation instructions:

Then install the egg :

pip install django-screamshot

USAGE

  • Add screamshot to your INSTALLED_APPS

As a screenshot Web API

Add it to your project URLs :

urlpatterns = patterns('',
    ...
    url(r'^capture/$',  include('screamshot.urls', namespace='screamshot', app_name='screamshot')),
)

You can then obtain a screenshot using the following GET parameters :

url

The website URL to capture. This can be a fully qualified URL, or the name of a URL to be reversed in your Django project. Note: do not forget to encode the url.

selector

CSS3 selector. It will restrict the screenshot to the selected element.

method

HTTP method to be used (default: GET)

width

Viewport width (default: 1400)

height

Viewport height (default: 900)

data

HTTP data to be posted (default: {})

waitfor

CSS3 selector. The screenshot will be performed only once this selector is satisfied. Typical usage: if your page contains a heavy javascript processing, you can add a CSS class on an element when the processing is finished to make sure the screenshot will get the page properly rendered.

render

If render=html, it will return an HTML page containing the image and where the print diaplo box will be automatically opened.

size

Resize image (width x height, e.g: 500x500), need install PIL or Pillow.

crop

If true, then resulting image is cropped to match specified size.

For example : http://server/capture/?url=http://django-fr.org&selector=body&width=1024&height=768&size=500x500

As a template tag

You can include screenshots in your pages using a template tag. It will perform the capture and return the base64 version of the resulting image.

This is very useful if you don't want to expose the capture API publicly.

{% base64capture URL SELECTOR %}

For example, in a SVG template :

{% load screamshot %}
...

<image
   y="200"
   x="300"
   id="imagemap"
   xlink:href="data:{% base64capture "company:map" "#map" %}"
   width="640" />

If you run the capture server on a different instance, you can specify the root url for reversing (default is local) :

SCREAMSHOT_CONFIG = {
    'CAPTURE_ROOT_URL': 'http://127.0.0.1:8001',
}

As a library - render local Django template

Sometimes, you don't have access to the request object. A typical example would be creating a PDF receipt for a customer and emailing it. Both of these tasks can take a while, so it is natural to push them into some queue (like RabbitMQ). But if your pdf-rendering task get's executed, you don't have access to the request object. Don't worry - you can still use screamshot as a library. Here's how.

from screamshot.utils import render_template

# you can either render the template to a TemporaryFile:

with render_template('my-template.html', {'context': 'variables'}) as output:
    # do anything you want with the output
    # like attach it to email message, etc.
    print(output.name)

# or you can specify a path instead:
render_template('my-template.html',
    {'context': 'variables'},
    output='/home/you/rendering.png',
    format='png')

Please note, that in order to load your static files, screamshot will try to replace all STATIC_URL occurence with a local path to your static files (only if they are not hosted via https of course)

Customizing the page rendering

The CasperJS script appends the screamshot CSS class on the body element. You can easily customize the rendering for printing using this CSS marker in your CSS stylesheet:

.screamshot #navigation {
  display: none;
}
.screamshot #main {
  margin: 2em;
}

Capture views with authentication

You can use Basic HTTP authentication in your Django project, create a dedicated user for screenshots and capture the full URL with credentials (http://user:password@host/page/).

Alternatively, you can use a specific view decorator.

Define the authorized IP to capture your pages in your settings :

SCREAMSHOT_CONFIG = {
    'CAPTURE_ALLOWED_IPS': ('127.0.0.1',),
}

And use the provided decorator :

from screamshot.decorators import login_required_capturable


@login_required_capturable
def your_view(request):
    ...

Renderer command and CLI arguments

You can specify which renderer you would like to use, by setting the CAPTURE_METHOD setting. The default value is 'casperjs'. Possible values are 'casperjs' and 'phantomjs'.

SCREAMSHOT_CONFIG = {
    'CAPTURE_METHOD': 'phantomjs',
}

By default, we look for thr CasperJS/PhantomJS binary in the PATH environment variable (like which), but you can bypass this:

SCREAMSHOT_CONFIG = {
    'CASPERJS_CMD': '/home/you/Downloads/apps/casperjs',
    'PHANTOMJS_CMD': '/home/you/Downloads/apps/phantomjs'
}

Please note, that the CAPTURE_METHOD setting specifies which location would be evaluated, i.e. if you set CAPTURE_METHOD to 'phantomjs', PHANTOMJS_CMD would be evaluated.

You can also specify PhantomJS/CasperJS extra-args, such as

--disk-cache=true with the CLI_ARGS setting :

SCREAMSHOT_CONFIG = {
    'CLI_ARGS': ['--disk-cache=true', '--max-disk-cache-size=30000']
}

See related documentation on PhantomJS and CasperJS homepages.

You can also override the capture script. A default implementation uses capture script written for CasperJS. A default capture script for PhantomJS is also provided.

If you have your own script which you would like to use, specify it in CAPTURE_SCRIPT option.

SCREAMSHOT_CONFIG = {
    'CAPTURE_SCRIPT': '/home/you/scripts/capture.js',
}

You can add timeout corresponding to maximum time to wait for CSS3 selector (see waitfor option)

SCREAMSHOT_CONFIG = {
    'TIMEOUT': 7000 #ms 5000 by default,
}

Notes about runserver

If you want to test it using manage.py runserver, you won't be able to capture pages coming from the same instance.

Run it twice (on two ports) and configure CAPTURE_ROOT_URL.

AUTHORS

makinacom_

LICENSE

  • Lesser GNU Public License

django-screamshot's People

Contributors

atsnisov avatar ebrehault avatar fle avatar gabrieldumbrava avatar gmunozc avatar lepetittim avatar leplatrem avatar mo-mughrabi avatar mozillazg avatar nikolaik avatar ptitluc avatar submarcos avatar toudi avatar vinayinfo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-screamshot's Issues

Secure process calls

Make sure processes are always well terminated, on request interruption, server restart, etc.

Section of screenshot

Does this app enable the ability to take just a section of a screenshot and not the whole page?

Thanks,
Chris

CasperJS returned status code 8

Trying to run screamshot but all I can seem to produce is this:

"""
Exception Type: ImproperlyConfigured
Exception Value: CasperJS returned status code 8
Exception Location: /usr/local/lib/python2.6/dist-packages/screamshot/utils.py in casperjs_command, line 49
"""

I can't find any documentation on what that status code means or what I should do about it. Any advice would be greatly appreciated.

Too many PhantomJS process

In my application, at certain time i need to take screenshots of a lot of sites within minutes. The process appears to create a lof PhantomJS processes. This consumes a lot of memory and eventually causes my application to terminate. I am running screamshot as a background service.

My system details:
Amazaon instance.
4G RAM

Any idea on how I can resolve this.

Thanks in advance.

update PyPi version

Hi. Do you think that you could update the version which is on PyPi? The current code (on master branch) contains my fix for encoding the middleware template file to utf-8, while PyPi version (0.6.0) doesn't.

Thanks in advance!

add a parameter to control how to resize

add a parameter to control how to resize.

like this:

is it a good idea?

if it is, is there an another better parameter name for this function and better words to describe this parameter.

Django 3, Python 3.6 ImportError: cannot import name 'python_2_unicode_compatible'

Hi all,
Getting ImportError: cannot import name 'python_2_unicode_compatible'

File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:\code\TFSITE\venv\lib\site-packages\screamshot\models.py", line 8, in <module> from django.utils.encoding import python_2_unicode_compatible ImportError: cannot import name 'python_2_unicode_compatible'

This seems like a Django 3 issue due to deprecating pythong 2 API calls.

Is there potential to get a patch for this? or do you know of a work around?

thanks all.

local variable 'sys_path' referenced before assignment

Traceback (most recent call last):
File "manage.py", line 9, in
execute_from_command_line(sys.argv)
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/django/core/management/init.py", line 364, in execute_from_command_line
utility.execute()
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute
django.setup()
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/django/init.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/opt/circleci/python/2.7.11/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
File "/home/ubuntu/CCC/apps/campaign/models.py", line 19, in
from screamshot.utils import casperjs_capture
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/screamshot/utils.py", line 100, in
CASPERJS_CMD = casperjs_command()
File "/home/ubuntu/virtualenvs/venv-system/lib/python2.7/site-packages/screamshot/utils.py", line 80, in casperjs_command
msg = "%s binary cannot be found in PATH (%s)" % (method, sys_path)
UnboundLocalError: local variable 'sys_path' referenced before assignment

((and (cd "/home/ubuntu/CCC") (group (pipe (echo "'import json;from django.conf import settings;print('\''OUT: '\'' + settings.TEST_RUNNER)'") (python manage.py shell))))) returned exit code 1

Casperjs binary cannot be found in PATH

It is on PATH, I can use casperjs via command line.

I have installed casperjs with brew on Mac OS X

Still, screamshot doesn't find casperjs.

"which casperjs" gives:

/usr/local/bin/casperjs

"casperjs" gives:

CasperJS version 1.0.2 at /usr/local/Cellar/casperjs/1.0.2/libexec, using PhantomJS version 1.9.1

Usage: casperjs [options] script.[js|coffee] [script argument [script argument ...]]
casperjs [options] test [test path [test path ...]]
casperjs [options] selftest

Options:

--help Prints this help
--version Prints out CasperJS version

Read the docs http://casperjs.org/

My .bash_profile has:

export PATH=/usr/local/bin:$PATH

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.