diff --git a/plugins/envssh.go b/plugins/envssh.go new file mode 100644 index 0000000..72c3232 --- /dev/null +++ b/plugins/envssh.go @@ -0,0 +1,59 @@ +package plugins + +import ( + "bytes" + "fmt" + "io" + "mime/multipart" + "net/http" +) + +// UploadEnvssh uploads photo/video to envs.sh. +// Media type should either be "video" or "photo". "Animation" is considered "video" here. +func UploadEnvssh(data *bytes.Buffer, id string) (string, error) { + // Create a buffer and a multipart writer + var requestBody bytes.Buffer + writer := multipart.NewWriter(&requestBody) + + // Create the file field + part, err := writer.CreateFormFile("file", id+".jpg") + if err != nil { + return "", fmt.Errorf("error creating form file: %v", err) + } + + // Copy the file into the field + _, err = io.Copy(part, data) + if err != nil { + return "", fmt.Errorf("error copying file: %v", err) + } + + // Close the writer to finalize the request + err = writer.Close() + if err != nil { + return "", fmt.Errorf("error closing writer: %v", err) + } + + // Send the POST request + req, err := http.NewRequest("POST", "https://envs.sh", &requestBody) + if err != nil { + return "", fmt.Errorf("error creating request: %v", err) + } + req.Header.Set("Content-Type", writer.FormDataContentType()) + + // Make the request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", fmt.Errorf("error sending request: %v", err) + } + defer resp.Body.Close() + + // Read the response + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("error reading response: %v", err) + } + + // Print the response from the server + return string(body), nil +} diff --git a/plugins/justwatch.go b/plugins/justwatch.go index 988724f..6dce688 100644 --- a/plugins/justwatch.go +++ b/plugins/justwatch.go @@ -174,11 +174,6 @@ func GetJWTitle(id string) (gotgbot.InputMediaPhoto, [][]gotgbot.InlineKeyboardB } posterURL := content.Poster.FullURL() - if posterURL == "" { - posterURL = jWBanner - } - - poster := gotgbot.InputFileByURL(posterURL) if len(content.Backdrops) > 0 { if s, ok := jWPosterCache[id]; ok { @@ -186,19 +181,21 @@ func GetJWTitle(id string) (gotgbot.InputMediaPhoto, [][]gotgbot.InlineKeyboardB } else { file := CreateJWPoster(content.FullBackdrops[0].FullURL(), posterURL, id) if file != nil { - poster = gotgbot.InputFileByReader(id, file) + imageURL, err := UploadEnvssh(file, id) + if err == nil { + posterURL = imageURL + jWPosterCache[id] = imageURL + } else { + fmt.Println("failed to upload to telegraph " + err.Error()) + } } - - // tGraphURL, err := UploadTelegraph(file, "photo") - // if err == nil { - // posterURL = tGraphURL - // jWPosterCache[id] = tGraphURL - // } else { - // fmt.Println("failed to upload to telegraph " + err.Error()) - // } } } + if posterURL == "" { + posterURL = jWBanner + } + if len(content.Clips) > 0 { var row []gotgbot.InlineKeyboardButton @@ -223,7 +220,7 @@ func GetJWTitle(id string) (gotgbot.InputMediaPhoto, [][]gotgbot.InlineKeyboardB } photo = gotgbot.InputMediaPhoto{ - Media: poster, + Media: gotgbot.InputFileByURL(posterURL), Caption: captionBuilder.String(), ParseMode: gotgbot.ParseModeHTML, HasSpoiler: true,