-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (74 loc) · 2.04 KB
/
main.go
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
package main
import (
"context"
"flag"
"fmt"
"image"
"image/png"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"github.com/kbinani/screenshot"
"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
slop, err := exec.Command("slop").Output()
if err != nil {
panic(err)
}
slopOut := string(slop)
re := regexp.MustCompile(`\d+`)
slopValues := re.FindAllStringSubmatch(slopOut, -1)
width, _ := strconv.Atoi(slopValues[0][0])
height, _ := strconv.Atoi(slopValues[1][0])
x, _ := strconv.Atoi(slopValues[2][0])
y, _ := strconv.Atoi(slopValues[3][0])
rec := image.Rectangle{Max: image.Point{x, y}, Min: image.Point{X: x + width, Y: y + height}}
img, err := screenshot.CaptureRect(rec)
if err != nil {
panic(err)
}
imgFile, err := os.Create("screencap.png")
if err != nil {
panic(err)
}
defer imgFile.Close()
png.Encode(imgFile, img)
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
client := s3.NewFromConfig(cfg)
bucketName := flag.String("b", "my-cheeky-screenshots", "Should be the name of an AWS bucket")
flag.Parse()
keyName := fmt.Sprintf("screencap%d_%d-%d.png", width, height, time.Now().Unix())
freshPtr, err := os.Open(imgFile.Name())
if err != nil {
panic(err)
}
if _, err := client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(*bucketName),
Key: aws.String(keyName),
Body: freshPtr,
ContentType: aws.String("image/png"),
}); err != nil {
panic(err)
}
psClient := s3.NewPresignClient(client)
presignOptions := s3.WithPresignExpires(time.Second * 604800)
resp, err := psClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{
Bucket: aws.String(*bucketName),
Key: aws.String(keyName),
ResponseContentDisposition: aws.String("inline"),
}, presignOptions)
if err != nil {
panic(err)
}
clipboard.WriteAll(resp.URL)
exec.Command("notify-send", resp.URL).Run()
}