Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix opgg provider #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion Legendary Rune Maker/Data/Providers/OpGGProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ internal class OpGGProvider : Provider
};

public override string Name => "OP.GG";
public override Options ProviderOptions => Options.RunePages | Options.SkillOrder;
public override Options ProviderOptions => Options.ItemSets | Options.RunePages | Options.SkillOrder;

private static string GetRoleUrl(int championId, Position position)
=> $"https://op.gg/champion/{Riot.GetChampion(championId).Key}/statistics/{PositionToName[position]}";

private static string GetItemUrl(int championId, Position position)
=> $"https://op.gg/champion/{Riot.GetChampion(championId).Key}/statistics{(string.IsNullOrEmpty(PositionToName[position]) ? "" : $"/{PositionToName[position]}")}/item";

public override async Task<Position[]> GetPossibleRoles(int championId)
{
return PositionToName.Keys.Except(new []{ Position.Fill }).ToArray();
Expand Down Expand Up @@ -83,5 +86,84 @@ public override async Task<string> GetSkillOrder(int championId, Position positi

return $"({string.Join(">", tips)}) {new string(slong)}";
}

public async Task<Position> GetPopularPosition(int championId)
{
var doc = new HtmlDocument();
doc.LoadHtml(await WebCache.String(GetItemUrl(championId, Position.Fill), soft: true));
var popularPositionNode = doc.DocumentNode.Descendants("link")
.FirstOrDefault(l => l.Attributes["rel"]?.Value == "canonical")
.Attributes["href"]?.Value;
var popularPosition = Regex.Match(popularPositionNode, @"/statistics/(\w+)");
return PositionToName.FirstOrDefault(p => p.Value.Equals(popularPosition.Groups[1].Value, StringComparison.CurrentCultureIgnoreCase)).Key;
}

public override async Task<ItemSet> GetItemSet(int championId, Position position)
{
if (position == Position.Fill)
{
position = await GetPopularPosition(championId);
}

var doc = new HtmlDocument();
doc.LoadHtml(await WebCache.String(GetItemUrl(championId, position), soft: true));
var contentTables = doc.DocumentNode.Descendants("table")
.Where(o => o.HasClass("champion-stats__table")).ToList();

var starterItem = GetRows("Starter Items");
var coreItems = GetRows("Core Build");
var bootsItem = GetRows("Boots");

var blocks = new List<ItemSet.SetBlock>();
AddBlockEntry(starterItem, "Starter Items");
AddBlockEntry(coreItems, "Core Build");
AddBlockEntry(bootsItem, "Boots");

return new ItemSet
{
Champion = championId,
Name = this.Name + ": " + position,
Position = position,
Blocks = blocks.ToArray()
};

List<HtmlNode> GetRows(string tableHeader)
{
var table = contentTables.FirstOrDefault(d1 => d1.Descendants("th").Any(d2 => d2.InnerText.Contains(tableHeader)));
return table?.Descendants("tr").Where(tr => tr.ChildNodes.Any(td => td.HasClass("champion-stats__table__cell"))).ToList();
}

void AddBlockEntry(List<HtmlNode> nodes, string blockName)
{
foreach (var node in nodes)
{
var items = new List<int>();
var itemCounter = 0;
node.Descendants("img").ToList().ForEach(n =>
{
if (n.ParentNode.HasClass("champion-stats__list__item"))
{
itemCounter++;
if (int.TryParse(Regex.Match(n.Attributes["src"].Value, "/item/(\\d+).png").Groups[1].Value, out var item))
{
items.Add(item);
}
}
});
if (items.Count != itemCounter)
continue;

var pickRate = node.Descendants("td").FirstOrDefault(td => td.HasClass("champion-stats__table__cell--pickrate"))?.InnerText ?? "";
pickRate = Regex.Replace(pickRate, @"[^\w|\.|%]", "");
var matches = Regex.Match(pickRate, @"(\d{1,2}\.\d{2})%(\d+)");
var winRate = node.Descendants("td").FirstOrDefault(td => td.HasClass("champion-stats__table__cell--winrate"))?.InnerText;
blocks.Add(new ItemSet.SetBlock
{
Name = $"{blockName} | PickRate {matches?.Groups[1].Value}% | PickCount {matches?.Groups[2].Value} | WinRate {winRate}",
Items = items.ToArray()
});
}
}
}
}
}