more fixes idk this history gonna be public n fun

This commit is contained in:
blank X 2021-11-18 17:44:24 +07:00
parent c5ceea00fb
commit 0849c99d3c
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 43 additions and 9 deletions

View File

@ -21,6 +21,7 @@ func main() {
fmt.Fprintf(os.Stderr, "Usage: %s <submission id/url>\n", os.Args[0])
os.Exit(1)
}
LoadMimetypes()
submissionUrl, err := url.Parse(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Error when parsing submission url: %s\n", err)
@ -196,10 +197,10 @@ func main() {
} else if submission.IsRedditMediaDomain && submission.Preview != nil {
preview := submission.Preview.Images[0]
if pathExt == ".gif" {
if preview.Variants.Mp4 != nil {
urls[0] = preview.Variants.Mp4.Url
} else if preview.Variants.Gif != nil {
urls[0] = preview.Variants.Gif.Url
if preview.Variants.Mp4.Source != nil {
urls[0] = preview.Variants.Mp4.Source.Url
} else if preview.Variants.Gif.Source != nil {
urls[0] = preview.Variants.Gif.Source.Url
}
} else if submission.IsVideo {
urls[0] = preview.Source.Url

View File

@ -64,8 +64,12 @@ type Submission struct {
Preview *struct {
Images []struct {
Variants struct {
Mp4 *PreviewSource `json:"mp4"`
Gif *PreviewSource `json:"gif"`
Mp4 struct {
Source *PreviewSource `json:"source"`
} `json:"mp4"`
Gif struct {
Source *PreviewSource `json:"source"`
} `json:"gif"`
} `json:"variants"`
Source *PreviewSource `json:"source"`
} `json:"images"`

View File

@ -17,9 +17,10 @@ import (
)
var (
configDir string
config *Config
data *Data
configDir string
config *Config
data *Data
mimeOverride map[string]string
)
func LoadConfigAndData() error {
@ -74,6 +75,30 @@ func LoadConfigAndData() error {
return nil
}
// a jpeg file can have a jpe extension and i personally don't like it
// https://github.com/LonamiWebs/Telethon/blob/2e1be01ad4f6462de2e9e1f96a33537e51f44980/telethon/utils.py#L33
func LoadMimetypes() {
mimeOverride = make(map[string]string)
mimeOverride["image/png"] = ".png"
mimeOverride["image/jpeg"] = ".jpeg"
mimeOverride["image/webp"] = ".webp"
mimeOverride["image/gif"] = ".gif"
mimeOverride["image/bmp"] = ".bmp"
mimeOverride["image/x-tga"] = ".tga"
mimeOverride["image/tiff"] = ".tiff"
mimeOverride["image/vnd.adobe.photoshop"] = ".psd"
mimeOverride["video/mp4"] = ".mp4"
mimeOverride["video/quicktime"] = ".mov"
mimeOverride["video/avi"] = ".avi"
mimeOverride["audio/mpeg"] = ".mp3"
mimeOverride["audio/m4a"] = ".m4a"
mimeOverride["audio/aac"] = ".aac"
mimeOverride["audio/ogg"] = ".ogg"
mimeOverride["audio/flac"] = ".flac"
}
func WriteData() error {
contents, err := json.Marshal(data)
if err != nil {
@ -256,6 +281,10 @@ func GetExtension(mimeType string) (string, error) {
if err != nil {
return "", fmt.Errorf("Failed to parse media type of %s: %s", mimeType, err)
}
ext, exists := mimeOverride[mediaType]
if exists {
return ext, nil
}
exts, err := mime.ExtensionsByType(mediaType)
if err != nil {
return "", fmt.Errorf("Failed to find file extensions of %s: %s", mediaType, err)