-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulli.go
143 lines (124 loc) · 3.73 KB
/
gulli.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
package gulli
import (
"context"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
tvhttp "github.com/simulot/aspiratv/net/http"
"github.com/simulot/aspiratv/parsers/htmlparser"
"github.com/simulot/aspiratv/providers"
)
type getter interface {
Get(ctx context.Context, uri string) (io.ReadCloser, error)
}
// Gulli provider gives access to Gulli catchup tv
type Gulli struct {
getter getter
htmlParserFactory *htmlparser.Factory
seenShows map[string]bool
debug bool
cacheFile string
}
// init registers Gulli provider
func init() {
p, err := New()
if err != nil {
panic(err)
}
providers.Register(p)
}
// New creates a Gulli provider with given configuration
func New(conf ...func(p *Gulli)) (*Gulli, error) {
p := &Gulli{
getter: tvhttp.DefaultClient,
htmlParserFactory: nil,
seenShows: map[string]bool{},
}
for _, f := range conf {
f(p)
}
if rt, ok := p.getter.(http.RoundTripper); ok {
p.htmlParserFactory = htmlparser.NewFactory(htmlparser.SetTransport(rt))
} else {
p.htmlParserFactory = htmlparser.NewFactory()
}
cacheDir, err := os.UserCacheDir()
if err != nil {
return nil, err
}
p.cacheFile = path.Join(cacheDir, "aspiratv", "gulli-catalog.json")
return p, nil
}
// DebugMode set debug mode
func (p *Gulli) DebugMode(b bool) {
p.debug = b
}
// withGetter set a getter for Gulli
func withGetter(g getter) func(p *Gulli) {
return func(p *Gulli) {
p.getter = g
}
}
// Name return the name of the provider
func (p Gulli) Name() string { return "gulli" }
// Shows download the shows catalog from the web site.
func (p *Gulli) Shows(ctx context.Context, mm []*providers.MatchRequest) chan *providers.Show {
shows := make(chan *providers.Show)
go func() {
defer close(shows)
cat, err := p.downloadCatalog(ctx)
if err != nil {
log.Printf("[%s] Can't call replay catalog: %q", p.Name(), err)
return
}
for _, s := range cat {
for _, m := range mm {
if strings.Contains(strings.ToLower(s.Title), m.Show) {
ID, err := p.getFirstEpisodeID(ctx, s)
showTitles, err := p.getPlayer(ctx, ID)
if err != nil {
log.Printf("[%s] Can't decode replay catalog: %q", p.Name(), err)
return
}
for _, t := range showTitles {
t.Destination = m.Destination
shows <- t
}
}
}
}
}()
return shows
}
// GetShowStreamURL return the show's URL, a mp4 file
func (p *Gulli) GetShowStreamURL(ctx context.Context, s *providers.Show) (string, error) {
return s.StreamURL, nil
}
// GetShowInfo gather show information from dedicated web page.
// It load the html page of the show to extract availability date used as airdate and production year as season
func (p *Gulli) GetShowInfo(ctx context.Context, s *providers.Show) error {
return nil
}
// GetShowFileName return a file name with a path that is compatible with PLEX server:
// ShowName/Season NN/ShowName - sNNeMM - Episode title
// Show and Episode names are sanitized to avoid problem when saving on the file system
func (p *Gulli) GetShowFileName(ctx context.Context, s *providers.Show) string {
return filepath.Join(
providers.PathNameCleaner(s.Show),
"Season "+providers.Format2Digits(s.Season),
providers.FileNameCleaner(s.Show)+" - s"+providers.Format2Digits(s.Season)+"e"+providers.Format2Digits(s.Episode)+" - "+providers.FileNameCleaner(s.Title)+".mp4",
)
}
// GetShowFileNameMatcher return a file pattern of this show
// used for detecting already got episode even when episode or season is different
func (Gulli) GetShowFileNameMatcher(ctx context.Context, s *providers.Show) string {
return filepath.Join(
providers.PathNameCleaner(s.Show),
"*",
providers.FileNameCleaner(s.Show)+" - * - "+providers.FileNameCleaner(s.Title)+".mp4",
)
}