public DownloadWebSocketService( IWebSocketHandler webSocketHandler, IDirectoryHandler directoryHandler, IDownloadHandler downloadHandler, IFileHandler fileHandler, IFileHistoryHandler fileHistoryHandler, ISettingsHandler settingsHandler, IDebugHandler debugHandler) { debugHandler.TraceMessage("Constructor called.", DebugSource.CONSTRUCTOR, DebugType.ENTRY_EXIT); DebugHandler = debugHandler; WebSocketHandler = webSocketHandler; DirectoryHandler = directoryHandler; DownloadHandler = downloadHandler; FileHandler = fileHandler; FileHistoryHandler = fileHistoryHandler; SettingsHandler = settingsHandler; LastDownloadedInfo = new JsonDownloadInfo(); LittleWeebSettings = SettingsHandler.GetLittleWeebSettings(); IrcSettings = SettingsHandler.GetIrcSettings(); downloadHandler.OnDownloadUpdateEvent += OnDownloadUpdateEvent; }
public async Task AddDownloads(JObject downloadJsonBatch) { DebugHandler.TraceMessage("AddDownloads called.", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(downloadJsonBatch.ToString(), DebugSource.TASK, DebugType.PARAMETERS); try { JArray listWithDownloads = downloadJsonBatch.Value <JArray>("batch"); List <JsonDownloadInfo> batch = new List <JsonDownloadInfo>(); foreach (JObject downloadJson in listWithDownloads) { JsonDownloadInfo downloadInfo = new JsonDownloadInfo() { animeInfo = new JsonAnimeInfo() { anime_id = downloadJson.Value <JObject>("animeInfo").Value <string>("animeid"), anime_title = downloadJson.Value <JObject>("animeInfo").Value <string>("title"), anime_cover_original = downloadJson.Value <JObject>("animeInfo").Value <string>("cover_original"), anime_cover_small = downloadJson.Value <JObject>("animeInfo").Value <string>("cover_small") }, id = downloadJson.Value <string>("id"), episodeNumber = downloadJson.Value <string>("episodeNumber"), pack = downloadJson.Value <string>("pack"), bot = downloadJson.Value <string>("bot"), fullfilepath = downloadJson.Value <string>("dir"), filename = downloadJson.Value <string>("filename"), progress = downloadJson.Value <string>("progress"), speed = downloadJson.Value <string>("speed"), status = downloadJson.Value <string>("status"), filesize = downloadJson.Value <string>("filesize") }; LastDownloadedInfo = downloadInfo; batch.Add(downloadInfo); } string result = DownloadHandler.AddDownloads(batch); await WebSocketHandler.SendMessage(result); } catch (Exception e) { DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING); JsonError error = new JsonError() { type = "parse_downloads_to_add_error", errormessage = "Could not parse json containing downloads to add information.", errortype = "exception", exception = e.ToString() }; await WebSocketHandler.SendMessage(error.ToJson()); } }
public void StartDownload(JsonDownloadInfo download) { DebugHandler.TraceMessage("StartDownload Called.", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(download.ToString(), DebugSource.TASK, DebugType.PARAMETERS); try { string xdccMessage = "/msg " + download.bot + " xdcc send #" + download.pack; IrcClient.SetCustomDownloadDir(Path.Combine(IrcSettings.fullfilepath, download.fullfilepath)); SendMessage(xdccMessage); } catch (Exception e) { DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING); } }
private async void OnDownloadUpdateEvent(object sender, DownloadUpdateEventArgs args) { DebugHandler.TraceMessage("OnDownloadUpdateEvent called.", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS); try { JsonDownloadInfo update = new JsonDownloadInfo() { id = args.id, animeInfo = new JsonAnimeInfo() { anime_cover_original = args.animeCoverOriginal, anime_id = args.animeid, anime_cover_small = args.animeCoverSmall, anime_title = args.animeTitle }, episodeNumber = args.episodeNumber, bot = args.bot, pack = args.pack, progress = args.progress, speed = args.speed, status = args.status, filename = args.filename, filesize = args.filesize, downloadIndex = args.downloadIndex, fullfilepath = args.fullfilepath }; await WebSocketHandler.SendMessage(update.ToJson()); if (update.filename != null && update.fullfilepath != null) { FileHistoryHandler.AddFileToFileHistory(update); } if (update.status == "FAILED" || update.status == "ABORTED") { FileHistoryHandler.RemoveFileFromFileHistory(update.id); } } catch (Exception e) { DebugHandler.TraceMessage(e.ToString(), DebugSource.TASK, DebugType.WARNING); } }
private async Task DownloadQueueHandler() { DebugHandler.TraceMessage("DownloadQueueHandler Called.", DebugSource.TASK, DebugType.ENTRY_EXIT); while (!Stop) { if (DownloadQueue.Count > 0 && !DownloadProcesOnGoing) { DownloadProcesOnGoing = true; CurrentlyDownloading = DownloadQueue[0]; DebugHandler.TraceMessage("Requesting start of the following download: " + CurrentlyDownloading.ToJson(), DebugSource.TASK, DebugType.INFO); IrcClientHandler.StartDownload(CurrentlyDownloading); } await Task.Delay(1000); } }
public DownloadHandler(IIrcClientHandler ircClientHandler, IDebugHandler debugHandler) { debugHandler.TraceMessage("DownloadHandler", DebugSource.CONSTRUCTOR, DebugType.ENTRY_EXIT); DebugHandler = debugHandler; try { DownloadQueue = new List <JsonDownloadInfo>(); IrcClientHandler = ircClientHandler; IrcClientHandler.OnIrcClientDownloadEvent += OnIrcClientDownloadEvent; CurrentlyDownloading = new JsonDownloadInfo(); Stop = false; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed DownloadQueueHandler(); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } catch (Exception e) { DebugHandler.TraceMessage(e.ToString(), DebugSource.CONSTRUCTOR, DebugType.ERROR); } }
public void AddFileToFileHistory(JsonDownloadInfo downloadInfo) { DebugHandler.TraceMessage("AddFileToFileHistory Called", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(downloadInfo.ToString(), DebugSource.TASK, DebugType.PARAMETERS); if (!File.Exists(Path.Combine(fileHistoryPath, fileName))) { JsonDownloadHistory downloadHistoryObj = new JsonDownloadHistory() { animeInfo = downloadInfo.animeInfo }; downloadHistoryObj.downloadHistory.Add(downloadInfo); JsonDownloadHistoryList list = new JsonDownloadHistoryList(); if (!list.downloadHistorylist.Contains(downloadHistoryObj)) { list.downloadHistorylist.Add(downloadHistoryObj); using (var fileStream = File.Open(Path.Combine(fileHistoryPath, fileName), FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (var streamWriter = new StreamWriter(fileStream)) { streamWriter.Write(list.ToJson()); } } } } else { JsonDownloadHistoryList list = GetCurrentFileHistory(); bool animeAlreadyExists = false; bool fileAlreadyExists = false; int listIndex = 0; foreach (JsonDownloadHistory downloadHistoryObject in list.downloadHistorylist) { if (downloadHistoryObject.animeInfo.anime_id == downloadInfo.animeInfo.anime_id) { animeAlreadyExists = true; int downloadIndex = 0; foreach (JsonDownloadInfo info in downloadHistoryObject.downloadHistory) { if (info.id == downloadInfo.id || info.filename == downloadInfo.filename || (info.pack == downloadInfo.pack && info.bot == downloadInfo.bot)) { list.downloadHistorylist[listIndex].downloadHistory[downloadIndex] = downloadInfo; fileAlreadyExists = true; break; } downloadIndex++; } if (!fileAlreadyExists) { list.downloadHistorylist[listIndex].downloadHistory.Add(downloadInfo); } break; } listIndex++; } if (!fileAlreadyExists && !animeAlreadyExists) { JsonDownloadHistory downloadHistoryObj = new JsonDownloadHistory() { animeInfo = downloadInfo.animeInfo }; downloadHistoryObj.downloadHistory.Add(downloadInfo); list.downloadHistorylist.Add(downloadHistoryObj); } using (var fileStream = File.Open(Path.Combine(fileHistoryPath, fileName), FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (var streamWriter = new StreamWriter(fileStream)) { streamWriter.Write(list.ToJson()); } } } }
public string AddDownload(JsonDownloadInfo download) { DebugHandler.TraceMessage("AddDownload Called.", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(download.ToString(), DebugSource.TASK, DebugType.PARAMETERS); try { if (download.filesize.Contains(".")) { download.filesize = ((int)(double.Parse(download.filesize, System.Globalization.CultureInfo.InvariantCulture) * 1024)).ToString(); } if (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) > (int.Parse(download.filesize) * 1024 * 1024)) { download.downloadIndex = DownloadQueue.Count - 1; if (!DownloadQueue.Contains(download) || CurrentlyDownloading != download) { DownloadQueue.Add(download); DebugHandler.TraceMessage("Added download to queue: " + download.ToString(), DebugSource.TASK, DebugType.INFO); JsonSuccess succes = new JsonSuccess() { message = "Succesfully added download to download queue." }; return(succes.ToJson()); } else { DebugHandler.TraceMessage("Could not add download: " + download.ToString() + ", already exist in queue or is already being downloaded ", DebugSource.TASK, DebugType.WARNING); JsonError error = new JsonError() { type = "file_already_being_downloaded_error", errormessage = "Could not add download: " + download.ToString() + ", already exist in queue or is already being downloaded ", errortype = "warning", exception = "none" }; return(error.ToJson()); } } else { DebugHandler.TraceMessage("Could not add download with filesize: " + download.filesize + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(), DebugSource.TASK, DebugType.WARNING); JsonError error = new JsonError() { type = "unsufficient_space_error", errormessage = "Could not add download with filesize: " + download.filesize + " due to insufficient space required: " + (UtilityMethods.GetFreeSpace(IrcSettings.fullfilepath) / 1024 / 1024).ToString(), errortype = "warning" }; return(error.ToJson()); } } catch (Exception e) { DebugHandler.TraceMessage("Could not add download with filesize: " + e.ToString(), DebugSource.TASK, DebugType.ERROR); JsonError error = new JsonError() { type = "add_download_error", errormessage = "Could not add download to queue.", errortype = "exception", exception = e.ToString() }; return(error.ToJson()); } }
private void OnIrcClientDownloadEvent(object sender, IrcClientDownloadEventArgs args) { DebugHandler.TraceMessage("OnIrcClientDownloadEvent Called.", DebugSource.TASK, DebugType.ENTRY_EXIT); DebugHandler.TraceMessage(args.ToString(), DebugSource.TASK, DebugType.PARAMETERS); if (CurrentlyDownloading != null) { if (args.DownloadStatus == "PARSING" || args.DownloadStatus == "WAITING") { OnDownloadUpdateEvent?.Invoke(this, new DownloadUpdateEventArgs() { id = CurrentlyDownloading.id, animeid = CurrentlyDownloading.animeInfo.anime_id, animeTitle = CurrentlyDownloading.animeInfo.anime_title, animeCoverSmall = CurrentlyDownloading.animeInfo.anime_cover_small, animeCoverOriginal = CurrentlyDownloading.animeInfo.anime_cover_original, episodeNumber = CurrentlyDownloading.episodeNumber, bot = CurrentlyDownloading.bot, pack = CurrentlyDownloading.pack, progress = args.DownloadProgress.ToString(), speed = args.DownloadSpeed.ToString(), status = args.DownloadStatus, filename = CurrentlyDownloading.filename, filesize = CurrentlyDownloading.filesize, fullfilepath = CurrentlyDownloading.fullfilepath, downloadIndex = CurrentlyDownloading.downloadIndex }); } else { OnDownloadUpdateEvent?.Invoke(this, new DownloadUpdateEventArgs() { id = CurrentlyDownloading.id, animeid = CurrentlyDownloading.animeInfo.anime_id, animeTitle = CurrentlyDownloading.animeInfo.anime_title, animeCoverSmall = CurrentlyDownloading.animeInfo.anime_cover_small, animeCoverOriginal = CurrentlyDownloading.animeInfo.anime_cover_original, episodeNumber = CurrentlyDownloading.episodeNumber, bot = CurrentlyDownloading.bot, pack = CurrentlyDownloading.pack, progress = args.DownloadProgress.ToString(), speed = args.DownloadSpeed.ToString(), status = args.DownloadStatus, filename = args.FileName, filesize = args.FileSize.ToString(), fullfilepath = args.FileLocation, downloadIndex = CurrentlyDownloading.downloadIndex }); } if (args.DownloadStatus.Contains("COMPLETED")) { OnDownloadUpdateEvent?.Invoke(this, new DownloadUpdateEventArgs() { id = CurrentlyDownloading.id, animeid = CurrentlyDownloading.animeInfo.anime_id, animeTitle = CurrentlyDownloading.animeInfo.anime_title, animeCoverSmall = CurrentlyDownloading.animeInfo.anime_cover_small, animeCoverOriginal = CurrentlyDownloading.animeInfo.anime_cover_original, episodeNumber = CurrentlyDownloading.episodeNumber, bot = CurrentlyDownloading.bot, pack = CurrentlyDownloading.pack, progress = args.DownloadProgress.ToString(), speed = args.DownloadSpeed.ToString(), status = args.DownloadStatus, filename = CurrentlyDownloading.filename, filesize = CurrentlyDownloading.filesize, fullfilepath = CurrentlyDownloading.fullfilepath, downloadIndex = CurrentlyDownloading.downloadIndex }); if (CurrentlyDownloading.id != string.Empty) { RemoveDownload(CurrentlyDownloading.id); CurrentlyDownloading = new JsonDownloadInfo(); } DownloadProcesOnGoing = false; } else if (args.DownloadStatus.Contains("FAILED") || args.DownloadStatus.Contains("ABORTED")) { if (CurrentlyDownloading.id != string.Empty) { RemoveDownload(CurrentlyDownloading.id); CurrentlyDownloading = new JsonDownloadInfo(); } DownloadProcesOnGoing = false; } } else { DebugHandler.TraceMessage("Got download event, but no CurrrentlyDownloading object has been set!", DebugSource.TASK, DebugType.WARNING); } }