zdf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. )
  9. var mediaZdf = &Mediathek{
  10. Parse: zdfParse,
  11. UrlRegexp: regexp.MustCompile(`http://[w\.]*zdf.de/ZDFmediathek/beitrag/video/([\d]+)/`),
  12. UsageLine: "zdf contentId",
  13. Short: "helper for www.zdf.de/ZDFmediathek/..",
  14. Long: `
  15. input: http://zdf.de/ZDFmediathek/beitrag/video/....
  16. `,
  17. }
  18. func zdfFindMetaUrl(url string) (string, error) {
  19. type zdfTeaserimage struct {
  20. Alt string `xml:"alt,attr"`
  21. Key string `xml:"key,attr"`
  22. Image string `xml:",innerxml"`
  23. }
  24. type zdfFormitaet struct {
  25. Type string `xml:"basetype,attr"`
  26. Quality string `xml:"quality"`
  27. Url string `xml:"url"`
  28. }
  29. type zdfResponse struct {
  30. Status string `xml:"status>statuscode"`
  31. Video struct {
  32. Type string `xml:"type"`
  33. Title string `xml:"information>title"`
  34. Context string `xml:"context>contextLink"`
  35. Formitaeten []zdfFormitaet `xml:"formitaeten>formitaet"`
  36. // Images []zdfTeaserimage `xml:"teaserimages>teaserimage"`
  37. } `xml:"video"`
  38. }
  39. httpResp, err := http.Get(url)
  40. if err != nil {
  41. return "", err
  42. }
  43. defer httpResp.Body.Close()
  44. xmlDecoder := xml.NewDecoder(httpResp.Body)
  45. var decodedResponse zdfResponse
  46. err = xmlDecoder.Decode(&decodedResponse)
  47. if err != nil {
  48. return "", err
  49. }
  50. // fmt.Printf("XML Result:%+v\n", decodedResponse)
  51. for _, v := range decodedResponse.Video.Formitaeten {
  52. if v.Type == "h264_aac_mp4_rtmp_zdfmeta_http" && v.Quality == "veryhigh" {
  53. return v.Url, nil
  54. }
  55. }
  56. return "", fmt.Errorf("Error: Meta XML-URL not found")
  57. }
  58. func zdfFindZdfRtmpUrl(url string) (string, error) {
  59. httpResp, err := http.Get(url)
  60. if err != nil {
  61. return "", err
  62. }
  63. defer httpResp.Body.Close()
  64. type zdfMetaResponse struct {
  65. StreamUrl string `xml:"default-stream-url"`
  66. }
  67. xmlDecoder := xml.NewDecoder(httpResp.Body)
  68. var decodedResponse zdfMetaResponse
  69. err = xmlDecoder.Decode(&decodedResponse)
  70. if err != nil {
  71. return "", err
  72. }
  73. // fmt.Printf("Meta Response:%+v\n", decodedResponse)
  74. if strings.HasPrefix(decodedResponse.StreamUrl, "rtmp://") {
  75. return decodedResponse.StreamUrl, nil
  76. }
  77. return "", fmt.Errorf("Error: RTMP-URL not found")
  78. }
  79. func zdfParse(media *Mediathek, url string) {
  80. idMatch := media.UrlRegexp.FindStringSubmatch(url)
  81. if len(idMatch) != 2 {
  82. fmt.Println("Not a valid ZDFmediathek url.")
  83. setExitStatus(1)
  84. exit()
  85. }
  86. // fmt.Printf("Id found:%s\n", idMatch[1])
  87. url = fmt.Sprintf("http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?id=%s", idMatch[1])
  88. metaUrl, err := zdfFindMetaUrl(url)
  89. if err != nil {
  90. fmt.Printf("Error during zdfFindMetaUrl: %s\n", err)
  91. setExitStatus(1)
  92. exit()
  93. }
  94. rtmpUrl, err := zdfFindZdfRtmpUrl(metaUrl)
  95. if err != nil {
  96. fmt.Printf("Error during zdfFindZdfRtmpUrl: %s\n", err)
  97. setExitStatus(1)
  98. exit()
  99. }
  100. fmt.Println(rtmpUrl)
  101. }