Spaces:
Sleeping
Sleeping
Update src/utils/helper.py
Browse files- src/utils/helper.py +16 -2
src/utils/helper.py
CHANGED
|
@@ -125,10 +125,14 @@ def extract_transcript(video_link: str):
|
|
| 125 |
# extract video id from video link
|
| 126 |
video_id = video_link.split("v=")[1]
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# Call Supadata API
|
| 129 |
url = f"https://api.supadata.ai/v1/youtube/transcript"
|
| 130 |
headers = {
|
| 131 |
-
"x-api-key":
|
| 132 |
}
|
| 133 |
params = {
|
| 134 |
"videoId": video_id
|
|
@@ -138,6 +142,10 @@ def extract_transcript(video_link: str):
|
|
| 138 |
response.raise_for_status() # Raise exception for non-200 status codes
|
| 139 |
|
| 140 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
text = ""
|
| 142 |
for item in data["content"]:
|
| 143 |
if "text" in item:
|
|
@@ -145,9 +153,15 @@ def extract_transcript(video_link: str):
|
|
| 145 |
|
| 146 |
logger.info(f"Transcript: {text}")
|
| 147 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
logger.error(f"Failed to extract transcript: {str(e)}")
|
| 150 |
-
raise
|
| 151 |
|
| 152 |
|
| 153 |
def extract_comment(video_link: str):
|
|
|
|
| 125 |
# extract video id from video link
|
| 126 |
video_id = video_link.split("v=")[1]
|
| 127 |
|
| 128 |
+
api_key = os.getenv("SUPADATA_API_KEY")
|
| 129 |
+
if not api_key:
|
| 130 |
+
raise ValueError("SUPADATA_API_KEY environment variable is not set")
|
| 131 |
+
|
| 132 |
# Call Supadata API
|
| 133 |
url = f"https://api.supadata.ai/v1/youtube/transcript"
|
| 134 |
headers = {
|
| 135 |
+
"x-api-key": api_key
|
| 136 |
}
|
| 137 |
params = {
|
| 138 |
"videoId": video_id
|
|
|
|
| 142 |
response.raise_for_status() # Raise exception for non-200 status codes
|
| 143 |
|
| 144 |
data = response.json()
|
| 145 |
+
logger.info(f"Data: {data}")
|
| 146 |
+
if not data.get("content"):
|
| 147 |
+
raise ValueError("No transcript content found in the API response")
|
| 148 |
+
|
| 149 |
text = ""
|
| 150 |
for item in data["content"]:
|
| 151 |
if "text" in item:
|
|
|
|
| 153 |
|
| 154 |
logger.info(f"Transcript: {text}")
|
| 155 |
return text
|
| 156 |
+
except requests.exceptions.RequestException as e:
|
| 157 |
+
logger.error(f"API request failed: {str(e)}")
|
| 158 |
+
raise
|
| 159 |
+
except ValueError as e:
|
| 160 |
+
logger.error(str(e))
|
| 161 |
+
raise
|
| 162 |
except Exception as e:
|
| 163 |
logger.error(f"Failed to extract transcript: {str(e)}")
|
| 164 |
+
raise
|
| 165 |
|
| 166 |
|
| 167 |
def extract_comment(video_link: str):
|