-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (87 loc) · 3.01 KB
/
Copy pathmain.py
File metadata and controls
104 lines (87 loc) · 3.01 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#author : G Mohan Teja
#last edited : 22/10/2022
"""
this module is responsible for navingating into webpage and getting the chat msgs
then use them in text rank algorithim to give most relavent msgs
"""
#imports
from selenium import webdriver
from time import time
from textrank import textrank
from http.server import HTTPServer, BaseHTTPRequestHandler
import re
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
#tell the vid link here or give as input?
vid = 'jfKfPfyJRdk'
driver.get(f'https://www.youtube.com/watch?v={vid}')
#remove html tags from a text
clean = lambda datas:[re.sub('<.*?>','',data).strip() for data in datas]
#wait until element is provided with a time out of 1 sec
def getElementXpath(value,multiple=False,timeout=1):
global driver
start = time()
diff = 0
while(diff<timeout):
try:
if(multiple):
element = driver.find_elements(by='xpath',value=value)
else:
element = driver.find_element(by='xpath',value=value)
break;
except:
end = time()
pass
diff = end-start
return element,round(diff,2)
#get chat element src
def getSrc():
start = time()
global driver
element,timetaken = getElementXpath('//*[@id="chatframe"]')
src = element.get_attribute("src")
end = time()
return src,end-start
#getting the chats from src
def getChats():
global clean
id = '/html/body/yt-live-chat-app/div/yt-live-chat-renderer/iron-pages/div/div[1]/div[3]/div[1]/yt-live-chat-item-list-renderer/div/div[1]/div/div/yt-live-chat-text-message-renderer'
elements,_ = getElementXpath(id,True)
msgs = []
for element in elements:
msgs.append(element.find_element(by='xpath',value='./div[1]/span[2]').get_attribute("innerHTML"))
return clean(msgs)
#basic http hosting server
class Server(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
# get the page
path = self.path
if path == '/':
msgs = textrank(getChats())[0]
driver.refresh()
string ='<head><title>Chat</title><meta http-equiv="refresh" content="5"></head>'
string += "<ul>"
for msg in msgs:
if(msg.strip()!=""):
string+="<li>"
string+=msg
string+="</li>"
string+="</ul>"
self.wfile.write(bytes(string,"utf-8"))
if __name__ == "__main__":
#load the chat box
src,timetaken = getSrc()
print(f"Loaded in {timetaken:2f} s")
driver.get(src)
#turn on the server if any error print the error and close the server and driver
try:
server = HTTPServer(('localhost', 8080), Server)
server.serve_forever()
except Exception as e:
print(e)
server.server_close()
driver.close()