mirror of
https://github.com/nichkara/InfinitumBotty.git
synced 2026-06-11 06:36:24 +02:00
add parsing for music.youtube, youtube/shorts, security: Do not parse url with ips/ports
This commit is contained in:
@@ -23,14 +23,7 @@ class TitleObserver(PrivMsgObserverPrototype):
|
|||||||
url = url.group()
|
url = url.group()
|
||||||
print(url)
|
print(url)
|
||||||
try:
|
try:
|
||||||
headers = {
|
title = self.getTitle(url)
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
|
||||||
}
|
|
||||||
|
|
||||||
url = url
|
|
||||||
req = urllib.request.Request(url, None, headers)
|
|
||||||
resource = urllib.request.urlopen(req)
|
|
||||||
title = self.getTitle(resource)
|
|
||||||
print(title)
|
print(title)
|
||||||
title = title[:350]
|
title = title[:350]
|
||||||
connection.send_back(title, data)
|
connection.send_back(title, data)
|
||||||
@@ -38,28 +31,52 @@ class TitleObserver(PrivMsgObserverPrototype):
|
|||||||
print(exc)
|
print(exc)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getTitle(self, resource):
|
def getTitle(self, url):
|
||||||
encoding = resource.headers.get_content_charset()
|
headers = {
|
||||||
url = resource.geturl()
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
||||||
# der erste Fall kann raus, wenn ein anderer Channel benutzt wird
|
}
|
||||||
if url.find("rehakids.de") != -1:
|
|
||||||
encoding = "windows-1252"
|
|
||||||
if not encoding:
|
|
||||||
encoding = "utf-8"
|
|
||||||
content = resource.read().decode(encoding, errors="replace")
|
|
||||||
|
|
||||||
if re.search("http[s]+://[^/]*youtube.com/", url):
|
if re.search("https?://\\[[^/]*", url):
|
||||||
|
raise (Exception("Refusing to parse bare IPv6 Addresses"))
|
||||||
|
if re.search("https?://[^/:]*:[^/:]*", url):
|
||||||
|
raise (Exception("Refusing to parse URLs with Ports"))
|
||||||
|
if re.search("https?://[0-9]+.[0-9]+.[0-9]+.[^/]*", url):
|
||||||
|
raise (Exception("Refusing to parse bare IPv4 Addresses"))
|
||||||
|
if re.search("https?://music.youtube.com/", url):
|
||||||
|
url = url.replace("music.youtube.com/", "www.youtube.com/", 1)
|
||||||
|
|
||||||
|
if re.search("https?://[^/]*youtube.com/shorts/", url):
|
||||||
|
title_re = re.compile('''"reelPlayerHeaderRenderer":{"reelTitleText":{"runs":\[{"text":"([^"]*)"''')
|
||||||
|
headers["User-Agent"] = "curl/7.81.0"
|
||||||
|
elif re.search("https?://[^/]*youtube.com/", url):
|
||||||
title_re = re.compile(
|
title_re = re.compile(
|
||||||
'''"results":{"contents":\[{"videoPrimaryInfoRenderer":{"title":{"runs":\[{"text":"([^"]*)"'''
|
'''"results":{"contents":\[{"videoPrimaryInfoRenderer":{"title":{"runs":\[{"text":"([^"]*)"'''
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
title_re = re.compile("<title>(.+?)</title>")
|
title_re = re.compile("<title>(.+?)</title>")
|
||||||
|
|
||||||
|
req = urllib.request.Request(url, None, headers)
|
||||||
|
|
||||||
|
# Keep the urlopen scope as short as possible (connection leaks)
|
||||||
|
with urllib.request.urlopen(req, timeout=10) as response:
|
||||||
|
encoding = response.headers.get_content_charset()
|
||||||
|
content_raw = response.read()
|
||||||
|
|
||||||
|
# der erste Fall kann raus, wenn ein anderer Channel benutzt wird
|
||||||
|
if url.find("rehakids.de") != -1:
|
||||||
|
encoding = "windows-1252"
|
||||||
|
if not encoding:
|
||||||
|
encoding = "utf-8"
|
||||||
|
|
||||||
|
content = content_raw.decode(encoding, errors="replace")
|
||||||
|
|
||||||
title_matches = title_re.search(content)
|
title_matches = title_re.search(content)
|
||||||
if title_matches:
|
if title_matches:
|
||||||
title = title_matches.group(1)
|
title = title_matches.group(1)
|
||||||
else:
|
else:
|
||||||
return "Could not Parse Title"
|
#with open("content.html", "w") as file:
|
||||||
|
# file.write(content)
|
||||||
|
raise Exception("Could not Parse Title for {}".format(url))
|
||||||
|
|
||||||
title = html.unescape(title)
|
title = html.unescape(title)
|
||||||
title = title.replace("\n", " ").replace("\r", "")
|
title = title.replace("\n", " ").replace("\r", "")
|
||||||
@@ -67,5 +84,5 @@ class TitleObserver(PrivMsgObserverPrototype):
|
|||||||
title = title.replace(">", ">")
|
title = title.replace(">", ">")
|
||||||
title = title.replace("&", "&")
|
title = title.replace("&", "&")
|
||||||
if title == "":
|
if title == "":
|
||||||
title = "Empty Title"
|
raise Exception("Empty Title for {}".format(url))
|
||||||
return title
|
return title
|
||||||
|
|||||||
Reference in New Issue
Block a user