forked from jbenet/ipfs-screencap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs-screencap
executable file
·111 lines (94 loc) · 2.02 KB
/
ipfs-screencap
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
#!/bin/bash
set -e
set -o pipefail
USAGE="USAGE
$0 [OPTIONS] [<name>]
publish a screenshot to ipfs, copy gateway link to clipboard
OPTIONS
-h, --help - display this help
-v, --verbose - print messages for the actions
-p, --preload - preload url on the gateways
-c, --copy - copy url to clipboard
-s, --sound - play a sound when done (useful with -p -c)
"
usage() {
echo "$USAGE"
exit 0
}
die() {
echo >&2 "error: $@"
exit 1
}
log() {
if [ $verbose ]; then
printf >&2 "$@"
fi
}
# todo: make this cross-platform
case $(uname) in
Darwin)
clicopy="pbcopy"
clipaste="pbpaste"
playsound="afplay /System/Library/Sounds/Ping.aiff"
;;
Linux)
clicopy="xsel -i"
clipaste="xsel -o"
playsound='echo -e "\007"'
;;
*) ;;
esac
# get user options
while [ $# -gt 0 ]; do
# get options
arg="$1"
shift
case "$arg" in
-h|--help) usage ;;
-v|--verbose) verbose=1 ;;
-p|--preload) preload=1 ;;
-c|--copy) copyopt=1 ;;
-s|--sound) soundopt=1 ;;
--*) die "unrecognised option: '$arg'\n$USAGE" ;;
*)
if [ "$name" == "" ]; then
name="$arg"
else
die "too many arguments\n$USAGE"
fi
;;
esac
done
if [ "$name" == "" ]; then
name=screencap.$(date +"%Y-%m-%dT%H:%M:%SZ").png
fi
log "capturing screen..."
if type portable-screencap >/dev/null; then
img=$(portable-screencap | ipfs add -q)
elif [ -f "./portable-screencap" ]; then
img=$(./portable-screencap | ipfs add -q)
else
die "please install portable-screencap"
fi
log " $img\n"
log "constructing dir..."
dir=$(ipfs object new unixfs-dir)
dir=$(ipfs object patch "$dir" add-link "$name" "$img" )
pin=$(ipfs pin add -r "$dir")
log " $dir\n"
out="https://ipfs.io/ipfs/$dir/$name"
echo "$out"
if [ $copyopt ]; then
log "copying url to clipboard... "
echo "$out" | $clicopy
log "copied\n"
fi
if [ $preload ]; then
log "preloading on the gateways..."
(curl "$out" 2>/dev/null >/dev/null && log " ok\n") || (true && log " n/a\n")
fi
if [ $soundopt ]; then
log "playing sound..."
eval $playsound
log "ping\n"
fi