HtmlExport.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using K4os.Compression.LZ4.Encoders;
  2. using K4os.Compression.LZ4;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using WechatBakTool.Model;
  10. using System.Xml;
  11. namespace WechatBakTool.Export
  12. {
  13. public class HtmlExport : IExport
  14. {
  15. private string HtmlBody = "";
  16. private WXSession? Session = null;
  17. private string Path = "";
  18. public void InitTemplate(WXSession session)
  19. {
  20. Session = session;
  21. HtmlBody = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>WechatBakTool</title><style>p{margin:0px;}.msg{padding-bottom:10px;}.nickname{font-size:10px;}.content{font-size:14px;}</style></head><body>";
  22. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>与 {0}({1}) 的聊天记录</b></p>", Session.NickName, Session.UserName);
  23. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>导出时间:{0}</b></p><hr/>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  24. }
  25. public void InitTemplate(WXContact contact, string p)
  26. {
  27. Path = p;
  28. WXSession session = new WXSession();
  29. session.NickName = contact.NickName;
  30. session.UserName = contact.UserName;
  31. InitTemplate(session);
  32. }
  33. public void Save(string path = "", bool append = false)
  34. {
  35. if (!append)
  36. {
  37. File.WriteAllText(path, HtmlBody);
  38. }
  39. else
  40. {
  41. File.AppendAllText(path, HtmlBody);
  42. HtmlBody = "";
  43. }
  44. }
  45. public void SetEnd()
  46. {
  47. HtmlBody += "</body></html>";
  48. }
  49. public void SetMsg(WXUserReader reader, WXContact contact)
  50. {
  51. if (Session == null)
  52. throw new Exception("请初始化模版:Not Use InitTemplate");
  53. List<WXMsg>? msgList = reader.GetWXMsgs(contact.UserName);
  54. if (msgList == null)
  55. throw new Exception("获取消息失败,请确认数据库读取正常");
  56. msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime));
  57. foreach (var msg in msgList)
  58. {
  59. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\">{0} <span style=\"padding-left:10px;\">{1}</span></p>", msg.IsSender ? "我" : msg.NickName, TimeStampToDateTime(msg.CreateTime).ToString("yyyy-MM-dd HH:mm:ss"));
  60. if (msg.Type == 1)
  61. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
  62. else if (msg.Type == 3)
  63. {
  64. string? path = reader.GetAttachment(WXMsgType.Image, msg);
  65. if (path == null)
  66. {
  67. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
  68. continue;
  69. }
  70. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
  71. }
  72. else if (msg.Type == 43)
  73. {
  74. string? path = reader.GetAttachment(WXMsgType.Video, msg);
  75. if (path == null)
  76. {
  77. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  78. continue;
  79. }
  80. HtmlBody += string.Format("<p class=\"content\"><video controls style=\"max-height:300px;max-width:300px;\"><source src=\"{0}\" type=\"video/mp4\" /></video></p></div>", path);
  81. }
  82. else if (msg.Type == 49)
  83. {
  84. using (var decoder = LZ4Decoder.Create(true, 64))
  85. {
  86. byte[] target = new byte[10240];
  87. int res = 0;
  88. if (msg.CompressContent != null)
  89. res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length);
  90. byte[] data = target.Skip(0).Take(res).ToArray();
  91. string xml = Encoding.UTF8.GetString(data);
  92. if (!string.IsNullOrEmpty(xml))
  93. {
  94. xml = xml.Replace("\n", "");
  95. XmlDocument xmlObj = new XmlDocument();
  96. xmlObj.LoadXml(xml);
  97. if (xmlObj.DocumentElement != null)
  98. {
  99. string title = "";
  100. string appName = "";
  101. string url = "";
  102. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  103. if (findNode != null)
  104. {
  105. if (findNode.Count > 0)
  106. {
  107. title = findNode[0]!.InnerText;
  108. }
  109. }
  110. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/sourcedisplayname");
  111. if (findNode != null)
  112. {
  113. if (findNode.Count > 0)
  114. {
  115. appName = findNode[0]!.InnerText;
  116. }
  117. }
  118. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/url");
  119. if (findNode != null)
  120. {
  121. if (findNode.Count > 0)
  122. {
  123. url = findNode[0]!.InnerText;
  124. }
  125. }
  126. HtmlBody += string.Format("<p class=\"content\">{0}|{1}</p><p><a href=\"{2}\">点击访问</a></p></div>", appName, title, url);
  127. }
  128. }
  129. }
  130. }
  131. else if (msg.Type == 34)
  132. {
  133. string? path = reader.GetAttachment(WXMsgType.Audio, msg);
  134. if (path == null)
  135. {
  136. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
  137. continue;
  138. }
  139. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  140. }
  141. else
  142. {
  143. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  144. }
  145. }
  146. }
  147. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  148. {
  149. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  150. return dateTimeOffset.LocalDateTime;
  151. }
  152. }
  153. }