mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
feat: ()dl command: download given video with yt-dlp, then upload it to imagehost and post the link in response
This commit is contained in:
parent
2e7f672644
commit
5ae08fad7c
|
@ -1,199 +0,0 @@
|
|||
package main
|
||||
|
||||
// This whole file has been pretty much straight up copied
|
||||
// from: https://github.com/wabarc/go-catbox/blob/main/catbox.go
|
||||
// since I couldn't otherwise use litterbox instead.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/wabarc/helper"
|
||||
)
|
||||
|
||||
const (
|
||||
//ENDPOINT = "https://catbox.moe/user/api.php"
|
||||
ENDPOINT = "https://litterbox.catbox.moe/resources/internals/api.php"
|
||||
)
|
||||
|
||||
type catbox struct {
|
||||
Client *http.Client
|
||||
Userhash string
|
||||
}
|
||||
|
||||
func New(client *http.Client) *catbox {
|
||||
if client == nil {
|
||||
client = &http.Client{
|
||||
Timeout: 300 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
return &catbox{
|
||||
Client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// Upload file or URI to the Catbox. It returns an URL string and error.
|
||||
func (cat *catbox) Upload(v ...interface{}) (string, error) {
|
||||
if len(v) == 0 {
|
||||
return "", fmt.Errorf(`must specify file path or byte slice`)
|
||||
}
|
||||
|
||||
switch t := v[0].(type) {
|
||||
case string:
|
||||
path := t
|
||||
parse := func(s string, _ error) (string, error) {
|
||||
uri, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return uri.String(), nil
|
||||
}
|
||||
switch {
|
||||
case helper.IsURL(path):
|
||||
return parse(cat.urlUpload(path))
|
||||
case helper.Exists(path):
|
||||
return parse(cat.fileUpload(path))
|
||||
default:
|
||||
return "", errors.New(`path invalid`)
|
||||
}
|
||||
case []byte:
|
||||
if len(v) != 2 {
|
||||
return "", fmt.Errorf(`must specify file name`)
|
||||
}
|
||||
return cat.rawUpload(t, v[1].(string))
|
||||
}
|
||||
return "", fmt.Errorf(`unhandled`)
|
||||
}
|
||||
|
||||
func (cat *catbox) rawUpload(b []byte, name string) (string, error) {
|
||||
r, w := io.Pipe()
|
||||
m := multipart.NewWriter(w)
|
||||
|
||||
go func() {
|
||||
defer w.Close()
|
||||
defer m.Close()
|
||||
|
||||
m.WriteField("reqtype", "fileupload")
|
||||
m.WriteField("userhash", cat.Userhash)
|
||||
part, err := m.CreateFormFile("fileToUpload", filepath.Base(name))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = io.Copy(part, bytes.NewBuffer(b)); err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
req, _ := http.NewRequest(http.MethodPost, ENDPOINT, r)
|
||||
req.Header.Add("Content-Type", m.FormDataContentType())
|
||||
|
||||
resp, err := cat.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func (cat *catbox) fileUpload(path string) (string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// if size := helper.FileSize(path); size > 209715200 {
|
||||
// return "", fmt.Errorf("file too large, size: %d MB", size/1024/1024)
|
||||
// }
|
||||
|
||||
r, w := io.Pipe()
|
||||
m := multipart.NewWriter(w)
|
||||
|
||||
go func() {
|
||||
defer w.Close()
|
||||
defer m.Close()
|
||||
|
||||
m.WriteField("reqtype", "fileupload")
|
||||
//m.WriteField("userhash", cat.Userhash)
|
||||
m.WriteField("time", "24h")
|
||||
part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name()))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = io.Copy(part, file); err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, ENDPOINT, r)
|
||||
req.Header.Add("Content-Type", m.FormDataContentType())
|
||||
|
||||
resp, err := cat.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func (cat *catbox) urlUpload(uri string) (string, error) {
|
||||
b := new(bytes.Buffer)
|
||||
w := multipart.NewWriter(b)
|
||||
w.WriteField("reqtype", "urlupload")
|
||||
w.WriteField("userhash", cat.Userhash)
|
||||
w.WriteField("url", uri)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, ENDPOINT, b)
|
||||
req.Header.Add("Content-Type", w.FormDataContentType())
|
||||
|
||||
resp, err := cat.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func (cat *catbox) Delete(files ...string) error {
|
||||
// TODO
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *application) UploadCatbox(target, path string) {
|
||||
app.Send(target, "Uploading to catbox... dankCircle")
|
||||
|
||||
cat := New(nil)
|
||||
if url, err := cat.fileUpload(path); err != nil {
|
||||
app.Send(target, "Something went wrong FeelsBadMan")
|
||||
} else {
|
||||
app.Send(target, string(url))
|
||||
}
|
||||
}
|
|
@ -4,7 +4,10 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/wader/goutubedl"
|
||||
|
@ -18,13 +21,12 @@ func (app *application) Download(target, link string) {
|
|||
if err != nil {
|
||||
app.Log.Errorln(err)
|
||||
}
|
||||
rExt := result.Info.Ext
|
||||
rExt := "mp4"
|
||||
downloadResult, err := result.Download(context.Background(), "best")
|
||||
if err != nil {
|
||||
app.Log.Errorln(err)
|
||||
}
|
||||
app.Send(target, "Downloaded..")
|
||||
defer downloadResult.Close()
|
||||
fn, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
app.Log.Errorln(err)
|
||||
|
@ -40,10 +42,80 @@ func (app *application) Download(target, link string) {
|
|||
app.Log.Errorln(err)
|
||||
}
|
||||
|
||||
app.UploadCatbox(target, fmt.Sprintf("%s.%s", fn, rExt))
|
||||
downloadResult.Close()
|
||||
f.Close()
|
||||
duration := 5 * time.Second
|
||||
app.Send(target, "ResidentSleeper ..")
|
||||
time.Sleep(duration)
|
||||
|
||||
//os.Remove(fmt.Sprintf("%s.mp4", fn))
|
||||
|
||||
//app.TwitchClient.Say(channel, b.String())
|
||||
app.upload(target, fmt.Sprintf("%s.%s", fn, rExt))
|
||||
|
||||
}
|
||||
|
||||
func (app *application) upload(target, path string) {
|
||||
const URL = "https://i.yaf.ee/upload"
|
||||
app.Send(target, "Uploading .. dankCircle")
|
||||
pr, pw := io.Pipe()
|
||||
form := multipart.NewWriter(pw)
|
||||
|
||||
go func() {
|
||||
defer pw.Close()
|
||||
|
||||
err := form.WriteField("name", "xd")
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open(path) // path to image file
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
|
||||
w, err := form.CreateFormFile("file", path)
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, file)
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
|
||||
form.Close()
|
||||
}()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, URL, pr)
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", form.FormDataContentType())
|
||||
|
||||
httpClient := http.Client{}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
app.Log.Errorln("Error while sending HTTP request:", err)
|
||||
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
app.Send(target, "Uploaded .. PogChamp")
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
app.Log.Errorln("Error while reading response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var reply = string(body[:])
|
||||
|
||||
app.Send(target, fmt.Sprintf("Removing file: %s", path))
|
||||
os.Remove(path)
|
||||
app.Send(target, reply)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue