4 "github.com/valyala/fastjson"
12 var MissingData
= errors
.New("missing data")
13 var ServerError
= errors
.New("server error")
15 func ParseChannelVideoURLs(res
*http
.Response
) ([]string, error
) {
16 if res
.StatusCode
!= 200 {
17 return nil, fmt
.Errorf("HTTP error: %s", res
.Request
.URL
.String())
21 defer res
.Body
.Close()
22 buf
, err
:= ioutil
.ReadAll(res
.Body
)
23 if err
!= nil { return nil, err
}
27 rootObj
, err
:= p
.ParseBytes(buf
)
28 if err
!= nil { return nil, err
}
31 root
, err
:= rootObj
.Array()
32 if err
!= nil { return nil, err
}
34 // Find response container
35 var container
*fastjson
.Value
36 for _
, item
:= range root
{
37 if item
.Exists("response") {
42 if container
== nil { return nil, MissingData
}
45 errorExists
:= container
.Exists(
51 if errorExists
{ return nil, ServerError
}
53 // Get items from grid
54 itemsObj
:= container
.Get(
56 "continuationContents",
60 if itemsObj
== nil { return nil, MissingData
}
63 items
, err
:= itemsObj
.Array()
64 if err
!= nil { return nil, err
}
66 urls
:= make([]string, 0)
69 for _
, item
:= range items
{
78 if urlObj
== nil { return nil, MissingData
}
81 urlBytes
, err
:= urlObj
.StringBytes()
82 if err
!= nil { return nil, err
}
83 url
:= string(urlBytes
)
85 if strings
.HasPrefix(url
, "/watch?v") {
86 urls
= append(urls
, "https://www.youtube.com" + url
)