Guest User

Translate.py

a guest
Dec 11th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. import psutil
  2. import tkinter as tk
  3. import pygetwindow as gw
  4. import pyautogui
  5. from PIL import Image
  6. import subprocess
  7. import os
  8. import sys
  9. import OCR
  10. from pathlib import Path
  11.  
  12. #Return exe name selected by drop box
  13. def pass_exe_name():
  14. if len(sys.argv) > 2: # Check if a value was passed
  15. received_value = sys.argv[1]
  16. ffd = (f"{received_value}")
  17. result = ffd
  18. #print(result)
  19. return result
  20. else:
  21. print("No value was passed")
  22.  
  23. #return activation key
  24. def acti():
  25. if len(sys.argv) > 2: # Check if a value was passed
  26. received_value2 = sys.argv[2]
  27. ddf = (f"{received_value2}")
  28. activ_code = ddf
  29. #print(activ_code)
  30. return activ_code
  31. else:
  32. print("No value was passed")
  33.  
  34. #passed retur to variables
  35. name_exe=pass_exe_name()
  36. matrix=acti()
  37.  
  38. # Read text line if it conatins string
  39. def read_Line():
  40. # Reaads directory of py file
  41. if getattr(sys, 'frozen', False):
  42. script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  43. else:
  44. script_dir = os.path.dirname(os.path.abspath(__file__))
  45. # Constructs the file path for settings file
  46. path_set = os.path.join(script_dir, "settings.ini")
  47. with open(path_set, 'r') as file:
  48. content = file.readlines()
  49. row_without_None = [name.strip() for name in content]
  50. for item in row_without_None: # 'item' takes each value from the list in turn
  51. if "testing_name" in item:
  52. #Return line where string is found
  53. cu = item
  54. #Return the text on line (cu)
  55. index = row_without_None.index(cu)
  56. #Removes spaces that line
  57. lines = content[index].strip()
  58. #return lines
  59. #splits line into chars are reutrn last elemts
  60. last_element = lines.split()[-1]
  61. return last_element
  62.  
  63. #Reads of Shirtcut key in setting
  64. def read_Short():
  65. if getattr(sys, 'frozen', False):
  66. script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  67. else:
  68. script_dir = os.path.dirname(os.path.abspath(__file__))
  69. path_set = os.path.join(script_dir, "settings.ini")
  70. with open(path_set, 'r') as file:
  71. content = file.readlines()
  72. row_without_None = [name.strip() for name in content]
  73. for item in row_without_None: # 'item' takes each value from the list in turn
  74. if "Shortcut" in item:
  75. cu = item
  76. index = row_without_None.index(cu)
  77. lines = content[index].strip()
  78. spl_word = '= '
  79. res = lines.split(spl_word, 1)
  80. splitString = res[1]
  81. return splitString
  82.  
  83. #Reads of API key in setting
  84. def read_API():
  85. if getattr(sys, 'frozen', False):
  86. script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  87. else:
  88. script_dir = os.path.dirname(os.path.abspath(__file__))
  89. path_set = os.path.join(script_dir, "settings.ini")
  90. with open(path_set, 'r') as file:
  91. content = file.readlines()
  92. row_without_None = [name.strip() for name in content]
  93. for item in row_without_None: # 'item' takes each value from the list in turn
  94. if "API_key" in item:
  95. cu = item
  96. index = row_without_None.index(cu)
  97. lines = content[index].strip()
  98. last_element = lines.split()[-1]
  99. return last_element
  100.  
  101. def modi_var():
  102. if getattr(sys, 'frozen', False):
  103. script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  104. else:
  105. script_dir = os.path.dirname(os.path.abspath(__file__))
  106. # Construct the path for saving the screenshot
  107. path_set = os.path.join(script_dir, "settings.ini")
  108. with open(path_set, 'r+') as file:
  109. content = file.readlines()
  110. row_without_None = [name.strip() for name in content]
  111. for item in row_without_None:
  112. if "testing_co" in item:
  113. cu = item
  114. index = row_without_None.index(cu)
  115. print(index)
  116. lines = content[index].strip()
  117. last_element = lines.split()[-1]
  118. s1 = lines.replace(last_element, "3")
  119. file.seek(0)
  120. content[index] = s1 + '\n'
  121. file.writelines(content)
  122. return s1
  123.  
  124. #Modifies of API key in setting
  125. def modi_API(key):
  126. if getattr(sys, 'frozen', False):
  127. script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  128. else:
  129. script_dir = os.path.dirname(os.path.abspath(__file__))
  130. path_set = os.path.join(script_dir, "settings.ini")
  131. with open(path_set, 'r+') as file:
  132. content = file.readlines()
  133. row_without_None = [name.strip() for name in content]
  134. for item in row_without_None: # 'item' takes each value from the list in turn
  135. if "API_key" in item:
  136. cu = item
  137. index = row_without_None.index(cu)
  138. lines = content[index].strip()
  139. last_element = lines.split()[-1]
  140. s1 = lines.replace(last_element, key)
  141. file.seek(0)
  142. content[index] = s1 + '\n'
  143. file.writelines(content)
  144. file.truncate()
  145. return s1
  146.  
  147. #check if the sleceted app is open
  148. def check_Status(exe_name):
  149. mic_Check=exe_name in (i.name() for i in psutil.process_iter())
  150. if mic_Check == True:
  151. #print(exe_name)
  152. appl_name=exe_name.replace(".exe","")
  153. screenshot(appl_name)
  154. elif mic_Check == False :
  155. NOPE = tk.Tk()
  156. NOPE.title("Error 404")
  157. NOPE.minsize(300, 200)
  158. NOPE.maxsize(600, 200)
  159. NOPE.configure(bg='grey')
  160. NOPE.columnconfigure(0, weight=1)
  161. NOPE.columnconfigure(1, weight=1)
  162. NOPE.columnconfigure(2, weight=1)
  163. NOPE.rowconfigure(0, weight=1)
  164. l = tk.Label(NOPE,text="Error 404: Program is not runnning \n 1.)Please fully close this application\n 2.)Start your desired game\n 3.)restart this application and try again\n Sorry for the inconvience ;-)", font=("Noto Sans JP Bold", 10))
  165. l.grid(column=1, row=0)
  166. NOPE.mainloop()
  167.  
  168. def screenshot(app_name):
  169. exe_ad = app_name
  170. # Ensure the console uses UTF-8 encoding
  171. cmd = f'powershell -Command "$OutputEncoding = [System.Text.Encoding]::UTF8; chcp 65001; Get-Process -Name {exe_ad} | ForEach-Object {{ $_.MainWindowTitle }}"'
  172.  
  173. # Execute PowerShell command and capture the output
  174. proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  175.  
  176. w = None
  177. for line in proc.stdout:
  178. if line.strip(): # Skip empty lines
  179. try:
  180. w = line.decode('utf-8').strip() # Attempt UTF-8 decoding
  181. except UnicodeDecodeError:
  182. print("Failed to decode output with UTF-8. Trying UTF-16.")
  183. w = line.decode('utf-16-le').strip() # Try UTF-16LE as a fallback
  184.  
  185. if not w:
  186. print("No window title found. Is the application running?")
  187. return
  188.  
  189. window_title = w # Replace with the actual window title
  190. #print(window_title)
  191. # Get the window with the given title
  192. window = gw.getWindowsWithTitle(window_title)[0]
  193. left, top, right, bottom = window.left, window.top, window.right, window.bottom
  194.  
  195. script_dir = os.path.dirname(os.path.abspath(__file__))
  196. # Construct the path for saving the screenshot
  197. if getattr(sys, 'frozen', False):
  198. path = os.path.dirname(os.path.abspath(sys.argv[0]))
  199. else:
  200. path = Path(__file__).parent.absolute() / "result.png"
  201. #path = os.path.join(script_dir, "result.png")
  202. # Take a screenshot of the window region and save it to the specified path
  203. screenshot = pyautogui.screenshot(region=(left, top, right - left, bottom - top))
  204. screenshot.save(path)
  205. OCR.exec(path)
  206.  
  207. #If activitaion key is sent by button
  208. if "1" in str(matrix):
  209. check_Status(name_exe)
  210.  
  211. # ff=read_API()
  212. # print(ff)
Advertisement
Add Comment
Please, Sign In to add comment