Example #1
0
    def take_screenshot(self):
        """Takes a screenshot and creates a new ElectionScreenshot object.
        Returns either None or the ElectionScreenshot object."""
        local_timezone = pytz.timezone(settings.TIME_ZONE)
        now = pytz.datetime.datetime.now(tz=local_timezone)
        previous = self.latest_screenshot()

        def upload_or_link(tmpfile):
            """This is called from inside screenshot_url to avoid
            uploading duplicate files to S3 when the page has not
            changed."""
            with file(tmpfile) as fil_ro:
                bytes = fil_ro.read()
                image_sha1 = hashlib.sha1(bytes).hexdigest()
                if previous and previous.image_sha1 == image_sha1:
                    return (previous.image_sha1, previous.image_url)
                else:
                    filename = "{hash}/{hash}_{timestamp}.png".format(
                        hash=self.url_sha1, timestamp=abbrev_isoformat(now))
                    new_url = upload_image(tmpfile, filename, 'image/png')
                    if new_url is None:
                        return None
                    return (image_sha1, new_url)

        screenshot_info = screenshot_url(self.url, upload_or_link)
        if screenshot_info is not None:
            (image_sha1, image_url) = screenshot_info
            screenshot = ElectionScreenshot.objects.create(
                election_url=self,
                timestamp=now,
                image_url=image_url,
                image_sha1=image_sha1)
            return screenshot
        else:
            return None
Example #2
0
    def take_screenshot(self):
        """Takes a screenshot and creates a new ElectionScreenshot object.
        Returns either None or the ElectionScreenshot object."""
        local_timezone = pytz.timezone(settings.TIME_ZONE)
        now = pytz.datetime.datetime.now(tz=local_timezone)
        previous = self.latest_screenshot()

        def upload_or_link(tmpfile):
            """This is called from inside screenshot_url to avoid
            uploading duplicate files to S3 when the page has not
            changed."""
            with file(tmpfile) as fil_ro:
                bytes = fil_ro.read()
                image_sha1 = hashlib.sha1(bytes).hexdigest()
                if previous and previous.image_sha1 == image_sha1:
                    return (previous.image_sha1, previous.image_url)
                else:
                    filename = "{hash}/{hash}_{timestamp}.png".format(hash=self.url_sha1,
                                                                      timestamp=abbrev_isoformat(now))
                    new_url = upload_image(tmpfile, filename, 'image/png')
                    if new_url is None:
                        return None
                    return (image_sha1, new_url)

        screenshot_info = screenshot_url(self.url, upload_or_link)
        if screenshot_info is not None:
            (image_sha1, image_url) = screenshot_info
            screenshot = ElectionScreenshot.objects.create(election_url=self,
                                                           timestamp=now,
                                                           image_url=image_url,
                                                           image_sha1=image_sha1)
            return screenshot
        else:
            return None
Example #3
0
    def take_screenshot(self):
        local_timezone = pytz.timezone(settings.TIME_ZONE)

        screenshot_info = screenshot_url(self.url)
        if screenshot_info is not None:
            (image_sha1, image_url) = screenshot_info
            screenshot = ElectionScreenshot(election_url=self,
                                            timestamp=pytz.datetime.datetime.now(tz=local_timezone),
                                            image_url=image_url,
                                            image_sha1=image_sha1)
            screenshot.save()
            return screenshot
        else:
            return None