mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-20 14:06:09 +05:00
update
This commit is contained in:
@@ -1,15 +1,42 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/dustin/go-humanize"
|
||||
tele "gopkg.in/telebot.v4"
|
||||
"gopkg.in/telebot.v4/middleware"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"server/log"
|
||||
"server/settings"
|
||||
"server/torr"
|
||||
"server/web"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
WhiteIds []int64
|
||||
}
|
||||
|
||||
var cfg *Config
|
||||
|
||||
func init() {
|
||||
cfg = &Config{}
|
||||
fn := filepath.Join(settings.Path, "tg.cfg")
|
||||
buf, err := os.ReadFile(fn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(buf, &cfg)
|
||||
if err != nil {
|
||||
log.TLogln("Error read tg config:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func Start(token string) {
|
||||
pref := tele.Settings{
|
||||
Token: token,
|
||||
@@ -26,8 +53,11 @@ func Start(token string) {
|
||||
return
|
||||
}
|
||||
|
||||
//Commands
|
||||
if len(cfg.WhiteIds) > 0 {
|
||||
b.Use(middleware.Whitelist(cfg.WhiteIds...))
|
||||
}
|
||||
|
||||
//Commands
|
||||
b.Handle("help", help)
|
||||
b.Handle("Help", help)
|
||||
b.Handle("/help", help)
|
||||
@@ -50,14 +80,48 @@ func Start(token string) {
|
||||
args := c.Args()
|
||||
if len(args) > 0 {
|
||||
if args[0] == "\ffiles" {
|
||||
msg, err := c.Bot().Send(c.Sender(), "Подключение к торренту...")
|
||||
t := torr.GetTorrent(args[1])
|
||||
if t == nil {
|
||||
c.Send("Torrent not connected:", args[1])
|
||||
} else {
|
||||
|
||||
}
|
||||
c.Edit(msg, "Torrent not connected: "+args[1])
|
||||
return nil
|
||||
}
|
||||
if err == nil {
|
||||
go func() {
|
||||
for !t.WaitInfo() {
|
||||
time.Sleep(time.Second)
|
||||
t = torr.GetTorrent(args[1])
|
||||
}
|
||||
c.Bot().Delete(msg)
|
||||
host := settings.PubIPv4
|
||||
if host == "" {
|
||||
ips := web.GetLocalIps()
|
||||
if len(ips) == 0 {
|
||||
host = "127.0.0.1"
|
||||
} else {
|
||||
host = ips[0]
|
||||
}
|
||||
}
|
||||
|
||||
t = torr.GetTorrent(args[1])
|
||||
st := t.Status()
|
||||
txt := "Файлы:\n"
|
||||
for _, file := range st.FileStats {
|
||||
ff := "<b>" + filepath.Base(file.Path) + "</b> <i>" + humanize.Bytes(uint64(file.Length)) + "</i> " +
|
||||
"<a href=\"http://" + host + ":" + settings.Port + "/stream/" + filepath.Base(file.Path) + "?link=" + t.Hash().HexString() + "&index=" + strconv.Itoa(file.Id) + "&play\">Download</a>\n\n"
|
||||
if len(txt+ff) > 4096 {
|
||||
c.Send(txt)
|
||||
txt = ""
|
||||
}
|
||||
txt += ff
|
||||
}
|
||||
if len(txt) > 0 {
|
||||
c.Send(txt)
|
||||
}
|
||||
}()
|
||||
}
|
||||
return err
|
||||
}
|
||||
if args[0] == "\fdelete" {
|
||||
return nil
|
||||
}
|
||||
@@ -82,7 +146,7 @@ func list(c tele.Context) error {
|
||||
btnFiles := tele.InlineButton{Text: "Файлы", Unique: "files", Data: t.Hash().String()}
|
||||
btnDelete := tele.InlineButton{Text: "Удалить", Unique: "delete", Data: t.Hash().String()}
|
||||
torrKbd := &tele.ReplyMarkup{InlineKeyboard: [][]tele.InlineButton{{btnFiles, btnDelete}}}
|
||||
c.Send(t.Title, torrKbd)
|
||||
c.Send(t.Title+" "+humanize.Bytes(uint64(t.Size)), torrKbd)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user