public string GenerateTextline() { string msg = "[" + Date.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "]"; msg += " " + VKTools.GetProfile("", FromID).name; var MsgText = Text.Replace("\n", "\t"); msg += ": " + MsgText; if (Attachments.Count > 0) { msg += " [" + Attachments.Count + " attachments: "; for (var a = 0; a < Attachments.Count; a++) { if (a > 0) { msg += ","; } msg += Attachments[a].Type; if (!string.IsNullOrEmpty(Attachments[a].Filename)) { msg += " '" + Attachments[a].Filename + "'"; } } msg += "]"; } return(msg); }
public void Download(Conversation convo, string dirPath) { var convoName = ""; if (convo.type == "user") { var profile = VKTools.GetProfile(token, convo.id); convoName = profile.name; } else if (!string.IsNullOrEmpty(convo.title)) { convoName = convo.title; } if (string.IsNullOrEmpty(convoName)) { convoName = "" + convo.id; } else { convoName += " - " + convo.id; } convoName = CleanFileName(convoName); var convoFilename = convoName + ".txt"; form.LogThis(" Filename: " + convoFilename); var filePath = Path.Combine(dir, convoFilename); int lineCount = 0; if (!File.Exists(filePath)) { form.LogThis("Creating new file"); File.Create(filePath).Close(); } else { lineCount = Tools.GetLineCount(filePath); form.LogThis("File already has " + lineCount + " lines"); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, true)) { int lastMessageCount = 0; do { var response = VKTools.GetMessages(token, convo.id, lineCount); lastMessageCount = response.Messages.Count; form.LogThis(" " + lineCount + " / " + response.Total + " messages"); List <Attachment> attachments = new List <Attachment>(); for (var m = 0; m < response.Messages.Count; m++) { attachments.AddRange(response.Messages[m].Attachments); file.WriteLine(response.Messages[m].GenerateTextline()); } if (form.IsDownloadAttachmentsChecked()) { for (var a = 0; a < attachments.Count && !stop; a++) { DownloadAttachment(attachments[a], convoName); while (downloadCounter > MAX_DOWNLOADS && !stop) { Task.Delay(100); } } } lineCount += response.Messages.Count; } while (lastMessageCount == 200 && !stop); } }