Skip to content

Instantly share code, notes, and snippets.

@border
Created March 11, 2012 15:53
Show Gist options
  • Save border/2016902 to your computer and use it in GitHub Desktop.
Save border/2016902 to your computer and use it in GitHub Desktop.
Youtube subtitle and download URL
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
func main() {
c := new(http.Client)
//resp, err := c.Get("http://www.youtube.com/watch?v=OOUD2j-l7BI&feature=g-u-u&context=G2c38d77FUAAAAAAACAA")
resp, err := c.Get("http://127.0.0.1/youtube.htm")
if err != nil {
fmt.Println("Error: ", err)
return
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
beginRegStr := `(?Usmi)yt.playerConfig.=.{.*"args":.{"ttsurl":."(.*)",.*"url_encoded_fmt_stream_map":."(.*)",.*}.*};`
beginRegex, _ := regexp.Compile(beginRegStr)
pos := beginRegex.FindAllSubmatch(buf, 1024)
if pos == nil {
fmt.Println("Nothing matched!")
return
}
subtitle := make([]byte, 1024)
urlsArray := make([]byte, 1024*20)
for i := 0; i < len(pos); i++ {
subtitle = pos[i][1]
urlsArray = pos[i][2]
}
fmt.Println("subtitle: ", string(subtitle))
urlStr := strings.Split(string(urlsArray), ",")
for i := 0; i < len(urlStr); i++ {
fmt.Println(i, ", ", urlStr[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment