appinstaller.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. rpc = Wsrpc('localhost',6800)
  11. def dict_factory(cursor, row):
  12. d = {}
  13. for idx, col in enumerate(cursor.description):
  14. d[col[0]] = row[idx]
  15. return d
  16. @misc.threaded
  17. def game_install_thread(gid):
  18. try:
  19. conn = sqlite3.connect(aria2_db)
  20. conn.row_factory = dict_factory
  21. c = conn.cursor()
  22. ret = c.execute("SELECT * FROM tasks WHERE gid='%s'" % gid ).fetchone()
  23. print(ret)
  24. remote_file_url = ret["file"]
  25. menu_file = remote_file_url.split("master")[1]
  26. local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file )
  27. if os.path.exists(local_menu_file) == True and "arm" not in platform.machine():
  28. gametype = ret["type"]
  29. if gametype == "launcher":
  30. #tar zxvf
  31. _cmd = "tar zxvf '%s' -C %s" % (local_menu_file, "~/apps/Menu/21_Indie\ Games/")
  32. print(_cmd)
  33. os.system(_cmd)
  34. if gametype == "pico8":
  35. _cmd="cp -rf '%s' ~/.lexaloffle/pico-8/carts/" % local_menu_file
  36. print(_cmd)
  37. os.system(_cmd)
  38. if gametype == "tic80":
  39. _cmd = "cp -rf '%s' ~/games/TIC-80/" % local_menu_file
  40. print(_cmd)
  41. os.system(_cmd)
  42. conn.close()
  43. except Exception as ex:
  44. print("Sqlite3 error: ",ex)
  45. def on_message(ws, message):
  46. global rpc
  47. print("got message ",message)
  48. #decode json
  49. #lookup in the sqlite db ,update the status[error,complete],
  50. #uncompress the game into destnation folder in the game_install_thread
  51. aria2_noti = json.loads(message)
  52. if "method" in aria2_noti and aria2_noti["method"] == "aria2.onDownloadError":
  53. gid = aria2_noti["params"][0]["gid"]
  54. msg = rpc.tellStatus(gid)
  55. ws.send(msg)
  56. if "method" in aria2_noti and aria2_noti["method"] == "aria2.onDownloadComplete":
  57. gid = aria2_noti["params"][0]["gid"]
  58. msg = rpc.tellStatus(gid)
  59. ws.send(msg)
  60. game_install_thread(gid)
  61. def on_error(ws, error):
  62. print(error)
  63. def on_close(ws):
  64. print("### closed ###")
  65. def on_open(ws):
  66. print "on open"
  67. def create_connection(db_file):
  68. conn = None
  69. try:
  70. conn = sqlite3.connect(db_file)
  71. return conn
  72. except Error as e:
  73. print(e)
  74. return conn
  75. def create_table(conn, create_table_sql):
  76. try:
  77. c = conn.cursor()
  78. c.execute(create_table_sql)
  79. except Error as e:
  80. print(e)
  81. def init_sqlite3():
  82. database = r"aria2tasks.db"
  83. sql_create_tasks_table = """ CREATE TABLE IF NOT EXISTS tasks (
  84. id integer PRIMARY KEY,
  85. gid text NOT NULL,
  86. title text NOT NULL,
  87. file text NOT NULL,
  88. type text NOT NULL,
  89. status text,
  90. totalLength text,
  91. completedLength text,
  92. fav text
  93. ); """
  94. conn = create_connection(database)
  95. if conn is not None:
  96. create_table(conn, sql_create_tasks_table)
  97. else:
  98. print("Error! cannot create the database connection.")
  99. exit()
  100. if __name__ == "__main__":
  101. init_sqlite3()
  102. websocket.enableTrace(True)
  103. ws = websocket.WebSocketApp(aria2_ws,
  104. on_message = on_message,
  105. on_error = on_error,
  106. on_close = on_close)
  107. # ws.on_open = on_open
  108. ws.run_forever()