ExamplescriptadvancedRunnableguided-flow
Service
Runnable example (advanced) for script using requests.
Key Facts
- Level
- advanced
- Runtime
- Python • Requests
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
service.py
python
import os
from requests import Session
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import (
TextFormatter,
)
from youtube_transcript_api.proxies import WebshareProxyConfig
from .utils import extract_video_id
class YouTubeTranscriptService:
"""Service class wrapping youtube-transcript-api with proxy support."""
def __init__(self, use_proxy: bool = True):
self.api = self._create_api(use_proxy)
def _create_api(self, use_proxy: bool) -> YouTubeTranscriptApi:
"""Create API instance with optional proxy support."""
if not use_proxy:
return YouTubeTranscriptApi()
proxy_username = os.getenv("WEBSHARE_USERNAME")
proxy_password = os.getenv("WEBSHARE_PASSWORD")
if proxy_username and proxy_password:
proxy_config = WebshareProxyConfig(
proxy_username=proxy_username,
proxy_password=proxy_password,
)
http_client = Session()
http_client.proxies = proxy_config.to_requests_dict()
return YouTubeTranscriptApi(http_client=http_client)
return YouTubeTranscriptApi()
def fetch(
self,
video_url_or_id: str,
):
"""Fetch transcript."""
video_id = extract_video_id(video_url_or_id)
return self.api.fetch(video_id)
def get_transcript_text(
self,
video_url_or_id: str,
) -> str:
"""Get transcript as plain text."""
transcript = self.fetch(video_url_or_id)
formatter = TextFormatter()
return formatter.format_transcript(transcript)
Related principles
- P6visibilityExpose meaningful operational state, not internal complexityPresent the state of the system in language and structures that are relevant to the user, rather than exposing low-level internals that do not support action or understanding.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P9orchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →