Vine 将视频的元数据以 JSON 格式嵌入到<script type="application/ld+json">
. 所以你需要从标签中提取 JSON blob 并解码 JSON 以获得视频的 src。
以下是获取 Vine 视频的 src URL 的完整工作代码:
package main
import (
"encoding/json"
"github.com/PuerkitoBio/goquery"
)
type SharedContent struct {
ContentUrl string `json:"contentUrl"`
}
type VineVideoMetadata struct {
SC SharedContent `json:"sharedContent"`
}
func DecodeVineJsonBlob(blob string) VineVideoMetadata {
meta := VineVideoMetadata{}
err := json.Unmarshal([]byte(blob), &meta)
if err != nil {
panic(err)
}
return meta
}
func GetVineVideoJsonBlob(url string) string {
doc, err := goquery.NewDocument(url)
if err != nil {
panic(err)
}
return doc.Find("script[type=\"application/ld+json\"]").Text()
}
func GetVineVideoSrc(url string) string {
jsonBlob := GetVineVideoJsonBlob(url)
meta := DecodeVineJsonBlob(jsonBlob)
return meta.SC.ContentUrl
}
func main() {
println(GetVineVideoSrc("https://vine.co/v/MlWtKgwh7WY"))
}
输出:
https://mtc.cdn.vine.co/r/videos/67FAC9DFA21115619347885645824_22a564aec15.5.0.17428816123715427422.mp4?versionId=4zcm5ySoFhqUQBXU7Ehm3YOuOSjFbkg3