mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-20 22:16:09 +05:00
update
This commit is contained in:
@@ -1,44 +1,23 @@
|
||||
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"
|
||||
"server/tgbot/config"
|
||||
up "server/tgbot/upload"
|
||||
"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) {
|
||||
config.LoadConfig()
|
||||
|
||||
pref := tele.Settings{
|
||||
URL: config.Cfg.HostTG,
|
||||
Token: token,
|
||||
Poller: &tele.LongPoller{Timeout: 5 * time.Minute},
|
||||
ParseMode: tele.ModeHTML,
|
||||
@@ -53,8 +32,11 @@ func Start(token string) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(cfg.WhiteIds) > 0 {
|
||||
b.Use(middleware.Whitelist(cfg.WhiteIds...))
|
||||
if len(config.Cfg.WhiteIds) > 0 {
|
||||
b.Use(middleware.Whitelist(config.Cfg.WhiteIds...))
|
||||
}
|
||||
if len(config.Cfg.BlackIds) > 0 {
|
||||
b.Use(middleware.Blacklist(config.Cfg.BlackIds...))
|
||||
}
|
||||
|
||||
//Commands
|
||||
@@ -63,14 +45,32 @@ func Start(token string) {
|
||||
b.Handle("/help", help)
|
||||
b.Handle("/Help", help)
|
||||
b.Handle("/start", help)
|
||||
b.Handle("/id", help)
|
||||
|
||||
b.Handle("/list", list)
|
||||
b.Handle("/clear", clear)
|
||||
|
||||
//Text
|
||||
b.Handle(tele.OnText, func(c tele.Context) error {
|
||||
txt := c.Text()
|
||||
if strings.HasPrefix(strings.ToLower(txt), "magnet:") || isHash(txt) {
|
||||
return addTorrent(c, txt)
|
||||
err := addTorrent(c, txt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return list(c)
|
||||
} else if c.Message().ReplyTo != nil && c.Message().ReplyTo.ReplyMarkup != nil && len(c.Message().ReplyTo.ReplyMarkup.InlineKeyboard) > 0 {
|
||||
data := c.Message().ReplyTo.ReplyMarkup.InlineKeyboard[0][0].Data
|
||||
if strings.HasPrefix(strings.ToLower(data), "\fall|") {
|
||||
hash := strings.TrimPrefix(data, "\fall|")
|
||||
from, to, err := ParseRange(c.Message().Text)
|
||||
if err != nil {
|
||||
c.Send("Ошибка, нужно указывать числа, пример: 2-12")
|
||||
return err
|
||||
}
|
||||
up.AddRange(c, hash, from, to)
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return c.Send("Вставьте магнет/хэш торрента чтоб добавить его на сервер")
|
||||
}
|
||||
@@ -80,75 +80,43 @@ 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.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
|
||||
return files(c)
|
||||
}
|
||||
if args[0] == "\fdelete" {
|
||||
deleteTorrent(c)
|
||||
return list(c)
|
||||
}
|
||||
if args[0] == "\fupload" {
|
||||
return upload(c)
|
||||
}
|
||||
if args[0] == "\fuploadall" {
|
||||
uploadall(c)
|
||||
return nil
|
||||
}
|
||||
if args[0] == "\fcancel" {
|
||||
if num, err := strconv.Atoi(args[1]); err == nil {
|
||||
up.Cancel(num)
|
||||
c.Bot().Delete(c.Callback().Message)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.New("Ошибка кнопка не распознана")
|
||||
})
|
||||
|
||||
up.Start()
|
||||
|
||||
go b.Start()
|
||||
}
|
||||
|
||||
func help(c tele.Context) error {
|
||||
id := strconv.FormatInt(c.Sender().ID, 10)
|
||||
return c.Send("Бот для управления TorrServer\n\n" +
|
||||
"Список комманд:\n" +
|
||||
" /help - эта справка\n" +
|
||||
" /list - показать список торрентов на сервере")
|
||||
}
|
||||
|
||||
func list(c tele.Context) error {
|
||||
list := torr.ListTorrent()
|
||||
|
||||
for _, t := range list {
|
||||
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+" "+humanize.Bytes(uint64(t.Size)), torrKbd)
|
||||
}
|
||||
return nil
|
||||
" /help - Эта справка\n" +
|
||||
" /list - Показать список торрентов на сервере\n" +
|
||||
" /clear - Удалить все торренты\n\n" +
|
||||
"Ваш id: <code>" + id + "</code>, " + strings.Join([]string{c.Sender().Username, c.Sender().FirstName, c.Sender().LastName}, ", "))
|
||||
}
|
||||
|
||||
func isHash(txt string) bool {
|
||||
@@ -164,3 +132,22 @@ func isHash(txt string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ParseRange(rng string) (int, int, error) {
|
||||
parts := strings.Split(rng, "-")
|
||||
|
||||
if len(parts) != 2 {
|
||||
return -1, -1, errors.New("Неверный формат строки")
|
||||
}
|
||||
|
||||
num1, err1 := strconv.Atoi(strings.TrimSpace(parts[0]))
|
||||
if err1 != nil {
|
||||
return -1, -1, err1
|
||||
}
|
||||
|
||||
num2, err2 := strconv.Atoi(strings.TrimSpace(parts[1]))
|
||||
if err2 != nil {
|
||||
return -1, -1, err2
|
||||
}
|
||||
return num1, num2, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user