-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
78 lines (66 loc) · 2.53 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, request
from get_transcript import id_to_transcript
from get_chat import id_to_chat, id_to_chat_split
from magic import magic
from get_video import id_to_name
from match_chat import match_chat
from chat_downloader.errors import NoChatReplay, ChatDownloaderError
from youtube_transcript_api._errors import TranscriptsDisabled, CouldNotRetrieveTranscript
from convert_timestamp import timestamp_to_seconds
from errors import *
APP = Flask(__name__, static_folder="./build", static_url_path="/")
@APP.errorhandler(APIError)
def handle_exception(err):
response = {"error": err.description}
response["message"] = "" if len(err.args) == 0 else err.args[0]
APP.logger.error(f"{err.description}: {response['message']}")
return response, err.code
@APP.route("/")
def index():
return APP.send_static_file("index.html")
@APP.route("/transcript", methods=["get"])
def get_transcript_from_id():
video_id = request.args.get("id")
transcript = id_to_transcript(video_id)
transcriptString = "\n".join(transcript)
return {
"transcript": transcriptString
}
@APP.route("/title", methods=["get"])
def get_title():
video_id = request.args.get("id")
try:
name = id_to_name(video_id)
except CouldNotGetName:
raise APIVideoError("Could not retrieve this video. Please check that the link is correct.")
except:
raise APIError("Could not retrieve this video.")
return {
"name": name
}
@APP.route("/magic", methods=["get"])
def get_magic():
video_id = request.args.get("id")
try:
transcript = id_to_transcript(video_id)
live_chat = id_to_chat_split(video_id)
qna = magic(transcript, live_chat)
qna.extend(match_chat(live_chat))
qna = sorted(qna, key=lambda k: timestamp_to_seconds(k['time']))
if (len(qna) == 0):
raise NoQuestionsFound("No questions found")
except TranscriptsDisabled:
raise APITranscriptError("Transcripts have been disabled on this video.")
except CouldNotRetrieveTranscript as e:
raise APITranscriptError(f"Transcript could not be retrieved from this video: {e}")
except NoChatReplay:
raise APIChatError("This video does not have a chat replay.")
except ChatDownloaderError:
raise APIChatError("Chat could not be retrieved from this video.")
except NoQuestionsFound:
raise APIError("No questions found for this video.")
except:
raise APIError("Could not retrieve video details.")
return {
"qna": qna
}