File size: 3,119 Bytes
936d43f b4cb4da 936d43f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from smolagents import Tool
class FlashDataTool(Tool):
name = 'youtube_download'
description = 'Download YouTube video, audio or clips to your own S3 or other supported cloud storage. Submit a job once, then read its status and delivery manifest. Requires your own writable storage and permission for your intended use. Downloads are asynchronous and charged by delivered data volume; storage charges are separate. Uses paid FlashData API credits. Never automatically retry an uncertain submission; check Jobs first.'
inputs = {'job_id': {'description': 'Required for status/result. ID returned by a previous submit.',
'nullable': True,
'type': 'string'},
'operation': {'description': 'submit starts one paid download; status or result reads an existing '
'job without resubmitting.',
'type': 'string'},
'parameters': {'description': 'Submit only: video_id and media options from input-schema.json. '
'Never include storage credentials.',
'nullable': True,
'type': 'object'}}
output_type = 'object'
def forward(self, operation: str, parameters: dict | None = None, job_id: str | None = None) -> dict:
import os
api_key = os.environ.get('FLASHDATA_API_KEY', '')
storage_json = os.environ.get('FLASHDATA_DOWNLOAD_STORAGE', '')
import json
from urllib.parse import urlsplit
from langchain_flashdata import FlashDataYouTubeDownload, FlashDataDownloadJob, FlashDataDownloadResult
if operation not in ('submit', 'status', 'result'):
raise ValueError('operation must be submit, status or result.')
if operation != 'submit':
if parameters:
raise ValueError('Status/result reads take job_id only; omit parameters.')
reader = FlashDataDownloadJob if operation == 'status' else FlashDataDownloadResult
return reader(api_key=api_key).invoke({'job_id': job_id})
if job_id:
raise ValueError('Submit does not accept job_id. To read an existing job, select status or result.')
if not isinstance(parameters, dict):
raise ValueError('Submit requires a parameters object with video_id and media options.')
try:
storage = json.loads(storage_json)
if not isinstance(storage, dict):
raise ValueError()
endpoint = storage.get('endpoint_url')
if endpoint:
url = urlsplit(endpoint)
if (url.scheme != 'https' or not url.hostname or url.username or url.password
or url.query or url.fragment or url.port not in (None, 443)):
raise ValueError()
except (TypeError, ValueError):
raise ValueError('Configure valid customer storage JSON; endpoints must be HTTPS without credentials, query or fragment.') from None
return FlashDataYouTubeDownload(api_key=api_key, download_storage=storage_json).invoke(parameters)
|