示例#1
0
文件: tasks.py 项目: rdavydov/demozoo
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
        if prod_link.production.screenshots.count():
            return  # don't create a screenshot if there's one already

        production_id = prod_link.production_id
        url = prod_link.download_url
        download, file_content = fetch_url(url)
        buf = cStringIO.StringIO(file_content)

        if prod_link.is_zip_file():
            z = zipfile.ZipFile(buf, 'r')
            # catalogue the zipfile contents if we don't have them already
            if not download.archive_members.all():
                download.log_zip_contents(z)
            # select the archive member to extract a screenshot from, if we don't have
            # a candidate already
            if not prod_link.file_for_screenshot:
                file_for_screenshot = download.select_screenshot_file()
                if file_for_screenshot:
                    prod_link.file_for_screenshot = file_for_screenshot
                    prod_link.is_unresolved_for_screenshotting = False
                else:
                    prod_link.is_unresolved_for_screenshotting = True
                prod_link.save()

            image_extension = prod_link.file_for_screenshot.split(
                '.')[-1].lower()
            if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
                # we encode the filename as iso-8859-1 before retrieving it, because we
                # decoded it that way on insertion into the database to ensure that it had
                # a valid unicode string representation - see mirror/models.py
                member_buf = cStringIO.StringIO(
                    z.read(prod_link.file_for_screenshot.encode('iso-8859-1')))
                z.close()
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            else:  # image is not a usable format
                z.close()
                return
        else:
            img = PILConvertibleImage(buf, name_hint=url.split('/')[-1])

        screenshot = Screenshot(production_id=production_id,
                                source_download_id=download.id)
        u = download.sha1
        basename = u[0:2] + '/' + u[2:4] + '/' + u[4:8] + '.pl' + str(
            production_link_id) + '.'
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass
示例#2
0
def import_screenshot(production_id, janeway_id, url, suffix):
    blob = fetch_origin_url(url)
    sha1 = blob.sha1
    img = PILConvertibleImage(blob.as_io_buffer(), name_hint=blob.filename)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.jw' + str(
        janeway_id) + suffix + '.'

    screenshot = Screenshot(production_id=production_id,
                            data_source='janeway',
                            janeway_id=janeway_id,
                            janeway_suffix=suffix)
    upload_original(img, screenshot, basename, reduced_redundancy=False)
    upload_standard(img, screenshot, basename)
    upload_thumb(img, screenshot, basename)
    screenshot.save()
示例#3
0
def import_screenshot(production_id, janeway_id, url, suffix):
    blob = fetch_origin_url(url)
    sha1 = blob.sha1
    img = PILConvertibleImage(blob.as_io_buffer(), name_hint=blob.filename)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.jw' + str(
        janeway_id) + suffix + '.'

    screenshot = Screenshot(production_id=production_id,
                            data_source='janeway',
                            janeway_id=janeway_id,
                            janeway_suffix=suffix)
    upload_standard(img, screenshot, basename)
    upload_thumb(img, screenshot, basename)
    # leave upload_original until last to prevent things screwing up if the storage
    # closes the original file handle
    upload_original(img, screenshot, basename)
    screenshot.save()
示例#4
0
文件: tasks.py 项目: rdavydov/demozoo
def create_screenshot_from_remote_file(url, production_id):
    try:
        download, file_content = fetch_url(url)
        screenshot = Screenshot(production_id=production_id,
                                source_download_id=download.id)

        buf = cStringIO.StringIO(file_content)
        img = PILConvertibleImage(buf, name_hint=url.split('/')[-1])

        u = download.sha1
        basename = u[0:2] + '/' + u[2:4] + '/' + u[4:8] + '.p' + str(
            production_id) + '.'
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

    except (urllib2.URLError, FileTooBig):
        # oh well.
        pass
