Update app.py
Browse files
app.py
CHANGED
|
@@ -26,19 +26,23 @@ class MirrorScraper:
|
|
| 26 |
homepage_url = f"{self.base_url}/"
|
| 27 |
|
| 28 |
logger.info("π Extracting CSRF token from homepage...")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
-
logger.error("β
|
| 39 |
return None
|
| 40 |
-
|
| 41 |
-
logger.error(f"β
|
| 42 |
return None
|
| 43 |
|
| 44 |
def get_server_data_and_csrf(self) -> tuple:
|
|
@@ -63,24 +67,28 @@ class MirrorScraper:
|
|
| 63 |
}
|
| 64 |
|
| 65 |
logger.info("π Getting server data and new CSRF token...")
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if response.status_code == 200:
|
| 69 |
-
# Extract NEW CSRF token from the JavaScript data
|
| 70 |
-
csrf_match = re.search(r'data-csrf=""([^&]+)""', response.text)
|
| 71 |
-
if csrf_match:
|
| 72 |
-
new_csrf = csrf_match.group(1)
|
| 73 |
-
logger.info("β
New CSRF token extracted")
|
| 74 |
-
else:
|
| 75 |
-
logger.warning("β οΈ New CSRF token not found, using fallback")
|
| 76 |
-
new_csrf = "RXFOUlV0ZjFxdHg5L1VGd1ZrazRKVFRDUGdNQ1ZhUG01N2l3ZitXTFFMUU81Z2tUb1JXY3JoOXJveWtaSHA2clNBRHZhSFNIN1N0Rk8rak13c3dJNFBEcEcwa3VjYndCMVo5emEyd0ZUQ2RzK0U3RCtNR3dYcGVVSkxTamx6bzl4M2orNkN0Q2JwLzZHU0F6NHkwSTlRPT0="
|
| 77 |
-
|
| 78 |
-
# Extract server ID (use first one from the list or fallback)
|
| 79 |
-
server_id = "161" # Fallback to known working server
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
return None, None
|
| 85 |
|
| 86 |
def get_complete_url(self) -> Dict[str, str]:
|
|
@@ -89,7 +97,7 @@ class MirrorScraper:
|
|
| 89 |
# Get server ID and NEW CSRF token
|
| 90 |
server_id, new_csrf = self.get_server_data_and_csrf()
|
| 91 |
if not server_id or not new_csrf:
|
| 92 |
-
return {"error": "Cannot proceed without server data"}
|
| 93 |
|
| 94 |
# Use the NEW CSRF token for the final request
|
| 95 |
url = f"{self.base_url}/requests?fso="
|
|
@@ -110,7 +118,7 @@ class MirrorScraper:
|
|
| 110 |
}
|
| 111 |
|
| 112 |
logger.info("π Getting complete URL from mirror site...")
|
| 113 |
-
response = requests.post(url, data=data, headers=headers, allow_redirects=False)
|
| 114 |
|
| 115 |
logger.info(f"π Status: {response.status_code}")
|
| 116 |
|
|
@@ -128,11 +136,11 @@ class MirrorScraper:
|
|
| 128 |
if 'location' not in response.headers:
|
| 129 |
error_msg += " - No location header in response"
|
| 130 |
logger.error(f"β {error_msg}")
|
| 131 |
-
return {"error": error_msg}
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
logger.error(f"β Exception occurred: {str(e)}")
|
| 135 |
-
return {"error": f"Internal server error: {str(e)}"}
|
| 136 |
|
| 137 |
# Initialize the scraper
|
| 138 |
scraper = MirrorScraper()
|
|
@@ -140,17 +148,44 @@ scraper = MirrorScraper()
|
|
| 140 |
@app.route('/')
|
| 141 |
def home():
|
| 142 |
return jsonify({
|
| 143 |
-
"message": "Mirror URL Generator API",
|
| 144 |
"status": "active",
|
|
|
|
| 145 |
"endpoints": {
|
| 146 |
"/generate": "GET - Generate a new URL",
|
| 147 |
-
"/health": "GET - Health check"
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
})
|
| 150 |
|
| 151 |
@app.route('/health')
|
| 152 |
def health():
|
| 153 |
-
return jsonify({"status": "healthy"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
@app.route('/generate', methods=['GET'])
|
| 156 |
def generate_url():
|
|
@@ -159,7 +194,10 @@ def generate_url():
|
|
| 159 |
result = scraper.get_complete_url()
|
| 160 |
return jsonify(result)
|
| 161 |
except Exception as e:
|
| 162 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
if __name__ == '__main__':
|
| 165 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 26 |
homepage_url = f"{self.base_url}/"
|
| 27 |
|
| 28 |
logger.info("π Extracting CSRF token from homepage...")
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(homepage_url, headers=self.headers, timeout=10)
|
| 31 |
+
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
csrf_match = re.search(r'name="csrf"[^>]*value="([^"]+)"', response.text)
|
| 34 |
+
if csrf_match:
|
| 35 |
+
csrf_token = csrf_match.group(1)
|
| 36 |
+
logger.info("β
CSRF token extracted from homepage")
|
| 37 |
+
return csrf_token
|
| 38 |
+
else:
|
| 39 |
+
logger.error("β CSRF token not found in homepage")
|
| 40 |
+
return None
|
| 41 |
else:
|
| 42 |
+
logger.error(f"β Failed to get homepage. Status: {response.status_code}")
|
| 43 |
return None
|
| 44 |
+
except Exception as e:
|
| 45 |
+
logger.error(f"β Error extracting CSRF: {str(e)}")
|
| 46 |
return None
|
| 47 |
|
| 48 |
def get_server_data_and_csrf(self) -> tuple:
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
logger.info("π Getting server data and new CSRF token...")
|
| 70 |
+
try:
|
| 71 |
+
response = requests.post(servers_url, data=data, headers=headers, timeout=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
if response.status_code == 200:
|
| 74 |
+
# Extract NEW CSRF token from the JavaScript data
|
| 75 |
+
csrf_match = re.search(r'data-csrf=""([^&]+)""', response.text)
|
| 76 |
+
if csrf_match:
|
| 77 |
+
new_csrf = csrf_match.group(1)
|
| 78 |
+
logger.info("β
New CSRF token extracted")
|
| 79 |
+
else:
|
| 80 |
+
logger.warning("β οΈ New CSRF token not found, using fallback")
|
| 81 |
+
new_csrf = "RXFOUlV0ZjFxdHg5L1VGd1ZrazRKVFRDUGdNQ1ZhUG01N2l3ZitXTFFMUU81Z2tUb1JXY3JoOXJveWtaSHA2clNBRHZhSFNIN1N0Rk8rak13c3dJNFBEcEcwa3VjYndCMVo5emEyd0ZUQ2RzK0U3RCtNR3dYcGVVSkxTamx6bzl4M2orNkN0Q2JwLzZHU0F6NHkwSTlRPT0="
|
| 82 |
+
|
| 83 |
+
# Extract server ID (use first one from the list or fallback)
|
| 84 |
+
server_id = "161" # Fallback to known working server
|
| 85 |
+
|
| 86 |
+
return server_id, new_csrf
|
| 87 |
+
else:
|
| 88 |
+
logger.error(f"β Failed to get servers page. Status: {response.status_code}")
|
| 89 |
+
return None, None
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.error(f"β Error getting server data: {str(e)}")
|
| 92 |
return None, None
|
| 93 |
|
| 94 |
def get_complete_url(self) -> Dict[str, str]:
|
|
|
|
| 97 |
# Get server ID and NEW CSRF token
|
| 98 |
server_id, new_csrf = self.get_server_data_and_csrf()
|
| 99 |
if not server_id or not new_csrf:
|
| 100 |
+
return {"success": False, "error": "Cannot proceed without server data"}
|
| 101 |
|
| 102 |
# Use the NEW CSRF token for the final request
|
| 103 |
url = f"{self.base_url}/requests?fso="
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
logger.info("π Getting complete URL from mirror site...")
|
| 121 |
+
response = requests.post(url, data=data, headers=headers, allow_redirects=False, timeout=10)
|
| 122 |
|
| 123 |
logger.info(f"π Status: {response.status_code}")
|
| 124 |
|
|
|
|
| 136 |
if 'location' not in response.headers:
|
| 137 |
error_msg += " - No location header in response"
|
| 138 |
logger.error(f"β {error_msg}")
|
| 139 |
+
return {"success": False, "error": error_msg}
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
logger.error(f"β Exception occurred: {str(e)}")
|
| 143 |
+
return {"success": False, "error": f"Internal server error: {str(e)}"}
|
| 144 |
|
| 145 |
# Initialize the scraper
|
| 146 |
scraper = MirrorScraper()
|
|
|
|
| 148 |
@app.route('/')
|
| 149 |
def home():
|
| 150 |
return jsonify({
|
| 151 |
+
"message": "Mirror URL Generator API - huijio/urltemplate",
|
| 152 |
"status": "active",
|
| 153 |
+
"version": "1.0",
|
| 154 |
"endpoints": {
|
| 155 |
"/generate": "GET - Generate a new URL",
|
| 156 |
+
"/health": "GET - Health check",
|
| 157 |
+
"/docs": "GET - API documentation"
|
| 158 |
+
},
|
| 159 |
+
"example_usage": "GET https://huijio-urltemplate.hf.space/generate"
|
| 160 |
})
|
| 161 |
|
| 162 |
@app.route('/health')
|
| 163 |
def health():
|
| 164 |
+
return jsonify({"status": "healthy", "service": "mirror-url-generator"})
|
| 165 |
+
|
| 166 |
+
@app.route('/docs')
|
| 167 |
+
def docs():
|
| 168 |
+
return jsonify({
|
| 169 |
+
"api_documentation": {
|
| 170 |
+
"base_url": "https://huijio-urltemplate.hf.space",
|
| 171 |
+
"endpoints": {
|
| 172 |
+
"/generate": {
|
| 173 |
+
"method": "GET",
|
| 174 |
+
"description": "Generate a new proxy URL",
|
| 175 |
+
"response": {
|
| 176 |
+
"success": "boolean",
|
| 177 |
+
"url": "string (the generated URL)",
|
| 178 |
+
"message": "string"
|
| 179 |
+
}
|
| 180 |
+
},
|
| 181 |
+
"/health": {
|
| 182 |
+
"method": "GET",
|
| 183 |
+
"description": "Health check endpoint",
|
| 184 |
+
"response": {"status": "string"}
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
})
|
| 189 |
|
| 190 |
@app.route('/generate', methods=['GET'])
|
| 191 |
def generate_url():
|
|
|
|
| 194 |
result = scraper.get_complete_url()
|
| 195 |
return jsonify(result)
|
| 196 |
except Exception as e:
|
| 197 |
+
return jsonify({
|
| 198 |
+
"success": False,
|
| 199 |
+
"error": f"Internal server error: {str(e)}"
|
| 200 |
+
}), 500
|
| 201 |
|
| 202 |
if __name__ == '__main__':
|
| 203 |
app.run(host='0.0.0.0', port=7860, debug=False)
|