Skip to content

Instantly share code, notes, and snippets.

@theGeekPirate
Last active March 5, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theGeekPirate/e52e7c382afa11864c02b4cff4851ac8 to your computer and use it in GitHub Desktop.
Save theGeekPirate/e52e7c382afa11864c02b4cff4851ac8 to your computer and use it in GitHub Desktop.
5m Reddit Video Link Grabber Hack
package main
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
// https://www.reddit.com/r/aww/comments/75ihto/best_buds/
// FROM: https://v.redd.it/sif2cf6xb1rz/DASHPlaylist.mpd
// https://v.redd.it/sif2cf6xb1rz/DASH_2_4_M is the largest link
const (
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
)
type MPD struct {
Period Period `xml:"Period,omitempty"`
}
type Period struct {
AdaptationSet []AdaptationSet `xml:"AdaptationSet,omitempty"`
}
type AdaptationSet struct {
Representation []Representation `xml:"Representation,omitempty"`
}
type Representation struct {
BaseURL *BaseURL `xml:"BaseURL,omitempty"`
}
type BaseURL struct {
Text string `xml:",chardata"`
}
func main() {
args := os.Args[1]
doc, err := URLToDocument(args)
if err != nil {
log.Fatalln(errors.New("Unable to get document: " + err.Error()))
}
doc.Find("div.media-preview-content.video-player div.portrait").Each(func(i int, s *goquery.Selection) {
mpdURL, exists := s.Attr("data-mpd-url")
if !exists {
log.Println("The mpd link does not exist")
return
}
resp, err := http.Get(mpdURL)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
XML := new(MPD)
err = xml.Unmarshal(html, XML)
if err != nil {
log.Println(err)
return
}
fmt.Println(strings.TrimSuffix(mpdURL, "DASHPlaylist.mpd") + XML.Period.AdaptationSet[0].Representation[0].BaseURL.Text)
})
}
func URLToDocument(URL string) (*goquery.Document, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
return &goquery.Document{}, errors.New("Unable to send request to the URL: " + URL)
}
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return &goquery.Document{}, errors.New("Unable to receive a response from: " + URL)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return &goquery.Document{}, errors.New("Unable to turn URL into a Document for: " + URL)
}
return doc, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment