4 "github.com/valyala/fastjson"
5 "github.com/terorie/yt-mango/data"
11 var missingData
= errors
.New("missing data")
12 var unexpectedType
= errors
.New("unexpected type")
14 func ParseVideo(v
*data
.Video
, res
*http
.Response
) error
{
15 defer res
.Body
.Close()
18 body
, err
:= ioutil
.ReadAll(res
.Body
)
19 if err
!= nil { return err
}
23 root
, err
:= p
.ParseBytes(body
)
24 if err
!= nil { return err
}
26 rootArray
:= root
.GetArray()
27 if rootArray
== nil { return unexpectedType
}
29 var videoDetails
*fastjson
.Value
30 for _
, sub
:= range rootArray
{
31 videoDetails
= sub
.Get("page", "playerResponse", "videoDetails")
32 if videoDetails
!= nil { break }
35 keywords
:= videoDetails
.GetArray("keywords")
36 if keywords
== nil { return missingData
}
37 for _
, keywordValue
:= range keywords
{
38 keywordBytes
, _
:= keywordValue
.StringBytes()
39 if keywordBytes
== nil { continue }
41 keyword
:= string(keywordBytes
)
42 v
.Tags
= append(v
.Tags
, keyword
)
45 titleBytes
:= videoDetails
.GetStringBytes("title")
46 if titleBytes
!= nil { v
.Title
= string(titleBytes
) }