-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCGamingWikiHTMLParser.cs
132 lines (119 loc) · 4.83 KB
/
PCGamingWikiHTMLParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using Playnite.SDK;
using System.Globalization;
namespace PCGamingWikiMetadata
{
public class PCGamingWikiHTMLParser
{
private readonly ILogger logger = LogManager.GetLogger();
private HtmlDocument doc;
private PCGWGame game;
public PCGamingWikiHTMLParser(string html, PCGWGame game)
{
this.doc = new HtmlDocument();
this.doc.LoadHtml(html);
this.game = game;
}
public void ApplyGameMetadata()
{
var table = this.doc.DocumentNode.SelectSingleNode("//table[@id='infobox-game']");
string currentHeader = "";
foreach (var row in table.SelectNodes(".//tr"))
{
string key = "";
foreach (var child in row.SelectNodes(".//th|td"))
{
const string pattern = @"[\t\s]";
string text = Regex.Replace(child.InnerText.Trim(), pattern, " ");
switch (child.Name)
{
case "th":
currentHeader = text;
break;
case "td":
switch (child.Attributes["class"].Value)
{
case "template-infobox-type":
if (text == "")
break;
key = text;
break;
case "template-infobox-icons":
foreach (var c in child.ChildNodes)
{
string[] linkTitle = c.Attributes["Title"].Value.Split(' ');
string title = linkTitle[linkTitle.Length - 1];
string url = c.ChildNodes[0].Attributes["href"].Value;
this.game.Links.Add(new Playnite.SDK.Models.Link(title, url));
}
break;
case "template-infobox-info":
if (text == "")
break;
switch (currentHeader)
{
case "Taxonomy":
this.game.AddTaxonomy(key, text);
break;
case "Release dates":
ApplyReleaseDate(key, text);
break;
case "Developers":
AddCompany(child, this.game.Developers);
break;
case "Publishers":
AddCompany(child, this.game.Publishers);
break;
default:
logger.Debug($"ApplyGameMetadata unknown header {currentHeader}");
break;
}
break;
}
break;
}
}
}
}
private void ApplyReleaseDate(string platform, string releaseDate)
{
this.game.AddReleaseDate(platform, ParseWikiDate(releaseDate));
}
private DateTime? ParseWikiDate(string dateString)
{
if (DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date) == true)
{
return date;
}
else
{
return null;
}
}
private void AddCompany(HtmlNode node, IList<string> list)
{
string company = ParseCompany(node);
if (company == null)
{
logger.Debug("Unable to parse company");
return;
}
list.Add(company);
}
private string ParseCompany(HtmlNode node)
{
// foreach (var c in node.ChildNodes)
// {
// var attr = c.Attributes["target"];
// if (attr != null)
// {
// return c.InnerText;
// }
// }
return node.ChildNodes[1].InnerText;
}
}
}