コード例 #1
0
 static SearchService()
 {
     Instance = new SearchService();
 }
コード例 #2
0
ファイル: HtmlModifier.cs プロジェクト: Ekwav/HypixelSkyblock
        public static async Task <string> ModifyContent(string path, byte[] contents, Server.RequestContext res)
        {
            string parameter = "";
            var    urlParts  = path.Split('/', '?', '#');

            if (urlParts.Length > 2)
            {
                parameter = urlParts[2];
            }
            string description     = "Browse over 200 million auctions, and the bazaar of Hypixel SkyBlock.";
            string longDescription = null;
            string title           = defaultTitle;
            string imageUrl        = "https://sky.coflnet.com/logo192.png";
            string keyword         = "";

            var    start       = Encoding.UTF8.GetString(contents).Split("<title>");
            var    headerStart = start[0] + "<title>";
            var    parts       = start[1].Split("</head>");
            string header      = parts.First();
            string html        = parts.Last().Substring(0, parts.Last().Length - 14);


            if (path.StartsWith("/p/"))
            {
                return(res.RedirectSkyblock(parameter, "player"));
            }
            if (path.StartsWith("/a/"))
            {
                return(res.RedirectSkyblock(parameter, "auction"));
            }
            if (path == "/item/" || path == "/item")
            {
                return(res.RedirectSkyblock());
            }

            // try to fill in title
            if (path.Contains("auction/"))
            {
                await WriteStart(res, headerStart);

                // is an auction
                using (var context = new HypixelContext())
                {
                    var result = context.Auctions.Where(a => a.Uuid == parameter)
                                 .Select(a => new { a.Tag, a.AuctioneerId, a.ItemName, a.End, bidCount = a.Bids.Count, a.Tier, a.Category }).FirstOrDefault();
                    if (result == null)
                    {
                        await WriteHeader("/error", res, "This site was not found", "Error", imageUrl, null, header);

                        await res.WriteEnd(html);

                        return("");
                    }


                    var playerName = PlayerSearch.Instance.GetNameWithCache(result.AuctioneerId);
                    title       = $"Auction for {result.ItemName} by {playerName}";
                    description = $"{title} ended on {result.End.ToString("yyyy-MM-dd HH\\:mm\\:ss")} with {result.bidCount} bids, Category: {result.Category}, {result.Tier}.";


                    if (!string.IsNullOrEmpty(result.Tag))
                    {
                        imageUrl = "https://sky.lea.moe/item/" + result.Tag;
                    }
                    else
                    {
                        imageUrl = SearchService.PlayerHeadUrl(result.AuctioneerId);
                    }

                    await WriteHeader(path, res, description, title, imageUrl, keyword, header);

                    longDescription = description
                                      + $"<ul><li> <a href=\"/player/{result.AuctioneerId}/{playerName}\"> other auctions by {playerName} </a></li>"
                                      + $" <li><a href=\"/item/{result.Tag}/{result.ItemName}\"> more auctions for {result.ItemName} </a></li></ul>";
                    keyword = $"{result.ItemName},{playerName}";
                }
            }
            else if (path.Contains("player/"))
            {
                if (parameter.Length < 30)
                {
                    var uuid = PlayerSearch.Instance.GetIdForName(parameter);
                    return(res.RedirectSkyblock(uuid, "player", uuid));
                }

                await WriteStart(res, headerStart);

                keyword     = PlayerSearch.Instance.GetNameWithCache(parameter);
                title       = $"{keyword} Auctions and bids";
                description = $"Auctions and bids for {keyword} in hypixel skyblock.";

                imageUrl = SearchService.PlayerHeadUrl(parameter);

                await WriteHeader(path, res, description, title, imageUrl, keyword, header);


                var auctions = GetAuctions(parameter, keyword);
                var bids     = GetBids(parameter, keyword);
                await res.WriteAsync(html);

                await res.WriteAsync(DETAILS_START + $"<h1>{title}</h1>{description} " + await auctions);

                await res.WriteEnd(await bids + PopularPages());

                return("");
            }
            else if (path.Contains("item/") || path.Contains("i/"))
            {
                if (path.Contains("i/"))
                {
                    return(res.RedirectSkyblock(parameter, "item", keyword));
                }
                if (!ItemDetails.Instance.TagLookup.ContainsKey(parameter))
                {
                    var upperCased = parameter.ToUpper();
                    if (ItemDetails.Instance.TagLookup.ContainsKey(upperCased))
                    {
                        return(res.RedirectSkyblock(upperCased, "item"));
                    }
                    // likely not a tag
                    parameter = HttpUtility.UrlDecode(parameter);
                    var thread = ItemDetails.Instance.Search(parameter, 1);
                    thread.Wait();
                    var item = thread.Result.FirstOrDefault();
                    keyword   = item?.Name;
                    parameter = item?.Tag;
                    return(res.RedirectSkyblock(parameter, "item", keyword));
                }
                await WriteStart(res, headerStart);

                keyword = ItemDetails.TagToName(parameter);


                var i = await ItemDetails.Instance.GetDetailsWithCache(parameter);

                path = CreateCanoicalPath(urlParts, i);
                var name = i?.Names?.FirstOrDefault();
                if (name != null)
                {
                    keyword = name;
                }

                title = $"{keyword} price ";
                float price = await GetAvgPrice(parameter);

                description = $"Price for item {keyword} in hypixel SkyBlock is {price.ToString("0,0.0")} on average. Visit for a nice chart and filter options";
                imageUrl    = "https://sky.lea.moe/item/" + parameter;
                if (parameter.StartsWith("PET_") && !parameter.StartsWith("PET_ITEM") || parameter.StartsWith("POTION"))
                {
                    imageUrl = i.IconUrl;
                }
                await WriteHeader(path, res, description, title, imageUrl, keyword, header);

                longDescription = description
                                  + AddAlternativeNames(i);

                longDescription += await GetRecentAuctions(i.Tag == "Unknown" || i.Tag == null?parameter : i.Tag);
            }
            else
            {
                if (path.Contains("/flipper"))
                {
                    title       = "Skyblock AH history auction flipper";
                    description = "Free auction house item flipper for Hypixel Skyblock";
                    keyword     = "flipper";
                }
                // unkown site, write the header
                await WriteStart(res, headerStart);
                await WriteHeader(path, res, description, title, imageUrl, keyword, header);
            }
            if (longDescription == null)
            {
                longDescription = description;
            }


            var newHtml = html + DETAILS_START
                          + BottomText(title, longDescription);

            await res.WriteEnd(newHtml);

            return(newHtml);
        }