Skip to content

Commit fe6256b

Browse files
committed
add support for fastapi
- support for fastapi - first endpoint
1 parent 04c3a82 commit fe6256b

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ mp3_link="https://anchor.fm/s/7d083a4/podcast/play/64348045/https%3A%2F%2Fd3ctxl
144144
tstbtc transcribe $mp3_link --loc "stephan-livera-podcast" --title 'SLP455 Anant Tapadia - Single Sig or Multi Sig?' --date '2023-02-01' --tags 'multisig' --speakers 'Anant Tapadia' --speakers 'Stephan Livera' --category 'podcast'
145145
```
146146

147+
## Running as a Server
148+
149+
In addition to running as a CLI application, you can also run tstbtc as a server.
150+
This allows you to interact with the transcription functionalities via HTTP requests.
151+
152+
This is WIP
153+
154+
### Starting the Server
155+
156+
To start the server, navigate to the application directory and run:
157+
158+
```sh
159+
fastapi run server.py
160+
```
161+
162+
The server will be accessible at http://localhost:8000.
163+
147164
## Testing
148165

149166
To run the unit tests

app/media_processor.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import soundfile as sf
33
import os
44
import ffmpeg
5+
import yt_dlp
56

67
from app import (
78
logging,
@@ -86,3 +87,23 @@ def convert_to_mp3(self, input_path, output_path=None):
8687
except ffmpeg.Error as e:
8788
logger.error(f"Error converting {input_path} to mp3: {e}")
8889
raise Exception(f"Error converting {input_path} to mp3: {e}")
90+
91+
def get_youtube_video_url(self, youtube_url):
92+
"""
93+
Extracts and returns the direct URL of a YouTube video, allowing it
94+
to be played directly without going through the YouTube platform.
95+
"""
96+
ydl_opts = {
97+
'format': 'best',
98+
'quiet': True,
99+
'no_warnings': True,
100+
}
101+
102+
try:
103+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
104+
info_dict = ydl.extract_info(youtube_url, download=False)
105+
video_url = info_dict.get("url", None)
106+
return video_url
107+
except Exception as e:
108+
logger.error(f"Error extracting video URL: {e}")
109+
return None

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ flake8==6.0.0
1515
isort==5.12.0
1616
feedparser==6.0.10
1717
PyYAML==6.0.1
18-
librosa==0.10.2.post1
18+
librosa==0.10.2.post1
19+
fastapi==0.111.0

server.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
from fastapi import FastAPI, HTTPException
3+
from fastapi.middleware.cors import CORSMiddleware
4+
from pydantic import BaseModel
5+
6+
from app.logging import configure_logger, get_logger
7+
from app.media_processor import MediaProcessor
8+
9+
logger = get_logger()
10+
app = FastAPI()
11+
12+
origins = [
13+
"http://localhost:3000",
14+
"https://staging-review.btctranscripts.com/",
15+
"https://review.btctranscripts.com/"
16+
]
17+
18+
app.add_middleware(
19+
CORSMiddleware,
20+
allow_origins=origins,
21+
allow_credentials=True,
22+
allow_methods=["*"], # Allows all methods
23+
allow_headers=["*"], # Allows all headers
24+
)
25+
26+
class VideoURLRequest(BaseModel):
27+
youtube_url: str
28+
29+
@app.post("/media/youtube-video-url")
30+
async def youtube_video_url(request: VideoURLRequest):
31+
"""Extract the direct video URL from a YouTube link"""
32+
try:
33+
configure_logger(log_level=logging.INFO)
34+
processor = MediaProcessor()
35+
video_url = processor.get_youtube_video_url(request.youtube_url)
36+
if video_url:
37+
return {"status": "success", "video_url": video_url}
38+
else:
39+
error_msg = f"Failed to extract the video URL for {request.youtube_url}."
40+
logger.error(error_msg)
41+
raise HTTPException(status_code=500, detail=error_msg)
42+
except Exception as e:
43+
logger.error(f"Error extracting video URL: {e}")
44+
raise HTTPException(status_code=500, detail=str(e))

0 commit comments

Comments
 (0)