Created
September 20, 2019 05:56
-
-
Save monkut/5970efef6c72de29dc83364ed25ec314 to your computer and use it in GitHub Desktop.
a python parser for `mediainfo` output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import subprocess | |
from xml.etree import ElementTree | |
import boto3 | |
from . import settings | |
S3 = boto3.client( | |
's3', | |
endpoint_url=settings.AWS_SERVICE_ENDPOINTS['s3'], | |
) | |
DEFAULT_EXPIRESIN_SECONDS = 300 | |
def get_presigned_url(bucket, key, expires_in_seconds: int = DEFAULT_EXPIRESIN_SECONDS) -> str: | |
""" | |
Generate a pre-signed URL | |
""" | |
presigned_url = S3.generate_presigned_url( | |
'get_object', | |
Params={'Bucket': bucket, 'Key': key}, | |
ExpiresIn=expires_in_seconds | |
) | |
return presigned_url | |
def _convert_xml_to_dict(xmldata: str) -> dict: | |
def parse_xml_to_dict(xml) -> dict: | |
children = [] | |
for child in xml: | |
response = {} | |
key = child.tag.strip() | |
# remove namespace | |
key = key.split('}', 1)[-1] | |
if len(list(child)) > 0: | |
response[key] = {} | |
response[key]['attrib'] = child.attrib | |
values = parse_xml_to_dict(child) | |
response[key]['values'] = values | |
else: | |
response[key] = child.text or '' | |
children.append(response) | |
return children | |
xml = ElementTree.fromstring(xmldata) | |
parsed = parse_xml_to_dict(xml) | |
return parsed | |
def get_mediainfo_from_url(url: str) -> dict: | |
output = subprocess.check_output(["./mediainfo", "--full", "--output=XML", url]) | |
return json.loads(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment