This commit is contained in:
ForeverPyrite
2024-09-27 21:55:22 -04:00
parent 6fefd65f86
commit e66a16ed9a
12 changed files with 225 additions and 26 deletions

52
main.py
View File

@@ -3,6 +3,7 @@
import re
# Youtube Transcript stuff import
import youtube_transcript_api._errors
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
@@ -40,6 +41,7 @@ class EventHandler(AssistantEventHandler):
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)
# Setting up OpenAI Client with API Key
api_key =os.getenv("OPENAI_API_KEY")
@@ -65,44 +67,42 @@ def create_and_stream(transcript):
event_handler=EventHandler()
) as stream:
stream.until_done()
return
messages = stream.get_final_messages()
return messages[0].content[0].text.value
def get_video_id():
# local consts
def get_video_id(url):
youtu_be = r'(?<=youtu.be/)([A-Za-z0-9_-]{11})'
youtube_com = r'(?<=youtube\.com\/watch\?v=)([A-Za-z0-9_-]{11})'
# local vars
valid_id = False
# Get URL from user
while(not valid_id):
user_url = input("Enter the URL of the YouTube video: ")
id = re.search(youtu_be, user_url)
if (not id):
id = re.search(youtube_com, user_url)
if (not id):
print("Error: Couldn't parse video ID from URL, please make sure you are pasting a full \"youtube.com\" or \"youtu.be url\"\n")
else:
valid_id = True
id = id.group(1)
print(id)
return id
id = re.search(youtu_be, url)
if not id:
id = re.search(youtube_com, url)
if not id:
print("Couldn't parse video ID from URL")
return None
return id.group(1)
# Takes the transcript and formats it in basic text before writing it to auto-transcript.txt
def get_auto_transcript(video_id):
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'], proxies=None, cookies=None, preserve_formatting=False)
formatter = TextFormatter
trans_api_errors = youtube_transcript_api._errors
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'], proxies=None, cookies=None, preserve_formatting=False)
except trans_api_errors.TranscriptsDisabled:
return None
formatter = TextFormatter() # Ensure that you create an instance of TextFormatter
txt_transcript = formatter.format_transcript(self=any, transcript=transcript)
print(txt_transcript)
txt_transcript = formatter.format_transcript(transcript)
return txt_transcript
# Stores the video id imputted by the user
"""
video_id = get_video_id()
transcript = get_auto_transcript(video_id)
create_and_stream(transcript)
create_and_stream(transcript)
"""