diff options
author | jdlugosz963 <jdlugosz963@gmail.com> | 2024-09-20 14:16:56 +0200 |
---|---|---|
committer | jdlugosz963 <jdlugosz963@gmail.com> | 2024-09-20 14:16:56 +0200 |
commit | 45bb0d11161b1c5077a1415eed6dbd0fd25ccb6a (patch) | |
tree | b564c1c43920def6641cd482a7b4dca453dbc043 /files/.bin/whois-at-hsp | |
parent | 07dd8e37eb767c3dd6abf63e5e4a720cd778da15 (diff) | |
download | dotfiles-45bb0d11161b1c5077a1415eed6dbd0fd25ccb6a.tar.gz dotfiles-45bb0d11161b1c5077a1415eed6dbd0fd25ccb6a.zip |
Change dotfiles structure, and add guix-channels declaration.
Diffstat (limited to 'files/.bin/whois-at-hsp')
-rwxr-xr-x | files/.bin/whois-at-hsp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/files/.bin/whois-at-hsp b/files/.bin/whois-at-hsp new file mode 100755 index 0000000..982a90e --- /dev/null +++ b/files/.bin/whois-at-hsp | |||
@@ -0,0 +1,84 @@ | |||
1 | #!/usr/bin/env -S guix shell guile guile-json -- guile --no-auto-compile -e main -s | ||
2 | -*- scheme -*- | ||
3 | !# | ||
4 | |||
5 | (use-modules (srfi srfi-1) | ||
6 | (ice-9 iconv) | ||
7 | (ice-9 receive) | ||
8 | (web client) | ||
9 | (json)) | ||
10 | |||
11 | (define whois-at-hsp-endpoint "https://whois.at.hsp.sh/api/now") | ||
12 | |||
13 | (define (http-get-serialize-json url) | ||
14 | (receive (response data) (http-request url) | ||
15 | (values (json-string->scm (bytevector->string data "UTF-8")) | ||
16 | response))) | ||
17 | |||
18 | (define (whois-data->users whois-data) | ||
19 | (assoc-ref whois-data "users")) | ||
20 | |||
21 | (define (whois-data->unknown-devices whois-data) | ||
22 | (assoc-ref whois-data "unknown_devices")) | ||
23 | |||
24 | (define (whois-data->head-count whois-data) | ||
25 | (assoc-ref whois-data "headcount")) | ||
26 | |||
27 | (define (whois-data->formated-users whois-data) | ||
28 | (let ((users (vector->list (whois-data->users whois-data)))) | ||
29 | (if (> (length users) 0) | ||
30 | (fold-right | ||
31 | (lambda (a b) (string-append a " " b)) | ||
32 | "" | ||
33 | users) | ||
34 | "No visible users!"))) | ||
35 | |||
36 | (define (whois-data->summary whois-data) | ||
37 | (string-append | ||
38 | "Unknown devices: " | ||
39 | (number->string (whois-data->unknown-devices whois-data)) | ||
40 | "\nUsers: [" (number->string (whois-data->head-count whois-data)) "] " | ||
41 | (whois-data->formated-users whois-data))) | ||
42 | |||
43 | (define (notify data) | ||
44 | (system* "notify-send" | ||
45 | "WHOIS AT HSP" | ||
46 | (whois-data->summary data))) | ||
47 | |||
48 | (define (command-line-option? option args) | ||
49 | (> (length (or (member option args) | ||
50 | '())) | ||
51 | 0)) | ||
52 | |||
53 | (define-syntax-rule (command-line-args-handle args default ((option ...) body ...) ...) | ||
54 | (let ((option? (lambda (o) (command-line-option? o args)))) | ||
55 | (cond ((or (option? option) ...) body ...) ... | ||
56 | (#t default)))) | ||
57 | |||
58 | ;; (command-line-args-handle '("a" "--help") | ||
59 | ;; 0 | ||
60 | ;; (("-h" "--help") "dupa")) | ||
61 | |||
62 | (define (main args) | ||
63 | (let ((whois-data (http-get-serialize-json whois-at-hsp-endpoint))) | ||
64 | (command-line-args-handle args | ||
65 | (begin (display (whois-data->summary whois-data)) | ||
66 | (newline)) | ||
67 | (("-h" "--help") | ||
68 | (display (string-append "-h, --help Help message\n" | ||
69 | "-n, --notify Notification massage with whois information.\n" | ||
70 | "-u Get users\n" | ||
71 | "-H Get head count\n" | ||
72 | "-U Get unknown devices\n"))) | ||
73 | (("-n" "--notify") | ||
74 | (notify whois-data)) | ||
75 | (("-u") | ||
76 | (display (whois-data->formated-users whois-data)) | ||
77 | (newline)) | ||
78 | (("-H") | ||
79 | (display (whois-data->head-count whois-data)) | ||
80 | (newline)) | ||
81 | (("-U") | ||
82 | (display (whois-data->unknown-devices whois-data)) | ||
83 | (newline))))) | ||
84 | |||