public static async ValueTask <CQCode> Image(string url, CQFileType type = default, IHttpClientFactory httpClientFactory = default) { HttpClient client; byte[] data; switch (type) { case CQFileType.Url: return(CQCode.CQImage(url)); case CQFileType.Base64: client = httpClientFactory == default ? new HttpClient() : httpClientFactory.CreateClient("default"); data = await client.GetByteArrayAsync(url); return(CQCode.CQImage("base64://" + Convert.ToBase64String(data))); case CQFileType.File: Uri uri = new(url); client = httpClientFactory == default ? new HttpClient() : httpClientFactory.CreateClient("default"); data = await client.GetByteArrayAsync(uri); var filePath = ImageCachePath + HashHelp.MD5Encrypt(data) + Path.GetExtension(uri.Segments.Last()); await File.WriteAllBytesAsync(filePath, data); return(CQCode.CQImage(new Uri(filePath).AbsoluteUri)); default: return(null); } }
public static async ValueTask <CQCode> Video(string url, CQFileType type = default, string cover = default, IHttpClientFactory httpClientFactory = default) { switch (type) { case CQFileType.Url: return(CQCode.CQVideo(url, cover)); case CQFileType.Base64: throw new NotSupportedException("Video不支持Base64发送"); case CQFileType.File: Uri uri = new(url); var client = httpClientFactory == default ? new HttpClient() : httpClientFactory.CreateClient("default"); var data = await client.GetByteArrayAsync(uri); var filePath = VideoCachePath + HashHelp.MD5Encrypt(data) + Path.GetExtension(uri.Segments.Last()); await File.WriteAllBytesAsync(filePath, data); return(CQCode.CQVideo(new Uri(filePath).AbsoluteUri, cover)); default: return(null); } }
private List <CQCode> GetTweetContent(Tweet tweet) { var temp = new List <CQCode> { CQCode.CQText(tweet.Content) }; var img = new List <CQCode>(); if (tweet.Media != null) { foreach (var item in tweet.Media) { try { var data = HttpNet.Get(item["media_url_https"].ToString(), proxy: _config.Proxy); img.Add(CQCode.CQImage("base64://" + Convert.ToBase64String(data), useCache: true)); } catch (Exception e) { img.Add(CQCode.CQText($"Error: {e.Message}")); } switch (item["type"].ToString()) { case "photo": { break; } case "video": { var mp4 = item["video_info"]["variants"] .FirstOrDefault(video => video["content_type"].ToString() == "video/mp4"); if (mp4 != null) { img.Add(CQCode.CQText(mp4["url"].ToString())); var data = HttpNet.Get(mp4["url"].ToString(), proxy: _config.Proxy); var tempPath = AppDomain.CurrentDomain.BaseDirectory + "cache\\" + HashHelp.MD5Encrypt(data); File.WriteAllBytes(tempPath, data); img.Add(CQCode.CQVideo(tempPath, useCache: true)); } else { img.Add(CQCode.CQText(item["video_info"]["variants"][0]["url"].ToString())); } break; } case "animated_gif": { var mp4 = item["video_info"]["variants"] .FirstOrDefault(video => video["content_type"].ToString() == "video/mp4"); if (mp4 != null) { img.Add(CQCode.CQText(mp4["url"].ToString())); var data = HttpNet.Get(mp4["url"].ToString(), proxy: _config.Proxy); var tempPath = AppDomain.CurrentDomain.BaseDirectory + "cache\\" + HashHelp.MD5Encrypt(data); File.WriteAllBytes(tempPath, data); img.Add(CQCode.CQVideo(tempPath, useCache: true)); } else { img.Add(CQCode.CQText(item["video_info"]["variants"][0]["url"].ToString())); } break; } } } } if (tweet.IsOnlyRetweet) { if (tweet.Retweet == null) { return(new List <CQCode> { CQCode.CQText("error") }); } else { var a = new List <CQCode> { CQCode.CQText(tweet.Retweet.UserName + ":\n") }; a.AddRange(GetTweetContent(tweet.Retweet)); return(a); } } else { var time = CQCode.CQText("\n发送时间:" + tweet.CreatTime.ToString("yyyy-MM-dd HH:mm")); if (tweet.Retweet == null) { temp.AddRange(img); temp.Add(time); return(temp); } else { temp.AddRange(img); temp.Add(time); temp.Add(CQCode.CQText("\n" + tweet.Retweet.UserName + ":\n")); temp.AddRange(GetTweetContent(tweet.Retweet)); return(temp); } } }