Ejemplo n.º 1
0
        private async ValueTask <CQCode[]> TryGetUrlImage(string url, string id = null)
        {
            var client = _httpClientFactory.CreateClient("default");
            var html   = await client.GetStringAsync(url);

            var parse = new HtmlParser();
            var doc   = await parse.ParseDocumentAsync(html);

            if (id == "381813198")
            {
                var imgs  = doc.QuerySelectorAll(".mce-content-body img").Select(x => x.GetAttribute("src")).ToList();
                var tasks = imgs.Select(x =>
                {
                    return(Task.Run(async() =>
                    {
                        return await CQHelper.Image(x, CQFileType.Base64, _httpClientFactory);
                    }));
                });
                await Task.WhenAll(tasks);

                return(tasks.Select(x => x.Result).ToArray());
            }

            return(null);
        }
Ejemplo n.º 2
0
        private async ValueTask <EventResult <BaseMsgQuickOperation> > Handler(BaseMessageEventArgs args, IOneBotApi api)
        {
            if (args.RawMessage.Equals(_globalService.BiliSetting.HotCos.Command,
                                       StringComparison.CurrentCultureIgnoreCase))
            {
                var result = await BiliApi.GetCosHotAsync();

                if (result.Any())
                {
                    _logger.LogDebug("{Command}数量:{Count}", _globalService.BiliSetting.HotCos.Command, result.Length);
                    var cqCodes = new List <CQCode>();
                    var tasks   = result.Select(url => Task.Run(async() =>
                    {
                        try
                        {
                            cqCodes.Add(await CQHelper.Image(url, CQFileType.Base64));
                        }
                        catch (Exception e)
                        {
                            _logger.LogWarning("[SendImage]下载图片失败:{Url}\r\nError: {Error}", e.ToString(), url);
                        }
                    }));

                    await Task.WhenAll(tasks);

                    return(1, cqCodes.ToArray());
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        private async ValueTask GetTweetContent(List <CQCode> codes, Tweet tweet)
        {
            //var temp = new List<CQCode> { CQCode.CQText(tweet.Content) };
            codes.Add(CQCode.CQText(tweet.Content));
            var img = new List <CQCode>();

            if (tweet.Media != null)
            {
                foreach (var item in tweet.Media)
                {
                    try
                    {
                        img.Add(await CQHelper.Image(item["media_url_https"].ToString(), CQFileType.Base64, _httpClientFactory));
                    }
                    catch (Exception e)
                    {
                        img.Add(CQCode.CQText($"Error: {e.Message}"));
                    }

                    switch (item["type"].ToString())
                    {
                    case "photo":
                        break;

                    case "video":
                    case "animated_gif":
                        var mp4 = item["video_info"]["variants"]
                                  .FirstOrDefault(video => video["content_type"].ToString() == "video/mp4");
                        if (mp4 != null)
                        {
                            var videoUrl = mp4["url"].ToString();
                            img.Add(CQCode.CQText(videoUrl));
                            img.Add(await CQHelper.Video(videoUrl, CQFileType.File, null, _httpClientFactory));
                        }
                        else
                        {
                            img.Add(CQCode.CQText(item["video_info"]["variants"][0]["url"].ToString()));
                        }

                        break;
                    }
                }
            }

            if (img.Count == 0 && tweet.Retweet == null)
            {
                if (tweet.Entities.TryGetValue("urls", out var urls))
                {
                    var temp = await TryGetUrlImage(((JArray)urls)[0]["expanded_url"].ToString(), tweet.UserId);

                    if (temp != null)
                    {
                        codes.AddRange(temp);
                    }
                }
            }

            if (tweet.IsOnlyRetweet)
            {
                if (tweet.Retweet == null)
                {
                    //return new List<CQCode> { CQCode.CQText("error") };
                    codes.Add(CQCode.CQText("error"));
                }
                else
                {
                    //var a = new List<CQCode> { CQCode.CQText(tweet.Retweet.UserName + ":\n") };
                    //a.AddRange(await GetTweetContent(tweet.Retweet));
                    //return a;
                    codes.Add(CQCode.CQText(tweet.Retweet.UserName + ":\n"));
                    await GetTweetContent(codes, tweet.Retweet);
                }
            }
            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;
                    codes.AddRange(img);
                    codes.Add(time);
                }
                else
                {
                    //temp.AddRange(img);
                    //temp.Add(time);
                    //temp.Add(CQCode.CQText("\n" + tweet.Retweet.UserName + ":\n"));
                    //temp.AddRange(await GetTweetContent(tweet.Retweet));
                    //return temp;
                    codes.AddRange(img);
                    codes.Add(time);
                    codes.Add(CQCode.CQText("\n" + tweet.Retweet.UserName + ":\n"));
                    await GetTweetContent(codes, tweet.Retweet);
                }
            }
        }