-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulli.go
141 lines (122 loc) · 3.05 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
package gulli
import (
"context"
"io"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/simulot/aspiratv/metadata/nfo"
"github.com/simulot/aspiratv/net/myhttp"
"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
deadline time.Duration
cartoonList []ShowEntry
tvshows map[string]*nfo.TVShow
keepBonuses bool
}
// 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() (*Gulli, error) {
p := &Gulli{
getter: myhttp.DefaultClient,
htmlParserFactory: nil,
seenShows: map[string]bool{},
deadline: 30 * time.Second,
tvshows: map[string]*nfo.TVShow{},
}
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
}
func (p *Gulli) Configure(c providers.Config) {
p.keepBonuses = c.KeepBonus
p.debug = c.Debug
if p.debug {
p.deadline = time.Hour
} else {
p.deadline = 30 * time.Second
}
}
// 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" }
// MediaList download the shows catalog from the web site.
func (p *Gulli) MediaList(ctx context.Context, mm []*providers.MatchRequest) chan *providers.Media {
shows := make(chan *providers.Media)
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, m, ID)
if err != nil {
log.Printf("[%s] Can't decode replay catalog: %q", p.Name(), err)
return
}
for _, s := range showTitles {
shows <- s
}
}
}
}
}()
return shows
}
// GetMediaDetails gather show information from dedicated web page.
func (p *Gulli) GetMediaDetails(ctx context.Context, m *providers.Media) error {
var err error
info := m.Metadata.GetMediaInfo()
title := strings.ToLower(info.Showtitle)
if info.TVShow == nil {
tvshow, ok := p.tvshows[title]
if !ok {
tvshow, err = p.getShowInfo(ctx, title)
if err != nil {
return err
}
}
info.TVShow = tvshow
return nil
}
return nil
}