-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLsiteScrapper.cs
288 lines (238 loc) · 11.6 KB
/
DLsiteScrapper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Extensions;
using Playnite.SDK;
namespace DLsiteMetadata;
public class DLsiteScrapper(ILogger logger)
{
private const SupportedLanguages DefaultLanguage = SupportedLanguages.en_US;
public const string SiteBaseUrl = "https://www.dlsite.com";
public const string ProductFormatUrl = SiteBaseUrl + "/{0}/work/=/product_id/{1}.html";
public const string SearchFormatUrl = SiteBaseUrl +
"{0}/fsr/=/language/jp/keyword/{1}/age_category%5B0%5D/general/order%5B0%5D/trend/options_and_or/and/per_page/{2}/show_type/1/from/fs.header/?locale={3}";
public async Task<DLsiteScrapperResult> ScrapGamePage(string url, SupportedLanguages language = DefaultLanguage)
{
if (!IsValidUrl(url))
{
logger.Error($"Invalid URL: {url}");
return null;
}
if (!url.Contains("?locale="))
{
url += $"?locale={language.ToString()}";
}
var result = new DLsiteScrapperResult
{
Links = new Dictionary<string, string>
{
{ "DLsite", url }
}
};
var client = new HttpClient();
string responseBody;
try
{
responseBody = await client.GetStringAsync(url);
}
catch (HttpRequestException e)
{
logger.Error(e, $"Failed to fetch URL: {url}");
throw;
}
var context = BrowsingContext.New(Configuration.Default);
var document = await context.OpenAsync(req => req.Content(responseBody));
var title = document.GetElementById("work_name")?.Text().Trim();
result.Title = title;
var rating = document.QuerySelector(".point.average_count")?.Text().Trim();
result.Rating = float.TryParse(rating, out var parsedRating) ? parsedRating : null;
var circle = document.QuerySelector("[itemprop='brand']")?.Text().Trim();
result.Circle = circle;
var ciEn = document.QuerySelector(".link_cien a")?.GetAttribute("href");
if (ciEn is not null) result.Links.Add("Ci-en", ciEn);
var table = document.GetElementById("work_outline");
if (table is not null)
{
var tableHeaders = table.QuerySelectorAll("th");
foreach (var header in tableHeaders)
{
if (header.TextContent.Contains(TranslationDictionary.ReleaseDate[language]))
{
var releaseDateString = header.NextElementSibling?.Text().Trim();
DateTime releaseDate;
switch (language)
{
case SupportedLanguages.ja_JP:
case SupportedLanguages.zh_CN:
case SupportedLanguages.zh_TW:
DateTime.TryParseExact(releaseDateString, "yyyy'年'MM'月'dd'日'", CultureInfo.InvariantCulture,
DateTimeStyles.None, out releaseDate);
break;
case SupportedLanguages.en_US:
DateTime.TryParseExact(releaseDateString, "MMM/dd/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out releaseDate);
break;
case SupportedLanguages.ko_KR:
DateTime.TryParseExact(releaseDateString, "yyyy'년 'MM'월 'dd'일'",
CultureInfo.InvariantCulture, DateTimeStyles.None, out releaseDate);
break;
case SupportedLanguages.es_ES:
DateTime.TryParseExact(releaseDateString, "MM/dd/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out releaseDate);
break;
case SupportedLanguages.de_DE:
case SupportedLanguages.fr_FR:
case SupportedLanguages.id_ID:
case SupportedLanguages.it_IT:
case SupportedLanguages.pt_BR:
case SupportedLanguages.sv_SE:
case SupportedLanguages.th_TH:
case SupportedLanguages.vi_VN:
DateTime.TryParseExact(releaseDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out releaseDate);
break;
default:
throw new ArgumentOutOfRangeException(nameof(language), language, "Unsupported language");
}
result.ReleaseDate = releaseDate;
continue;
}
if (header.TextContent.Contains(TranslationDictionary.UpdateDate[language]))
{
result.UpdateDate = header.NextElementSibling?.ChildNodes
.FirstOrDefault(node => node.NodeType == NodeType.Text)?.TextContent.Trim();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Series[language]))
{
result.Series = header.NextElementSibling?.Text().Trim();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Author[language]))
{
result.Author = header.NextElementSibling?.Text().Trim();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Scenario[language]))
{
result.ScenarioWriters = header.NextElementSibling?
.QuerySelectorAll("a")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Illustration[language]))
{
result.Illustrators = header.NextElementSibling?
.QuerySelectorAll("a")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.VoiceActor[language]))
{
result.VoiceActors = header.NextElementSibling?
.QuerySelectorAll("a")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Music[language]))
{
result.MusicCreators = header.NextElementSibling?
.QuerySelectorAll("a")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Age[language]))
{
var ageRating = header.NextElementSibling?.Text().Trim();
if (TranslationDictionary.AllAges[language].Contains(ageRating))
result.Age = DLsiteScrapperResult.AgeRating.AllAges;
else if (ageRating == "R-15")
result.Age = DLsiteScrapperResult.AgeRating.RRated;
else if (TranslationDictionary.Adult[language].Contains(ageRating))
result.Age = DLsiteScrapperResult.AgeRating.Adult;
else
logger.Error($"Unknown age rating: {ageRating}");
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Miscellaneous[language]))
{
result.Miscellaneous = header.NextElementSibling?
.QuerySelectorAll("span")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Genre[language]))
{
result.Genres = header.NextElementSibling?
.QuerySelectorAll("a")
.Select(x => x.Text().Trim())
.ToList();
continue;
}
if (header.TextContent.Contains(TranslationDictionary.Filesize[language]))
result.Filesize = header.NextElementSibling?.Text().Trim();
}
}
var images = ParseImages(document);
result.MainImage = images.FirstOrDefault(x => x.Contains("_img_main"));
result.Icon = result.MainImage?.Replace("_img_main", "_img_sam_mini");
result.ProductImages = images;
var descriptionHtml = document.QuerySelector("[itemprop='description']")?.InnerHtml;
var description = descriptionHtml?.Replace("//img.dlsite.jp", "https://img.dlsite.jp");
result.Description = Regex.Replace(description?.Trim(), @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline);
return result;
}
public async Task<List<DLsiteSearchResult>> ScrapSearchPage(
string searchCategory,
string query,
int maxSearchResults = 30,
SupportedLanguages language = DefaultLanguage
)
{
var searchUrl = string.Format(SearchFormatUrl, searchCategory, query, maxSearchResults, language.ToString());
var client = new HttpClient();
var responseBody = await client.GetStringAsync(searchUrl);
var context = BrowsingContext.New(Configuration.Default);
var document = await context.OpenAsync(req => req.Content(responseBody));
var searchResults = new List<DLsiteSearchResult>();
var searchResultsTable = document.QuerySelector(".work_1col_table.n_worklist");
if (searchResultsTable is null) return searchResults;
var searchResultsRows = searchResultsTable.QuerySelectorAll("tr");
searchResults.AddRange(from row in searchResultsRows
let title = row.QuerySelector(".work_name a")?.Text().Trim()
let link = row.QuerySelector(".work_name a")?.GetAttribute("href")
let excerpt = row.QuerySelector(".work_text")?.Text().Trim()
select new DLsiteSearchResult { Title = title, Link = link, Excerpt = excerpt });
return searchResults;
}
private static List<string> ParseImages(IDocument document)
{
var images = new List<string>();
var imagesContainer = document.GetElementById("work_left");
if (imagesContainer is null) return images;
var imageElements = imagesContainer.QuerySelectorAll(".product-slider-data div")
.Where(x => x.GetAttribute("data-src") is not null)
.Where(x => x.GetAttribute("data-src")!.Contains("img.dlsite.jp"))
.Select(x => x.GetAttribute("data-src"))
.Select(x => x?.Replace("//img.dlsite.jp", "https://img.dlsite.jp"))
.ToList();
images.AddRange(imageElements);
return images;
}
public static bool IsValidUrl(string url)
{
var match = Regex.Match(url, @"https://www\.dlsite\.com/(home|soft|maniax|pro)/work/=/product_id/(RJ|BJ|VJ)(\d{6}|\d{8})\.html");
return match.Success;
}
}