main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "sync"
  10. "text/template"
  11. )
  12. // A Mediathek is an implementation of a mediathek-rtmp extractor
  13. // taken from https://code.google.com/p/go/source/browse/src/cmd/go/main.go
  14. type Mediathek struct {
  15. // Parse runs the parser
  16. // The args are the arguments after the site name.
  17. Parse func(cmd *Mediathek, url string)
  18. UrlRegexp *regexp.Regexp
  19. // UsageLine is the one-line usage message.
  20. // The first word in the line is taken to be the site name.
  21. UsageLine string
  22. // Short is the short description shown in the 'gema help' output.
  23. Short string
  24. // Long is the long message shown in the 'gema help <this-command>' output.
  25. Long string
  26. // Flag is a set of flags specific to this mediathek.
  27. Flag flag.FlagSet
  28. // CustomFlags indicates that the Mediathek will do its own
  29. // flag parsing.
  30. CustomFlags bool
  31. }
  32. // Name returns the mediatheks's name: the first word in the usage line.
  33. func (m *Mediathek) Name() string {
  34. name := m.UsageLine
  35. i := strings.Index(name, " ")
  36. if i >= 0 {
  37. name = name[:i]
  38. }
  39. return name
  40. }
  41. func (m *Mediathek) Usage() {
  42. fmt.Fprintf(os.Stderr, "usage: %s\n\n", m.UsageLine)
  43. fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(m.Long))
  44. os.Exit(2)
  45. }
  46. // list of available mediatheken
  47. // The order here is the order in which they are printed by 'go help'.
  48. var mediatheken = []*Mediathek{
  49. mediaArd,
  50. mediaZdf,
  51. mediaArtePlus7,
  52. mediaArteVideos,
  53. }
  54. var exitStatus = 0
  55. var exitMu sync.Mutex
  56. func setExitStatus(n int) {
  57. exitMu.Lock()
  58. if exitStatus < n {
  59. exitStatus = n
  60. }
  61. exitMu.Unlock()
  62. }
  63. func main() {
  64. flag.Usage = usage
  65. flag.Parse()
  66. // log.SetFlags(0)
  67. args := flag.Args()
  68. if len(args) < 1 {
  69. usage()
  70. }
  71. if args[0] == "help" {
  72. help(args[1:])
  73. return
  74. }
  75. for _, theken := range mediatheken {
  76. if theken.UrlRegexp.MatchString(args[0]) {
  77. theken.Flag.Usage = func() { theken.Usage() }
  78. theken.Parse(theken, args[0])
  79. exit()
  80. return
  81. }
  82. }
  83. fmt.Fprintf(os.Stderr, "gema: unkown media site %q\nRun 'gema help' for usage.\n", args[0])
  84. setExitStatus(2)
  85. exit()
  86. }
  87. var usageTemplate = `gib erstma alles (gema) is a helper for extracting rtmp urls from media sites.
  88. usually you run it like rtmpdump -r $(gema siteName url) -o filename.
  89. Usage:
  90. gema siteName [arguments]
  91. The sites are:
  92. {{range .}}
  93. {{.Name | printf "%-11s"}} {{.Short}}{{end}}
  94. Use "gema help siteName" to get more information.
  95. `
  96. var helpTemplate = `usage: gema {{.UsageLine}}
  97. {{.Long | trim}}
  98. `
  99. func tmpl(w io.Writer, text string, data interface{}) {
  100. t := template.New("top")
  101. t.Funcs(template.FuncMap{"trim": strings.TrimSpace})
  102. template.Must(t.Parse(text))
  103. if err := t.Execute(w, data); err != nil {
  104. panic(err)
  105. }
  106. }
  107. func printUsage(w io.Writer) {
  108. tmpl(w, usageTemplate, mediatheken)
  109. }
  110. func usage() {
  111. printUsage(os.Stderr)
  112. os.Exit(2)
  113. }
  114. // help implements the 'help' command.
  115. func help(args []string) {
  116. if len(args) == 0 {
  117. printUsage(os.Stdout)
  118. // not exit 2: succeeded at 'go help'.
  119. return
  120. }
  121. if len(args) != 1 {
  122. fmt.Fprintf(os.Stderr, "usage: gema help site\n\nToo many arguments given.\n")
  123. os.Exit(2) // failed at 'go help'
  124. }
  125. arg := args[0]
  126. for _, cmd := range mediatheken {
  127. if cmd.Name() == arg {
  128. tmpl(os.Stdout, helpTemplate, cmd)
  129. // not exit 2: succeeded at 'go help cmd'.
  130. return
  131. }
  132. }
  133. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'gema help'.\n", arg)
  134. os.Exit(2) // failed at 'go help cmd'
  135. }
  136. var atexitFuncs []func()
  137. func atexit(f func()) {
  138. atexitFuncs = append(atexitFuncs, f)
  139. }
  140. func exit() {
  141. for _, f := range atexitFuncs {
  142. f()
  143. }
  144. os.Exit(exitStatus)
  145. }