Skip to content

Commit

Permalink
feat(justwatch): upload image to envs.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisin0 committed Sep 10, 2024
1 parent 974717d commit dd347c7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
59 changes: 59 additions & 0 deletions plugins/envssh.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 12 additions & 15 deletions plugins/justwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,28 @@ 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 {
posterURL = s
} 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

Expand All @@ -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,
Expand Down

0 comments on commit dd347c7

Please sign in to comment.