This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hitomi.fs
400 lines (318 loc) · 12.9 KB
/
Hitomi.fs
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
module QwQ.Sources.Hitomi
open FSharp.Data
open FSharp.Control
open QwQ
open QwQ.Utils
open QwQ.Sources.Moebooru
open QwQ.Sources.Nozomi
type Json = JsonProvider<"./Sources/HitomiJsonSample.json">
let referer = "https://hitomi.la/"
module private MagicJS =
open System.Text.RegularExpressions
// Magic JS Code
// Maybe throw some Exception!!!!!
let subdomainFromUrlRegex =
Regex @"\/[0-9a-f]\/([0-9a-f]{2})\/"
let subdomainFromUrl url =
// function subdomain_from_url(url, base) {
// var retval = 'b';
let mutable retval = "b"
// if (base) {
// retval = base;
// }
// var number_of_frontends = 2;
let number_of_frontends = 2
// var b = 16;
// var r = /\/[0-9a-f]\/([0-9a-f]{2})\//;
// var m = r.exec(url);
// if (!m) {
// return 'a';
// }
let m =
Option.protect (fun () ->
subdomainFromUrlRegex.Match(url).Groups.[1].Value)
// var g = parseInt(m[1], b);
let g =
m
|> Option.bind (fun m -> Option.protect(fun () ->
System.Int32.Parse(m, System.Globalization.NumberStyles.HexNumber)))
// if (!isNaN(g)) {
// var o = 0;
// if (g < 0x7c) {
// o = 1;
// }
// retval = String.fromCharCode(97 + o) + retval;
// }
match g with
| None -> ()
| Some g ->
let mutable o = 0
if g < 0x7c then
o <- 1
retval <- $"{char (97 + o)}{retval}"
// return retval;
retval
// }
let urlFromUrlRegex = Regex @"\/\/..?\.hitomi\.la\/"
let urlFromUrl url =
// function url_from_url(url, base) {
// return url.replace(/\/\/..?\.hitomi\.la\//, '//'+subdomain_from_url(url, base)+'.hitomi.la/');
urlFromUrlRegex.Replace(url, "//" + subdomainFromUrl url + ".hitomi.la/")
// }
let fullPathFromHash hash =
// function full_path_from_hash(hash) {
// if (hash.length < 3) {
// return hash;
// }
// return hash.replace(/^.*(..)(.)$/, '$2/$1/'+hash);
// }
if String.length hash < 3
then hash
else $"{hash.[^0]}/{hash.[^2..^1]}/{hash}"
let urlFromHash (image: Json.File) =
// function url_from_hash(galleryid, image, dir, ext) {
// ext = ext || dir || image.name.split('.').pop();
let ext = image.Name.Split '.' |> Seq.skip 1 |> Seq.head
// dir = dir || 'images';
let dir = "images"
// return 'https://a.hitomi.la/'+dir+'/'+full_path_from_hash(image.hash)+'.'+ext;
"https://a.hitomi.la/" + dir + "/" + fullPathFromHash image.Hash + "." + ext
// }
let urlFromUrlFromHash (postId: uint32) (image: Json.File) : string =
// function url_from_url_from_hash(galleryid, image, dir, ext, base) {
// return url_from_url(url_from_hash(galleryid, image, dir, ext), base);
urlFromUrl (urlFromHash image)
// }
// url_from_url_from_hash(galleryid, image);
let hitomiHttpOption = { Headers = [ "referer", referer ]}
let getContentsFromJson (postJson: Json.Root) =
Result.protect (fun () ->
let postId = uint32 postJson.Id
postJson.Files
|> Seq.map (fun x ->
let url = MagicJS.urlFromUrlFromHash postId x
{ FileName = x.Name
DownloadMethod =
Https (url, hitomiHttpOption) }))
let requestDetailsJson (postId: uint32) =
async {
let! json =
Http.AsyncRequestString
($"https://ltn.hitomi.la/galleries/{postId}.js", headers = hitomiHttpOption.Headers)
let json = json.Replace("var galleryinfo =", "") |> Json.Parse
(*let title =
json.JsonValue.TryGetProperty "title"
|> Option.orElseWith (fun () -> json.JsonValue.TryGetProperty "japanese_title")
|> Option.map (fun x -> x.AsString ())
let tags =
json.Tags
|> Seq.map (fun x -> x.Tag.Replace(' ', '_'))
|> Seq.toList*)
return json
}
let parseDetailsHtml this (postId: uint32) : Async<Result<Option<Post>, exn>> =
async {
let! htmlStr =
Http.AsyncRequestString
($"https://ltn.hitomi.la/galleryblock/{postId}.html",
headers = [ "referer", "https://hitomi.la"])
let html =
HtmlDocument.Parse htmlStr
let title =
html.CssSelect "h1 a"
|> Seq.tryHead
|> Option.bind (fun x -> String.nullOrWhitespace <| x.InnerText ())
let artist =
html.CssSelect ".artist-list a"
let tags =
html.CssSelect ".dj-desc td ul li a"
|> List.append artist
|> List.choose (fun x ->
x
.InnerText()
.Replace("♀", "")
.Replace("♂", "")
.Trim()
.Replace(' ', '_')
|> String.nullOrWhitespace)
let preview =
html.CssSelect ".dj-img-cont img"
|> Seq.choose (fun x -> x.TryGetAttribute "src")
|> Seq.map (fun x -> x.Value())
|> Seq.choose String.nullOrWhitespace
|> Seq.tryHead
|> Option.map ((+) "https:" >> Moebooru.mapHttpsContent hitomiHttpOption)
let hitomiViewPage =
html.CssSelect "a"
|> Seq.choose (fun x -> x.TryGetAttribute "href")
|> Seq.choose (fun x -> String.nullOrWhitespace <| x.Value())
|> Seq.head
|> (+) "https://hitomi.la"
return
{ Id = uint64 postId
Title = title
Source = this
Rating = Unrated
SourceUrl = AsyncSeq.singleton hitomiViewPage
Tags = tags
PreviewImage = preview
Content =
asyncSeq {
let! json =
requestDetailsJson postId
|> Async.protect
let json = Result.unwrap json
yield!
getContentsFromJson json
|> Result.unwrap
|> Seq.map AsyncSeq.singleton
|> AsyncSeq.ofSeq
}
}
}
|> Async.protect
|> Async.map (function
| Ok x -> Ok <| Some x
| Error x when x.Message.Contains "404" -> Ok None
| Error e -> Error e)
let mapOrdering order =
match order with
| Popular | Score -> "popular/week/"
| _ -> ""
let allPosts this (nozomiCache: AsyncCache<string, uint32 seq>) order =
asyncSeq {
let! nozomiBin =
nozomiCache.GetAsyncProtected <|
match order with
| Popular | Score -> "https://ltn.hitomi.la/popular/week-all.nozomi"
| _ -> "https://ltn.hitomi.la/index-all.nozomi"
match nozomiBin with
| Error x -> yield Error x
| Ok nozomiBin ->
yield!
nozomiBin
|> AsyncSeq.ofSeq
|> AsyncSeq.mapAsync (parseDetailsHtml this)
|> AsyncSeq.choose (function
| Ok (Some x) -> Some <| Ok [x]
| Ok None -> None
| Error x -> Some <| Error x)
}
let requestTags' (cat: string) (tagPageName: string) =
async {
let! html =
$"https://hitomi.la/{cat}-{tagPageName}.html"
|> HtmlDocument.AsyncLoad
return
html.CssSelect "ul.posts li a"
|> List.map (fun x ->
x
.InnerText()
.Replace("♀", "")
.Replace("♂", "")
.Trim()
.Replace(' ', '_'))
}
|> Async.protect
let requestTags tagPageName =
seq { "allseries"; "allcharacters"; "alltags"; "allartists" }
|> Seq.map (fun cat -> requestTags' cat tagPageName |> Async.map (Result.map (List.map (fun x -> cat, x))))
|> AsyncSeq.ofSeqAsync
let convertTags: AsyncSeq<Result<(string * string) list, exn>> seq -> AsyncSeq<Result<string * Tag, exn>> =
AsyncSeq.ofSeq
>> AsyncSeq.concat
>> AsyncSeq.map (function
| Ok x -> Seq.map Ok x
| Error x -> Seq.singleton <| Error x)
>> AsyncSeq.concatSeq
let allTags =
seq { yield "123"; yield! seq { 'a'..'z' } |> Seq.map string }
|> Seq.map requestTags
|> convertTags
let searchTag (tagTerm: string) =
match String.nullOrWhitespace <| String.trim tagTerm with
| None -> allTags
| Some x ->
match System.Char.ToLower <| x.[0] with
| x when Seq.exists ((=) x) (seq { '0' .. '9' }) -> requestTags "123"
| x when Seq.exists ((=) x) (seq { 'a' .. 'z' }) -> requestTags <| string x
| _ -> AsyncSeq.singleton <| Ok []
|> Seq.singleton
|> convertTags
|> AsyncSeq.filter (function
| Ok (_, x) -> x.StartsWith tagTerm
| Error _ -> true)
let hitomi =
let nozomiCache = newNozomiCache "https://hitomi.la/"
{ new ISource with
member _.Name = "Hitomi"
member this.AllPosts = allPosts this nozomiCache Date
interface IGetPostById with
member this.GetPostById id =
parseDetailsHtml (this :?> ISource) (id |> uint32)
interface ISearch with
member this.Search opt =
let requestNozomiFromTag (cat, tag: string) =
let cat =
match cat with
| "alltags" -> "tag"
| "allartists" -> "artist"
| "allseries" -> "series"
| "allcharacters" -> "character"
| x -> failwith $"What is cat of {x}???"
$"https://ltn.hitomi.la/{cat}/{mapOrder opt.Order}{tag.Replace('_', ' ')}-all.nozomi"
|> nozomiCache.GetAsyncProtected
|> Async.map (function
| Ok x -> Ok x
| Error e when e.Message.Contains "404" -> Ok Seq.empty
| Error e -> Error e)
if Seq.length opt.Tags = 0
then this.AllPosts
else
let tags = opt.Tags |> Seq.map searchTag |> AsyncSeq.ofSeq
let processOrGroup (orGroup: AsyncSeq<Result<string * Tag, exn>>) : Async<Result<uint32 seq, exn>> =
async {
let! postIds =
orGroup
|> AsyncSeq.map (Result.map (requestNozomiFromTag))
|> AsyncSeq.toArrayAsync
let postIdOks, errs1 = Result.eitherArray postIds
let! postIdOks = postIdOks |> Async.Parallel
let postIdOks, errs2 = Result.eitherArray postIdOks
if Array.forall (Seq.isEmpty >> not) postIdOks
then return Ok <| Seq.concat postIdOks
else
if errs1 |> Array.isEmpty |> not then return Error errs1.[0]
elif errs2 |> Array.isEmpty |> not then return Error errs2.[0]
else return Ok Seq.empty
}
asyncSeq {
let! tags = AsyncSeq.map processOrGroup tags |> AsyncSeq.toArrayAsync
let! tags = tags |> Async.Parallel
let tagOks, tagErrs = Result.eitherArray tags
if Array.isEmpty tagErrs |> not
then yield! tagErrs |> Array.map Error |> AsyncSeq.ofSeq
else
let first = Array.head tagOks
let tail = Seq.tail tagOks
yield!
first
|> Seq.filter (fun postId ->
Seq.forall (fun x -> Seq.exists ((=) postId) x) tail)
|> Seq.map (parseDetailsHtml this)
|> AsyncSeq.ofSeqAsync
|> AsyncSeq.choose (function
| Ok None -> None
| Ok (Some x) -> Some <| Ok [x]
| Error x -> Some <| Error x)
}
|> AntiGuro.antiThat opt.ExludeTags
interface ITags with
member _.Tags =
allTags
|> AsyncSeq.map (Result.map snd)
interface ISearchTag with
member _.SearchTag s =
searchTag s
|> AsyncSeq.map (Result.map snd)
}