# Path: webalyzer/collected/models.py
# class Screenshot(models.Model):
#     page = models.ForeignKey(Page)
#     file = ImageField(upload_to='screenshots')
#     width = models.PositiveIntegerField()
#     height = models.PositiveIntegerField()
#     engine = models.CharField(max_length=100, default='slimerjs')
#     added = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def get(cls, page):
#         for each in cls.objects.filter(page=page).order_by('-added'):
#             return each
# 
# Path: webalyzer/collector/models.py
# class Page(models.Model):
#     domain = models.CharField(max_length=100, db_index=True)
#     url = models.URLField()
#     html = models.TextField()
#     size = models.PositiveIntegerField(default=0)
#     title = models.TextField(null=True)
#     added = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
#     source_hash = models.IntegerField(default=0, db_index=True)
# 
#     def __unicode__(self):
#         return self.url
# 
#     def __repr__(self):
#         return '<%s: %s %.2fKb>' % (
#             self.__class__.__name__,
#             self.url,
#             len(self.html) / 1024.0
#         )
# 
#     def __len__(self):
#         return len(self.html)


import os
import subprocess
import urlparse
import shutil
import tempfile
from contextlib import contextmanager
from lxml import etree
from lxml.cssselect import CSSSelector
from celery import shared_task
from django.core.files import File
from webalyzer.collected.models import Screenshot
from webalyzer.collector.models import Page
                root,
                pretty_print=False,
                method='html',
                encoding='utf-8',
            ))

        # that 'screenshot.js' file is in the same directory as this file
        _here = os.path.dirname(__file__)
        width, height = 1400, 900
        cmd = [
            'phantomjs',
            '--ignore-ssl-errors=yes',
            os.path.join(_here, 'screenshot.js'),
            html_file,
            png_file,
            str(width),
            str(height)
        ]
        # print cmd
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
        stdout, stderr = process.communicate()
        # print (stdout, stderr)
        if not os.path.isfile(png_file):
            raise Exception("No PNG file created")

        with open(png_file, 'rb') as f:

# Path: webalyzer/collected/models.py
# class Screenshot(models.Model):
#     page = models.ForeignKey(Page)
#     file = ImageField(upload_to='screenshots')
#     width = models.PositiveIntegerField()
#     height = models.PositiveIntegerField()
#     engine = models.CharField(max_length=100, default='slimerjs')
#     added = models.DateTimeField(auto_now_add=True)
# 
#     @classmethod
#     def get(cls, page):
#         for each in cls.objects.filter(page=page).order_by('-added'):
#             return each
# 
# Path: webalyzer/collector/models.py
# class Page(models.Model):
#     domain = models.CharField(max_length=100, db_index=True)
#     url = models.URLField()
#     html = models.TextField()
#     size = models.PositiveIntegerField(default=0)
#     title = models.TextField(null=True)
#     added = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
#     source_hash = models.IntegerField(default=0, db_index=True)
# 
#     def __unicode__(self):
#         return self.url
# 
#     def __repr__(self):
#         return '<%s: %s %.2fKb>' % (
#             self.__class__.__name__,
#             self.url,
#             len(self.html) / 1024.0
#         )
# 
#     def __len__(self):
#         return len(self.html)


# Path: webalyzer/collected/tasks.py
import os
import subprocess
import urlparse
import shutil
import tempfile
from contextlib import contextmanager
from lxml import etree
from lxml.cssselect import CSSSelector
from celery import shared_task
from django.core.files import File
from webalyzer.collected.models import Screenshot
from webalyzer.collector.models import Page

                root,
                pretty_print=False,
                method='html',
                encoding='utf-8',
            ))

        # that 'screenshot.js' file is in the same directory as this file
        _here = os.path.dirname(__file__)
        width, height = 1400, 900
        cmd = [
            'phantomjs',
            '--ignore-ssl-errors=yes',
            os.path.join(_here, 'screenshot.js'),
            html_file,
            png_file,
            str(width),
            str(height)
        ]
        # print cmd
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
        stdout, stderr = process.communicate()
        # print (stdout, stderr)
        if not os.path.isfile(png_file):
            raise Exception("No PNG file created")

        with open(png_file, 'rb') as f:

            Screenshot.objects.create(