-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
193 lines (163 loc) · 4.61 KB
/
result.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
// Package youtube provides a few utilities around youtube clips.
package youtube
import (
"bytes"
"context"
"fmt"
"net/url"
"os/exec"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/frizinak/libym/scraper"
)
// Result represents a youtube.com search result, i.e.: a youtube clip.
type Result struct {
videoID string
title string
u *url.URL
}
// NewResult creates a new youtube result.
// ID is required and should not be empty for it to be a valid youtube clip.
// Title is an arbitrary string that will be used as the title,
// this can be fetched using Title(id string) or Result.UpdateTitle().
func NewResult(id, title string) *Result {
return &Result{videoID: id, title: title}
}
// ID returns a the clip id.
func (r *Result) ID() string { return r.videoID }
// Title returns the title associated with this Result.
func (r *Result) Title() string { return r.title }
// URL constructs the youtube url for this clip.
func (r *Result) URL() *url.URL {
if r.u != nil {
return r.u
}
u, err := Page(r.videoID)
if err != nil {
panic(err)
}
r.u = u
return u
}
// SetTitle updates the title.
func (r *Result) SetTitle(title string) { r.title = title }
// DownloadURL asks youtube-dl to create a (temporary) download / stream url
// of the clip's contents.
func (r *Result) DownloadURL() (*url.URL, error) {
cmd := exec.Command("youtube-dl", "-g", "-f", "bestaudio", r.URL().String())
buf := bytes.NewBuffer(nil)
bufe := bytes.NewBuffer(nil)
cmd.Stdout = buf
cmd.Stderr = bufe
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("%w: %s", err, strings.TrimSpace(bufe.String()))
}
return url.Parse(strings.TrimSpace(buf.String()))
}
// UpdateTitle uses Title to update the clips title using its id.
func (r *Result) UpdateTitle() error {
n, err := Title(r.ID())
if err != nil {
return fmt.Errorf("%s: %w", r.ID(), err)
}
if n == "" {
return fmt.Errorf("%s: received empty title", r.ID())
}
r.title = n
return nil
}
var schemeRE = regexp.MustCompile(`^(https?://)|^(//)?`)
// FromURL parses the given url to extract the id and create a youtube
// result. see NewResult.
func FromURL(u, title string) (*Result, error) {
r := &Result{title: title}
u = schemeRE.ReplaceAllString(u, "https://")
n, err := url.Parse(u)
if err != nil {
return nil, err
}
direct := false
switch n.Hostname() {
case "youtu.be":
direct = true
case "www.youtube.com", "m.youtube.com", "youtube.com":
default:
return nil, fmt.Errorf("'%s' seems to not be a youtube url", u)
}
p := strings.Split(n.Path, "/")
q := n.Query()
if len(p) > 1 && (p[1] == "embed" || p[1] == "v") {
if len(p) > 2 {
r.videoID = p[2]
return r, nil
}
}
if v := q.Get("v"); v != "" {
r.videoID = v
return r, nil
}
if direct {
if len(p) > 1 {
r.videoID = p[1]
return r, nil
}
}
return nil, fmt.Errorf("'%s' does not seem to be a youtube video url", u)
}
// Scraper is a wrapper around github.com/frizinak/libym/scraper to extract
// youtube Results.
type Scraper struct {
s *scraper.Scraper
cb *ScraperCallback
}
// NewScraper creates a new youtube url scraper with the given scraper.
// cb will be called with each match after a call to Scrape or
// ScrapeWithContext.
func NewScraper(s *scraper.Scraper, cb func(*Result)) *Scraper {
return &Scraper{s: s, cb: NewScraperCallback(cb)}
}
// Scrape calls ScrapeWithContext without context.
func (s *Scraper) Scrape(uri string) error {
return s.ScrapeWithContext(context.Background(), uri)
}
// Scrape start the scrape of the given url and can be canceled using ctx.
func (s *Scraper) ScrapeWithContext(ctx context.Context, uri string) error {
return s.s.ScrapeWithContext(ctx, uri, s.cb.Callback).Error()
}
// ScraperCallback is the actual url matcher for Scraper which you probably
// want to use.
type ScraperCallback struct {
re *regexp.Regexp
uniq map[string]struct{}
cb func(*Result)
}
// NewScraperCallback creates a new ScraperCallback.
func NewScraperCallback(cb func(*Result)) *ScraperCallback {
return &ScraperCallback{
cb: cb,
re: regexp.MustCompile(`(?i)https?://(?:m\.|www\.)?youtu[a-z0-9\-_\./]+`),
uniq: make(map[string]struct{}),
}
}
// Callback is the actual function that can be passed to a
// github.com/frizinak/libym/scraper.Scraper.
func (s *ScraperCallback) Callback(uri *url.URL, doc *goquery.Document, depth, item, total int) error {
html, err := doc.Html()
if err != nil {
return err
}
for _, u := range s.re.FindAllString(html, -1) {
result, err := FromURL(u, "")
if err != nil {
continue
}
id := result.ID()
if _, ok := s.uniq[id]; ok {
continue
}
s.uniq[id] = struct{}{}
s.cb(result)
}
return nil
}