-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (101 loc) · 2.74 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Messages struct {
Messages []Message `json:"messages"`
}
type Message struct {
Id string `json:"id"`
Content string `json:"content"`
Attachments []Attachment `json:"attachments"`
}
type Attachment struct {
Url string `json:"url"`
FileName string `json:"fileName"`
}
func main() {
// Read messages.json into byte array
jsonFile, err := os.Open("messages.json")
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
// Unmarshal byte array into Messages object
var messages Messages
json.Unmarshal(byteValue, &messages)
// Make sure a downloads folder exists
_ = os.Mkdir("downloads", 0755)
// Extract data from parsed json
var messageIds = ""
for _, message := range messages.Messages {
if message.Content == "" && message.Attachments != nil {
for _, attachment := range message.Attachments {
url := attachment.Url
name := attachment.FileName
if !IsMediaFile(name) {
fmt.Println("Rejecting URL: " + url)
continue
}
fmt.Println("Downloading (attach) : " + url)
DownloadFile("downloads\\"+name, url)
messageIds += "," + message.Id
}
} else if strings.HasPrefix(message.Content, "http") {
url := ExtractUrl(message.Content)
name := ExtractFileName(url)
if !IsMediaFile(name) {
fmt.Println("Rejecting URL: " + url)
continue
}
fmt.Println("Downloading (url msg): " + url)
DownloadFile("downloads\\"+name, url)
messageIds += "," + message.Id
}
}
fmt.Println("Deleted Messages: " + messageIds)
}
func IsMediaFile(name string) bool {
return (strings.HasSuffix(name, ".png") || strings.HasSuffix(name, ".jpg") || strings.HasSuffix(name, ".jpeg") || strings.HasSuffix(name, ".gif") || strings.HasSuffix(name, ".mp4") || strings.HasSuffix(name, ".webm"))
}
func ExtractFileName(url string) string {
return url[strings.LastIndex(url, "/")+1:]
}
func ExtractUrl(content string) string {
extracted := strings.Split(content, " ")[0]
questionRemoved := strings.Split(extracted, "?")[0]
return questionRemoved
}
func DownloadFile(filepath string, url string) error {
// Get data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// If file already exists
_, err = os.Stat(filepath)
coutner := 1
for !os.IsNotExist(err) {
// Add counter
index := strings.LastIndex(filepath, ".")
filepath = filepath[:index] + fmt.Sprintf("_%d", coutner) + filepath[index:]
// Check with new name
_, err = os.Stat(filepath)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}