-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.go
794 lines (771 loc) · 24.1 KB
/
observer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*
* BSD-3-Clause
* Copyright 2020 sot (PR_713, C_rho_272)
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
package TtKVC
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
tr "github.com/hekmon/transmissionrpc"
"html"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"sot-te.ch/HTExtractor"
tg "sot-te.ch/MTHelper"
"strconv"
"strings"
"syscall"
tmpl "text/template"
"time"
)
type Observer struct {
Log struct {
File string `json:"file"`
Level string `json:"level"`
} `json:"log"`
Crawler struct {
BaseURL string `json:"baseurl"`
ContextURL string `json:"contexturl"`
Delay uint `json:"delay"`
Threshold uint `json:"threshold"`
ReloadDelay uint `json:"reloaddelay"`
IgnoreRegexp string `json:"ignoreregexp"`
MetaActions []HTExtractor.ExtractAction `json:"metaactions"`
MetaExtractor *HTExtractor.Extractor `json:"-"`
} `json:"crawler"`
Transmission struct {
Host string `json:"host"`
Port uint16 `json:"port"`
Login string `json:"login"`
Password string `json:"password"`
Path string `json:"path"`
Encryption bool `json:"encryption"`
Trackers []string `json:"trackers"`
Client *tr.Client `json:"-"`
} `json:"transmission"`
DB Database `json:"db"`
Telegram struct {
ApiId int32 `json:"apiid"`
ApiHash string `json:"apihash"`
BotToken string `json:"bottoken"`
DBPath string `json:"dbpath"`
FileStore string `json:"filestorepath"`
OTPSeed string `json:"otpseed"`
Messages struct {
tg.TGMessages
State string `json:"state"`
stateTmpl *tmpl.Template
VideoIgnored string `json:"videoignored"`
videoIgnoredTmpl *tmpl.Template
VideoForced string `json:"videoforced"`
videoForcedTmpl *tmpl.Template
KUpload string `json:"kupload"`
kuploadTmpl *tmpl.Template
TUpload string `json:"tupload"`
tuploadTmpl *tmpl.Template
} `json:"msg"`
Video struct {
Upload bool `json:"upload"`
SequentialUpload bool `json:"sequential"`
TempPath string `json:"temppath"`
} `json:"video"`
Client *tg.Telegram `json:"-"`
} `json:"telegram"`
Kaltura struct {
Kaltura
WatchPath string `json:"watchpath"`
Tags map[string]bool `json:"tags"`
EntryName string `json:"entryname"`
entryNameTmpl *tmpl.Template
} `json:"kaltura"`
ignorePattern *regexp.Regexp
}
func ReadConfig(path string) (*Observer, error) {
config := new(Observer)
confData, err := ioutil.ReadFile(filepath.Clean(path))
if err == nil {
err = json.Unmarshal(confData, config)
}
return config, err
}
func (cr *Observer) getState(chat int64) (string, error) {
var err error
var isMob, isAdmin bool
var pending []TorrentFile
var index uint
if isMob, err = cr.DB.GetChatExist(chat); err != nil {
return "", err
}
if isAdmin, err = cr.DB.GetAdminExist(chat); err != nil {
return "", err
}
if index, err = cr.DB.GetCrawlOffset(); err != nil {
return "", err
}
pendingSB := strings.Builder{}
if strings.Index(cr.Telegram.Messages.State, pFilesPending) >= 0 {
if pending, err = cr.DB.GetTorrentFilesNotReady(); err != nil {
return "", err
}
if pending != nil {
for _, val := range pending {
pendingSB.WriteString(val.String())
pendingSB.WriteRune('\n')
}
}
}
return formatMessage(cr.Telegram.Messages.stateTmpl, map[string]interface{}{
pWatch: isMob,
pAdmin: isAdmin,
pFilesPending: pendingSB.String(),
pIndex: index,
pVersion: Version,
})
}
func (cr *Observer) InitTg() error {
logger.Debug("Initiating telegram bot")
telegram := tg.New(cr.Telegram.ApiId, cr.Telegram.ApiHash, cr.Telegram.DBPath, cr.Telegram.FileStore, cr.Telegram.OTPSeed)
telegram.Messages = cr.Telegram.Messages.TGMessages
telegram.BackendFunctions = tg.TGBackendFunction{
GetOffset: cr.DB.GetTgOffset,
SetOffset: cr.DB.UpdateTgOffset,
ChatExist: cr.DB.GetChatExist,
ChatAdd: cr.DB.AddChat,
ChatRm: cr.DB.DelChat,
AdminExist: cr.DB.GetAdminExist,
AdminAdd: cr.DB.AddAdmin,
AdminRm: cr.DB.DelAdmin,
State: cr.getState,
}
if err := telegram.LoginAsBot(cr.Telegram.BotToken, tg.MtLogWarning); err == nil {
cr.Telegram.Client = telegram
logger.Debug("Telegram bot init complete")
_ = cr.Telegram.Client.AddCommand(tCmdForceUpload, cr.cmdCheckTorrent)
return cr.Telegram.Client.AddCommand(tCmdSwitchIgnore, cr.cmdSwitchFileReadyStatus)
} else {
return err
}
}
func (cr *Observer) InitKaltura() error {
var err error
logger.Debug("Initiating kaltura")
if isEmpty(cr.Kaltura.URL) || isEmpty(cr.Kaltura.UserId) || isEmpty(cr.Kaltura.Secret) {
err = errors.New("invalid kaltura connection data")
} else {
err = cr.Kaltura.CreateSession()
}
logger.Debug("Kaltura init complete, err", err)
if !isEmpty(cr.Kaltura.EntryName) {
var msgErr error
if cr.Kaltura.entryNameTmpl, msgErr = tmpl.New("entryName").Parse(cr.Kaltura.EntryName); msgErr != nil {
logger.Error(msgErr)
}
}
return err
}
func (cr *Observer) InitTransmission() error {
var err error
logger.Debug("Initiating transmission rpc")
if !isEmpty(cr.Transmission.Host) && cr.Transmission.Port > 0 {
var t *tr.Client
if t, err = tr.New(cr.Transmission.Host, cr.Transmission.Login, cr.Transmission.Password, &tr.AdvancedConfig{
HTTPS: cr.Transmission.Encryption,
Port: cr.Transmission.Port,
}); err == nil {
cr.Transmission.Client = t
}
} else {
err = errors.New("invalid transmission connection data")
}
logger.Debug("Transmission rpc init complete, err", err)
return err
}
func (cr *Observer) InitMetaExtractor() error {
var err error
if cr.Crawler.MetaExtractor == nil {
logger.Debug("Initiating meta extractor")
if len(cr.Crawler.MetaActions) == 0 {
err = errors.New("extract actions not set")
} else {
ex := HTExtractor.New()
if err = ex.Compile(cr.Crawler.MetaActions); err == nil {
cr.Crawler.MetaExtractor = ex
}
}
logger.Debug("Meta extractor init complete, err ", err)
}
return err
}
func (cr *Observer) InitMessages() error {
var err error
sb := strings.Builder{}
logger.Debug("Initiating message templates")
if cr.Telegram.Messages.stateTmpl, err = tmpl.New("state").Parse(cr.Telegram.Messages.State); err != nil {
sb.WriteString("state: ")
sb.WriteString(err.Error())
sb.WriteRune('\n')
}
if cr.Telegram.Messages.kuploadTmpl, err = tmpl.New("kupload").Parse(cr.Telegram.Messages.KUpload); err != nil {
sb.WriteString("kupload: ")
sb.WriteString(err.Error())
sb.WriteRune('\n')
}
if cr.Telegram.Messages.tuploadTmpl, err = tmpl.New("tupload").Parse(cr.Telegram.Messages.TUpload); err != nil {
sb.WriteString("tupload: ")
sb.WriteString(err.Error())
sb.WriteRune('\n')
}
if cr.Telegram.Messages.videoForcedTmpl, err = tmpl.New("videoForced").Parse(cr.Telegram.Messages.VideoForced); err != nil {
sb.WriteString("videoForced: ")
sb.WriteString(err.Error())
sb.WriteRune('\n')
}
if cr.Telegram.Messages.videoIgnoredTmpl, err = tmpl.New("videoIgnored").Parse(cr.Telegram.Messages.VideoIgnored); err != nil {
sb.WriteString("videoIgnored: ")
sb.WriteString(err.Error())
sb.WriteRune('\n')
}
if sb.Len() > 0 {
err = errors.New(sb.String())
}
logger.Debug("Message templates init complete, err", err)
return err
}
func (cr *Observer) Init() error {
var err error
if isEmpty(cr.Crawler.IgnoreRegexp) {
cr.ignorePattern = nonEmptyRegexp
} else if cr.ignorePattern, err = regexp.Compile(cr.Crawler.IgnoreRegexp); err != nil {
return err
}
if err = cr.DB.Connect(); err != nil {
return err
}
if err = cr.InitTg(); err != nil {
return err
}
if err = cr.InitKaltura(); err != nil {
return err
}
if err = cr.InitMessages(); err != nil {
logger.Error(err)
err = nil
}
if err = cr.InitMetaExtractor(); err != nil {
logger.Error(err)
err = nil
}
if err = cr.InitTransmission(); err != nil {
logger.Error(err)
err = nil
}
return nil
}
func (cr *Observer) Engage() {
defer cr.DB.Close()
defer cr.Kaltura.EndSession()
defer cr.Telegram.Client.Close()
var err error
var nextOffset uint
if nextOffset, err = cr.DB.GetCrawlOffset(); err == nil {
go cr.Telegram.Client.HandleUpdates()
for {
newNextOffset := nextOffset
torrents := make([]*Torrent, 0, cr.Crawler.Threshold)
for offsetToCheck := nextOffset; offsetToCheck < nextOffset+cr.Crawler.Threshold; offsetToCheck++ {
var torrent *Torrent
torrent = cr.checkTorrent(offsetToCheck, false)
if torrent != nil {
newNextOffset = offsetToCheck + 1
torrents = append(torrents, torrent)
}
}
if newNextOffset > nextOffset {
nextOffset = newNextOffset
if err = cr.DB.UpdateCrawlOffset(nextOffset); err != nil {
logger.Error(err)
}
}
if len(torrents) > 0 {
go cr.uploadTorrents(torrents)
}
cr.checkVideo()
sleepTime := time.Duration(rand.Intn(int(cr.Crawler.Delay)) + int(cr.Crawler.Delay))
logger.Debugf("Sleeping %d sec", sleepTime)
time.Sleep(sleepTime * time.Second)
}
} else {
logger.Fatal(err)
}
}
func (cr *Observer) getTorrentMeta(context string) (map[string]string, error) {
var err error
var meta map[string]string
if cr.Crawler.MetaExtractor != nil {
var rawMeta map[string][]byte
if rawMeta, err = cr.Crawler.MetaExtractor.ExtractData(cr.Crawler.BaseURL, context); err == nil && len(rawMeta) > 0 {
meta = make(map[string]string, len(rawMeta))
for k, v := range rawMeta {
if !isEmpty(k) {
meta[k] = strings.TrimSpace(html.UnescapeString(string(v)))
}
}
}
}
return meta, err
}
func (cr *Observer) cmdCheckTorrent(chat int64, _, args string) error {
var err error
var isAdmin bool
if isAdmin, err = cr.DB.GetAdminExist(chat); err == nil {
if isAdmin {
var offset uint64
if offset, err = strconv.ParseUint(args, 10, 64); err == nil {
if torrent := cr.checkTorrent(uint(offset), true); torrent != nil {
go cr.uploadTorrents([]*Torrent{torrent})
} else {
err = errors.New("<nil>")
}
}
} else {
logger.Infof("ForceUpload unauthorized %d", chat)
cr.Telegram.Client.SendMsg(cr.Telegram.Messages.Unauthorized, []int64{chat}, false)
}
}
return err
}
func (cr *Observer) checkTorrent(offset uint, force bool) *Torrent {
var err error
var torrent *Torrent
logger.Debug("Checking offset ", offset)
fullContext := fmt.Sprintf(cr.Crawler.ContextURL, offset)
if torrent, err = GetTorrent(cr.Crawler.BaseURL+fullContext, cr.Crawler.ReloadDelay); err == nil {
if torrent != nil {
logger.Info("New file", torrent.Info.Name)
size := torrent.FullSize()
logger.Info("New torrent size", size)
if size > 0 {
var pushTorrent bool
if force {
pushTorrent = true
} else {
if id, err := cr.DB.GetTorrent(torrent.Info.Name); err == nil {
if id == TorrentInvalidId {
pushTorrent = !cr.ignorePattern.MatchString(torrent.Info.Name)
} else {
pushTorrent = true
}
} else {
logger.Error(err)
}
}
if pushTorrent {
files := torrent.Files()
logger.Debug("Adding torrent", torrent.Info.Name)
logger.Debug("Files: ", files)
var id int64
if id, err = cr.DB.AddTorrent(torrent.Info.Name, offset, files); err == nil {
var newMeta, existMeta map[string]string
if newMeta, err = cr.getTorrentMeta(fullContext); err != nil {
logger.Error(err)
}
if existMeta, err = cr.DB.GetTorrentMeta(id); err != nil {
logger.Error(err)
}
if len(newMeta) > 0 && len(newMeta) >= len(existMeta) {
logger.Debug("Writing newMeta: ", newMeta)
err = cr.DB.AddTorrentMeta(id, newMeta)
}
}
if err != nil {
logger.Error(err)
}
} else {
logger.Infof("Torrent %s ignored", torrent.Info.Name)
}
} else {
logger.Error("Zero torrent size, offset", offset)
}
} else {
logger.Debugf("%s not a torrent", fullContext)
}
}
return torrent
}
func (cr *Observer) uploadTorrents(newTorrents []*Torrent) {
if cr.Transmission.Client != nil {
if existingTorrents, err := cr.Transmission.Client.TorrentGet([]string{"id", "name"}, nil); err == nil {
if existingTorrents != nil {
torrentsToRm := make([]int64, 0, len(newTorrents))
for _, existingTorrent := range existingTorrents {
if existingTorrent != nil && existingTorrent.Name != nil &&
existingTorrent.ID != nil {
for _, newTorrent := range newTorrents {
if *(existingTorrent.Name) == newTorrent.Info.Name {
logger.Debug("Torrent marked as toDelete", *(existingTorrent.Name))
torrentsToRm = append(torrentsToRm, *existingTorrent.ID)
}
}
}
}
if len(torrentsToRm) > 0 {
if err := cr.Transmission.Client.TorrentRemove(&tr.TorrentRemovePayload{
IDs: torrentsToRm,
DeleteLocalData: false,
}); err == nil {
logger.Debug("Torrents deleted", torrentsToRm)
} else {
logger.Error(err)
}
}
}
} else {
logger.Error(err)
}
falsePtr := new(bool)
addedTorrents := make([]int64, 0, len(newTorrents))
for _, newTorrent := range newTorrents {
b64 := base64.StdEncoding.EncodeToString(newTorrent.RawData)
if addedTorrent, err := cr.Transmission.Client.TorrentAdd(&tr.TorrentAddPayload{
DownloadDir: &cr.Transmission.Path,
MetaInfo: &b64,
Paused: falsePtr,
}); err == nil {
if addedTorrent != nil {
addedTorrents = append(addedTorrents, *addedTorrent.ID)
logger.Debug("Added torrent", *(addedTorrent.Name))
} else {
logger.Warning("AddTorrent undefined result", newTorrent.Info.Name)
}
} else {
logger.Error(err)
}
}
if len(addedTorrents) > 0 && len(cr.Transmission.Trackers) > 0 {
trackers := tr.TorrentSetPayload{
IDs: addedTorrents,
TrackerAdd: cr.Transmission.Trackers,
}
if err := cr.Transmission.Client.TorrentSet(&trackers); err != nil {
logger.Warning("Unable to append trackers ", err)
}
}
} else {
logger.Warning("Transmission client not inited")
}
}
func (cr *Observer) checkVideo() {
var err error
var session KSessionInfo
if session, err = cr.Kaltura.GetSession(); err != nil {
logger.Error(err)
err = cr.InitKaltura()
} else {
logger.Debug("Logged as", session.UserID)
}
if err == nil {
var files []TorrentFile
if files, err = cr.DB.GetTorrentFilesNotReady(); err == nil && files != nil {
for _, file := range files {
if !isEmpty(file.Name) {
if file.Status == FilePendingStatus {
var err error
var stat os.FileInfo
fullPath := filepath.Join(cr.Kaltura.WatchPath, file.Name)
fullPath = filepath.FromSlash(fullPath)
stat, err = os.Stat(fullPath)
if err != nil {
switch osErr := err.(type) {
case *os.PathError:
switch osErr.Err {
case syscall.EINTR:
stat, err = os.Stat(fullPath)
case syscall.ENOENT:
continue
default:
logger.Error(err)
}
}
}
if err == nil {
if stat == nil {
logger.Warning("Unable to stat file", fullPath)
} else {
var admins []int64
if admins, err = cr.DB.GetAdmins(); err == nil {
fName := stat.Name()
logger.Debugf("Found ready file %s, size: %d", fName, stat.Size())
var entryId string
entryName, entryTags := cr.prepareKOptions(file)
if entryId, err = cr.Kaltura.CreateMediaEntry(fullPath, entryName, entryTags);
err == nil && !isEmpty(entryId) {
logger.Debug("Updating file entry id", entryId)
if err = cr.DB.SetTorrentFileEntryId(file.Id, entryId); err == nil {
logger.Debug("Uploading file", fName)
if err = cr.Kaltura.UploadMediaContent(fullPath, entryId); err == nil {
var msg string
if msg, err = formatMessage(cr.Telegram.Messages.kuploadTmpl,
map[string]interface{}{
pName: filepath.Base(file.Name),
pId: entryId,
pIndex: file.Id,
}); err != nil {
msg = err.Error()
}
cr.Telegram.Client.SendMsg(msg, admins, true)
if cr.Telegram.Video.Upload {
file.Status = FileReadyStatus
} else {
file.Status = FileConvertingStatus
}
err = cr.switchFileReadyStatus(file, admins)
}
}
}
if err != nil {
logger.Error(err)
cr.Telegram.Client.SendMsg(fmt.Sprint(cr.Telegram.Messages.Error, err,
" entry id ", entryId,
" file ", file.String()),
admins, false)
err = cr.DB.SetTorrentFileStatus(file.Id, FileErrorStatus)
}
}
}
}
if err != nil {
logger.Error(err)
}
} else if file.Status == FileConvertingStatus {
var err error
if isEmpty(file.EntryId) {
err = errors.New("entry id not set for file " + file.String())
} else {
var entry KMediaEntry
if entry, err = cr.Kaltura.GetMediaEntry(file.EntryId); err == nil {
if entry.Status == KEntryStatusReady {
if cr.checkUploadFile(file) {
if err = cr.DB.SetTorrentFileStatus(file.Id, FileReadyStatus); err == nil {
var flavors KFlavorAssetSearchResult
if flavors, err = cr.Kaltura.GetMediaEntryFlavorAssets(file.EntryId); err == nil {
if len(flavors.Objects) == 0 {
err = errors.New("flavors for entry " + file.EntryId + " not found")
} else {
cr.sendTelegramVideo(file, entry, flavors.Objects[0])
}
}
}
}
}
}
}
if err != nil {
logger.Error(err)
}
}
}
}
}
}
if err != nil {
logger.Error(err)
}
}
func (cr *Observer) prepareKOptions(torrentFile TorrentFile) (string, []string) {
var name string
var err error
tags := make([]string, 0, len(cr.Kaltura.Tags))
if len(cr.Kaltura.Tags) > 0 {
var meta map[string]string
if meta, err = cr.DB.GetTorrentMeta(torrentFile.Torrent); err == nil {
if len(meta) > 0 {
for tag, multival := range cr.Kaltura.Tags {
m := meta[tag]
if !isEmpty(m) {
if multival {
for _, e := range strings.Split(m, ",") {
e = strings.TrimSpace(e)
if !isEmpty(e) {
e = nonLetterNumberSpaceRegexp.ReplaceAllString(e, "")
tags = append(tags, allSpacesRegexp.ReplaceAllString(e, "_"))
}
}
} else {
m = strings.TrimSpace(m)
m = nonLetterNumberSpaceRegexp.ReplaceAllString(m, "")
tags = append(tags, allSpacesRegexp.ReplaceAllString(m, "_"))
}
}
}
if cr.Kaltura.entryNameTmpl != nil {
data := map[string]interface{}{
pMeta: meta,
pId: torrentFile.Id,
pName: torrentFile.Name,
}
var index int64
if index, err = cr.DB.GetTorrentFileIndex(torrentFile.Torrent, torrentFile.Id); err == nil {
data[pIndex] = index
}
name, err = formatMessage(cr.Kaltura.entryNameTmpl, data)
}
}
}
}
if err != nil {
logger.Error(err)
}
return name, tags
}
func (cr *Observer) cmdSwitchFileReadyStatus(chat int64, _, args string) error {
var err error
var id int64
var isAdmin bool
if isAdmin, err = cr.DB.GetAdminExist(chat); err == nil {
if isAdmin {
if id, err = strconv.ParseInt(args, 10, 64); err == nil {
var file TorrentFile
if file, err = cr.DB.GetTorrentFile(id); err == nil {
if isEmpty(file.Name) {
err = errors.New("no such entry")
} else {
err = cr.switchFileReadyStatus(file, []int64{chat})
}
}
}
} else {
logger.Infof("SwitchFileReadyStatus unauthorized %d", chat)
cr.Telegram.Client.SendMsg(cr.Telegram.Messages.Unauthorized, []int64{chat}, false)
}
}
return err
}
func (cr *Observer) switchFileReadyStatus(file TorrentFile, chats []int64) error {
var err error
var newFileStatus uint8
var ignoreMsg *tmpl.Template
if file.Status == FileConvertingStatus {
ignoreMsg = cr.Telegram.Messages.videoIgnoredTmpl
newFileStatus = FileReadyStatus
} else {
ignoreMsg = cr.Telegram.Messages.videoForcedTmpl
newFileStatus = FileConvertingStatus
}
if err = cr.DB.SetTorrentFileStatus(file.Id, newFileStatus); err == nil {
var msg string
if msg, err = formatMessage(ignoreMsg,
map[string]interface{}{
pName: filepath.Base(file.Name),
pIgnore: tCmdSwitchIgnore + "_" + strconv.FormatInt(file.Id, 10),
}); err != nil {
msg = err.Error()
}
cr.Telegram.Client.SendMsg(msg, chats, true)
}
return err
}
func (cr *Observer) sendTelegramVideo(file TorrentFile, entry KMediaEntry, flavor KFlavorAsset) {
var err error
var meta map[string]string
if meta, err = cr.DB.GetTorrentMeta(file.Torrent); err == nil {
var chats []int64
if chats, err = cr.DB.GetChats(); err == nil {
var index int64
var msg string
if index, err = cr.DB.GetTorrentFileIndex(file.Torrent, file.Id); err != nil {
logger.Error(err)
}
if msg, err = formatMessage(cr.Telegram.Messages.tuploadTmpl, map[string]interface{}{
pMeta: meta,
pVideoUrl: entry.DownloadURL,
pIndex: index,
pTags: formatHashTags(entry.Tags),
}); err != nil {
msg = err.Error()
}
var tmpVideoFileName string
if tmpVideoFileName, err = downloadToDirectory(cr.Telegram.Video.TempPath, entry.DownloadURL, flavor.FileExt);
err == nil {
defer os.Remove(tmpVideoFileName)
var thumb *tg.MediaParams
if tmpThumbFileName, err := downloadToDirectory(cr.Telegram.Video.TempPath,
FormatThumbnailURL(entry.ThumbnailUrl, flavor.Width, flavor.Height), "jpg");
err == nil {
defer os.Remove(tmpThumbFileName)
thumb = &tg.MediaParams{
Path: tmpThumbFileName,
Width: int32(flavor.Width),
Height: int32(flavor.Width),
Streaming: false,
}
} else {
logger.Error(err)
}
video := tg.MediaParams{
Path: tmpVideoFileName,
Width: int32(flavor.Width),
Height: int32(flavor.Height),
Streaming: true,
Thumbnail: thumb,
}
cr.Telegram.Client.SendVideo(video, msg, chats, true)
}
}
}
if err != nil {
logger.Error(err)
}
}
func (cr *Observer) checkUploadFile(file TorrentFile) bool {
doUpload := true
if cr.Telegram.Video.SequentialUpload {
var err error
var allTorrentFiles []TorrentFile
if allTorrentFiles, err = cr.DB.GetTorrentFiles(file.Torrent); err == nil {
var currentFileIndex int64 = TorrentInvalidId
for _, tf := range allTorrentFiles {
if tf.Id == file.Id {
currentFileIndex = tf.Index
break
}
}
for i, allFilesLen := 0, len(allTorrentFiles);
doUpload && i < allFilesLen && allTorrentFiles[i].Index < currentFileIndex;
i++{
if allTorrentFiles[i].Status != FileReadyStatus {
doUpload = false
}
}
}
if err != nil {
logger.Error(err)
}
}
return doUpload
}