ard.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "fmt"
  4. "html"
  5. "io"
  6. "net/http"
  7. "regexp"
  8. "strings"
  9. htmpParser "golang.org/x/net/html"
  10. )
  11. var mediaArd = &Mediathek{
  12. Parse: ardParse,
  13. UrlRegexp: regexp.MustCompile(`http://mediathek.daserste.de/`),
  14. UsageLine: "ard url",
  15. Short: "helper for www.ardmediathek.de/das-erste...",
  16. Long: `Todo`,
  17. }
  18. var streamRegexp = regexp.MustCompile(`mediaCollection.addMediaStream\(0, 2, "(.*)", "(.*)",`)
  19. type playPathArgs struct {
  20. AppUrl, PlayPath string
  21. }
  22. // step 1 - get dynamic js code
  23. func ardFindPlayPath(url string) (string, error) {
  24. var foundTag = false
  25. plainHtmlResp, err := http.Get(url)
  26. if err != nil {
  27. return "", err
  28. }
  29. defer plainHtmlResp.Body.Close()
  30. d := htmpParser.NewTokenizer(plainHtmlResp.Body)
  31. for {
  32. // token type
  33. tokenType := d.Next()
  34. if tokenType == htmpParser.ErrorToken {
  35. if err := d.Err(); err == io.EOF {
  36. break
  37. } else {
  38. return "", fmt.Errorf("Error: Invalid HTML Token %s", err)
  39. }
  40. }
  41. token := d.Token()
  42. switch tokenType {
  43. case htmpParser.StartTagToken: // <tag>
  44. if strings.HasPrefix(token.String(), "<script type=\"text/javascript\"") {
  45. foundTag = true
  46. }
  47. case htmpParser.TextToken:
  48. if foundTag == true {
  49. body := html.UnescapeString(token.String())
  50. matches := streamRegexp.FindStringSubmatch(body)
  51. if len(matches) == 3 {
  52. return fmt.Sprintf("%s%s\n", matches[1], matches[2]), nil
  53. }
  54. foundTag = false
  55. }
  56. }
  57. }
  58. return "", fmt.Errorf("Error: Player JS not found")
  59. }
  60. func ardParse(media *Mediathek, url string) {
  61. pPath, err := ardFindPlayPath(url)
  62. if err != nil {
  63. fmt.Printf("Error during ardFindPlayerJs: %s\n", err)
  64. setExitStatus(1)
  65. exit()
  66. }
  67. fmt.Println(pPath)
  68. }