def from_file(path, query, duration=None): media_type = guess_type(path) if media_type is MEDIA_TYPE_VIDEO: if duration is None: duration = get_video_duration(path) tmp = create_temporary_file(".snap.mp4") output_path = tmp.name subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait() elif media_type is MEDIA_TYPE_IMAGE: image = Image.open(path) draw = ImageDraw.Draw(image) font = ImageFont.truetype("aller-font.ttf", 28) draw.text((10, 10), 'Name:' + query['title'] + ' Artist:' + query['artist'], (255, 0, 0), font=font) del draw tmp = create_temporary_file(".jpg") output_path = tmp.name resize_image(image, output_path) if not duration: duration = DEFAULT_DURATION else: raise UnknownMediaType( "Could not determine media type of the file") return Snap(path=output_path, media_type=media_type, duration=duration)
def from_file(path, duration=None): media_type = guess_type(path) if media_type is MEDIA_TYPE_VIDEO: if duration is None: duration = get_video_duration(path) tmp = create_temporary_file(".snap.mp4") output_path = tmp.name subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait() elif media_type is MEDIA_TYPE_IMAGE: image = Image.open(path) tmp = create_temporary_file(".jpg") output_path = tmp.name resize_image(image, output_path) if duration is None: duration = DEFAULT_DURATION else: raise Exception, "Could not determine media type of the file" return Snap(path=output_path, media_type=media_type, duration=duration)
def from_file(path, duration = None): media_type = guess_type(path) if media_type is MEDIA_TYPE_VIDEO: if duration is None: duration = get_video_duration(path) tmp = create_temporary_file(".snap.mp4") output_path = tmp.name subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait() elif media_type is MEDIA_TYPE_IMAGE: image = Image.open(path) tmp = create_temporary_file(".jpg") output_path = tmp.name resize_image(image, output_path) if duration is None: duration = DEFAULT_DURATION else: raise Exception, "Could not determine media type of the file" return Snap(path = output_path, media_type = media_type, duration = duration)
def prepare_asset(request): data = request.POST or request.FORM or {} if 'model' in data: data = json.loads(data['model']) def get(key): val = data.get(key, '') if isinstance(val, unicode): return val.strip() elif isinstance(val, basestring): return val.strip().decode('utf-8') else: return val if all([ get('name'), get('uri') or (request.files.file_upload != ""), get('mimetype') ]): asset = { 'name': get('name'), 'mimetype': get('mimetype'), 'asset_id': get('asset_id'), 'is_enabled': get('is_enabled'), 'nocache': get('nocache'), } uri = get('uri') or False if not asset['asset_id']: asset['asset_id'] = uuid.uuid4().hex try: file_upload = request.files.file_upload filename = file_upload.filename except AttributeError: file_upload = None filename = None if filename and 'web' in asset['mimetype']: raise Exception( "Invalid combination. Can't upload a web resource.") if uri and filename: raise Exception( "Invalid combination. Can't select both URI and a file.") if uri and not uri.startswith('/'): if not validate_url(uri): raise Exception("Invalid URL. Failed to add asset.") else: asset['uri'] = uri else: asset['uri'] = uri if filename: asset['uri'] = path.join(settings['assetdir'], asset['asset_id']) with open(asset['uri'], 'w') as f: while True: chunk = file_upload.file.read(1024) if not chunk: break f.write(chunk) if "video" in asset['mimetype']: video_duration = get_video_duration(asset['uri']) if video_duration: asset['duration'] = int(video_duration.total_seconds()) else: asset['duration'] = 'N/A' else: # Crashes if it's not an int. We want that. asset['duration'] = int(get('duration')) if get('start_date'): asset['start_date'] = datetime.strptime( get('start_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['start_date'] = "" if get('end_date'): asset['end_date'] = datetime.strptime( get('end_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['end_date'] = "" if not asset['asset_id']: raise Exception if not asset['uri']: raise Exception return asset else: raise Exception( "Not enough information provided. Please specify 'name', 'uri', and 'mimetype'." )
def prepare_asset(request): req = Request(request.environ) data = None data = json.loads(req.form['model']) if 'model' in req.form else req.form def get(key): val = data.get(key, '') if isinstance(val, unicode): return val.strip() elif isinstance(val, basestring): return val.strip().decode('utf-8') else: return val if all([get('name'), get('uri') or req.files.get('file_upload'), get('mimetype')]): asset = { 'name': get('name'), 'mimetype': get('mimetype'), 'asset_id': get('asset_id'), 'is_enabled': get('is_enabled'), 'nocache': get('nocache'), } uri = get('uri') or False if not asset['asset_id']: asset['asset_id'] = uuid.uuid4().hex try: file_upload = req.files.get('file_upload') filename = file_upload.filename except AttributeError: file_upload = None filename = None if filename and 'web' in asset['mimetype']: raise Exception("Invalid combination. Can't upload a web resource.") if uri and filename: raise Exception("Invalid combination. Can't select both URI and a file.") if uri and not uri.startswith('/'): if not validate_url(uri): raise Exception("Invalid URL. Failed to add asset.") else: asset['uri'] = uri else: asset['uri'] = uri if filename: asset['uri'] = path.join(settings['assetdir'], asset['asset_id']) file_upload.save(asset['uri']) if "video" in asset['mimetype']: video_duration = get_video_duration(asset['uri']) if video_duration: asset['duration'] = int(video_duration.total_seconds()) else: asset['duration'] = 'N/A' else: # Crashes if it's not an int. We want that. asset['duration'] = int(get('duration')) if get('start_date'): asset['start_date'] = datetime.strptime(get('start_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['start_date'] = "" if get('end_date'): asset['end_date'] = datetime.strptime(get('end_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['end_date'] = "" if not asset['asset_id']: raise Exception if not asset['uri']: raise Exception return asset else: raise Exception("Not enough information provided. Please specify 'name', 'uri', and 'mimetype'.")
def prepare_asset(request): data = request.POST or request.FORM or {} if 'model' in data: data = json.loads(data['model']) def get(key): val = data.get(key, '') return val.strip() if isinstance(val, basestring) else val if all([get('name'), get('uri') or (request.files.file_upload != ""), get('mimetype')]): asset = { 'name': get('name'), 'mimetype': get('mimetype'), 'asset_id': get('asset_id'), 'is_enabled': get('is_enabled'), 'nocache': get('nocache'), } uri = get('uri') or False if not asset['asset_id']: asset['asset_id'] = uuid.uuid4().hex try: file_upload = request.files.file_upload filename = file_upload.filename except AttributeError: file_upload = None filename = None if filename and 'web' in asset['mimetype']: raise Exception("Invalid combination. Can't upload a web resource.") if uri and filename: raise Exception("Invalid combination. Can't select both URI and a file.") if uri and not uri.startswith('/'): if not validate_url(uri): raise Exception("Invalid URL. Failed to add asset.") else: asset['uri'] = uri else: asset['uri'] = uri if filename: asset['uri'] = path.join(settings['assetdir'], asset['asset_id']) with open(asset['uri'], 'w') as f: while True: chunk = file_upload.file.read(1024) if not chunk: break f.write(chunk) if "video" in asset['mimetype']: video_duration = get_video_duration(asset['uri']) if video_duration: asset['duration'] = int(video_duration.total_seconds()) else: asset['duration'] = 'N/A' else: # Crashes if it's not an int. We want that. asset['duration'] = int(get('duration')) if get('start_date'): asset['start_date'] = datetime.strptime(get('start_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['start_date'] = "" if get('end_date'): asset['end_date'] = datetime.strptime(get('end_date').split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset['end_date'] = "" if not asset['asset_id']: raise Exception if not asset['uri']: raise Exception return asset else: raise Exception("Not enough information provided. Please specify 'name', 'uri', and 'mimetype'.")
def prepare_asset(request): data = request.POST or request.FORM or {} if "model" in data: data = json.loads(data["model"]) def get(key): val = data.get(key, "") return val.strip() if isinstance(val, basestring) else val if all([get("name"), get("uri") or (request.files.file_upload != ""), get("mimetype")]): asset = { "name": get("name"), "mimetype": get("mimetype"), "asset_id": get("asset_id"), "is_enabled": get("is_enabled"), "nocache": get("nocache"), } uri = get("uri") or False if not asset["asset_id"]: asset["asset_id"] = uuid.uuid4().hex try: file_upload = request.files.file_upload filename = file_upload.filename except AttributeError: file_upload = None filename = None if filename and "web" in asset["mimetype"]: raise Exception("Invalid combination. Can't upload a web resource.") if uri and filename: raise Exception("Invalid combination. Can't select both URI and a file.") if uri and not uri.startswith("/"): if not validate_url(uri): raise Exception("Invalid URL. Failed to add asset.") else: asset["uri"] = uri else: asset["uri"] = uri if filename: asset["uri"] = path.join(settings["assetdir"], asset["asset_id"]) with open(asset["uri"], "w") as f: while True: chunk = file_upload.file.read(1024) if not chunk: break f.write(chunk) if "video" in asset["mimetype"]: video_duration = get_video_duration(asset["uri"]) if video_duration: asset["duration"] = int(video_duration.total_seconds()) else: asset["duration"] = "N/A" else: # Crashes if it's not an int. We want that. asset["duration"] = int(get("duration")) if get("start_date"): asset["start_date"] = datetime.strptime(get("start_date").split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset["start_date"] = "" if get("end_date"): asset["end_date"] = datetime.strptime(get("end_date").split(".")[0], "%Y-%m-%dT%H:%M:%S") else: asset["end_date"] = "" if not asset["asset_id"]: raise Exception if not asset["uri"]: raise Exception return asset else: raise Exception("Not enough information provided. Please specify 'name', 'uri', and 'mimetype'.")