-
Notifications
You must be signed in to change notification settings - Fork 2
/
apis.nu
615 lines (513 loc) · 16.1 KB
/
apis.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#get bitly short link
export def bitly [longurl] {
if ($longurl | is-empty) {
return-error "no url provided!"
}
let bitly_credential = $env.MY_ENV_VARS.api_keys.bitly
let Accesstoken = $bitly_credential | get token
let guid = $bitly_credential | get guid
let url = "https://api-ssl.bitly.com/v4/shorten"
let content = {
"group_guid": $guid,
"domain": "bit.ly",
"long_url": $longurl
}
let response = http post $url $content --content-type "application/json" -H ["Authorization", $"Bearer ($Accesstoken)"]
let shorturl = $response | get link
$shorturl | copy
print (echo-g $"($shorturl) copied to clipboard!")
}
#translate text using mymemmory or openai api
#
#Use ISO standar names for the languages, for example:
#english: en-US
#spanish: es-ES
#italian: it-IT
#swedish: sv-SV
#
#More in: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
export def trans [
...text:string #search query
--from:string #from which language you are translating (default english)
--to:string #to which language you are translating (default spanish)
] {
let search = get-input $in $text
if ($search | is-empty) {
return-error "no search query provided!"
}
let trans_credential = $env.MY_ENV_VARS.api_keys.mymemmory
let apikey = $trans_credential | get token
let user = $trans_credential | get username
let from = get-input "en-US" $from
let to = get-input "es-ES" $to
let to_translate = $search | str join "%20"
let url = {
scheme: "https",
host: "api.mymemory.translated.net",
path: "/get",
params: {
q: $to_translate,
langpair: ($from + "%7C" + $to),
of: "json",
key: $apikey,
de: $user
}
} | url join
let response = http get $url
let status = $response | get responseStatus
let translated = $response | get responseData | get translatedText
if $status != 200 {
return-error $"error: bad request ($status)!"
}
let quota = $response | get quotaFinished
if $quota {
return-error "error: word quota limit excedeed!"
}
return $translated
}
#get rebrandly short link
export def "rebrandly get" [longurl] {
if ($longurl | is-empty) {
return-error "no url provided"
}
let credential = $env.MY_ENV_VARS.api_keys.rebrandly
let api_key = $credential | get api_key
let url = "https://api.rebrandly.com/v1/links"
let content = {"destination": $longurl}
let response = http post $url $content -H ["apikey", $api_key] --content-type "application/json" -H ["UserAgent:","UserAgent,curl/7.68.0"]
let shorturl = $response | get shortUrl
$shorturl | copy
print (echo-g $"($shorturl) copied to clipboard!")
}
#list rebrandly last 25 short links
export def "rebrandly list" [longurl="www.google.com"] {
if ($longurl | is-empty) {
return-error "no url provided"
}
let credential = $env.MY_ENV_VARS.api_keys.rebrandly
let apikey = $credential | get api_key
let url = {
scheme: "https",
host: "api.rebrandly.com",
path: "/v1/links",
params: {
domain.id: $longurl,
orderBy: "createdAt",
orderDir: "desc",
limit: 25
}
} | url join
http get $url -H ["apikey", $apikey] -H ["accept", "application/json"]
}
#get eta via maps api
export def "maps eta" [
origin:string #origin gps coordinates or address
destination:string #destination gps coordinates or address
--mode = "driving" #driving mode (driving, transit, walking)
--avoid #whether to avoid highways (default:false)
] {
let apikey = $env.MY_ENV_VARS.api_keys.google.general
let origin_address = (
if $origin =~ '^(-?\d+\.\d+),(-?\d+\.\d+)$' {
{
scheme: "https",
host: "maps.googleapis.com",
path: "/maps/api/geocode/json",
params: {
latlng: $origin,
sensor: "true",
key: $apikey
}
}
| url join
| http get $in
| get results.formatted_address.0
} else {
$origin
}
)
let destination_address = (
if $destination =~ '^(-?\d+\.\d+),(-?\d+\.\d+)$' {
{
scheme: "https",
host: "maps.googleapis.com",
path: "/maps/api/geocode/json",
params: {
latlng: $destination,
sensor: "true",
key: $apikey
}
}
| url join
| http get $in
| get results.formatted_address.0
} else {
$destination
}
)
let avoid_option = if $avoid {"&avoid=highways"} else {""}
let response = {
scheme: "https",
host: "maps.googleapis.com",
path: "/maps/api/directions/json",
params: {
origin: ($origin | url encode),
destination: ($destination | url encode),
mode: $mode,
departure_time: "now",
sensor: "true",
key: $apikey
}
}
| url join
| str append $avoid_option
| http get $in
let distance = $response.routes.legs.0.distance.text.0
let steps = $response.routes.legs.0.steps
let duration = $response.routes.legs.0.duration.text.0
let directions_steps = (
$steps.0.html_instructions
| each {|g| $g | html2text}
| str trim
| wrap directions
| polars into-df
| polars append ($steps.0.duration.text | wrap duration | polars into-df)
| polars into-nu
)
let info = {
origin: $origin_address,
destination: $destination_address,
distance: $distance,
duration: $duration,
mode: $mode
}
let output = {
info: $info
direction: $directions_steps
}
return $output
}
#get geo-coordinates from address
export def "maps loc-from-address" [address] {
let mapsAPIkey = $env.MY_ENV_VARS.api_keys.google.general
let url = $"https://maps.google.com/maps/api/geocode/json?address=($address)&key=($mapsAPIkey)"
return (http get $url | get results | get geometry | get location | flatten)
}
#get address from geo-coordinates
export def "maps address-from-loc" [latitude:number,longitude:number] {
let mapsAPIkey = $env.MY_ENV_VARS.api_keys.google.general
let url = $"https://maps.googleapis.com/maps/api/geocode/json?latlng=($latitude),($longitude)&key=($mapsAPIkey)"
let response = http get $url
if $response.status != OK {
return-error "address not found!"
}
return $response.results.0.formatted_address
}
#clp exchange rates via fixer.io API
#
#Show CLP/CLF,USD,BTC,new_currency exchange
export def exchange_rates [
new_currency?:string #include unique new currency
--symbols(-s) #only show available symbols
--update_dataset(-u) #update local dataset
] {
let api_key = $env.MY_ENV_VARS.api_keys.fixer_io.api_key
if $symbols {
let url_symbols = $"http://data.fixer.io/api/symbols?access_key=($api_key)"
let symbols = (http get $url_symbols)
return $symbols.symbols
}
let url = (
if ($new_currency | is-empty) {
$"http://data.fixer.io/api/latest?access_key=($api_key)&symbols=CLP,CLF,USD,BTC"
} else {
$"http://data.fixer.io/api/latest?access_key=($api_key)&symbols=CLP,CLF,USD,BTC,($new_currency)"
}
)
let response = http get $url
if not $response.success {
return-error $response.error
}
let eur_usd = (1 / $response.rates.USD)
let eur_btc = (1 / $response.rates.BTC)
let eur_clf = (1 / $response.rates.CLF)
let eur_new = if ($new_currency | is-empty) {0} else {1 / ($response.rates | get $new_currency)}
let output = (
if ($new_currency | is-empty) {
{
UF: ($eur_clf * $response.rates.CLP)
USD: ($eur_usd * $response.rates.CLP),
EUR: $response.rates.CLP,
BTC: ($eur_btc * $response.rates.CLP)
}
} else {
{
UF: ($eur_clf * $response.rates.CLP)
USD: ($eur_usd * $response.rates.CLP),
EUR: $response.rates.CLP,
BTC: ($eur_btc * $response.rates.CLP),
$"($new_currency)": ($eur_new * $response.rates.CLP)
}
}
)
if $update_dataset {
let to_save = (
$output
| rename -c {UF: date}
| upsert date (date now | format date "%Y.%m.%d %H:%M:%S")
)
open ([$env.MY_ENV_VARS.datasets exchange_rates.csv] | path join)
| append $to_save
| save -f ([$env.MY_ENV_VARS.datasets exchange_rates.csv] | path join)
}
return $output
}
# Translate text using Google Translate
export def gg-trans [
text?: string # The text to translate
--source(-s): string = "auto", # The source language
--destination(-d): string = "es", # The destination language
--list(-l) # Select destination language from list
] {
let text = get-input $in $text
mut dest = ""
if $list {
let languages = open ([$env.MY_ENV_VARS.credentials google_translate_languages.json] | path join)
let selection = (
$languages
| columns
| input list -f (echo-g "Select destination language:")
)
$dest = $languages | get $selection
} else {
$dest = $destination
}
{
scheme: "https",
host: "translate.googleapis.com",
path: "/translate_a/single",
params: {
client: gtx,
sl: $source,
tl: $dest,
dt: t,
q: ($text | url encode),
}
}
| url join
| http get $in
| get 0.0.0
}
#google search
export def google_search [
...query:string
--number_of_results(-n):int = 5 #number of results to use
--verbose(-v) #show some debug messages
--md(-m) #md output instead of table
] {
let query = get-input $in $query | str join " "
if ($query | is-empty) {
return-error "empty query!"
}
let apikey = $env.MY_ENV_VARS.api_keys.google.search.apikey
let cx = $env.MY_ENV_VARS.api_keys.google.search.cx
if $verbose {print (echo-g $"asking to google search: ($query)")}
let response = {
scheme: "https",
host: "www.googleapis.com",
path: "/customsearch/v1",
params: {
key: $apikey,
cx: $cx
q: ($query | str replace -a " " "+")
}
}
| url join
| http get $in -e
if "items" not-in ($response | columns) {
return-error "empty search result!"
}
let search_result = (
$response
| get items
| first $number_of_results
| select title link displayLink
)
let n_result = $search_result | length
mut content = []
for i in 0..($n_result - 1) {
let web = $search_result | get $i
if $verbose {print (echo-c $"retrieving data from: ($web.displayLink)" "green")}
let raw_content = try {http get $web.link} catch {""}
let processed_content = (
try {
$raw_content
| html2text --ignore-links --ignore-images --dash-unordered-list
| lines
| uniq
| to text
} catch {
$raw_content
}
)
$content = $content ++ $processed_content
}
let final_content = $content | wrap "content"
let results = $search_result | append-table $final_content
if $md {
mut md_output = ""
for i in 0..(($results | length) - 1) {
let web = $results | get $i
$md_output = $md_output + "# " + $web.title + "\n"
$md_output = $md_output + "link: " + $web.link + "\n\n"
$md_output = $md_output + $web.content + "\n\n"
}
return $md_output
}
return $results
}
#check obsidian server
export def "obs check" [] {
let apikey = $env.MY_ENV_VARS.api_keys.obsidian.local_rest_apikey
let host = $env.MY_ENV_VARS.api_keys.obsidian.host
let port = $env.MY_ENV_VARS.api_keys.obsidian.port
let certificate = $env.MY_ENV_VARS.api_keys.obsidian.certificate
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
content?:string # content of the note
--v_path(-v):string # path for the note in vault, otherwise select from list
--sub_path(-s) # select subpath
] {
let content = get-input $in $content
if ($content | is-empty) {return-error "empty content!"}
let v_path = if ($v_path | is-empty) {
ls $env.MY_ENV_VARS.api_keys.obsidian.vault
| get name
| find -v "_resources"
| path parse
| get stem
| sort
| input list -f (echo-g "Select path for the note: ")
} else {
$v_path
}
let sub_path = if $sub_path {
ls ($env.MY_ENV_VARS.api_keys.obsidian.vault | path join $v_path)
| where type == "dir"
| get name
| path parse
| get stem
| sort
| input list -f (echo-g "Select sub_path for the note: ")
} else {
""
}
let check = obs check
if $check.status != "OK" {
return-error "something went wrong with the server!"
}
let v_path = if ($sub_path | is-empty) {$v_path} else {$v_path + "/" + $sub_path}
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)
}
}