@@ -51,7 +51,35 @@ private static Tag _FindCache(Namespace ns, string raw)
5151 return tag ;
5252 }
5353
54- public static async Task < ( Namespace , string ) > CheckAsync ( Namespace ns , string raw )
54+ private static async Task < TagSuggest > _PostApiAsync ( Namespace ? ns , string raw )
55+ {
56+ var text = raw . Length > 50 ? raw . Substring ( 0 , 50 ) : raw ;
57+ if ( ns != null )
58+ text = ns + ":" + raw ;
59+ var response = await _HttpClient . PostAsync ( "https://api.e-hentai.org/api.php" , new StringContent ( JsonConvert . SerializeObject ( new
60+ {
61+ method = "tagsuggest" ,
62+ text ,
63+ } ) ) ) ;
64+ var resultStr = await response . Content . ReadAsStringAsync ( ) ;
65+ if ( resultStr . Contains ( "{\" tags\" :[]}" ) )
66+ {
67+ if ( raw . Contains ( '.' ) )
68+ {
69+ return await _PostApiAsync ( ns , raw . Substring ( 0 , raw . IndexOf ( '.' ) - 1 ) ) ;
70+ }
71+ else
72+ {
73+ return null ;
74+ }
75+ }
76+
77+ var result = JsonConvert . DeserializeObject < TagSuggest > ( resultStr ) ;
78+ _FillCache ( result ) ;
79+ return result ;
80+ }
81+
82+ public static async Task < ( Namespace , string ) > _CheckTagAsync ( Namespace ns , string raw )
5583 {
5684 if ( raw . Length <= 2 )
5785 return ( ns , raw ) ;
@@ -60,22 +88,26 @@ private static Tag _FindCache(Namespace ns, string raw)
6088
6189 if ( match is null )
6290 {
63- var response = await _HttpClient . PostAsync ( "https://api.e-hentai.org/api.php" , new StringContent ( JsonConvert . SerializeObject ( new
64- {
65- method = "tagsuggest" ,
66- text = $ "{ ( raw . Length > 50 ? raw . Substring ( 0 , 50 ) : raw ) } ",
67- } ) ) ) ;
68- var resultStr = await response . Content . ReadAsStringAsync ( ) ;
69- if ( ! resultStr . Contains ( "{\" tags\" :[]}" ) )
91+ var result = await _PostApiAsync ( null , raw ) ;
92+ if ( result != null )
7093 {
71- var result = JsonConvert . DeserializeObject < TagSuggest > ( resultStr ) ;
72- _FillCache ( result ) ;
7394 // check exact match first
7495 match = result . tags . Values . FirstOrDefault ( tag => tag . ns == ns && tag . tn == raw )
7596 // find from misc ns and master in this ns
7697 ?? result . tags . Values . FirstOrDefault ( tag => tag . ns == Namespace . Misc && tag . tn == raw && tag . mns == ns ) ;
7798 }
99+ }
78100
101+ if ( match is null )
102+ {
103+ var result = await _PostApiAsync ( ns , raw ) ;
104+ if ( result != null )
105+ {
106+ // check exact match first
107+ match = result . tags . Values . FirstOrDefault ( tag => tag . ns == ns && tag . tn == raw )
108+ // find from misc ns and master in this ns
109+ ?? result . tags . Values . FirstOrDefault ( tag => tag . ns == Namespace . Misc && tag . tn == raw && tag . mns == ns ) ;
110+ }
79111 }
80112
81113 if ( match is null )
@@ -84,5 +116,81 @@ private static Tag _FindCache(Namespace ns, string raw)
84116 return ( ns , raw ) ;
85117 return ( match . mns ?? Namespace . Misc , match . mtn ) ;
86118 }
119+
120+ private static void _LogFailed ( RecordDictionary db , string key , Record value , Namespace newNs , string newKey )
121+ {
122+ db . AddOrReplace ( key , new Record ( value . Name . Raw , value . Intro . Raw , value . Links . Raw
123+ + $ "\n Now should be { newNs } :{ newKey } ") ) ;
124+ Console . WriteLine ( " -> Failed" ) ;
125+ }
126+ private static async Task _CheckNsAsync ( Database database , Namespace ns )
127+ {
128+ var db = database [ ns ] ;
129+ Console . WriteLine ( $ "Checking namespace { ns } with { db . RawData . Count } lines") ;
130+ for ( var i = 0 ; i < db . RawData . Count ; i ++ )
131+ {
132+ var data = db . RawData [ i ] ;
133+ Console . Write ( $ " [{ i , 4 } /{ db . RawData . Count } ] { data . Key } ") ;
134+ if ( string . IsNullOrWhiteSpace ( data . Key ) )
135+ {
136+ Console . WriteLine ( " -> Skipped empty tag" ) ;
137+ continue ;
138+ }
139+ var ( newNs , newKey ) = await _CheckTagAsync ( ns , data . Key ) ;
140+ if ( newKey is null )
141+ {
142+ db . Remove ( data . Key , false ) ;
143+ Console . WriteLine ( " -> Delete" ) ;
144+ }
145+ else if ( newNs != ns )
146+ {
147+ Console . Write ( $ " -> Move to { newNs } :{ newKey } ") ;
148+ try
149+ {
150+ database [ newNs ] . Add ( newKey , data . Value ) ;
151+ db . Remove ( data . Key , true ) ;
152+ Console . WriteLine ( " -> Succeed" ) ;
153+ }
154+ catch
155+ {
156+ _LogFailed ( db , data . Key , data . Value , newNs , newKey ) ;
157+ }
158+ }
159+ else if ( newKey != data . Key )
160+ {
161+ Console . Write ( $ " -> Rename to { newKey } ") ;
162+ try
163+ {
164+ db . Rename ( data . Key , newKey ) ;
165+ Console . WriteLine ( " -> Succeed" ) ;
166+ }
167+ catch
168+ {
169+ _LogFailed ( db , data . Key , data . Value , newNs , newKey ) ;
170+ }
171+ }
172+ else
173+ {
174+ Console . WriteLine ( " -> Valid" ) ;
175+ }
176+ }
177+ }
178+
179+ public static async Task CheckAsync ( Database database , Namespace checkTags )
180+ {
181+ try
182+ {
183+ // skip rows & reclass
184+ foreach ( var ns in database . Keys . Where ( v => v > Namespace . Reclass && checkTags . HasFlag ( v ) ) )
185+ {
186+ await _CheckNsAsync ( database , ns ) ;
187+ database . Save ( ) ;
188+ }
189+ }
190+ finally
191+ {
192+ database . Save ( ) ;
193+ }
194+ }
87195 }
88196}
0 commit comments