示例#5
0
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        return

    if prod_link.production.screenshots.count():
        # don't create a screenshot if there's one already
        if prod_link.is_unresolved_for_screenshotting:
            prod_link.is_unresolved_for_screenshotting = False
            prod_link.save()
        return

    if prod_link.has_bad_image:
        return  # don't create a screenshot if a previous attempt has failed during image processing

    production_id = prod_link.production_id
    url = prod_link.download_url
    blob = fetch_link(prod_link)
    sha1 = blob.sha1

    if prod_link.is_zip_file():
        # select the archive member to extract a screenshot from, if we don't have
        # a candidate already
        archive_members = ArchiveMember.objects.filter(archive_sha1=sha1)
        if not prod_link.file_for_screenshot:
            file_for_screenshot = select_screenshot_file(archive_members)
            if file_for_screenshot:
                prod_link.file_for_screenshot = file_for_screenshot
                prod_link.is_unresolved_for_screenshotting = False
            else:
                prod_link.is_unresolved_for_screenshotting = True
            prod_link.save()

        image_extension = prod_link.file_for_screenshot.split('.')[-1].lower()
        if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
            z = None
            try:
                z = blob.as_zipfile()
                # decode the filename as stored in the db
                filename = unpack_db_zip_filename(
                    prod_link.file_for_screenshot)

                member_buf = io.BytesIO(z.read(filename))
            except zipfile.BadZipfile:
                prod_link.has_bad_image = True
                prod_link.save()
                if z:  # pragma: no cover
                    z.close()
                return

            z.close()
            try:
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            except IOError:
                prod_link.has_bad_image = True
                prod_link.save()
                return
        else:  # image is not a usable format
            return
    else:
        try:
            img = PILConvertibleImage(blob.as_io_buffer(),
                                      name_hint=url.split('/')[-1])
        except IOError:
            prod_link.has_bad_image = True
            prod_link.save()
            return

    screenshot = Screenshot(production_id=production_id)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.pl' + str(
        production_link_id) + '.'
    try:
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        # leave original until last, because if it's already a websafe format it'll just return
        # the original file handle, and the storage backend might close the file after uploading
        # which screws with PIL's ability to create resized versions...
        upload_original(img, screenshot, basename)
    except IOError:  # pragma: no cover
        prod_link.has_bad_image = True
        prod_link.save()
        return
    screenshot.save()
示例#6
0
 def test_str(self):
     pondlife = Production.objects.get(title='Pondlife')
     screenshot = Screenshot(production=pondlife,
                             original_url='http://example.com/pondlife.png')
     self.assertEqual(str(screenshot),
                      "Pondlife - http://example.com/pondlife.png")
示例#7
0
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        return

    if prod_link.production.screenshots.count():
        # don't create a screenshot if there's one already
        if prod_link.is_unresolved_for_screenshotting:
            prod_link.is_unresolved_for_screenshotting = False
            prod_link.save()
        return

    if prod_link.has_bad_image:
        return  # don't create a screenshot if a previous attempt has failed during image processing

    production_id = prod_link.production_id
    url = prod_link.download_url
    blob = fetch_link(prod_link)
    sha1 = blob.sha1

    if prod_link.is_zip_file():
        # select the archive member to extract a screenshot from, if we don't have
        # a candidate already
        archive_members = ArchiveMember.objects.filter(archive_sha1=sha1)
        if not prod_link.file_for_screenshot:
            file_for_screenshot = select_screenshot_file(archive_members)
            if file_for_screenshot:
                prod_link.file_for_screenshot = file_for_screenshot
                prod_link.is_unresolved_for_screenshotting = False
            else:
                prod_link.is_unresolved_for_screenshotting = True
            prod_link.save()

        image_extension = prod_link.file_for_screenshot.split('.')[-1].lower()
        if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
            z = blob.as_zipfile()
            # we encode the filename as iso-8859-1 before retrieving it, because we
            # decoded it that way on insertion into the database to ensure that it had
            # a valid unicode string representation - see mirror/models.py
            try:
                member_buf = cStringIO.StringIO(
                    z.read(prod_link.file_for_screenshot.encode('iso-8859-1')))
            except zipfile.BadZipfile:
                prod_link.has_bad_image = True
                prod_link.save()
                z.close()
                return

            z.close()
            try:
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            except IOError:
                prod_link.has_bad_image = True
                prod_link.save()
                return
        else:  # image is not a usable format
            return
    else:
        try:
            img = PILConvertibleImage(blob.as_io_buffer(),
                                      name_hint=url.split('/')[-1])
        except IOError:
            prod_link.has_bad_image = True
            prod_link.save()
            return

    screenshot = Screenshot(production_id=production_id)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.pl' + str(
        production_link_id) + '.'
    try:
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
    except IOError:
        prod_link.has_bad_image = True
        prod_link.save()
        return
    screenshot.save()