run.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # (c) J.Y.Amihud 2023
  2. # GPL-3 or any later version
  3. # This is a script that should let people download.
  4. import os
  5. import json
  6. import time
  7. import hashlib
  8. import urllib.request
  9. import urllib.parse
  10. import subprocess
  11. website = "https://bass.madiator.cloud"
  12. #website = "http://4n3qimgvdr64ay2cbiqjw3e6fj73vbjnc3dagzxb7kov36gyixstk2yd.onion"
  13. #website = "http://localhost:8080"
  14. project = "Morias_Race_Sources"
  15. # Colors are used to make the
  16. clr = {
  17. "norm":"\033[00m", # Reset to normal
  18. "bold":"\033[01m", # Bold Text
  19. "ital":"\033[03m", # Italic Text
  20. "undr":"\033[04m", # Underlined
  21. "blnk":"\033[05m", # Blinking
  22. # Text
  23. "tdbl":"\033[30m", # Dark Black
  24. "tdrd":"\033[31m", # Dark Red
  25. "tdgr":"\033[32m", # Dark Green
  26. "tdyl":"\033[33m", # Dark Yellow
  27. "tdbu":"\033[34m", # Dark Blue
  28. "tdma":"\033[35m", # Dark Magenta
  29. "tdcy":"\033[36m", # Dark Cyan
  30. "tdwh":"\033[37m", # Dark White
  31. "tbbl":"\033[90m", # Bright Black
  32. "tbrd":"\033[91m", # Bright Red
  33. "tbgr":"\033[92m", # Bright Green
  34. "tbyl":"\033[93m", # Bright Yellow
  35. "tbbu":"\033[94m", # Bright Blue
  36. "tbma":"\033[95m", # Bright Magenta
  37. "tbcy":"\033[96m", # Bright Cyan
  38. "tbwh":"\033[97m", # Bright White
  39. # Background
  40. "bdbl":"\033[40m", # Dark Black
  41. "bdrd":"\033[41m", # Dark Red
  42. "bdgr":"\033[42m", # Dark Green
  43. "bdyl":"\033[43m", # Dark Yellow
  44. "bdbu":"\033[44m", # Dark Blue
  45. "bdma":"\033[45m", # Dark Magenta
  46. "bdcy":"\033[46m", # Dark Cyan
  47. "bdwh":"\033[47m", # Dark White
  48. "bbbl":"\033[100m", # Bright Black
  49. "bbrd":"\033[101m", # Bright Red
  50. "bbgr":"\033[102m", # Bright Green
  51. "bbyl":"\033[103m", # Bright Yellow
  52. "bbbu":"\033[104m", # Bright Blue
  53. "bbma":"\033[105m", # Bright Magenta
  54. "bbcy":"\033[106m", # Bright Cyan
  55. "bbwh":"\033[108m" # Bright White
  56. }
  57. # LOGO
  58. logo = open("big_logo.txt")
  59. logo = logo.read()
  60. print()
  61. print(logo)
  62. print()
  63. print(" type 'help' to start")
  64. print()
  65. def csize(x):
  66. x = float(x)
  67. l = ["B","KB", "MB", "GB", "TB"]
  68. for i in range(5):
  69. if x > 1024:
  70. x = x / 1024
  71. else:
  72. return str(round(x, 2))+" "+l[i]
  73. return str(round(x, 2))+" "+l[i]
  74. def pb(percent, action=""):
  75. percent = max(0, min(100, percent))
  76. percent = round(percent, 2)
  77. print("\r "+clr["bold"]+str(percent)+"%"+clr["tdyl"]+" ["+("#"*int(percent/5))+(" "*int(20-(percent/5)))+"] "+clr["norm"]+action+" ", end=clr["norm"])
  78. # RESPONSE
  79. def go(path):
  80. url = website+path
  81. response = urllib.request.urlopen(url)
  82. data = response.read()
  83. text = data.decode("utf-8")
  84. data = json.loads(text)
  85. return data
  86. def down(cur, filename, pp=0, pf=100, filesize=0):
  87. if filename.endswith(".blend"):
  88. dp = go("/blend"+cur+filename)
  89. # cp = (pp+(pf*0.1))
  90. # pb(cp, "["+clr["bold"]+filename+clr["norm"]+"]")
  91. dplen = len(dp.get("files", {}))
  92. for f in dp.get("files", {}):
  93. try:
  94. hashis = hashlib.md5(open(project+f,'rb').read()).hexdigest()
  95. except:
  96. hashis = ""
  97. if hashis != dp.get("files", {}).get(f, {}).get("md5"):
  98. pp = down( f[:f.rfind("/")+1],
  99. f[f.rfind("/")+1:],
  100. pp=pp,
  101. pf=pf/dplen,
  102. filesize=dp.get("files", {}).get(f, {}).get("filesize",0))
  103. try:
  104. os.makedirs(os.getcwd()+"/"+project+cur)
  105. #print(" 📁 Making Directory: "+project+cur+" ...")
  106. except:
  107. pass
  108. cp = pp+pf
  109. pb(min(cp, 98), clr["bold"]+filename+clr["norm"]+" ("+csize(filesize)+")")
  110. url = website+"/download"+cur+filename
  111. response = urllib.request.urlopen(url)
  112. savef = open(project+cur+filename, "wb")
  113. savef.write(response.read())
  114. savef.close()
  115. return cp
  116. # HELP
  117. def help():
  118. print("""
  119. This is a very simple program.
  120. Here are the commands:
  121. help - Shows this help.
  122. exit - Exits the software.
  123. ls - List file in the current folder
  124. cd [folder name] - Change directory
  125. op [file name] - Get and Open File
  126. """)
  127. # LIST
  128. def ls(cur):
  129. data = go("/list"+cur)
  130. for folder in data.get("folders", []):
  131. print(" 📁"+clr["tdyl"]+" "+folder)
  132. for f in data.get("files", []):
  133. print(" 📄"+clr["tbbu"]+" "+f)
  134. print(clr["norm"])
  135. # CHANGE DIRECTORY
  136. def cd(cur, where):
  137. if where != "..":
  138. data = go("/list"+cur)
  139. if where in data.get("folders", []):
  140. globals()["cur"] = cur+where+"/"
  141. else:
  142. globals()["cur"] = cur[:cur[:-1].rfind("/")+1]
  143. if not globals()["cur"]:
  144. globals()["cur"] = "/"
  145. # OPEN
  146. def op(cur, filename):
  147. data = go("/list"+cur)
  148. if filename in data.get("files", {}):
  149. # Checking hash
  150. #print(" ✅ Checking MD5 Hash...")
  151. pb(0, clr["bold"]+filename+clr["norm"])
  152. try:
  153. hashis = hashlib.md5(open(project+cur+filename,'rb').read()).hexdigest()
  154. except:
  155. hashis = ""
  156. # Downloading
  157. if hashis != data.get("files", {}).get(filename, {}).get("md5"):
  158. down(cur, filename,
  159. filesize=data.get("files", {}).get(filename, {}).get("filesize",0))
  160. pb(100, clr["bold"]+filename+clr["norm"])
  161. print()
  162. # Opening
  163. subprocess.Popen(["xdg-open", project+cur+filename], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
  164. print(clr["norm"])
  165. cur = "/"
  166. # MAIN LOOP
  167. while True:
  168. c = input(" 📁 "+clr["tbyl"]+cur.replace("/", clr["tbbu"]+clr["bold"]+" > "+clr["norm"]+clr["tbyl"])+clr["norm"]+" ")
  169. if c == "help":
  170. help()
  171. elif c == "exit":
  172. exit()
  173. elif c == "ls":
  174. ls(cur)
  175. elif c.startswith("cd "):
  176. cd(cur, c[3:])
  177. elif c.startswith("op "):
  178. op(cur, c[3:])