Download media attachments

This commit is contained in:
Gabriel Garrido 2024-05-09 01:13:10 +02:00
parent 1075339551
commit f9ae06fa3c
3 changed files with 101 additions and 7 deletions

View file

@ -25,6 +25,12 @@ type MediaAttachment struct {
Type string `json:"type"` Type string `json:"type"`
Url string `json:"url"` Url string `json:"url"`
Description string `json:"description"` Description string `json:"description"`
Id string `json:"id"`
Path string
}
func (ma MediaAttachment) SetPath(path string) {
ma.Path = path
} }
type Post struct { type Post struct {

View file

@ -2,11 +2,15 @@ package files
import ( import (
"fmt" "fmt"
"git.garrido.io/gabriel/mastodon-pesos/client" "io"
md "github.com/JohannesKaufmann/html-to-markdown" "mime"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"text/template" "text/template"
"git.garrido.io/gabriel/mastodon-pesos/client"
md "github.com/JohannesKaufmann/html-to-markdown"
) )
type FileWriter struct { type FileWriter struct {
@ -55,9 +59,41 @@ func (f FileWriter) Write(post client.Post) error {
var descendants []client.Post var descendants []client.Post
f.getReplies(post.Id, &descendants) f.getReplies(post.Id, &descendants)
var file *os.File
if len(post.MediaAttachments) == 0 {
name := fmt.Sprintf("%s.md", post.Id) name := fmt.Sprintf("%s.md", post.Id)
filename := filepath.Join(f.dir, name) filename := filepath.Join(f.dir, name)
file, err := os.Create(filename) file, err = os.Create(filename)
} else {
dir := filepath.Join(f.dir, post.Id)
imagedir := filepath.Join(dir, "images")
os.Mkdir(dir, os.ModePerm)
os.Mkdir(imagedir, os.ModePerm)
for i := 0; i < len(post.MediaAttachments); i++ {
media := &post.MediaAttachments[i]
if media.Type != "image" {
continue
}
image, err := downloadAttachment(imagedir, media.Id, media.Url)
if err != nil {
return err
}
media.Path = fmt.Sprintf("images/%s", image)
}
if err != nil {
return fmt.Errorf("error downloading media attachments: %w", err)
}
filename := filepath.Join(dir, "index.md")
file, err = os.Create(filename)
}
if err != nil { if err != nil {
return fmt.Errorf("error creating file: %w", err) return fmt.Errorf("error creating file: %w", err)
@ -92,3 +128,55 @@ func (f FileWriter) getReplies(postId string, replies *[]client.Post) {
f.getReplies(reply.Id, replies) f.getReplies(reply.Id, replies)
} }
} }
func downloadAttachment(dir string, id string, url string) (string, error) {
var filename string
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "image/*")
res, err := client.Do(req)
if err != nil {
return filename, err
}
defer res.Body.Close()
contentType := res.Header.Get("Content-Type")
extensions, err := mime.ExtensionsByType(contentType)
if err != nil {
return filename, err
}
var ext string
urlExt := filepath.Ext(url)
for _, i := range extensions {
if i == urlExt {
ext = i
break
}
}
if ext == "" {
return filename, fmt.Errorf("Could not match extension for media")
}
filename = fmt.Sprintf("%s%s", id, ext)
file, err := os.Create(filepath.Join(dir, filename))
if err != nil {
return filename, err
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
return filename, err
}
return filename, nil
}

View file

@ -12,7 +12,7 @@ post_ids:
{{ range .Post.MediaAttachments }} {{ range .Post.MediaAttachments }}
{{- if eq .Type "image" -}} {{- if eq .Type "image" -}}
![{{ .Description }}]({{ .Url }}) ![{{ .Description }}]({{ .Path }})
{{ end }} {{ end }}
{{- end -}} {{- end -}}
@ -20,7 +20,7 @@ post_ids:
{{ .Content | tomd -}} {{ .Content | tomd -}}
{{ range .MediaAttachments }} {{ range .MediaAttachments }}
{{- if eq .Type "image" -}} {{- if eq .Type "image" -}}
![{{ .Description }}]({{ .Url }}) ![{{ .Description }}]({{ .Path }})
{{ end }} {{ end }}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}