appinstaller.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import os
  2. import platform
  3. import sqlite3
  4. import json
  5. from wicd import misc
  6. from pyaria2_rpc.pyaria2 import Wsrpc
  7. import libs.websocket as websocket
  8. aria2_ws = "ws://localhost:6800/jsonrpc"
  9. aria2_db = "aria2tasks.db"
  10. warehouse_db = "warehouse.db"
  11. rpc = Wsrpc('localhost',6800)
  12. def dict_factory(cursor, row):
  13. d = {}
  14. for idx, col in enumerate(cursor.description):
  15. d[col[0]] = row[idx]
  16. return d
  17. @misc.threaded
  18. def game_install_thread(aria2_result):
  19. try:
  20. #print("game_install_thread ",aria2_result)
  21. if "files" in aria2_result:
  22. if len(aria2_result["files"]) <= 0:
  23. return
  24. if "arm" not in platform.machine():
  25. return
  26. ret = aria2_result["files"][0]['uris']
  27. remote_file_url = ret[0]['uri']
  28. menu_file = remote_file_url.split("raw.githubusercontent.com")[1]
  29. local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file )
  30. local_menu_file_path = os.path.dirname(local_menu_file)
  31. if os.path.exists(local_menu_file) == True:
  32. gametype = "launcher"
  33. if local_menu_file.endswith(".tar.gz"):
  34. gametype = "launcher"
  35. if local_menu_file.endswith(".p8.png"):
  36. gametype = "pico8"
  37. if local_menu_file.endswith(".tic"):
  38. gametype = "tic80"
  39. if gametype == "launcher":
  40. #tar zxvf
  41. _cmd = "tar zxvf '%s' -C %s" % (local_menu_file, local_menu_file_path)
  42. print(_cmd)
  43. os.system(_cmd)
  44. if gametype == "pico8":
  45. _cmd="cp -rf '%s' ~/.lexaloffle/pico-8/carts/" % local_menu_file
  46. print(_cmd)
  47. os.system(_cmd)
  48. if gametype == "tic80":
  49. _cmd = "cp -rf '%s' ~/games/TIC-80/" % local_menu_file
  50. print(_cmd)
  51. os.system(_cmd)
  52. except Exception as ex:
  53. print("app install error: ",ex)
  54. def on_message(ws, message):
  55. global rpc
  56. print("got message ",message)
  57. #decode json
  58. #lookup in the sqlite db ,update the status[error,complete],
  59. #uncompress the game into destnation folder in the game_install_thread
  60. aria2_noti = json.loads(message)
  61. if "method" in aria2_noti and aria2_noti["method"] == "aria2.onDownloadError":
  62. gid = aria2_noti["params"][0]["gid"]
  63. msg = rpc.tellStatus(gid)
  64. ws.send(msg)
  65. if "method" in aria2_noti and aria2_noti["method"] == "aria2.onDownloadComplete":
  66. gid = aria2_noti["params"][0]["gid"]
  67. msg = rpc.tellStatus(gid)
  68. ws.send(msg)
  69. #game_install_thread(gid)
  70. if "method" not in aria2_noti and "result" in aria2_noti:
  71. result = aria2_noti["result"]
  72. if "status" in result:
  73. if result["status"] == "error":
  74. try:
  75. print(result["errorMessage"])
  76. for x in result["files"]:
  77. if os.path.exists(x["path"]):
  78. os.remove(x["path"])
  79. if os.path.exists(x["path"]+".aria2"):
  80. os.remove(x["path"]+".aria2")
  81. except Exception as ex:
  82. print(ex)
  83. if result["status"] == "complete":
  84. game_install_thread(result)
  85. def on_error(ws, error):
  86. print(error)
  87. def on_close(ws):
  88. print("### closed ###")
  89. def on_open(ws):
  90. print "on open"
  91. def create_connection(db_file):
  92. conn = None
  93. try:
  94. conn = sqlite3.connect(db_file)
  95. return conn
  96. except Error as e:
  97. print(e)
  98. return conn
  99. def create_table(conn, create_table_sql):
  100. try:
  101. c = conn.cursor()
  102. c.execute(create_table_sql)
  103. except Error as e:
  104. print(e)
  105. def init_sqlite3():
  106. global aria2_db
  107. global warehouse_db
  108. database = aria2_db
  109. sql_create_tasks_table = """ CREATE TABLE IF NOT EXISTS tasks (
  110. id integer PRIMARY KEY,
  111. gid text NOT NULL,
  112. title text NOT NULL,
  113. file text NOT NULL,
  114. type text NOT NULL,
  115. status text,
  116. totalLength text,
  117. completedLength text,
  118. fav text
  119. ); """
  120. sql_create_warehouse_table = """ CREATE TABLE IF NOT EXISTS warehouse (
  121. id integer PRIMARY KEY,
  122. title text NOT NULL,
  123. file text NOT NULL,
  124. type text NOT NULL
  125. ); """
  126. conn = create_connection(database)
  127. if conn is not None:
  128. create_table(conn, sql_create_tasks_table)
  129. conn.close()
  130. else:
  131. print("Error! cannot create the database connection.")
  132. exit()
  133. database = warehouse_db
  134. conn = create_connection(database)
  135. if conn is not None:
  136. create_table(conn, sql_create_warehouse_table)
  137. conn.close()
  138. else:
  139. print("Error! cannot create the database connection.")
  140. exit()
  141. if __name__ == "__main__":
  142. init_sqlite3()
  143. websocket.enableTrace(True)
  144. ws = websocket.WebSocketApp(aria2_ws,
  145. on_message = on_message,
  146. on_error = on_error,
  147. on_close = on_close)
  148. # ws.on_open = on_open
  149. ws.run_forever()