-
Notifications
You must be signed in to change notification settings - Fork 2
/
GeminiJson2Obsidian.nu
174 lines (139 loc) · 4.74 KB
/
GeminiJson2Obsidian.nu
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
#!/usr/bin/env nu
export def main [tags?:string = "ai,ai_notes,bard"] {
use /home/kira/Yandex.Disk/Backups/linux/nu_scripts/string_manipulation.nu *
let files = ls ~/Dropbox/Aplicaciones/Gmail/* | find joplin
if ($files | length) == 0 {return}
$files
| each {|file|
let json = open ($file.name | ansi strip)
let title = $json.title
let content = $json.body
obs create $title "AI/AI_GeminiVoiceChat" $content
# $tags
# | split row ","
# | each {|tag|
# joplin tag add $tag $title
# sleep 0.1sec
# }
rm -f $file.name
}
}
#check obsidian server
export def "obs check" [] {
let apikey = "cf32804c5e2066aafdecfa16b3bc39456d84e12d88671f97b5ee8a38f2cc0964"
let host = "127.0.0.1"
let port = 27124
let certificate = "/home/kira/Yandex.Disk/obsidian/obsidian-local-rest-api.crt"
let url = {
"scheme": "https",
"host": $host,
"port": $port
} | url join
let status = curl -s -X 'GET' $url -H 'accept: application/json' -H $'Authorization: Bearer ($apikey)' --cacert $certificate | from json | get status
return {status: $status, apikey: $apikey, host: $host, port: $port, certificate: $certificate}
}
#check obsidian path
export def "obs check-path" [
v_path:string # path in vault
] {
let check = obs check
if $check.status != "OK" {
return-error "something went wrong with the server!"
}
let apikey = $check.apikey
let host = $check.host
let port = $check.port
let certificate = $check.certificate
let auth_header = $'Authorization: Bearer ($apikey)'
let url = {
"scheme": "https",
"host": $host,
"port": $port ,
"path": (["vault" $v_path] | path join)
} | url join
let response = curl -sX 'GET' $"($url)/" -H 'accept: application/json' -H $auth_header --cacert $certificate | from json
return ($response)
}
#obsidian search on body of notes
#
# mv to http get/post when ready
# let response = https post $url {} --content-type "application/json" -H ["Authorization:", $"Bearer ($apikey)"] --certificate
export def "obs search" [
...query #search query (in title and body)
--tag(-t):string #search in tag (use search, in progress)
--edit(-e) #edit selected note (??)
--raw(-r) #don't use syntax highlight
] {
if ($query | is-empty) {
return-error "empty search query!"
}
let check = obs check
if $check.status != "OK" {
return-error "something went wrong with the server!"
}
let apikey = $check.apikey
let host = $check.host
let port = $check.port
let certificate = $check.certificate
let auth_header = $'Authorization: Bearer ($apikey)'
let query = $query | str join " "
mut note = ""
# search
if ($tag | is-not-empty) {
return-error "work in progress!"
} else {
let url = {
"scheme": "https",
"host": $host,
"port": $port ,
"path": "search/simple",
"query": ("query=" + ($query | url encode) + "&contextLength=100"),
} | url join
let response = curl -sX 'POST' $url -H 'accept: application/json' -H $auth_header --cacert $certificate -d '' | from json
$note = ($response | get filename | input list -f (echo-g "Select note:"))
}
if not $edit {
# show
let note_url = {
"scheme": "https",
"host": $host,
"port": $port ,
"path": ("vault/" + ($note | url encode)),
} | url join
let content = curl -sX 'GET' $note_url -H 'accept: text/markdown' -H $auth_header --cacert $certificate
if $raw {$content} else {$content | glow}
} else {
# edit
return-error "work in progress!"
}
}
#obsidian create new note
export def "obs create" [
name:string # name of the note
v_path:string # path for the note in vault
content:string # content of the note
] {
let check = obs check
if $check.status != "OK" {
return-error "something went wrong with the server!"
}
let check_path = obs check-path $v_path
if ($check_path | get errorCode? | is-not-empty) {
return-error "path doesn't exists!"
}
let apikey = $check.apikey
let host = $check.host
let port = $check.port
let certificate = $check.certificate
let auth_header = $'Authorization: Bearer ($apikey)'
let url = {
"scheme": "https",
"host": $host,
"port": $port ,
"path": (["vault" $v_path $"($name | url encode).md"] | path join)
} | url join
let response = curl -sX 'PUT' $url -H 'accept: text/markdown' -H $auth_header --cacert $certificate -d $content | from json
if ($response.message? | is-not-empty) {
return ($response.message)
}
}