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
def handle(self, *args, **options): image_dir = args[0] connection = pymysql.connect(user="******", db="zxdemo") cur = connection.cursor() cur.execute("SELECT item_id, img from screenshot") for item_id, image_filename in cur: try: production = Production.objects.get(zxdemo_id=item_id) except Production.DoesNotExist, Production.MultipleObjectsReturned: print "Skipping production %s - missing or duplicate entries" continue print "%s - %s" % (production.id, production.title) screenshot = Screenshot(production=production) full_filename = os.path.join(image_dir, image_filename) f = File(open(full_filename)) screenshot.original.save(os.path.basename(image_filename), f)
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