arte-videos.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "regexp"
  8. "strings"
  9. "golang.org/x/net/html"
  10. )
  11. var mediaArteVideos = &Mediathek{
  12. Parse: arteVideosParse,
  13. UrlRegexp: regexp.MustCompile("http://www.videos.arte.tv/de/videos/"),
  14. UsageLine: "arteVideos url",
  15. Short: "helper for videos.arte.tv",
  16. Long: `
  17. input: http://videos.arte.tv/de/videos/...
  18. `,
  19. }
  20. func arteVideosFindPlayerXml(url string) (string, error) {
  21. httpMatcher := regexp.MustCompile("http://.*Xml.xml")
  22. plainHtmlResp, err := http.Get(url)
  23. if err != nil {
  24. return "", err
  25. }
  26. defer plainHtmlResp.Body.Close()
  27. d := html.NewTokenizer(plainHtmlResp.Body)
  28. var scriptFound bool
  29. for {
  30. // token type
  31. tokenType := d.Next()
  32. if tokenType == html.ErrorToken {
  33. if err := d.Err(); err == io.EOF {
  34. break
  35. } else {
  36. return "", fmt.Errorf("Error: Invalid HTML Token %s", err)
  37. }
  38. }
  39. token := d.Token()
  40. switch tokenType {
  41. case html.StartTagToken: // <tag>
  42. if strings.HasPrefix(token.String(), "<script") {
  43. scriptFound = true
  44. }
  45. case html.TextToken: // text between start and end tag
  46. if scriptFound == true {
  47. scriptLines := strings.Split(token.String(), "\n")
  48. for _, line := range scriptLines {
  49. if strings.HasPrefix(line, "vars_player.videorefFileUrl") {
  50. // log.Printf("video ref url:%s\n", line)
  51. matches := httpMatcher.FindStringSubmatch(line)
  52. if len(matches) == 1 {
  53. return matches[0], nil
  54. }
  55. }
  56. }
  57. }
  58. case html.EndTagToken: // </tag>
  59. if strings.HasPrefix(token.String(), "</script") {
  60. scriptFound = false
  61. }
  62. }
  63. }
  64. return "", fmt.Errorf("Error: asPlayerXml-URL not found")
  65. }
  66. func arteVideosFindStreamXml(url string) (string, error) {
  67. type Video struct {
  68. Lang string `xml:"lang,attr"`
  69. Ref string `xml:"ref,attr"`
  70. }
  71. type PlayerXml struct {
  72. // Videoref interface{} `xml:"id,attr"`
  73. Videos []Video `xml:"videos>video"`
  74. Subtitles []string `xml:"subtitles"`
  75. Url string `xml:"url"`
  76. }
  77. xmlResp, err := http.Get(url)
  78. if err != nil {
  79. return "", err
  80. }
  81. defer xmlResp.Body.Close()
  82. xmlDecoder := xml.NewDecoder(xmlResp.Body)
  83. var xmlResult PlayerXml
  84. err = xmlDecoder.Decode(&xmlResult)
  85. if err != nil {
  86. return "", err
  87. }
  88. // log.Printf("XML Result:%v\n", xmlResult)
  89. for _, v := range xmlResult.Videos {
  90. if v.Lang == "de" {
  91. return v.Ref, nil
  92. }
  93. }
  94. return "", fmt.Errorf("Error: Stream XML-URL not found")
  95. }
  96. func arteVideosFindStreamRtmp(url string) (string, error) {
  97. type Url struct {
  98. Quality string `xml:"quality,attr"`
  99. Address string `xml:",innerxml"`
  100. }
  101. type StreamXml struct {
  102. Name string `xml:"name"`
  103. Views int `xml:"numberOfViews"`
  104. Urls []Url `xml:"urls>url"`
  105. }
  106. rtmpXmlResp, err := http.Get(url)
  107. if err != nil {
  108. return "", err
  109. }
  110. defer rtmpXmlResp.Body.Close()
  111. xmlDecoder := xml.NewDecoder(rtmpXmlResp.Body)
  112. var xmlResult StreamXml
  113. err = xmlDecoder.Decode(&xmlResult)
  114. if err != nil {
  115. return "", err
  116. }
  117. // debug
  118. // log.Printf("XML RTMP Result:%v\n", xmlResult)
  119. for _, v := range xmlResult.Urls {
  120. if v.Quality == "hd" {
  121. return v.Address, nil
  122. }
  123. }
  124. return "", fmt.Errorf("Error: Stream XML-URL not found")
  125. }
  126. func arteVideosParse(media *Mediathek, url string) {
  127. xmlUrl, err := arteVideosFindPlayerXml(url)
  128. if err != nil {
  129. fmt.Printf("Error during arteVideosFindPlayerXml: %s\n", err)
  130. setExitStatus(1)
  131. exit()
  132. }
  133. // verbose
  134. // log.Printf("PlayerXML URL:%s\n", xmlUrl)
  135. streamXmlUrl, err := arteVideosFindStreamXml(xmlUrl)
  136. if err != nil {
  137. fmt.Printf("Error during arteVideosFindStreamXml: %s\n", err)
  138. setExitStatus(1)
  139. exit()
  140. }
  141. // verbose
  142. // log.Printf("StreamXML URL:%s\n", streamXmlUrl)
  143. rtmpUrl, err := arteVideosFindStreamRtmp(streamXmlUrl)
  144. if err != nil {
  145. fmt.Printf("Error during arteVideosFindStreamRtmp: %s\n", err)
  146. setExitStatus(1)
  147. exit()
  148. }
  149. // verbose
  150. // log.Printf("Rtmp URL:%s\n", rtmpUrl)
  151. fmt.Println(rtmpUrl)
  152. }