|
1019 | 1019 | "import urllib.request, requests\n",
|
1020 | 1020 | "from IPython.display import HTML, clear_output\n",
|
1021 | 1021 | "from urllib.parse import urlparse\n",
|
| 1022 | + "import subprocess\n", |
| 1023 | + "import psutil\n", |
1022 | 1024 | "\n",
|
1023 | 1025 | "#####################################\n",
|
| 1026 | + "\n", |
1024 | 1027 | "USE_FREE_TOKEN = True # @param {type:\"boolean\"}\n",
|
1025 | 1028 | "REGION = \"JP\" #@param [\"US\", \"EU\", \"AP\", \"AU\", \"SA\", \"JP\", \"IN\"]\n",
|
| 1029 | + "\n", |
1026 | 1030 | "# Aria2 Service\n",
|
1027 | 1031 | "Aria2_rpc = True # @param {type:\"boolean\"}\n",
|
1028 | 1032 | "Ariang_WEBUI = True # @param {type:\"boolean\"}\n",
|
1029 | 1033 | "TOKEN = \"\" # @param {type:\"string\"}\n",
|
1030 | 1034 | "PORT_FORWARD = \"localhost\" #@param [\"ngrok\", \"localhost\", \"argotunnel\"]\n",
|
1031 |
| - "# OUTPUT_DIR = \"\" # @param {type:\"string\"}\n", |
1032 | 1035 | "PORT = 8221\n",
|
1033 | 1036 | "HOME = os.path.expanduser(\"~\")\n",
|
| 1037 | + "BASE_PATH = 'tools/ariang'\n", |
1034 | 1038 | "\n",
|
1035 | 1039 | "if not os.path.exists(f\"{HOME}/.ipython/ocr.py\"):\n",
|
1036 | 1040 | " hCode = \"https://raw.githubusercontent.com/biplobsd/\" \\\n",
|
|
1049 | 1053 | "\n",
|
1050 | 1054 | "loadingAn()\n",
|
1051 | 1055 | "\n",
|
1052 |
| - "BASE_PATH = 'tools/ariang'\n", |
1053 |
| - "\n", |
1054 | 1056 | "# Ariang SETUP\n",
|
1055 | 1057 | "runSh('apt install -y aria2')\n",
|
1056 | 1058 | "os.makedirs(BASE_PATH, exist_ok=True)\n",
|
|
1063 | 1065 | " htmlF = urlopen(link+\"/releases/latest\").read().decode('UTF-8')\n",
|
1064 | 1066 | " return re.findall(r'.+\\/tag\\/([.0-9A-Za-z]+)\".+/', htmlF)[0]\n",
|
1065 | 1067 | "\n",
|
| 1068 | + "def run_process(command, shell=False, cwd=None, env=None):\n", |
| 1069 | + " for proc in psutil.process_iter():\n", |
| 1070 | + " try:\n", |
| 1071 | + " # Get process info as a named tuple containing the process name and command-line arguments\n", |
| 1072 | + " process_info = proc.as_dict(attrs=['pid', 'name', 'cmdline'])\n", |
| 1073 | + " process_cmdline = process_info['cmdline']\n", |
| 1074 | + "\n", |
| 1075 | + " # Check if the command matches the process command-line arguments\n", |
| 1076 | + " if process_cmdline == command.split():\n", |
| 1077 | + " print(\"Killing process with PID\", process_info['pid'])\n", |
| 1078 | + " proc.kill()\n", |
| 1079 | + " except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):\n", |
| 1080 | + " pass\n", |
| 1081 | + "\n", |
| 1082 | + " print(\"Starting process with command\", command)\n", |
| 1083 | + " subprocess.Popen(command, shell=shell, cwd=cwd, env=env)\n", |
| 1084 | + "\n", |
1066 | 1085 | "# Downloading latest version of ariang\n",
|
1067 | 1086 | "if not os.path.exists(f\"{BASE_PATH}/index.html\"):\n",
|
1068 |
| - " # BASE_URL = r\"https://github.com/mayswind/AriaNg\"\n", |
1069 |
| - " # LATEST_TAG = latestTag(BASE_URL)\n", |
1070 |
| - " # urlF = f'{BASE_URL}/releases/download/{LATEST_TAG}/' \\\n", |
1071 |
| - " # f'AriaNg-{LATEST_TAG}-AllInOne.zip'\n", |
1072 | 1087 | " urllib.request.urlretrieve(\n",
|
1073 | 1088 | " findPackageR('mayswind/AriaNg', 'AllInOne.zip'),\n",
|
1074 | 1089 | " f'{BASE_PATH}/new.zip')\n",
|
|
1080 | 1095 | " pass\n",
|
1081 | 1096 | "\n",
|
1082 | 1097 | "# START_ARIANG_WEBUI_AND_ARIA2_RPC\n",
|
1083 |
| - "try:\n", |
1084 |
| - " if not OUTPUT_DIR:\n", |
1085 |
| - " OUTPUT_DIR = f\"downloads/\"\n", |
1086 |
| - " elif not os.path.exists(OUTPUT_DIR):\n", |
1087 |
| - " clear_output()\n", |
1088 |
| - " print(\"Error: Your set path not found! Create path!\")\n", |
1089 |
| - " exx()\n", |
1090 |
| - "except:\n", |
1091 |
| - " OUTPUT_DIR = f\"{CWD}/downloads/\"\n", |
| 1098 | + "OUTPUT_DIR = f\"{CWD}/downloads/\"\n", |
1092 | 1099 | "if Aria2_rpc:\n",
|
1093 | 1100 | " if not findProcess(\"aria2c\", \"--enable-rpc\"):\n",
|
1094 | 1101 | " try:\n",
|
|
1137 | 1144 | " read_data = f.read()\n",
|
1138 | 1145 | " f.seek(0)\n",
|
1139 | 1146 | " f.truncate(0)\n",
|
1140 |
| - " read_data = re.sub(r'(rpcHost:\"\\w+.\")|rpcHost:\"\"', \n", |
1141 |
| - " f'rpcHost:\"{Host}\"', read_data)\n", |
| 1147 | + " read_data = re.sub(r'(rpcHost:\"[^\"]+.)|rpcHost:\"\"', f'rpcHost:\"{Host}\"', read_data)\n", |
1142 | 1148 | " read_data = re.sub(r'protocol:\"\\w+.\"', r'protocol:\"ws\"', read_data)\n",
|
1143 | 1149 | " read_data = re.sub(r'rpcPort:\"\\d+.\"', f'rpcPort:\"{port}\"', read_data)\n",
|
1144 | 1150 | " f.write(read_data)\n",
|
1145 |
| - " try:\n", |
1146 |
| - " urllib.request.urlopen(f\"http://localhost:{PORT}\")\n", |
1147 |
| - " except:\n", |
1148 |
| - " runSh(f\"python3 -m http.server {PORT} &\", shell=True, cd=BASE_PATH)\n", |
| 1151 | + " \n", |
| 1152 | + " run_process(f\"python3 -m http.server {PORT} &\", shell=True, cwd=BASE_PATH)\n", |
1149 | 1153 | " \n",
|
1150 | 1154 | " Server = PortForward_wrapper(\n",
|
1151 | 1155 | " PORT_FORWARD, TOKEN, USE_FREE_TOKEN, [['Ariang', PORT, 'http']], REGION.lower(), \n",
|
|
0 commit comments