[Golang] Get Vine Video src via goquery


Get Vine video mp4 source URL via goquery in Golang (Go programming language). My answer to Stack Overflow question: Getting blank return when using GoQuery to get video src

so25517885.go | repository | view raw
 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
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"))
}

Output:

https://mtc.cdn.vine.co/r/videos/67FAC9DFA21115619347885645824_22a564aec15.5.0.17428816123715427422.mp4?versionId=4zcm5ySoFhqUQBXU7Ehm3YOuOSjFbkg3

Tested on: Ubuntu Linux 15.10, Go 1.6.1.


References:

[1]go - Getting blank return when using GoQuery to get video src - Stack Overflow
[2]github.com/PuerkitoBio/goquery - GoDoc
[3]Posts containing 'goquery' - Stack Overflow
[4][Golang] Extract Title, Image, and URL via goquery