# 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



@contextmanager
def tmpdir(*args, **kwargs):
    dir_ = tempfile.mkdtemp(*args, **kwargs)
    try:
        yield dir_
    finally:
        # print "DELETE", dir_
        shutil.rmtree(dir_)


@shared_task
def find_page_title(page_id):
    page = Page.objects.get(id=page_id)
    parser = etree.HTMLParser()
    html = page.html.strip()
    tree = etree.fromstring(html, parser).getroottree()
    dom = tree.getroot()

    for element in CSSSelector('title')(dom):
        page.title = element.text.strip()
        page.save()
        return


@shared_task
def generate_screenshot(page_id):
    print "Generating screenshot for", page_id

# 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




@contextmanager
def tmpdir(*args, **kwargs):
    dir_ = tempfile.mkdtemp(*args, **kwargs)
    try:
        yield dir_
    finally:
        # print "DELETE", dir_
        shutil.rmtree(dir_)


@shared_task
def find_page_title(page_id):
    page = Page.objects.get(id=page_id)
    parser = etree.HTMLParser()
    html = page.html.strip()
    tree = etree.fromstring(html, parser).getroottree()
    dom = tree.getroot()

    for element in CSSSelector('title')(dom):
        page.title = element.text.strip()
        page.save()
        return


@shared_task
def generate_screenshot(page_id):
    print "Generating screenshot for", page_id

    page = Page.objects.get(id=page_id)