appinstaller.py 4.6 KB

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