Skip to content

Commit 3ca0428

Browse files
committed
automate trial attachments
currently only really works with images, but can easily be adapted to work with other files.
1 parent 8cf54fd commit 3ca0428

File tree

2 files changed

+95
-24
lines changed

2 files changed

+95
-24
lines changed

main.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,16 @@ func main() {
4343

4444
log.Info("Done getting messages")
4545

46+
// save transcript
47+
transcript.SaveTranscript()
48+
4649
stats := transcript.GetStats()
4750

4851
log.Info("Stats:")
4952
log.Infof("\tTotal messages: %d", stats.TotalMessages)
5053
log.Infof("\tTotal users: %d", stats.TotalUsers)
5154
log.Infof("\tStart date: %s", stats.StartDate)
5255
log.Infof("\tEnd date: %s", stats.EndDate)
53-
54-
// // print transcript
55-
// transcript.PrintTranscript()
56-
57-
// save transcript
58-
transcript.SaveTranscript()
5956
}
6057

6158
// parse name override into map

transcript.go

+92-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
5+
"io"
6+
"net/http"
47
"os"
58
"regexp"
69
"sort"
@@ -15,9 +18,18 @@ import (
1518
"github.com/spf13/viper"
1619
)
1720

21+
type MsgAttachment struct {
22+
ID snowflake.ID
23+
Filename string
24+
ContentType string
25+
URL string
26+
Size int
27+
}
28+
1829
type Msg struct {
19-
Content string
20-
ID snowflake.ID
30+
Content string
31+
ID snowflake.ID
32+
Attachments []MsgAttachment
2133
}
2234

2335
type MsgBlock struct {
@@ -45,25 +57,32 @@ func (t *Transcript) AddMessage(m discord.Message) {
4557
// we get messages in the reverse order they were send,
4658
// so we need to add them in the reverse order we get them
4759

60+
msg := Msg{
61+
Content: m.Content,
62+
ID: m.ID,
63+
Attachments: []MsgAttachment{},
64+
}
65+
66+
if len(m.Attachments) > 0 {
67+
for _, a := range m.Attachments {
68+
msg.Attachments = append(msg.Attachments, MsgAttachment{
69+
ID: a.ID,
70+
Filename: a.Filename,
71+
ContentType: *a.ContentType,
72+
URL: a.URL,
73+
Size: a.Size,
74+
})
75+
}
76+
}
77+
4878
// if first block is the same user
4979
if len(t.Blocks) > 0 && t.Blocks[0].UserId == m.Author.ID {
5080
// just put the message in the first block
51-
52-
// create a new message
53-
message := Msg{
54-
Content: m.Content,
55-
ID: m.ID,
56-
}
57-
t.Blocks[0].Messages = append([]Msg{message}, t.Blocks[0].Messages...)
81+
t.Blocks[0].Messages = append([]Msg{msg}, t.Blocks[0].Messages...)
5882

5983
return
6084
}
6185

62-
msg := Msg{
63-
Content: m.Content,
64-
ID: m.ID,
65-
}
66-
6786
var name string
6887

6988
// check if we have a name override
@@ -197,10 +216,13 @@ func (t *Transcript) PrintTranscript() {
197216

198217
// writes transcript to a file
199218
func (t *Transcript) SaveTranscript() {
200-
folderPath := "transcripts"
219+
log.Info("Saving transcript...")
220+
201221
trialName := viper.GetString("TRIAL_NAME")
222+
// format trial name for use as filename
223+
trailNameFormated := t.FormatFileName(trialName)
202224

203-
fileName := t.FormatFileName(trialName)
225+
folderPath := fmt.Sprintf("transcripts/%s", trailNameFormated)
204226

205227
// check if folder exists
206228
if _, err := os.Stat(folderPath); eris.Is(err, os.ErrNotExist) {
@@ -214,7 +236,7 @@ func (t *Transcript) SaveTranscript() {
214236
}
215237

216238
// create file
217-
f, err := os.Create(folderPath + "/" + fileName + ".md")
239+
f, err := os.Create(folderPath + "/" + trailNameFormated + ".md")
218240
if err != nil {
219241
eris.Wrap(err, "failed to create file")
220242
log.Panic(err)
@@ -236,15 +258,67 @@ func (t *Transcript) SaveTranscript() {
236258
// println(b.Name)
237259
for _, m := range b.Messages {
238260

261+
attachments := ""
262+
263+
for _, a := range m.Attachments {
264+
// construct attachment path for use on the website
265+
attachmentPath := fmt.Sprintf("../../assets/judiciary/%s/%s", trailNameFormated, a.Filename)
266+
267+
attachments += fmt.Sprintf("![%s](%s)\n", a.Filename, attachmentPath)
268+
t.saveAttachment(a, folderPath)
269+
}
270+
239271
// preprocess message to handle newlines
240272
content := strings.ReplaceAll(m.Content, "\n", "\n> ")
241273
content = t.replaceMentions(content)
242274

243275
writeToFile(f, "> "+content)
244-
writeToFile(f, "")
276+
// writeToFile(f, "")
277+
278+
// only write attachments if there are any
279+
if len(attachments) > 0 {
280+
writeToFile(f, "")
281+
writeToFile(f, attachments)
282+
}
245283
}
246284
writeToFile(f, "")
247285
}
286+
287+
log.Info("Transcript saved!")
288+
}
289+
290+
func (t *Transcript) saveAttachment(attachment MsgAttachment, folderPath string) {
291+
// Create blank file
292+
file, err := os.Create(folderPath + "/" + attachment.Filename)
293+
if err != nil {
294+
err := eris.Wrap(err, "failed to create attachment file")
295+
log.Fatal(err)
296+
}
297+
defer file.Close()
298+
299+
// http client to download attachment
300+
client := http.Client{
301+
CheckRedirect: func(r *http.Request, via []*http.Request) error {
302+
r.URL.Opaque = r.URL.Path
303+
return nil
304+
},
305+
}
306+
307+
// Put content on file
308+
resp, err := client.Get(attachment.URL)
309+
if err != nil {
310+
err := eris.Wrap(err, "failed to download attachment")
311+
log.Fatal(err)
312+
}
313+
defer resp.Body.Close()
314+
315+
size, err := io.Copy(file, resp.Body)
316+
if err != nil {
317+
err := eris.Wrap(err, "failed to write attachment to file")
318+
log.Fatal(err)
319+
}
320+
321+
log.Debugf("Saved attachment: %s (%d)", attachment.Filename, size)
248322
}
249323

250324
// creates a new transcript

0 commit comments

Comments
 (0)