Rewrote ytlookup to fix edge case issues
This commit is contained in:
parent
81523028ae
commit
978f6bc932
@ -71,6 +71,9 @@ class Onequestionmark {
|
|||||||
Sys.println('[${timestamp()}] Did not find echobox-db.json, generating');
|
Sys.println('[${timestamp()}] Did not find echobox-db.json, generating');
|
||||||
File.saveContent("echobox-db.json", Json.stringify(echoboxDB, "\t"));
|
File.saveContent("echobox-db.json", Json.stringify(echoboxDB, "\t"));
|
||||||
}
|
}
|
||||||
|
if (!FileSystem.exists("export")) {
|
||||||
|
FileSystem.createDirectory("export");
|
||||||
|
}
|
||||||
|
|
||||||
saveSystem();
|
saveSystem();
|
||||||
|
|
||||||
@ -223,6 +226,16 @@ class Onequestionmark {
|
|||||||
m.reply({content:"*Not Teh Face, but better,*\n*Tufac to the letter!*\n*Always two faces, never one,*\n*Tufac has you on the run!*"}, false);
|
m.reply({content:"*Not Teh Face, but better,*\n*Tufac to the letter!*\n*Always two faces, never one,*\n*Tufac has you on the run!*"}, false);
|
||||||
case "youtube":
|
case "youtube":
|
||||||
if (msg.length > 0) {ytlookup(m,msg);}
|
if (msg.length > 0) {ytlookup(m,msg);}
|
||||||
|
case "ytdebug":
|
||||||
|
if (FileSystem.exists("export/ytlookup_result.txt")) {
|
||||||
|
var debugRename = 'export/${datestamp()}_${StringTools.lpad(Std.string(Date.now().getHours()), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getMinutes()), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getSeconds()), "0", 2)}_ytdebug.txt';
|
||||||
|
FileSystem.rename("export/ytlookup_result.txt", debugRename);
|
||||||
|
Sys.println('[${timestamp()}] ytdebug: Created debug file ${debugRename}');
|
||||||
|
m.react('✅');
|
||||||
|
} else {
|
||||||
|
Sys.println('[${timestamp()}] ytdebug: No result to debug');
|
||||||
|
m.reply({content:"ytdebug: No result to debug"}, false);
|
||||||
|
}
|
||||||
// Gaming functions
|
// Gaming functions
|
||||||
case "coin":
|
case "coin":
|
||||||
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
|
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
|
||||||
@ -464,9 +477,11 @@ class Onequestionmark {
|
|||||||
* @param query The string to search.
|
* @param query The string to search.
|
||||||
*/
|
*/
|
||||||
public static function ytlookup(m:Message,query:String) {
|
public static function ytlookup(m:Message,query:String) {
|
||||||
var http = new haxe.Http('https://www.youtube.com/results?search_query=${query}');
|
var http = new haxe.Http('https://www.youtube.com/results?search_query=${query.urlEncode()}');
|
||||||
|
Sys.println('[${timestamp()}] ytlookup: URL - https://www.youtube.com/results?search_query=${query.urlEncode()}');
|
||||||
|
|
||||||
http.onData = function (request) {
|
http.onData = function (request) {
|
||||||
|
File.saveContent("export/ytlookup_result.txt", request);
|
||||||
var data = new HtmlDocument(request);
|
var data = new HtmlDocument(request);
|
||||||
var search = data.find("script"); // YouTube obfuscates everything into JS garbage so we have to check every <script>
|
var search = data.find("script"); // YouTube obfuscates everything into JS garbage so we have to check every <script>
|
||||||
|
|
||||||
@ -474,33 +489,31 @@ class Onequestionmark {
|
|||||||
var result = "Error: Unable to parse YouTube search result.";
|
var result = "Error: Unable to parse YouTube search result.";
|
||||||
|
|
||||||
for (i in search) {
|
for (i in search) {
|
||||||
if (i.toString().contains("https://youtu.be/")) { // Basic results link to the video like this
|
if (i.toString().contains("\"videoRenderer\":{\"videoId\":\"") || i.toString().contains("\"reelWatchEndpoint\":{\"videoId\":\"")) {
|
||||||
gotcha = i.toString();
|
gotcha = i.toString();
|
||||||
result = gotcha.substring(gotcha.indexOf("https://youtu.be/"), gotcha.indexOf(".", gotcha.indexOf("https://youtu.be/")+17));
|
if ((i.toString().contains("\"reelWatchEndpoint\":{\"videoId\":\"")) && (gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"") < gotcha.indexOf("\"videoRenderer\":{\"videoId\":\""))) {
|
||||||
break;
|
// Top result is a Short
|
||||||
}
|
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"")+32, gotcha.indexOf("\"", gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"")+32));
|
||||||
else if (i.toString().contains("watch?v=")) { // Because YouTube isn't consistent, alternative scrape
|
|
||||||
gotcha = i.toString();
|
Sys.println('[${timestamp()}] ytlookup: Result - $result (Short)');
|
||||||
|
break;
|
||||||
// Detect YouTube Shorts link and clean it up
|
} else {
|
||||||
if (gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\"", gotcha.indexOf("watch?v="))).contains("\\")) {
|
// Top result is a normal video
|
||||||
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\\", gotcha.indexOf("watch?v=")));
|
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("\"videoRenderer\":{\"videoId\":\"")+28, gotcha.indexOf("\"", gotcha.indexOf("\"videoRenderer\":{\"videoId\":\"")+28));
|
||||||
|
|
||||||
|
Sys.println('[${timestamp()}] ytlookup: Result - $result (Normal)');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
// Normal video
|
|
||||||
else {
|
|
||||||
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\"", gotcha.indexOf("watch?v=")));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.reply({content:result}, false);
|
m.reply({content:result}, false);
|
||||||
|
m.reply({content:"-# YouTube Lookup is still in beta. If the result seems inaccurate, please use the command `?ytdebug` so the developer can review the data that was received."}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
http.onError = function (error) {
|
http.onError = function (error) {
|
||||||
Sys.println('[${timestamp()}] YTLOOKUP: Error - $error, request was $query');
|
|
||||||
|
|
||||||
m.reply({content:'Error in YouTube Lookup: $error'}, false);
|
m.reply({content:'Error in YouTube Lookup: $error'}, false);
|
||||||
|
Sys.println('[${timestamp()}] ytlookup: Error - $error, request was $query');
|
||||||
}
|
}
|
||||||
|
|
||||||
http.request();
|
http.request();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user