poster.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/beanzilla/glom"
  12. "github.com/beego/x2j"
  13. )
  14. // https://zetcode.com/golang/getpostrequest/ post_req_form.go
  15. func Post(where string) *http.Response {
  16. resp, err := http.PostForm(where, nil)
  17. if err != nil {
  18. log.Printf("POST got non 200 code, %v", err)
  19. }
  20. return resp
  21. }
  22. func ListenToRequests(ln *net.UDPConn, data *string) {
  23. done := false
  24. for !done {
  25. buffer := make([]byte, 1024) // 1k
  26. ln.SetReadDeadline(time.Now().Add(3 * time.Second))
  27. for !done {
  28. b, err := ln.Read(buffer)
  29. if b > 0 {
  30. str := string(buffer[0:])
  31. *data += str
  32. }
  33. if err != nil {
  34. if _, ok := err.(net.Error); !ok {
  35. log.Print(err)
  36. done = true
  37. }
  38. }
  39. }
  40. }
  41. }
  42. func PostFind() error {
  43. file, err := os.ReadFile("finder.txt")
  44. if err != nil {
  45. return err
  46. }
  47. addr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
  48. if err != nil {
  49. return err
  50. }
  51. conn, err := net.DialUDP("udp", nil, addr)
  52. if err != nil {
  53. return err
  54. }
  55. defer conn.Close()
  56. conn.Write(file)
  57. return nil
  58. }
  59. func GetFind() []string {
  60. var data string
  61. addr, err := net.ResolveUDPAddr("udp", ":1900")
  62. if err != nil {
  63. log.Printf("GET Find failed getting port 1900, %v", err)
  64. return nil
  65. }
  66. ln, err := net.ListenUDP("udp", addr)
  67. if err != nil {
  68. log.Printf("GET Find started listener for 1900, %v", err)
  69. return nil
  70. }
  71. go ListenToRequests(ln, &data)
  72. err = PostFind()
  73. if err != nil {
  74. log.Printf("POST Find returned %v", err)
  75. }
  76. var list []string
  77. data_list := strings.Split(data, "\n")
  78. for _, line := range data_list {
  79. if strings.Contains(line, "Location") {
  80. str := strings.ReplaceAll(strings.ToLower(line), "location: ", "")
  81. str = strings.ReplaceAll(str, "http://", "")
  82. str = strings.ReplaceAll(str, ":8060/", "")
  83. // Location: http://192.168.1.134:8060/
  84. list = append(list, str)
  85. }
  86. }
  87. return list
  88. }
  89. func Get(where string) *http.Response {
  90. resp, err := http.Get(where)
  91. if err != nil {
  92. log.Printf("GET got non 200 code, %v", err)
  93. return nil
  94. } else {
  95. return resp
  96. }
  97. }
  98. // https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#keypress-key-values codes
  99. func KeyPress(ip, command string) string {
  100. return fmt.Sprintf("http://%s:8060/keypress/%s", ip, command)
  101. }
  102. // https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#general-ecp-commands
  103. func Query(ip, what string) string {
  104. return fmt.Sprintf("http://%s:8060/query/%s", ip, what)
  105. }
  106. func PerformKey(ip, command string) {
  107. r := Post(KeyPress(ip, command))
  108. defer r.Body.Close()
  109. }
  110. func GetCurrentPlay(ip string) map[string]interface{} {
  111. resp := Get(Query(ip, "media-player"))
  112. if resp == nil {
  113. return nil
  114. }
  115. body, err := io.ReadAll(resp.Body)
  116. if err != nil {
  117. log.Printf("Reading from body got error, %v", err)
  118. }
  119. r := make(map[string]interface{})
  120. err = x2j.Unmarshal(body, &r)
  121. if err != nil {
  122. log.Printf("Got a error parsing XML, %v", err)
  123. }
  124. defer resp.Body.Close()
  125. return r
  126. }
  127. func GetQuery(ip, command string) map[string]interface{} {
  128. resp := Get(Query(ip, command))
  129. if resp == nil {
  130. return nil
  131. }
  132. body, err := io.ReadAll(resp.Body)
  133. if err != nil {
  134. log.Printf("Reading from body got error, %v", err)
  135. }
  136. r := make(map[string]interface{})
  137. err = x2j.Unmarshal(body, &r)
  138. if err != nil {
  139. log.Printf("Got a error parsing XML, %v", err)
  140. }
  141. defer resp.Body.Close()
  142. return r
  143. }
  144. func GetDevices() []string {
  145. str := GetFind()
  146. //fmt.Print(str)
  147. return str
  148. }
  149. func DebugInfo(ip string) string {
  150. // Query the current playing thing, and get the tv channels
  151. r := GetQuery(ip, "media-player")
  152. if r == nil {
  153. return ""
  154. }
  155. name, _ := glom.Glom(r, "player.plugin.-name")
  156. id, _ := glom.Glom(r, "player.plugin.-id")
  157. /*r1 := GetQuery(ip, "tv-channels")
  158. chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
  159. chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
  160. chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")*/
  161. // Debug print
  162. fmt.Printf("Name: %v (%v)\r\n", name, id)
  163. //fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
  164. return fmt.Sprintf("%v (%v)", name, id)
  165. }
  166. func examplePost() {
  167. keys := GetKeys()
  168. _ = keys // Keep alive
  169. PerformKey("192.168.254.75", keys["mute"])
  170. //Post("http://192.168.254.75:8060/launch/74519")
  171. //PerformKey("192.168.254.75", keys["home"])
  172. /*
  173. //resp := Post("http://192.168.254.75:8060/keypress/volumeMute")
  174. //resp := Post(KeyPress("192.168.254.75", "volumeMute"))
  175. //resp := Get(Query("192.168.254.75", "device-info"))
  176. //resp := GetQuery("192.168.254.75", "tv-active-channel")
  177. resp := GetQuery("192.168.254.75", "tv-channels")
  178. //result, err := glom.Glom(resp, "tv-channels.channel")
  179. _, err := glom.Glom(resp, "*")
  180. if err != nil {
  181. log.Println(err)
  182. } else {
  183. //fmt.Println(result)
  184. fmt.Println(glom.GetPossible(result))
  185. }
  186. */
  187. // Query the current playing thing, and get the tv channels
  188. r := GetQuery("192.168.254.75", "media-player")
  189. name, _ := glom.Glom(r, "player.plugin.-name")
  190. id, _ := glom.Glom(r, "player.plugin.-id")
  191. r1 := GetQuery("192.168.254.75", "tv-channels")
  192. chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
  193. chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
  194. chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")
  195. // Debug print
  196. fmt.Printf("Name: %v (%v)\r\n", name, id)
  197. fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
  198. }