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
/
Copy pathFullTest.fsx
124 lines (100 loc) · 3.35 KB
/
FullTest.fsx
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
#r "nuget: FSharp.Control.AsyncSeq"
#r "nuget: FSharp.Data"
open System
open System.IO
open System.Diagnostics
open FSharp.Control
if not <| File.Exists "./QwQ/bin/Release/net6.0/publish/QwQ.Core.dll"
then Process.Start("dotnet", "publish ./QwQ/QwQ.fsproj -c Release -f net6.0").WaitForExit()
#r "./QwQ/bin/Release/net6.0/publish/QwQ.Core.dll"
let sourceName =
Environment.GetCommandLineArgs ()
|> Array.tryItem 2
|> function
| None ->
printfn "Usage:"
printfn " dotnet fsi FullTest.fsx <sourceName> [test] "
printfn ""
printfn "Tests:"
printfn " --list-tags"
printfn " --list-posts"
printfn " --download-preview"
printfn ""
exit -1
| Some x -> x
let testTarget =
Environment.GetCommandLineArgs ()
|> Array.tryItem 3
|> Option.defaultValue "--list-posts"
open QwQ
open QwQ.Utils
let source =
Sources.Sources.sources
|> List.tryFind (Source.name >> (=) sourceName)
|> function
| Some x -> x
| None ->
printfn "Can not find source named %A" sourceName
exit -1
let download =
function
| Https (url, opt) ->
async {
let! stream =
FSharp.Data.Http.AsyncRequestStream
(url, headers = opt.Headers)
if stream.StatusCode = 200
then return Ok ()
else return Error stream
}
match testTarget with
| "--list-tags" ->
match source with
| :? ITags as source ->
Tags.allTags source
|> AsyncSeq.iter (Result.unwrap >> printfn "%s")
|> Async.RunSynchronously
| _ -> printfn "%A source is not an ITags." <| Source.name source
| "--list-posts" ->
Source.allPosts source
|> AsyncSeq.map Result.unwrap
|> AsyncSeq.iteriAsync (fun i x -> async {
printfn $"{Source.name source}: Page {i} has {Seq.length x} posts" })
|> Async.RunSynchronously
| "--download-preview" ->
Source.allPosts source
|> AsyncSeq.map Result.unwrap
|> AsyncSeq.mapi (fun pageId page ->
asyncSeq {
for post in page ->
match post.PreviewImage with
| Some x ->
asyncSeq {
let! down = download x.DownloadMethod
match down with
| Ok () -> printfn $"{Source.name source} (Page {pageId}, Post {post.Id}) downloaded."
| Error response ->
printfn $"{Source.name source} (Page {pageId}, Post {post}, Response {response.StatusCode}) failed:"
printfn " %A" x.DownloadMethod
yield post
}
| None ->
asyncSeq {
printfn $"{Source.name source} (Page {pageId}, Post {post}) has no preview image."
}
}
)
|> AsyncSeq.concat
|> AsyncSeq.concat
|> AsyncSeq.toListAsync
|> Async.RunSynchronously
|> List.map (fun errPosts ->
printfn ""
printfn "===="
printfn "%A" errPosts
printfn "===="
printfn "")
|> function
| [] -> ()
| _ -> failwith "↑↑↑ Here is some errors ↑↑↑"
| x -> printfn "Here is no test target %A." x