This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
playlist.go
186 lines (176 loc) · 5.89 KB
/
playlist.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
package yt
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"github.com/terorie/ytpriv/types"
"github.com/valyala/fasthttp"
"github.com/valyala/fastjson"
)
// RequestPlaylistStart fetches the first page of a playlist.
func (c *Client) RequestPlaylistStart(id string) PlaylistStartRequest {
const playlistURL = "https://www.youtube.com/playlist?pbj=1&list="
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodGet)
req.SetRequestURI(playlistURL + url.QueryEscape(id))
setHeaders(&req.Header)
return PlaylistStartRequest{c, req}
}
type PlaylistStartRequest struct {
*Client
*fasthttp.Request
}
func (r PlaylistStartRequest) Do() (*types.Playlist, error) {
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
if err := r.Client.HTTP.Do(r.Request, res); err != nil {
return nil, err
}
return ParsePlaylist(res)
}
func ParsePlaylist(res *fasthttp.Response) (*types.Playlist, error) {
l := new(types.Playlist)
// TODO Dedupe
if res.StatusCode() != fasthttp.StatusOK {
return nil, fmt.Errorf("response status %d", res.StatusCode())
}
contentType := res.Header.ContentType()
switch {
case bytes.HasPrefix(contentType, []byte("application/json")):
break
case bytes.HasPrefix(contentType, []byte("text/html")):
return nil, ErrRateLimit
}
// Parse JSON
var p fastjson.Parser
root, err := p.ParseBytes(res.Body())
if err != nil {
return nil, err
}
rootArray := root.GetArray()
if rootArray == nil {
return nil, unexpectedType
}
// Get interesting objects
var response *fastjson.Value
for _, sub := range rootArray {
if r := sub.Get("response"); r != nil {
response = r
break
}
}
if response == nil {
return nil, errors.New("no response")
}
sidebar := response.Get("sidebar", "playlistSidebarRenderer",
"items", "0", "playlistSidebarPrimaryInfoRenderer")
l.Views, _ = strconv.ParseInt(string(sidebar.GetStringBytes("stats", "1", "simpleText")), 10, 64)
l.Title = string(sidebar.GetStringBytes("title", "runs", "0", "text"))
playlist := response.Get("contents", "twoColumnBrowseResultsRenderer",
"tabs", "0", "tabRenderer", "content", "sectionListRenderer",
"contents", "0", "itemSectionRenderer", "contents", "0", "playlistVideoListRenderer")
if playlist == nil {
return nil, errors.New("playlist not found")
}
contents := playlist.GetArray("contents")
// TODO Check for unavailable videos
for _, wrapper := range contents {
renderer := wrapper.Get("playlistVideoRenderer")
channel := renderer.Get("shortBylineText", "runs", "0")
firstThumbnail := string(renderer.GetStringBytes("thumbnail", "thumbnails", "0", "url"))
unavailable := firstThumbnail == "https://i.ytimg.com/img/no_thumbnail.jpg"
video := types.VideoItem{
ID: string(renderer.GetStringBytes("videoId")),
Title: string(renderer.GetStringBytes("title", "runs", "0", "text")),
ChannelID: string(channel.GetStringBytes("navigationEndpoint", "browseEndpoint", "browseId")),
ChannelName: string(channel.GetStringBytes("text")),
Unavailable: unavailable,
}
if video.ID != "" {
l.Page.Videos = append(l.Page.Videos, video)
}
if l.Page.Continuation == "" {
l.Page.Continuation = string(wrapper.GetStringBytes("continuationItemRenderer", "continuationEndpoint", "continuationCommand", "token"))
}
}
return l, nil
}
// RequestPlaylistPage fetches a page of a playlist.
func (c *Client) RequestPlaylistPage(id string) PlaylistPageRequest {
const uri = "https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
req := fasthttp.AcquireRequest()
req.Header.SetMethod(fasthttp.MethodPost)
req.Header.SetContentType("application/json")
setHeaders(&req.Header)
req.SetRequestURI(uri)
var params = ytiRequest{
Continuation: id,
Context: &defaultYTIContext,
}
reqBody, err := json.Marshal(¶ms)
if err != nil {
panic(err)
}
req.SetBody(reqBody)
return PlaylistPageRequest{c, req}
}
type PlaylistPageRequest struct {
*Client
*fasthttp.Request
}
func (r PlaylistPageRequest) Do() (*types.PlaylistPage, error) {
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
if err := r.Client.HTTP.Do(r.Request, res); err != nil {
return nil, err
}
return ParsePlaylistPage(res)
}
func ParsePlaylistPage(res *fasthttp.Response) (*types.PlaylistPage, error) {
if res.StatusCode() != fasthttp.StatusOK {
return nil, fmt.Errorf("response status %d", res.StatusCode())
}
contentType := res.Header.ContentType()
switch {
case bytes.HasPrefix(contentType, []byte("application/json")):
break
case bytes.HasPrefix(contentType, []byte("text/html")):
return nil, ErrRateLimit
}
var p fastjson.Parser
root, err := p.ParseBytes(res.Body())
if err != nil {
return nil, err
}
items := root.GetArray("onResponseReceivedActions", "0", "appendContinuationItemsAction", "continuationItems")
var videos []types.VideoItem
var continuation string
for _, item := range items {
renderer := item.Get("playlistVideoRenderer")
videoID := string(renderer.GetStringBytes("videoId"))
firstThumbnail := string(renderer.GetStringBytes("thumbnail", "thumbnails", "0", "url"))
unavailable := firstThumbnail == "https://i.ytimg.com/img/no_thumbnail.jpg"
title := string(renderer.GetStringBytes("title", "runs", "0", "text"))
channelID := string(renderer.GetStringBytes("shortBylineText", "runs", "0", "navigationEndpoint", "browseEndpoint", "browseId"))
channelName := string(renderer.GetStringBytes("shortBylineText", "runs", "0", "text"))
if videoID != "" {
videos = append(videos, types.VideoItem{
ID: videoID,
Title: title,
ChannelID: channelID,
ChannelName: channelName,
Unavailable: unavailable,
})
}
if continuation == "" {
continuation = string(item.GetStringBytes("continuationItemRenderer", "continuationEndpoint", "continuationCommand", "token"))
}
}
return &types.PlaylistPage{
Continuation: continuation,
Videos: videos,
}, nil
}