forked from ddupas/podnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.nu
105 lines (84 loc) · 2.85 KB
/
pod.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
#!/usr/bin/env nu
# pod.nu
# photo of the day nushell
#
# github @ddupas
# github @amtoine
use std log
#
# parse the name of a URL-based file path ending with a filename
#
def "url parse filename" [] { # -> string
url parse | get path | path parse | update parent "" | path join
}
def "bing url parse filename" [] { # -> string
url parse | get params | where key == 'id' | get value.0
}
# download any images that have been added to national geo photo of the day into the current directory
export def main [] {
let national_geo_pod = (
http get https://www.nationalgeographic.com/photo-of-the-day/
| rg --only-matching '"https.*?"'
| rg --only-matching 'http.*?jpg'
| rg --invert-match '16x9|3x2|2x3|3x4|4x3|_square|2x1'
| lines
| uniq
| sort
| wrap url
| upsert filename {|it| $it.url | url parse filename}
)
let bing_pod = (
[ $"http://bing.com(
http get https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US
| get images.url.0
)" ]
| wrap url
| upsert filename {|it| $it.url | bing url parse filename}
)
let all_pod = (
$national_geo_pod
| append $bing_pod
)
let already_downloaded_images = (
try { cd img; ls *.jpg | get name } catch { [] }
)
let photos_to_download = (
$all_pod | where {|it|
not ($it.filename in $already_downloaded_images)
}
)
if ($photos_to_download | is-empty) {
log warning "you have already downloaded all the images of the day!"
return ()
}
$photos_to_download | par-each {|photo|
log info $"downloading ($photo.url)"
print $"downloading ($photo.filename)"
http get $photo.url | save -f --progress $"($photo.filename)"
}
()
}
# TODO: gitinclude ddupas/nu-unit-test.git
# use nu-unit-test.nu
# use assert
# def 'test url parse' [] {
# let input = "https://i.natgeofe.com/n/ffe12b1d-8191-44ec-bfb9-298e0dd29825/NationalGeographic_2745739.jpg"
# let expected = "NationalGeographic_2745739.jpg"
# assert equal ($input | url parse filename) $expected
# }
# def 'test bing url parse' [] {
# let input = "http://bing.com/th?id=OHR.CorfuBeach_EN-US1955770867_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp"
# let expected = "OHR.CorfuBeach_EN-US1955770867_1920x1080.jpg"
# assert equal ($input | bing url parse filename) $expected
# }
# let tests = [ ( test bing url parse ) ( test url parse ) ]
export def --env 'trim file names' [] {
ls
| select name
| upsert len { |row| ($row.name | str length) }
| where len > 50
| upsert stem1 { |row| ( $row.name | path parse | get stem | str substring 0..45 ) }
| upsert ext { |row| ($row.name | path parse | get extension ) }
| upsert mv2 { |row| $"($row.stem1).($row.ext)" }
| each { mv $in.name $in.mv2 }
}