Writing a K-Relay script that downloads the closed captioning off of a youtube video and plays it back in chat. My problem is that the one of the libraries that I am using uses asynchronous methods. K-Relay does not seem to like this and will not load anything past my plugin. The library is YoutubeExplode.
Thanks!
Edit:
I guess i forgot to put a question in here. I was wondering if there is any way to call the method, and get the same return, without using an asynchronous method. Otherwise is it possible to edit the library and remove the asynchronous methods? Would this damage the functionality of the program?
Code:
namespace CaptionSpeak
{
public class Plugin : IPlugin
{
string userPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase), "CaptionSpeak"); //gets working directory of the K-Relay plugin
public string GetAuthor()
{
return "ElGato";
}
public string[] GetCommands()
{
string[] TODO = new String[1];
TODO[1] = "TODO";
return TODO;
}
public string GetDescription()
{
return "TODO";
}
public string GetName()
{
return "CaptionSpeak";
}
public void Initialize(Proxy proxy)
{
proxy.HookCommand("capspeak", captionDownload);
Console.WriteLine(userPath);
if (!File.Exists(userPath)) //creates directory for downloads if not already created
{
Directory.CreateDirectory(userPath);
}
}
private void captionParse(string file) //parses the file, removing timestamps and the like
{
var parser = new SubtitlesParser.Classes.Parsers.SrtParser();
using (var fileStream = File.OpenRead(file))
{
var items = parser.ParseStream(fileStream, Encoding.Unicode);
}
}
private PlayerTextPacket playback(string path) //TODO chats the text from the caption file
{
PlayerTextPacket captionSpeak = (PlayerTextPacket)Packet.Create(PacketType.PLAYERTEXT);
captionSpeak.Text = " ";
return captionSpeak;
}
private async void captionDownload(Client client, string command, string[] args)
{
if (args.Length == 0)
{
return;
}
if (args[0] == "get")
{
if (args[1] == null)
{
client.SendToClient(PluginUtils.CreateNotification(client.ObjectId, "No URL Specified"));
//TextPacket invalid = (TextPacket)Packet.Create(PacketType.TEXT);
//invalid.Name = "CaptionSpeak";
//invalid.Text = "No URL Specified";
//client.SendToClient(invalid); //using chat instead of notifications
return;
}
else
{
int num = 1;
string[] files = Directory.GetFiles(userPath);
for (int i = 0; i < files.Length; i++) //puts files in a numerical order
{
if (files[i][0] == Convert.ToChar(num))
{
num++;
}
else
{
break;
}
}
var youtubeClient = new YoutubeClient(); //code pretty much copied from the ****** page
var videoInfo = await youtubeClient.GetVideoInfoAsync(args[1]);
var captionTrackInfo = videoInfo.ClosedCaptionTracks.FirstOrDefault(c => c.Language.Code == "en" && c.IsAutoGenerated);
if (captionTrackInfo != null)
{
string fileName = Path.Combine(userPath, num.ToString() + videoInfo.Title);
await youtubeClient.DownloadClosedCaptionTrackAsync(captionTrackInfo, fileName);
//captionParse(fileName);
}
else
{
Console.WriteLine("Captions not found for specified video");
}
}
}
}
}
}