app_notifier.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "path/filepath"
  9. "context"
  10. "time"
  11. "github.com/zyxar/argo/rpc"
  12. //"database/sql"
  13. //_"github.com/mattn/go-sqlite3"
  14. )
  15. // The RPC server might send notifications to the client.
  16. // Notifications is unidirectional, therefore the client which receives the notification must not respond to it.
  17. // The method signature of a notification is much like a normal method request but lacks the id key
  18. func fileExists(filename string) bool {
  19. info, err := os.Stat(filename)
  20. if os.IsNotExist(err) {
  21. return false
  22. }
  23. return !info.IsDir()
  24. }
  25. func System(cmd string) string {
  26. ret := ""
  27. out, err := exec.Command("bash", "-c", cmd).Output()
  28. if err != nil {
  29. if _, ok := err.(*exec.ExitError); ok {
  30. //exit code !=0 ,but it can be ignored
  31. } else {
  32. fmt.Println(err)
  33. }
  34. } else {
  35. ret = string(out)
  36. }
  37. return ret
  38. }
  39. type AppNotifier struct{}
  40. func (AppNotifier) OnDownloadStart(events []rpc.Event) { log.Printf("%s started.", events) }
  41. func (AppNotifier) OnDownloadPause(events []rpc.Event) { log.Printf("%s paused.", events) }
  42. func (AppNotifier) OnDownloadStop(events []rpc.Event) { log.Printf("%s stopped.", events) }
  43. func (AppNotifier) OnDownloadComplete(events []rpc.Event) {
  44. log.Printf("AppNotifier %s completed.", events)
  45. rpcc_, err := rpc.New(context.Background(), rpcURI, rpcSecret, time.Second,nil)
  46. if err != nil {
  47. fmt.Fprintln(os.Stderr, err)
  48. return
  49. }
  50. defer rpcc_.Close()
  51. for _,v := range events {
  52. gid := v.Gid
  53. msg, err := rpcc_.TellStatus(gid)
  54. if err == nil {
  55. log.Printf("%+v\n",msg)
  56. if msg.Status == "complete" {
  57. go InstallGame(msg)
  58. }else if msg.Status == "error" {
  59. log.Println(msg.ErrorMessage)
  60. for _,v := range msg.Files {
  61. if fileExists(v.Path) {
  62. e := os.Remove(v.Path)
  63. if e != nil {
  64. log.Fatal(e)
  65. }
  66. }
  67. if fileExists( v.Path + ".aria2" ) {
  68. e := os.Remove(v.Path + ".aria2")
  69. if e != nil {
  70. log.Fatal(e)
  71. }
  72. }
  73. }
  74. }
  75. }else {
  76. log.Println("TellStatus err: ",err)
  77. }
  78. }
  79. }
  80. func (AppNotifier) OnDownloadError(events []rpc.Event) {
  81. log.Printf("%s error.", events)
  82. }
  83. func (AppNotifier) OnBtDownloadComplete(events []rpc.Event) {
  84. log.Printf("bt %s completed.", events)
  85. }
  86. func InstallGame(msg rpc.StatusInfo) {
  87. if len(msg.Files) <= 0 {
  88. return
  89. }
  90. ret := msg.Files[0].URIs
  91. home_path,_ := os.UserHomeDir()
  92. remote_file_url := ret[0].URI
  93. parts := strings.Split(remote_file_url,"raw.githubusercontent.com")
  94. if len(parts) < 1 {
  95. return
  96. }
  97. menu_file := parts[1]
  98. local_menu_file := fmt.Sprintf("%s/aria2downloads%s",home_path,menu_file)
  99. local_menu_file_path := filepath.Dir(local_menu_file)
  100. if fileExists(local_menu_file) {
  101. gametype := "launcher"
  102. if strings.HasSuffix(local_menu_file,".tar.gz") {
  103. gametype = "launcher"
  104. }
  105. if strings.HasSuffix(local_menu_file,".p8.png") {
  106. gametype = "pico8"
  107. }
  108. if strings.HasSuffix(local_menu_file,".tic") {
  109. gametype = "tic80"
  110. }
  111. if gametype == "launcher" {
  112. _cmd := fmt.Sprintf( "tar zxvf '%s' -C %s",local_menu_file, local_menu_file_path)
  113. fmt.Println(_cmd)
  114. System(_cmd)
  115. }
  116. if gametype == "pico8" {
  117. _cmd := fmt.Sprintf("cp -rf '%s' ~/.lexaloffle/pico-8/carts/", local_menu_file)
  118. fmt.Println(_cmd)
  119. System(_cmd)
  120. }
  121. if gametype == "tic80" {
  122. _cmd := fmt.Sprintf("cp -rf '%s' ~/games/TIC-80/",local_menu_file)
  123. fmt.Println(_cmd)
  124. System(_cmd)
  125. }
  126. }else {
  127. fmt.Println(local_menu_file, " not found")
  128. }
  129. }