なろうブックマーク分析用ツールのPrism+WPFサンプル実装
Revisión | 7c5d16549d2adb3f7aa393e55e6fb0af28411865 (tree) |
---|---|
Tiempo | 2022-08-06 13:41:38 |
Autor | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[MOD] なろうサービスから通信系の処理をリポジトリに分離
@@ -1,4 +1,4 @@ | ||
1 | -using TestNarou.Domain.Model.App; | |
1 | +using TestNarou.Domain.Model.Config; | |
2 | 2 | |
3 | 3 | namespace TestNarou.Domain.Boundary.Repository |
4 | 4 | { |
@@ -1,4 +1,4 @@ | ||
1 | -using TestNarou.Domain.Model.App; | |
1 | +using TestNarou.Domain.Model.Config; | |
2 | 2 | |
3 | 3 | namespace TestNarou.Domain.Boundary.Service |
4 | 4 | { |
@@ -1,4 +1,4 @@ | ||
1 | -namespace TestNarou.Domain.Model.App | |
1 | +namespace TestNarou.Domain.Model.Config | |
2 | 2 | { |
3 | 3 | public record class AppConfig( |
4 | 4 | NarouLoginSettings NarouLoginSettings |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using System; |
2 | 2 | |
3 | -namespace TestNarou.Domain.Model.App | |
3 | +namespace TestNarou.Domain.Model.Config | |
4 | 4 | { |
5 | 5 | public record class NarouLoginSettings( |
6 | 6 | string NarouID, |
@@ -15,8 +15,8 @@ namespace TestNarou.Domain.Model.Entity.Child | ||
15 | 15 | private readonly CompositeDisposable disposables = new(); |
16 | 16 | private bool disposedValue; |
17 | 17 | |
18 | - public BookmarkCategoryRow(): this( | |
19 | - Index: -1, | |
18 | + public BookmarkCategoryRow(int index): this( | |
19 | + Index: index, | |
20 | 20 | Name: new(), |
21 | 21 | Count: new(), |
22 | 22 | IsSelected: new() |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using TestNarou.Domain.Boundary.Repository; |
2 | 2 | using TestNarou.Domain.Boundary.Service; |
3 | -using TestNarou.Domain.Model.App; | |
3 | +using TestNarou.Domain.Model.Config; | |
4 | 4 | |
5 | 5 | namespace TestNarou.Domain.Service |
6 | 6 | { |
@@ -1,15 +1,13 @@ | ||
1 | -using CleanAuLait.Core.Converter; | |
2 | -using CleanAuLait.Domain.Service; | |
3 | -using NLog; | |
1 | +using NLog; | |
4 | 2 | using Prism.Navigation; |
5 | 3 | using System.Collections.Generic; |
6 | 4 | using System.Linq; |
7 | -using System.Text.RegularExpressions; | |
8 | 5 | using TestNarou.Domain.Boundary.Service; |
9 | 6 | using TestNarou.Domain.Model.API; |
10 | 7 | using TestNarou.Domain.Model.Entity; |
11 | 8 | using TestNarou.Domain.Model.Entity.Child; |
12 | 9 | using TestNarou.Domain.Translator; |
10 | +using TestNarou.OuterEdge.Boundary.Repository.API; | |
13 | 11 | |
14 | 12 | namespace TestNarou.Domain.Service |
15 | 13 | { |
@@ -17,55 +15,32 @@ namespace TestNarou.Domain.Service | ||
17 | 15 | { |
18 | 16 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
19 | 17 | |
20 | - private const string LOGIN_POST_URL = "https://ssl.syosetu.com/login/login/"; | |
21 | - private const string LOGIN_REFFERER_URL = "https://ssl.syosetu.com/login/input/"; | |
22 | - | |
23 | - private const string LOGOUT_POST_URL = "https://ssl.syosetu.com/login/logout/"; | |
24 | - private const string LOGOUT_REFFERER_URL = "https://syosetu.com/user/top/"; | |
25 | - | |
26 | - private const string BOOKMARK_CATEGORY_GET_URL = "https://syosetu.com/favnovelmain/list/"; | |
27 | - private const string BOOKMARK_CATEGORY_REFFERER_URL = "https://syosetu.com/user/top/"; | |
28 | - | |
29 | - private const string BOOKMARK_LIST_GET_URL = "https://syosetu.com/favnovelmain/list/?nowcategory={0}&order=new&p={1}"; | |
30 | - private const string BOOKMARK_LIST_REFFERER_URL = "https://syosetu.com/favnovelmain/list/"; | |
31 | - | |
32 | - private const string NOVEL_API_URL = "https://api.syosetu.com/novelapi/api/?out=json&gzip=5&ncode={0}&lim={1}"; | |
33 | - | |
34 | - private const string LOGIN_FORM_KEY_ID = "narouid"; | |
35 | - private const string LOGIN_FORM_KEY_PW = "pass"; | |
36 | - | |
37 | - private const string LOGOUT_FORM_KEY_TOKEN = "token"; | |
38 | - | |
39 | - private const string LOGIN_ERROR_MESSAGE = "エラーが発生しました"; | |
40 | - private const string LOGIN_ERROR_MESSAGE_REQUIRED_FIELD = "IDまたはメールアドレスが未入力です。"; | |
41 | - private const string LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL = "IDが存在しないかIDとパスワードが一致しません。"; | |
42 | - | |
43 | - private const string DATA_TOKEN_REGEX = @"<a id=""logout"" class=""js-logout"" href=""https://syosetu.com/login/logout/"" data-token=""([0-9a-f]+)"">"; | |
44 | - | |
45 | - private const string CATEGORY_LIST_REGEX = @"<ul class=""category_box"">(.+?)</ul>\s+<div class=""fav_box"">"; | |
46 | - private const string CATEGORY_ITEM_REGEX = @"<li.*><a href=""/favnovelmain/list/\?nowcategory=\d+&order=[a-z]+"">(.+?)</a></li>"; | |
47 | - private const string CATEGORY_NAME_COUNT_REGEX = @"(.+?)\((\d+)\)"; | |
48 | - | |
49 | - private const string BOOKMARK_ENTRY_REGEX = @"<table class=""favnovel"">(.+?)</table>"; | |
50 | - private const string BOOKMARK_ITEM_REGEX = @"<a class=""title"" href=""https://ncode.syosetu.com/(n[0-9a-z]+)/"">"; | |
51 | - | |
52 | 18 | private string DataToken { get; set; } |
53 | 19 | |
54 | 20 | private BookmarkCategory Categeory { get; set; } |
55 | 21 | private BookmarkDetailList DetailList { get; set; } |
56 | 22 | |
23 | + private readonly INarouRepository repo; | |
24 | + | |
57 | 25 | private readonly IBookmarkCategoryRowTranslator vmCategoryRowTranslator; |
58 | 26 | private readonly IBookmarkDetailListRowTranslator vmDetailRowTranslator; |
59 | 27 | |
60 | 28 | public NarouService( |
29 | + INarouRepository repo, | |
61 | 30 | IBookmarkCategoryRowTranslator vmCategoryRowTranslator, |
62 | 31 | IBookmarkDetailListRowTranslator vmDetailRowTranslator |
63 | 32 | ) |
64 | 33 | { |
34 | + this.repo = repo; | |
35 | + | |
65 | 36 | this.vmCategoryRowTranslator = vmCategoryRowTranslator; |
66 | 37 | this.vmDetailRowTranslator = vmDetailRowTranslator; |
67 | 38 | } |
68 | 39 | |
40 | + public void Login(string id, string password) | |
41 | + { | |
42 | + this.DataToken = this.repo.Login(id, password); | |
43 | + } | |
69 | 44 | |
70 | 45 | public void Logout() |
71 | 46 | { |
@@ -74,175 +49,31 @@ namespace TestNarou.Domain.Service | ||
74 | 49 | return; |
75 | 50 | } |
76 | 51 | |
77 | - var form = new Dictionary<string, string>() | |
78 | - { | |
79 | - { LOGOUT_FORM_KEY_TOKEN, this.DataToken }, | |
80 | - }; | |
81 | - | |
82 | - var response = HttpClientHelper.PostAsync(LOGOUT_POST_URL, LOGOUT_REFFERER_URL, form).Result; | |
83 | - | |
84 | -#if false | |
85 | - if (!response.IsSuccessStatusCode) | |
86 | - { | |
87 | - throw new ServiceException($"ログアウト応答が想定と異なります。{response.StatusCode}"); | |
88 | - } | |
89 | -#endif | |
90 | - } | |
91 | - | |
92 | - public void Login(string id, string password) | |
93 | - { | |
94 | - var form = new Dictionary<string, string>() | |
95 | - { | |
96 | - { LOGIN_FORM_KEY_ID, id }, | |
97 | - { LOGIN_FORM_KEY_PW, password }, | |
98 | - }; | |
99 | - | |
100 | - var response = HttpClientHelper.PostAsync(LOGIN_POST_URL, LOGIN_REFFERER_URL, form).Result; | |
101 | - | |
102 | - if (!response.IsSuccessStatusCode) | |
103 | - { | |
104 | - throw new ServiceException($"ログイン応答が想定と異なります。{response.StatusCode}"); | |
105 | - } | |
106 | - | |
107 | - string content = HttpClientHelper.ReadContentAsync(response).Result; | |
108 | - | |
109 | - if (content.Contains(LOGIN_ERROR_MESSAGE)) | |
110 | - { | |
111 | - if (content.Contains(LOGIN_ERROR_MESSAGE_REQUIRED_FIELD)) | |
112 | - { | |
113 | - throw new ServiceException(LOGIN_ERROR_MESSAGE_REQUIRED_FIELD); | |
114 | - } | |
115 | - | |
116 | - if (content.Contains(LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL)) | |
117 | - { | |
118 | - throw new ServiceException(LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL); | |
119 | - } | |
120 | - | |
121 | - logger.Trace(content); | |
122 | - | |
123 | - throw new ServiceException("ログインエラーが発生しました。"); | |
124 | - } | |
125 | - | |
126 | - //<a id="logout" class="js-logout" href="https://syosetu.com/login/logout/" data-token="27098ab9dd0d56d017c7cd3ac650cdeb"> | |
127 | - | |
128 | - Regex regex = new(DATA_TOKEN_REGEX); | |
129 | - Match matcher = regex.Match(content); | |
130 | - | |
131 | - if (matcher.Success) | |
132 | - { | |
133 | - this.DataToken = matcher.Groups[1].Value; | |
134 | - } | |
135 | - | |
136 | - logger.Trace("DataToken {0}", this.DataToken); | |
52 | + this.repo.Logout(this.DataToken); | |
137 | 53 | } |
138 | 54 | |
139 | 55 | public BookmarkCategory GetBookmarkCategory() |
140 | 56 | { |
141 | - var response = HttpClientHelper.GetAsync( | |
142 | - BOOKMARK_CATEGORY_GET_URL, BOOKMARK_CATEGORY_REFFERER_URL).Result; | |
143 | - | |
144 | - if (!response.IsSuccessStatusCode) | |
145 | - { | |
146 | - throw new ServiceException($"応答が想定と異なります。{response.StatusCode}"); | |
147 | - } | |
57 | + IEnumerable<NarouCategoryInfo> infos = this.repo.GetNarouCategoryList(); | |
148 | 58 | |
149 | - string content = HttpClientHelper.ReadContentAsync(response).Result; | |
150 | - | |
151 | - Regex regex = new(CATEGORY_LIST_REGEX, RegexOptions.Singleline); | |
152 | - Regex regexItem = new(CATEGORY_ITEM_REGEX, RegexOptions.Multiline); | |
153 | - Regex regexNameCount = new(CATEGORY_NAME_COUNT_REGEX); | |
154 | - | |
155 | - Match matcher = regex.Match(content); | |
156 | - | |
157 | - IList<NarouCategoryInfo> rows = new List<NarouCategoryInfo>(); | |
158 | - | |
159 | - if (matcher.Success) | |
160 | - { | |
161 | - string categoryList = matcher.Groups[1].Value; | |
162 | - | |
163 | - //logger.Trace("categoryList: {0}", categoryList); | |
164 | - | |
165 | - MatchCollection matcherItems = regexItem.Matches(categoryList); | |
166 | - | |
167 | - foreach (Match matchItem in matcherItems) | |
168 | - { | |
169 | - string item = matchItem.Groups[1].Value; | |
170 | - | |
171 | - logger.Trace("categoryItem: {0}", item); | |
172 | - | |
173 | - Match matcherNameCount = regexNameCount.Match(item); | |
174 | - | |
175 | - if (matcherNameCount.Success) | |
176 | - { | |
177 | - string title = matcherNameCount.Groups[1].Value; | |
178 | - int count = int.Parse(matcherNameCount.Groups[2].Value); | |
179 | - //logger.Trace("categoryNameCount: {0} {1}", title, count); | |
180 | - | |
181 | - rows.Add(new(title, count)); | |
182 | - } | |
183 | - } | |
184 | - } | |
185 | - | |
186 | - IList<BookmarkCategoryRow> rows2 = new List<BookmarkCategoryRow>(); | |
187 | - | |
188 | - // TODO LINQ ZIP | |
189 | - | |
190 | - int rowIndex = 1; | |
191 | - | |
192 | - foreach (var row in rows.Select(v => this.vmCategoryRowTranslator.Translate(v))) | |
193 | - { | |
194 | - rows2.Add(row with { Index = rowIndex++ }); | |
195 | - } | |
59 | + IEnumerable<BookmarkCategoryRow> rows = infos.Zip( | |
60 | + Enumerable.Range(1, infos.Count()), | |
61 | + (category, no) => (category, no) | |
62 | + ).Select( | |
63 | + pair => this.vmCategoryRowTranslator.Translate(pair.category, pair.no) | |
64 | + ); | |
196 | 65 | |
197 | 66 | this.Categeory?.Dispose(); |
198 | - this.Categeory = new(rows2); | |
67 | + this.Categeory = new(rows); | |
199 | 68 | |
200 | 69 | return this.Categeory; |
201 | 70 | } |
202 | 71 | |
203 | 72 | public BookmarkDetailList GetBookmarkDetailList(int categoryNo) |
204 | 73 | { |
205 | - IList<string> ncodeList = new List<string>(); | |
206 | - | |
207 | - int count = this.Categeory.Rows[categoryNo - 1].Count.Value; | |
208 | - | |
209 | - for (int page = 1; page <= (count - 1) / 50 + 1; page++) | |
210 | - { | |
211 | - var response = HttpClientHelper.GetAsync( | |
212 | - string.Format(BOOKMARK_LIST_GET_URL, categoryNo, page), BOOKMARK_LIST_REFFERER_URL).Result; | |
213 | - | |
214 | - if (!response.IsSuccessStatusCode) | |
215 | - { | |
216 | - throw new ServiceException($"応答が想定と異なります。{response.StatusCode}"); | |
217 | - } | |
218 | - | |
219 | - string content = HttpClientHelper.ReadContentAsync(response).Result; | |
220 | - | |
221 | - Regex regex = new(BOOKMARK_ENTRY_REGEX, RegexOptions.Singleline); | |
222 | - Regex regexItem = new(BOOKMARK_ITEM_REGEX, RegexOptions.Singleline); | |
223 | - | |
224 | - MatchCollection matchers = regex.Matches(content); | |
225 | - | |
226 | - foreach (Match matcher in matchers) | |
227 | - { | |
228 | - string entry = matcher.Groups[1].Value; | |
229 | - | |
230 | - Match matcherItem = regexItem.Match(entry); | |
231 | - | |
232 | - if (matcherItem.Success) | |
233 | - { | |
234 | - string ncode = matcherItem.Groups[1].Value; | |
235 | - | |
236 | - logger.Trace("bookmark: {0}", ncode); | |
237 | - | |
238 | - ncodeList.Add(ncode); | |
239 | - } | |
240 | - } | |
241 | - } | |
242 | - | |
243 | - string narouJson = GetNarouJson(ncodeList); | |
244 | - | |
245 | - NarouNovelDetailInfo[] novelInfos = narouJson.DeserializeJson<NarouNovelDetailInfo[]>(); | |
74 | + IEnumerable<NarouNovelDetailInfo> novelInfos = | |
75 | + this.repo.GetNovelDetailInfoList( | |
76 | + categoryNo, this.Categeory.Rows[categoryNo - 1].Count.Value); | |
246 | 77 | |
247 | 78 | IEnumerable<BookmarkDetailListRow> detailRows = |
248 | 79 | novelInfos.Skip(1).Select(v => vmDetailRowTranslator.Translate(v)); |
@@ -253,26 +84,12 @@ namespace TestNarou.Domain.Service | ||
253 | 84 | return this.DetailList; |
254 | 85 | } |
255 | 86 | |
256 | - private static string GetNarouJson(IList<string> ncodeList) | |
257 | - { | |
258 | - string apiUrl = string.Format(NOVEL_API_URL, string.Join('-', ncodeList), ncodeList.Count); | |
259 | - | |
260 | - var response = HttpClientHelper.GetAsync(apiUrl, null).Result; | |
261 | - | |
262 | - if (!response.IsSuccessStatusCode) | |
263 | - { | |
264 | - throw new ServiceException($"応答が想定と異なります。{response.StatusCode}"); | |
265 | - } | |
266 | - | |
267 | - //string json = HttpClientHelper.ReadContentAsync(response).Result; | |
268 | - string json = HttpClientHelper.ReadGzipContent(response); | |
269 | - | |
270 | - return json; | |
271 | - } | |
272 | - | |
273 | 87 | public void Destroy() |
274 | 88 | { |
275 | - HttpClientHelper.Destroy(); | |
89 | + this.Categeory?.Dispose(); | |
90 | + this.DetailList?.Dispose(); | |
91 | + | |
92 | + this.repo?.Destroy(); | |
276 | 93 | } |
277 | 94 | } |
278 | 95 | } |
@@ -17,10 +17,10 @@ namespace TestNarou.Domain.Translator | ||
17 | 17 | this.mapper = mapper; |
18 | 18 | } |
19 | 19 | |
20 | - public BookmarkCategoryRow Translate(NarouCategoryInfo info) | |
20 | + public BookmarkCategoryRow Translate(NarouCategoryInfo info, int index) | |
21 | 21 | { |
22 | 22 | BookmarkCategoryRow row = info != null |
23 | - ? mapper.Map(info, new BookmarkCategoryRow()) | |
23 | + ? mapper.Map(info, new BookmarkCategoryRow(index)) | |
24 | 24 | : null; |
25 | 25 | |
26 | 26 | return row; |
@@ -5,6 +5,6 @@ namespace TestNarou.Domain.Translator | ||
5 | 5 | { |
6 | 6 | internal interface IBookmarkCategoryRowTranslator |
7 | 7 | { |
8 | - BookmarkCategoryRow Translate(NarouCategoryInfo info); | |
8 | + BookmarkCategoryRow Translate(NarouCategoryInfo info, int index); | |
9 | 9 | } |
10 | 10 | } |
\ No newline at end of file |
@@ -2,7 +2,7 @@ | ||
2 | 2 | using CleanAuLait.UseCase.Interactor; |
3 | 3 | using CleanAuLait.UseCase.Response; |
4 | 4 | using TestNarou.Domain.Boundary.Service; |
5 | -using TestNarou.Domain.Model.App; | |
5 | +using TestNarou.Domain.Model.Config; | |
6 | 6 | using TestNarou.Domain.Model.Entity; |
7 | 7 | using TestNarou.UseCase.Boundary.Interactor; |
8 | 8 | using TestNarou.UseCase.Boundary.Presenter; |
@@ -2,10 +2,7 @@ | ||
2 | 2 | using CleanAuLait.UseCase.Interactor; |
3 | 3 | using CleanAuLait.UseCase.Response; |
4 | 4 | using TestNarou.Domain.Boundary.Service; |
5 | -using TestNarou.Domain.Model.App; | |
6 | -using TestNarou.Domain.Model.Entity; | |
7 | 5 | using TestNarou.UseCase.Boundary.Interactor; |
8 | -using TestNarou.UseCase.Boundary.Presenter; | |
9 | 6 | using TestNarou.UseCase.Request; |
10 | 7 | using TestNarou.UseCase.Response; |
11 | 8 |
@@ -1,5 +1,5 @@ | ||
1 | 1 | using CleanAuLait.UseCase.Response; |
2 | -using TestNarou.Domain.Model.App; | |
2 | +using TestNarou.Domain.Model.Config; | |
3 | 3 | |
4 | 4 | namespace TestNarou.UseCase.Response |
5 | 5 | { |
@@ -1,5 +1,5 @@ | ||
1 | 1 | using CleanAuLait.UseCase.Response; |
2 | -using TestNarou.Domain.Model.App; | |
2 | +using TestNarou.Domain.Model.Config; | |
3 | 3 | |
4 | 4 | namespace TestNarou.UseCase.Response |
5 | 5 | { |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using Reactive.Bindings; |
2 | 2 | using System; |
3 | -using TestNarou.Domain.Model.App; | |
3 | +using TestNarou.Domain.Model.Config; | |
4 | 4 | |
5 | 5 | namespace TestNarou.Adaptor.Boundary.Gateway.ViewModel |
6 | 6 | { |
@@ -7,7 +7,7 @@ using System.ComponentModel; | ||
7 | 7 | using System.Reactive.Disposables; |
8 | 8 | using TestNarou.Adaptor.Boundary.Controller; |
9 | 9 | using TestNarou.Adaptor.Boundary.Gateway.ViewModel; |
10 | -using TestNarou.Domain.Model.App; | |
10 | +using TestNarou.Domain.Model.Config; | |
11 | 11 | using TestNarou.UseCase.Request; |
12 | 12 | |
13 | 13 | namespace TestNarou.Adaptor.Gateway.ViewModel |
@@ -0,0 +1,15 @@ | ||
1 | +using Prism.Navigation; | |
2 | +using System.Collections.Generic; | |
3 | +using TestNarou.Domain.Model.API; | |
4 | + | |
5 | +namespace TestNarou.OuterEdge.Boundary.Repository.API | |
6 | +{ | |
7 | + internal interface INarouRepository : IDestructible | |
8 | + { | |
9 | + string Login(string id, string password); | |
10 | + void Logout(string dataToken); | |
11 | + | |
12 | + IEnumerable<NarouCategoryInfo> GetNarouCategoryList(); | |
13 | + IEnumerable<NarouNovelDetailInfo> GetNovelDetailInfoList(int categoryNo, int categoryItemsCount); | |
14 | + } | |
15 | +} | |
\ No newline at end of file |
@@ -5,6 +5,8 @@ using Prism.Mvvm; | ||
5 | 5 | using Prism.Regions; |
6 | 6 | using TestNarou.Adaptor.Boundary.Gateway.ViewModel; |
7 | 7 | using TestNarou.Domain.Boundary.Repository; |
8 | +using TestNarou.OuterEdge.Boundary.Repository.API; | |
9 | +using TestNarou.OuterEdge.Repository.API; | |
8 | 10 | using TestNarou.OuterEdge.Repository.Setting; |
9 | 11 | using TestNarou.OuterEdge.UI.View; |
10 | 12 |
@@ -28,6 +30,7 @@ namespace TestNarou.OuterEdge | ||
28 | 30 | /// |
29 | 31 | |
30 | 32 | containerRegistry.RegisterSingleton<IAppConfigRepository, AppConfigRepository>(); |
33 | + containerRegistry.RegisterSingleton<INarouRepository, NarouRepository>(); | |
31 | 34 | |
32 | 35 | logger.Trace("RegisterTypes end"); |
33 | 36 | } |
@@ -1,17 +1,17 @@ | ||
1 | 1 | using NLog; |
2 | +using Prism.Navigation; | |
2 | 3 | using System; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.IO; |
5 | 6 | using System.IO.Compression; |
6 | -using System.Linq; | |
7 | 7 | using System.Net; |
8 | 8 | using System.Net.Http; |
9 | 9 | using System.Text; |
10 | 10 | using System.Threading.Tasks; |
11 | 11 | |
12 | -namespace TestNarou.Domain.Service | |
12 | +namespace TestNarou.OuterEdge.Repository.API | |
13 | 13 | { |
14 | - internal static class HttpClientHelper | |
14 | + internal class HttpClientHelper : IDestructible | |
15 | 15 | { |
16 | 16 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
17 | 17 |
@@ -20,13 +20,13 @@ namespace TestNarou.Domain.Service | ||
20 | 20 | private const string REQUEST_HEADER_VALUE_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"; |
21 | 21 | private const string REQUEST_HEADER_KEY_REFERRER = "Referer"; |
22 | 22 | |
23 | - private const string RESPONSE_HEADER_KEY_COOKIE = "Set-Cookie"; | |
23 | + //private const string RESPONSE_HEADER_KEY_COOKIE = "Set-Cookie"; | |
24 | 24 | |
25 | - private static readonly CookieContainer cookies; | |
26 | - private static readonly HttpClientHandler httpClientHandler; | |
27 | - private static readonly HttpClient httpClient; | |
25 | + private readonly CookieContainer cookies; | |
26 | + private readonly HttpClientHandler httpClientHandler; | |
27 | + private readonly HttpClient httpClient; | |
28 | 28 | |
29 | - static HttpClientHelper() | |
29 | + public HttpClientHelper() | |
30 | 30 | { |
31 | 31 | cookies = new(); |
32 | 32 |
@@ -39,7 +39,7 @@ namespace TestNarou.Domain.Service | ||
39 | 39 | httpClient = new(httpClientHandler); |
40 | 40 | |
41 | 41 | // dummy cookies |
42 | - Uri uri = new Uri("https://syosetu.com"); | |
42 | + Uri uri = new("https://syosetu.com"); | |
43 | 43 | |
44 | 44 | (string key, string value)[] keyValues = new[] |
45 | 45 | { |
@@ -59,14 +59,14 @@ namespace TestNarou.Domain.Service | ||
59 | 59 | ("_ga", "GA1.2.636765982.1649058346"), |
60 | 60 | }; |
61 | 61 | |
62 | - foreach (var keyValue in keyValues) | |
62 | + foreach (var (key, value) in keyValues) | |
63 | 63 | { |
64 | - Cookie cookie = new Cookie(keyValue.key, keyValue.value); | |
64 | + Cookie cookie = new(key, value); | |
65 | 65 | cookies.Add(uri, cookie); |
66 | 66 | } |
67 | 67 | } |
68 | 68 | |
69 | - public static async Task<HttpResponseMessage> GetAsync(string url, string referer) | |
69 | + public async Task<HttpResponseMessage> GetAsync(string url, string referer) | |
70 | 70 | { |
71 | 71 | var request = CreateRequestMessage(url, referer, HttpMethod.Post); |
72 | 72 |
@@ -75,21 +75,21 @@ namespace TestNarou.Domain.Service | ||
75 | 75 | var response = await httpClient.SendAsync(request).ConfigureAwait(false); |
76 | 76 | |
77 | 77 | ShowResponseHeaders(response); |
78 | - ShowCookies(response); | |
78 | + ShowCookies(); | |
79 | 79 | |
80 | 80 | logger.Trace("StatusCode: {0}", response.StatusCode); |
81 | 81 | |
82 | 82 | return response; |
83 | 83 | } |
84 | 84 | |
85 | - public static async Task<HttpResponseMessage> PostAsync(string url, string referer, IDictionary<string, string> form) | |
85 | + public async Task<HttpResponseMessage> PostAsync(string url, string referer, IDictionary<string, string> form) | |
86 | 86 | { |
87 | 87 | var content = new FormUrlEncodedContent(form); |
88 | 88 | |
89 | 89 | return await PostAsync(url, referer, content).ConfigureAwait(false); |
90 | 90 | } |
91 | 91 | |
92 | - public static async Task<HttpResponseMessage> PostAsync(string url, string referer, FormUrlEncodedContent content) | |
92 | + public async Task<HttpResponseMessage> PostAsync(string url, string referer, FormUrlEncodedContent content) | |
93 | 93 | { |
94 | 94 | var request = CreateRequestMessage(url, referer, HttpMethod.Post); |
95 | 95 |
@@ -100,14 +100,14 @@ namespace TestNarou.Domain.Service | ||
100 | 100 | var response = await httpClient.SendAsync(request).ConfigureAwait(false); |
101 | 101 | |
102 | 102 | ShowResponseHeaders(response); |
103 | - ShowCookies(response); | |
103 | + ShowCookies(); | |
104 | 104 | |
105 | 105 | logger.Trace("StatusCode: {0}", response.StatusCode); |
106 | 106 | |
107 | 107 | return response; |
108 | 108 | } |
109 | 109 | |
110 | - public static HttpRequestMessage CreateRequestMessage(string url, string referer, HttpMethod method) | |
110 | + private static HttpRequestMessage CreateRequestMessage(string url, string referer, HttpMethod method) | |
111 | 111 | { |
112 | 112 | var request = new HttpRequestMessage(method, url); |
113 | 113 |
@@ -139,13 +139,8 @@ namespace TestNarou.Domain.Service | ||
139 | 139 | return request; |
140 | 140 | } |
141 | 141 | |
142 | - private static void ShowCookies(HttpResponseMessage response) | |
142 | + private void ShowCookies() | |
143 | 143 | { |
144 | - //foreach (var cookie in response.Headers.GetValues(RESPONSE_HEADER_KEY_COOKIE)) | |
145 | - //{ | |
146 | - // logger.Trace("Set-Cookie: {0}", cookie); | |
147 | - //} | |
148 | - | |
149 | 144 | foreach (Cookie cookie in cookies.GetAllCookies()) |
150 | 145 | { |
151 | 146 | logger.Trace("Cookie: {0}, {1}, {2}, {3}", |
@@ -181,7 +176,7 @@ namespace TestNarou.Domain.Service | ||
181 | 176 | return Encoding.UTF8.GetString(raw); |
182 | 177 | } |
183 | 178 | |
184 | - public static void Destroy() | |
179 | + public void Destroy() | |
185 | 180 | { |
186 | 181 | httpClient.Dispose(); |
187 | 182 | } |
@@ -0,0 +1,260 @@ | ||
1 | +using CleanAuLait.Core.Converter; | |
2 | +using CleanAuLait.OuterEdge.Repository; | |
3 | +using NLog; | |
4 | +using System.Collections.Generic; | |
5 | +using System.Text.RegularExpressions; | |
6 | +using TestNarou.Domain.Model.API; | |
7 | +using TestNarou.OuterEdge.Boundary.Repository.API; | |
8 | + | |
9 | +namespace TestNarou.OuterEdge.Repository.API | |
10 | +{ | |
11 | + internal class NarouRepository : INarouRepository | |
12 | + { | |
13 | + private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
14 | + | |
15 | + private const string LOGIN_POST_URL = "https://ssl.syosetu.com/login/login/"; | |
16 | + private const string LOGIN_REFFERER_URL = "https://ssl.syosetu.com/login/input/"; | |
17 | + | |
18 | + private const string LOGOUT_POST_URL = "https://ssl.syosetu.com/login/logout/"; | |
19 | + private const string LOGOUT_REFFERER_URL = "https://syosetu.com/user/top/"; | |
20 | + | |
21 | + private const string BOOKMARK_CATEGORY_GET_URL = "https://syosetu.com/favnovelmain/list/"; | |
22 | + private const string BOOKMARK_CATEGORY_REFFERER_URL = "https://syosetu.com/user/top/"; | |
23 | + | |
24 | + private const string BOOKMARK_LIST_GET_URL = "https://syosetu.com/favnovelmain/list/?nowcategory={0}&order=new&p={1}"; | |
25 | + private const string BOOKMARK_LIST_REFFERER_URL = "https://syosetu.com/favnovelmain/list/"; | |
26 | + | |
27 | + private const string NOVEL_API_URL = "https://api.syosetu.com/novelapi/api/?out=json&gzip=5&ncode={0}&lim={1}"; | |
28 | + | |
29 | + private const string LOGIN_FORM_KEY_ID = "narouid"; | |
30 | + private const string LOGIN_FORM_KEY_PW = "pass"; | |
31 | + | |
32 | + private const string LOGOUT_FORM_KEY_TOKEN = "token"; | |
33 | + | |
34 | + private const string LOGIN_ERROR_MESSAGE = "エラーが発生しました"; | |
35 | + private const string LOGIN_ERROR_MESSAGE_REQUIRED_FIELD = "IDまたはメールアドレスが未入力です。"; | |
36 | + private const string LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL = "IDが存在しないかIDとパスワードが一致しません。"; | |
37 | + | |
38 | + private const string DATA_TOKEN_REGEX = @"<a id=""logout"" class=""js-logout"" href=""https://syosetu.com/login/logout/"" data-token=""([0-9a-f]+)"">"; | |
39 | + | |
40 | + private const string CATEGORY_LIST_REGEX = @"<ul class=""category_box"">(.+?)</ul>\s+<div class=""fav_box"">"; | |
41 | + private const string CATEGORY_ITEM_REGEX = @"<li.*><a href=""/favnovelmain/list/\?nowcategory=\d+&order=[a-z]+"">(.+?)</a></li>"; | |
42 | + private const string CATEGORY_NAME_COUNT_REGEX = @"(.+?)\((\d+)\)"; | |
43 | + | |
44 | + private const string BOOKMARK_ENTRY_REGEX = @"<table class=""favnovel"">(.+?)</table>"; | |
45 | + private const string BOOKMARK_ITEM_REGEX = @"<a class=""title"" href=""https://ncode.syosetu.com/(n[0-9a-z]+)/"">"; | |
46 | + | |
47 | + private readonly HttpClientHelper httpClient = new(); | |
48 | + | |
49 | + public string Login(string id, string password) | |
50 | + { | |
51 | + var form = new Dictionary<string, string>() | |
52 | + { | |
53 | + { LOGIN_FORM_KEY_ID, id }, | |
54 | + { LOGIN_FORM_KEY_PW, password }, | |
55 | + }; | |
56 | + | |
57 | + var response = this.httpClient.PostAsync(LOGIN_POST_URL, LOGIN_REFFERER_URL, form).Result; | |
58 | + | |
59 | + if (!response.IsSuccessStatusCode) | |
60 | + { | |
61 | + throw new RepositoryException($"ログイン応答が想定と異なります。{response.StatusCode}"); | |
62 | + } | |
63 | + | |
64 | + string content = HttpClientHelper.ReadContentAsync(response).Result; | |
65 | + | |
66 | + if (content.Contains(LOGIN_ERROR_MESSAGE)) | |
67 | + { | |
68 | + if (content.Contains(LOGIN_ERROR_MESSAGE_REQUIRED_FIELD)) | |
69 | + { | |
70 | + throw new RepositoryException(LOGIN_ERROR_MESSAGE_REQUIRED_FIELD); | |
71 | + } | |
72 | + | |
73 | + if (content.Contains(LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL)) | |
74 | + { | |
75 | + throw new RepositoryException(LOGIN_ERROR_MESSAGE_INVALID_CREDENTIAL); | |
76 | + } | |
77 | + | |
78 | + logger.Trace(content); | |
79 | + | |
80 | + throw new RepositoryException("ログインエラーが発生しました。"); | |
81 | + } | |
82 | + | |
83 | + //<a id="logout" class="js-logout" href="https://syosetu.com/login/logout/" data-token="27098ab9dd0d56d017c7cd3ac650cdeb"> | |
84 | + | |
85 | + Regex regex = new(DATA_TOKEN_REGEX); | |
86 | + Match matcher = regex.Match(content); | |
87 | + | |
88 | + if (!matcher.Success) | |
89 | + { | |
90 | + throw new RepositoryException("ログイン後に DataToken が見つかりませんでした。"); | |
91 | + } | |
92 | + | |
93 | + string dataToken = matcher.Groups[1].Value; | |
94 | + | |
95 | + logger.Trace("DataToken {0}", dataToken); | |
96 | + | |
97 | + return dataToken; | |
98 | + } | |
99 | + | |
100 | + public void Logout(string dataToken) | |
101 | + { | |
102 | + if (string.IsNullOrEmpty(dataToken)) | |
103 | + { | |
104 | + return; | |
105 | + } | |
106 | + | |
107 | + var form = new Dictionary<string, string>() | |
108 | + { | |
109 | + { LOGOUT_FORM_KEY_TOKEN, dataToken }, | |
110 | + }; | |
111 | + | |
112 | + var response = this.httpClient.PostAsync(LOGOUT_POST_URL, LOGOUT_REFFERER_URL, form).Result; | |
113 | + | |
114 | +#if false | |
115 | + if (!response.IsSuccessStatusCode) | |
116 | + { | |
117 | + throw new RepositoryException($"ログアウト応答が想定と異なります。{response.StatusCode}"); | |
118 | + } | |
119 | +#endif | |
120 | + } | |
121 | + | |
122 | + public IEnumerable<NarouCategoryInfo> GetNarouCategoryList() | |
123 | + { | |
124 | + var response = this.httpClient.GetAsync( | |
125 | + BOOKMARK_CATEGORY_GET_URL, BOOKMARK_CATEGORY_REFFERER_URL).Result; | |
126 | + | |
127 | + if (!response.IsSuccessStatusCode) | |
128 | + { | |
129 | + throw new RepositoryException($"応答が想定と異なります。{response.StatusCode}"); | |
130 | + } | |
131 | + | |
132 | + string content = HttpClientHelper.ReadContentAsync(response).Result; | |
133 | + | |
134 | + Regex regex = new(CATEGORY_LIST_REGEX, RegexOptions.Singleline); | |
135 | + Regex regexItem = new(CATEGORY_ITEM_REGEX, RegexOptions.Multiline); | |
136 | + Regex regexNameCount = new(CATEGORY_NAME_COUNT_REGEX); | |
137 | + | |
138 | + Match matcher = regex.Match(content); | |
139 | + | |
140 | + if (!matcher.Success) | |
141 | + { | |
142 | + throw new RepositoryException("ブックマークカテゴリが見つかりません。"); | |
143 | + } | |
144 | + | |
145 | + string categoryList = matcher.Groups[1].Value; | |
146 | + | |
147 | + //logger.Trace("categoryList: {0}", categoryList); | |
148 | + | |
149 | + MatchCollection matcherItems = regexItem.Matches(categoryList); | |
150 | + | |
151 | + if (matcherItems.Count == 0) | |
152 | + { | |
153 | + throw new RepositoryException("ブックマークカテゴリの明細情報が見つかりません。"); | |
154 | + } | |
155 | + | |
156 | + IList<NarouCategoryInfo> infos = new List<NarouCategoryInfo>(); | |
157 | + | |
158 | + foreach (Match matchItem in matcherItems) | |
159 | + { | |
160 | + string item = matchItem.Groups[1].Value; | |
161 | + | |
162 | + logger.Trace("categoryItem: {0}", item); | |
163 | + | |
164 | + Match matcherNameCount = regexNameCount.Match(item); | |
165 | + | |
166 | + if (!matcherNameCount.Success) | |
167 | + { | |
168 | + throw new RepositoryException("ブックマークカテゴリ明細情報の形式が想定と異なります。"); | |
169 | + } | |
170 | + | |
171 | + string title = matcherNameCount.Groups[1].Value; | |
172 | + int count = int.Parse(matcherNameCount.Groups[2].Value); | |
173 | + //logger.Trace("categoryNameCount: {0} {1}", title, count); | |
174 | + | |
175 | + infos.Add(new(title, count)); | |
176 | + } | |
177 | + | |
178 | + return infos; | |
179 | + } | |
180 | + | |
181 | + public IEnumerable<NarouNovelDetailInfo> GetNovelDetailInfoList(int categoryNo, int categoryItemsCount) | |
182 | + { | |
183 | + IList<string> ncodeList = new List<string>(); | |
184 | + | |
185 | + for (int page = 1; page <= (categoryItemsCount - 1) / 50 + 1; page++) | |
186 | + { | |
187 | + var response = this.httpClient.GetAsync( | |
188 | + string.Format(BOOKMARK_LIST_GET_URL, categoryNo, page), BOOKMARK_LIST_REFFERER_URL).Result; | |
189 | + | |
190 | + if (!response.IsSuccessStatusCode) | |
191 | + { | |
192 | + throw new RepositoryException($"応答が想定と異なります。{response.StatusCode}"); | |
193 | + } | |
194 | + | |
195 | + string content = HttpClientHelper.ReadContentAsync(response).Result; | |
196 | + | |
197 | + Regex regex = new(BOOKMARK_ENTRY_REGEX, RegexOptions.Singleline); | |
198 | + Regex regexItem = new(BOOKMARK_ITEM_REGEX, RegexOptions.Singleline); | |
199 | + | |
200 | + MatchCollection matchers = regex.Matches(content); | |
201 | + | |
202 | + if (matchers.Count == 0) | |
203 | + { | |
204 | + throw new RepositoryException("ブックマークの明細リストが見つかりません。"); | |
205 | + } | |
206 | + | |
207 | + foreach (Match matcher in matchers) | |
208 | + { | |
209 | + string entry = matcher.Groups[1].Value; | |
210 | + | |
211 | + Match matcherItem = regexItem.Match(entry); | |
212 | + | |
213 | + if (!matcherItem.Success) | |
214 | + { | |
215 | + throw new RepositoryException("ブックマーク明細情報の形式が想定と異なります。"); | |
216 | + } | |
217 | + | |
218 | + string ncode = matcherItem.Groups[1].Value; | |
219 | + | |
220 | + logger.Trace("bookmark: {0}", ncode); | |
221 | + | |
222 | + ncodeList.Add(ncode); | |
223 | + } | |
224 | + } | |
225 | + | |
226 | + string narouJson = GetNarouJson(ncodeList); | |
227 | + | |
228 | + NarouNovelDetailInfo[] novelInfos = narouJson.DeserializeJson<NarouNovelDetailInfo[]>(); | |
229 | + | |
230 | + if (novelInfos == null) | |
231 | + { | |
232 | + throw new RepositoryException("なろうAPIからの応答が想定と異なります"); | |
233 | + } | |
234 | + | |
235 | + return novelInfos; | |
236 | + } | |
237 | + | |
238 | + private string GetNarouJson(IList<string> ncodeList) | |
239 | + { | |
240 | + string apiUrl = string.Format(NOVEL_API_URL, string.Join('-', ncodeList), ncodeList.Count); | |
241 | + | |
242 | + var response = this.httpClient.GetAsync(apiUrl, null).Result; | |
243 | + | |
244 | + if (!response.IsSuccessStatusCode) | |
245 | + { | |
246 | + throw new RepositoryException($"応答が想定と異なります。{response.StatusCode}"); | |
247 | + } | |
248 | + | |
249 | + string json = HttpClientHelper.ReadGzipContent(response); | |
250 | + | |
251 | + return json; | |
252 | + } | |
253 | + | |
254 | + public void Destroy() | |
255 | + { | |
256 | + this.httpClient?.Destroy(); | |
257 | + } | |
258 | + | |
259 | + } | |
260 | +} |
@@ -1,19 +1,26 @@ | ||
1 | 1 | using CleanAuLait.Core.Converter; |
2 | +using NLog; | |
2 | 3 | using TestNarou.Domain.Boundary.Repository; |
3 | -using TestNarou.Domain.Model.App; | |
4 | +using TestNarou.Domain.Model.Config; | |
4 | 5 | using TestNarou.Properties; |
5 | 6 | |
6 | 7 | namespace TestNarou.OuterEdge.Repository.Setting |
7 | 8 | { |
8 | 9 | internal class AppConfigRepository : IAppConfigRepository |
9 | 10 | { |
11 | + private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
12 | + | |
10 | 13 | public AppConfig Load() |
11 | 14 | { |
12 | 15 | string json = Settings.Default.AppConfigJson; |
13 | 16 | |
14 | - AppConfig appConfig = !string.IsNullOrEmpty(json) | |
15 | - ? json.DeserializeJson<AppConfig>() | |
16 | - : new(); | |
17 | + AppConfig appConfig = json.DeserializeJson<AppConfig>(); | |
18 | + | |
19 | + if (appConfig == null) | |
20 | + { | |
21 | + logger.Warn("restore application settings failed."); | |
22 | + appConfig = new(); | |
23 | + } | |
17 | 24 | |
18 | 25 | return appConfig; |
19 | 26 | } |
@@ -4,7 +4,6 @@ using CleanAuLait.Core.Log; | ||
4 | 4 | using NLog; |
5 | 5 | using Prism.Ioc; |
6 | 6 | using Prism.Modularity; |
7 | -using Prism.Regions; | |
8 | 7 | using Prism.Unity; |
9 | 8 | using System; |
10 | 9 | using System.Reflection; |
@@ -13,7 +12,6 @@ using System.Windows; | ||
13 | 12 | using System.Windows.Threading; |
14 | 13 | using TestNarou.Adaptor; |
15 | 14 | using TestNarou.Adaptor.Boundary.Controller; |
16 | -using TestNarou.Adaptor.Boundary.Gateway.ViewModel; | |
17 | 15 | using TestNarou.Core.Mapper; |
18 | 16 | using TestNarou.Domain; |
19 | 17 | using TestNarou.Infra; |
@@ -207,17 +205,20 @@ namespace TestNarou | ||
207 | 205 | logger.Info("{0} v{1}.{2}.{3}.{4}", |
208 | 206 | asm.Name, ver.Major, ver.Minor, ver.Build, ver.Revision); |
209 | 207 | |
210 | - { | |
211 | - var wc = Container.Resolve<IAppWindowController>(); ; | |
208 | + PrepareInitialView(); | |
212 | 209 | |
213 | - // Load Application Settings | |
214 | - _ = wc.Execute(new AppConfigLoadRequest()); | |
210 | + logger.Trace("OnStartup end"); | |
211 | + } | |
215 | 212 | |
216 | - // Navigate Login Form View | |
217 | - wc.NavigateLoginFormView(); | |
218 | - } | |
213 | + private void PrepareInitialView() | |
214 | + { | |
215 | + var wc = Container.Resolve<IAppWindowController>(); ; | |
219 | 216 | |
220 | - logger.Trace("OnStartup end"); | |
217 | + // Load Application Settings | |
218 | + _ = wc.Execute(new AppConfigLoadRequest()); | |
219 | + | |
220 | + // Navigate Login Form View | |
221 | + wc.NavigateLoginFormView(); | |
221 | 222 | } |
222 | 223 | |
223 | 224 | protected override void OnExit(ExitEventArgs e) |