Mubin1917 commited on
Commit
05116ba
1 Parent(s): ecd85b6

Update FC_tool_main.py

Browse files
Files changed (1) hide show
  1. FC_tool_main.py +34 -6
FC_tool_main.py CHANGED
@@ -24,6 +24,7 @@ import os
24
  import openai
25
  import json
26
  from typing import List, Dict, Any, Union, Type
 
27
  from youtube_transcript_api import YouTubeTranscriptApi
28
  from langchain_core.pydantic_v1 import BaseModel, Field
29
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
@@ -39,6 +40,7 @@ from langchain.memory import ConversationBufferWindowMemory
39
 
40
  # _ = load_dotenv(find_dotenv()) # read local .env file
41
  openai.api_key = os.getenv('OPENAI_API_KEY') #os.environ['OPENAI_API_KEY']
 
42
 
43
  def get_temperature():
44
  return 0 #Default value
@@ -108,7 +110,7 @@ class YouTubeTranscriptPointsExtractor:
108
  @staticmethod
109
  def _fetch_transcript(youtube_video_id: str) -> str:
110
  """
111
- Fetches the transcript for a YouTube video.
112
 
113
  Args:
114
  youtube_video_id (str): The ID of the YouTube video.
@@ -120,8 +122,21 @@ class YouTubeTranscriptPointsExtractor:
120
  Exception: If there's an error fetching the transcript.
121
  """
122
  try:
123
- transcript_json = YouTubeTranscriptApi.get_transcript(youtube_video_id)
124
- transcript_data = [f"{entry['start']:.2f}: {entry['text']} " for entry in transcript_json]
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  return "".join(transcript_data)
126
  except Exception as e:
127
  raise
@@ -265,9 +280,22 @@ class QuestionAnswerExtractor:
265
  Exception: If there's an error fetching the transcript.
266
  """
267
  try:
268
- transcript_json = YouTubeTranscriptApi.get_transcript(youtube_video_id)
269
- transcript_data = [entry['text'] for entry in transcript_json]
270
- return " ".join(transcript_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  except Exception as e:
272
  raise
273
 
 
24
  import openai
25
  import json
26
  from typing import List, Dict, Any, Union, Type
27
+ import requests
28
  from youtube_transcript_api import YouTubeTranscriptApi
29
  from langchain_core.pydantic_v1 import BaseModel, Field
30
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
 
40
 
41
  # _ = load_dotenv(find_dotenv()) # read local .env file
42
  openai.api_key = os.getenv('OPENAI_API_KEY') #os.environ['OPENAI_API_KEY']
43
+ rapid_api_key = os.getenv('RAPID_API_KEY')
44
 
45
  def get_temperature():
46
  return 0 #Default value
 
110
  @staticmethod
111
  def _fetch_transcript(youtube_video_id: str) -> str:
112
  """
113
+ Fetches the transcript for a YouTube video using a third-party API.
114
 
115
  Args:
116
  youtube_video_id (str): The ID of the YouTube video.
 
122
  Exception: If there's an error fetching the transcript.
123
  """
124
  try:
125
+ details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/details"
126
+ subtitles_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/subtitles"
127
+ querystring = {"videoId": youtube_video_id}
128
+ headers = {
129
+ "x-rapidapi-key": rapid_api_key,
130
+ "x-rapidapi-host": "youtube-media-downloader.p.rapidapi.com"
131
+ }
132
+ details_response = requests.get(details_url, headers=headers, params=querystring)
133
+ print(details_response)
134
+ sub_url = details_response.json()['subtitles']['items'][0]['url']
135
+ querystring = {"subtitleUrl": sub_url, "format": "json"}
136
+ subtitles_response = requests.get(subtitles_url, headers=headers, params=querystring)
137
+
138
+ transcript_json = subtitles_response.json()
139
+ transcript_data = [f"{entry['startMs']/1000:.2f}: {entry['text']} " for entry in transcript_json]
140
  return "".join(transcript_data)
141
  except Exception as e:
142
  raise
 
280
  Exception: If there's an error fetching the transcript.
281
  """
282
  try:
283
+ details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/details"
284
+ subtitles_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/subtitles"
285
+ querystring = {"videoId": youtube_video_id}
286
+ headers = {
287
+ "x-rapidapi-key": rapid_api_key,
288
+ "x-rapidapi-host": "youtube-media-downloader.p.rapidapi.com"
289
+ }
290
+ details_response = requests.get(details_url, headers=headers, params=querystring)
291
+ print(details_response)
292
+ sub_url = details_response.json()['subtitles']['items'][0]['url']
293
+ querystring = {"subtitleUrl": sub_url, "format": "json"}
294
+ subtitles_response = requests.get(subtitles_url, headers=headers, params=querystring)
295
+
296
+ transcript_json = subtitles_response.json()
297
+ transcript_data = [f"{entry['startMs']/1000:.2f}: {entry['text']} " for entry in transcript_json]
298
+ return "".join(transcript_data)
299
  except Exception as e:
300
  raise
301