-
-
Save obviyus/f1edbe266121d0ef12e8f393fa910a44 to your computer and use it in GitHub Desktop.
Single file Python server for a YouTube transcript API
This file contains hidden or 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
cat /tmp/t.py | |
# /// script | |
# requires-python = ">=3.12" | |
# dependencies = [ | |
# "flask", | |
# "youtube-transcript-api", | |
# ] | |
# /// | |
from flask import Flask, request, jsonify, Response | |
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled | |
app = Flask(__name__) | |
@app.route('/t') | |
def get_transcript(): | |
video_id = request.args.get('v') | |
fmt = request.args.get('format', 'json') | |
if not video_id: | |
return jsonify({'error': 'Missing video id parameter (v)'}), 400 | |
try: | |
transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
if fmt.lower() == 'markdown': | |
# Combine text segments into markdown paragraphs | |
texts = [entry['text'] for entry in transcript] | |
md = "\n\n".join(texts) | |
return Response(md, mimetype='text/markdown') | |
# default JSON response | |
return jsonify(transcript) | |
except NoTranscriptFound: | |
return jsonify({'error': 'No transcript found for this video'}), 404 | |
except TranscriptsDisabled: | |
return jsonify({'error': 'Transcripts are disabled for this video'}), 403 | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=1206) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment