Spaces:
Build error
Build error
Commit ·
fb20e23
1
Parent(s): 7a4585e
feat: test api passport checker
Browse files
visa_availability_scraper_playwright.py → app/util/visa_availability_scraper_playwright.py
RENAMED
|
@@ -1,397 +1,447 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import json
|
| 3 |
-
from typing import Dict, Optional, List
|
| 4 |
-
from playwright.async_api import async_playwright
|
| 5 |
-
|
| 6 |
-
class PassportIndexVisaScraper:
|
| 7 |
-
def __init__(self, debug: bool = True):
|
| 8 |
-
"""
|
| 9 |
-
Initialize the Passport Index visa scraper using Playwright
|
| 10 |
-
|
| 11 |
-
Args:
|
| 12 |
-
debug: Enable debug output
|
| 13 |
-
"""
|
| 14 |
-
self.base_url = "https://www.passportindex.org/travel-visa-checker/"
|
| 15 |
-
self.api_url = "https://www.passportindex.org/core/visachecker.php"
|
| 16 |
-
self.debug = debug
|
| 17 |
-
self.browser = None
|
| 18 |
-
self.context = None
|
| 19 |
-
self.page = None
|
| 20 |
-
|
| 21 |
-
async def __aenter__(self):
|
| 22 |
-
"""Initialize browser with stealth mode"""
|
| 23 |
-
self.playwright = await async_playwright().start()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
'--disable-
|
| 30 |
-
'--
|
| 31 |
-
'--
|
| 32 |
-
'--disable-
|
| 33 |
-
'--disable-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
const
|
| 154 |
-
|
| 155 |
-
formData.append('
|
| 156 |
-
formData.append('
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
'
|
| 164 |
-
'
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
await self.page.
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
await self.page.
|
| 212 |
-
await self.page.
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
await self.page.
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
await self.page.
|
| 222 |
-
await self.page.
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
const
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
""
|
| 303 |
-
print("
|
| 304 |
-
print("
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
print("
|
| 314 |
-
print("
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
print("
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
print("
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
'
|
| 346 |
-
'
|
| 347 |
-
'
|
| 348 |
-
'
|
| 349 |
-
'
|
| 350 |
-
'
|
| 351 |
-
'
|
| 352 |
-
'
|
| 353 |
-
'
|
| 354 |
-
'
|
| 355 |
-
'
|
| 356 |
-
'
|
| 357 |
-
'
|
| 358 |
-
'
|
| 359 |
-
'
|
| 360 |
-
'
|
| 361 |
-
'
|
| 362 |
-
'
|
| 363 |
-
'
|
| 364 |
-
'
|
| 365 |
-
'
|
| 366 |
-
'
|
| 367 |
-
'
|
| 368 |
-
'
|
| 369 |
-
'
|
| 370 |
-
'
|
| 371 |
-
'
|
| 372 |
-
'
|
| 373 |
-
'
|
| 374 |
-
'
|
| 375 |
-
'
|
| 376 |
-
'
|
| 377 |
-
'
|
| 378 |
-
'
|
| 379 |
-
'
|
| 380 |
-
'
|
| 381 |
-
'
|
| 382 |
-
'
|
| 383 |
-
'
|
| 384 |
-
'
|
| 385 |
-
'
|
| 386 |
-
'
|
| 387 |
-
'
|
| 388 |
-
'
|
| 389 |
-
'
|
| 390 |
-
'
|
| 391 |
-
'
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
from typing import Dict, Optional, List
|
| 4 |
+
from playwright.async_api import async_playwright
|
| 5 |
+
|
| 6 |
+
class PassportIndexVisaScraper:
|
| 7 |
+
def __init__(self, debug: bool = True):
|
| 8 |
+
"""
|
| 9 |
+
Initialize the Passport Index visa scraper using Playwright
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
debug: Enable debug output
|
| 13 |
+
"""
|
| 14 |
+
self.base_url = "https://www.passportindex.org/travel-visa-checker/"
|
| 15 |
+
self.api_url = "https://www.passportindex.org/core/visachecker.php"
|
| 16 |
+
self.debug = debug
|
| 17 |
+
self.browser = None
|
| 18 |
+
self.context = None
|
| 19 |
+
self.page = None
|
| 20 |
+
|
| 21 |
+
async def __aenter__(self):
|
| 22 |
+
"""Initialize browser with stealth mode"""
|
| 23 |
+
self.playwright = await async_playwright().start()
|
| 24 |
+
|
| 25 |
+
self.browser = await self.playwright.chromium.launch(
|
| 26 |
+
headless=True, # Using headless mode
|
| 27 |
+
args=[
|
| 28 |
+
'--disable-blink-features=AutomationControlled',
|
| 29 |
+
'--disable-dev-shm-usage',
|
| 30 |
+
'--no-sandbox',
|
| 31 |
+
'--disable-setuid-sandbox',
|
| 32 |
+
'--disable-web-security',
|
| 33 |
+
'--disable-features=IsolateOrigins,site-per-process'
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Create context with realistic settings
|
| 38 |
+
self.context = await self.browser.new_context(
|
| 39 |
+
viewport={'width': 1920, 'height': 1080},
|
| 40 |
+
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
|
| 41 |
+
locale='en-US',
|
| 42 |
+
timezone_id='America/New_York'
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
self.page = await self.context.new_page()
|
| 46 |
+
|
| 47 |
+
# Add stealth JavaScript to avoid detection
|
| 48 |
+
await self.page.add_init_script("""
|
| 49 |
+
// Override the navigator.webdriver property
|
| 50 |
+
Object.defineProperty(navigator, 'webdriver', {
|
| 51 |
+
get: () => undefined
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
// Override chrome property
|
| 55 |
+
window.chrome = {
|
| 56 |
+
runtime: {}
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
// Override permissions
|
| 60 |
+
const originalQuery = window.navigator.permissions.query;
|
| 61 |
+
window.navigator.permissions.query = (parameters) => (
|
| 62 |
+
parameters.name === 'notifications' ?
|
| 63 |
+
Promise.resolve({ state: Notification.permission }) :
|
| 64 |
+
originalQuery(parameters)
|
| 65 |
+
);
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
+
if self.debug:
|
| 69 |
+
print("🚀 Browser initialized with stealth mode")
|
| 70 |
+
|
| 71 |
+
return self
|
| 72 |
+
|
| 73 |
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
| 74 |
+
"""Clean up browser resources"""
|
| 75 |
+
if self.page:
|
| 76 |
+
await self.page.close()
|
| 77 |
+
if self.context:
|
| 78 |
+
await self.context.close()
|
| 79 |
+
if self.browser:
|
| 80 |
+
await self.browser.close()
|
| 81 |
+
if self.playwright:
|
| 82 |
+
await self.playwright.stop()
|
| 83 |
+
|
| 84 |
+
if self.debug:
|
| 85 |
+
print("🔒 Browser closed")
|
| 86 |
+
|
| 87 |
+
async def initialize_session(self) -> bool:
|
| 88 |
+
"""
|
| 89 |
+
Navigate to the website and wait for it to load properly
|
| 90 |
+
"""
|
| 91 |
+
try:
|
| 92 |
+
if self.debug:
|
| 93 |
+
print("📱 Initializing session...")
|
| 94 |
+
|
| 95 |
+
# Navigate to the page
|
| 96 |
+
try:
|
| 97 |
+
response = await self.page.goto(
|
| 98 |
+
self.base_url,
|
| 99 |
+
wait_until='domcontentloaded',
|
| 100 |
+
timeout=30000
|
| 101 |
+
)
|
| 102 |
+
await self.page.wait_for_timeout(3000)
|
| 103 |
+
|
| 104 |
+
# Get the cl value from the page
|
| 105 |
+
cl_value = await self.page.evaluate("""
|
| 106 |
+
() => {
|
| 107 |
+
const clInput = document.querySelector('#cl');
|
| 108 |
+
return clInput ? clInput.value : 'bc2140a2d83928ce1112d01e610bad89';
|
| 109 |
+
}
|
| 110 |
+
""")
|
| 111 |
+
|
| 112 |
+
if self.debug:
|
| 113 |
+
print(f"✅ Page loaded, session ID: {cl_value}")
|
| 114 |
+
|
| 115 |
+
return True
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
if self.debug:
|
| 119 |
+
print(f"⚠️ Page load issue: {e}, continuing anyway...")
|
| 120 |
+
return True
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(f"❌ Error initializing session: {e}")
|
| 124 |
+
return False
|
| 125 |
+
|
| 126 |
+
async def check_visa_requirement_browser(self, passport_country: str, destination_country: str) -> Optional[Dict]:
|
| 127 |
+
"""
|
| 128 |
+
Check visa requirements using browser automation
|
| 129 |
+
|
| 130 |
+
Args:
|
| 131 |
+
passport_country: Two-letter country code for passport
|
| 132 |
+
destination_country: Two-letter country code for destination
|
| 133 |
+
|
| 134 |
+
Returns:
|
| 135 |
+
Dictionary with visa information or None if failed
|
| 136 |
+
"""
|
| 137 |
+
try:
|
| 138 |
+
if self.debug:
|
| 139 |
+
print(f"🌐 Checking {passport_country.upper()} → {destination_country.upper()}")
|
| 140 |
+
|
| 141 |
+
# Get the current session ID from the page
|
| 142 |
+
cl_value = await self.page.evaluate("""
|
| 143 |
+
() => {
|
| 144 |
+
const clInput = document.querySelector('#cl');
|
| 145 |
+
return clInput ? clInput.value : 'bc2140a2d83928ce1112d01e610bad89';
|
| 146 |
+
}
|
| 147 |
+
""")
|
| 148 |
+
|
| 149 |
+
# Make the API request through the browser with proper argument passing
|
| 150 |
+
result = await self.page.evaluate("""
|
| 151 |
+
async (args) => {
|
| 152 |
+
const [passport, destination, sessionId] = args;
|
| 153 |
+
const formData = new URLSearchParams();
|
| 154 |
+
formData.append('d', destination);
|
| 155 |
+
formData.append('s', passport);
|
| 156 |
+
formData.append('cl', sessionId);
|
| 157 |
+
|
| 158 |
+
try {
|
| 159 |
+
const response = await fetch('https://www.passportindex.org/core/visachecker.php', {
|
| 160 |
+
method: 'POST',
|
| 161 |
+
headers: {
|
| 162 |
+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
| 163 |
+
'X-Requested-With': 'XMLHttpRequest',
|
| 164 |
+
'Accept': 'application/json, text/javascript, */*; q=0.01'
|
| 165 |
+
},
|
| 166 |
+
body: formData.toString(),
|
| 167 |
+
credentials: 'include'
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
+
if (!response.ok) {
|
| 171 |
+
throw new Error(`HTTP ${response.status}`);
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
const data = await response.json();
|
| 175 |
+
return data;
|
| 176 |
+
} catch (error) {
|
| 177 |
+
return { error: error.message };
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
""", [passport_country.lower(), destination_country.lower(), cl_value])
|
| 181 |
+
|
| 182 |
+
if result and 'error' not in result:
|
| 183 |
+
if self.debug:
|
| 184 |
+
print(f"✅ Got result: {result}")
|
| 185 |
+
return result
|
| 186 |
+
elif result and 'error' in result:
|
| 187 |
+
print(f"❌ API Error: {result['error']}")
|
| 188 |
+
return None
|
| 189 |
+
else:
|
| 190 |
+
return None
|
| 191 |
+
|
| 192 |
+
except Exception as e:
|
| 193 |
+
print(f"❌ Error checking visa requirement: {e}")
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
+
async def check_visa_interactive(self, passport_country: str, destination_country: str) -> Optional[Dict]:
|
| 197 |
+
"""
|
| 198 |
+
Alternative method: Use the interactive UI to check visa requirements
|
| 199 |
+
"""
|
| 200 |
+
try:
|
| 201 |
+
if self.debug:
|
| 202 |
+
print(f"🖱️ Using interactive method for {passport_country.upper()} → {destination_country.upper()}")
|
| 203 |
+
|
| 204 |
+
# Click on the passport selector
|
| 205 |
+
await self.page.click('.vch-select-pass')
|
| 206 |
+
await self.page.wait_for_timeout(500)
|
| 207 |
+
|
| 208 |
+
# Find and click the country in the list
|
| 209 |
+
passport_selector = f'.vch-passports .s-div[data-ccode="{passport_country.lower()}"]'
|
| 210 |
+
await self.page.wait_for_selector(passport_selector, timeout=5000)
|
| 211 |
+
await self.page.click(passport_selector)
|
| 212 |
+
await self.page.wait_for_timeout(500)
|
| 213 |
+
|
| 214 |
+
# Click on the destination selector
|
| 215 |
+
await self.page.click('.vch-select-des')
|
| 216 |
+
await self.page.wait_for_timeout(500)
|
| 217 |
+
|
| 218 |
+
# Find and click the destination country
|
| 219 |
+
dest_selector = f'.vch-destinations .s-div[data-ccode="{destination_country.lower()}"]'
|
| 220 |
+
await self.page.wait_for_selector(dest_selector, timeout=5000)
|
| 221 |
+
await self.page.click(dest_selector)
|
| 222 |
+
await self.page.wait_for_timeout(1000)
|
| 223 |
+
|
| 224 |
+
# Get the result from the page
|
| 225 |
+
result = await self.page.evaluate("""
|
| 226 |
+
() => {
|
| 227 |
+
const resultElement = document.querySelector('.vch-result');
|
| 228 |
+
if (resultElement) {
|
| 229 |
+
const text = resultElement.querySelector('.text');
|
| 230 |
+
const days = resultElement.querySelector('.days');
|
| 231 |
+
return {
|
| 232 |
+
text: text ? text.textContent : '',
|
| 233 |
+
days: days ? days.textContent : '',
|
| 234 |
+
pass: '""" + passport_country.lower() + """',
|
| 235 |
+
dest: '""" + destination_country.upper() + """'
|
| 236 |
+
};
|
| 237 |
+
}
|
| 238 |
+
return null;
|
| 239 |
+
}
|
| 240 |
+
""")
|
| 241 |
+
|
| 242 |
+
return result
|
| 243 |
+
|
| 244 |
+
except Exception as e:
|
| 245 |
+
if self.debug:
|
| 246 |
+
print(f"❌ Interactive method failed: {e}")
|
| 247 |
+
return None
|
| 248 |
+
|
| 249 |
+
async def check_multiple_destinations(self, passport_country: str, destinations: List[str], delay: float = 2.0) -> Dict:
|
| 250 |
+
"""
|
| 251 |
+
Check visa requirements for multiple destinations
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
passport_country: Two-letter country code for passport
|
| 255 |
+
destinations: List of two-letter country codes for destinations
|
| 256 |
+
delay: Delay between requests in seconds
|
| 257 |
+
|
| 258 |
+
Returns:
|
| 259 |
+
Dictionary mapping destination codes to visa information
|
| 260 |
+
"""
|
| 261 |
+
results = {}
|
| 262 |
+
|
| 263 |
+
for i, dest in enumerate(destinations, 1):
|
| 264 |
+
print(f"\n[{i}/{len(destinations)}] Checking {passport_country.upper()} → {dest.upper()}...")
|
| 265 |
+
|
| 266 |
+
# Try API method first
|
| 267 |
+
result = await self.check_visa_requirement_browser(passport_country, dest)
|
| 268 |
+
|
| 269 |
+
# If API fails, try interactive method
|
| 270 |
+
if not result:
|
| 271 |
+
result = await self.check_visa_interactive(passport_country, dest)
|
| 272 |
+
|
| 273 |
+
if result:
|
| 274 |
+
results[dest] = result
|
| 275 |
+
text = result.get('text', 'No text available')
|
| 276 |
+
print(f" ✅ Result: {text}")
|
| 277 |
+
else:
|
| 278 |
+
results[dest] = None
|
| 279 |
+
print(f" ❌ Failed to get result")
|
| 280 |
+
|
| 281 |
+
# Rate limiting
|
| 282 |
+
if i < len(destinations):
|
| 283 |
+
print(f" ⏳ Waiting {delay} seconds...")
|
| 284 |
+
await asyncio.sleep(delay)
|
| 285 |
+
|
| 286 |
+
return results
|
| 287 |
+
|
| 288 |
+
def format_result(self, result: Dict) -> str:
|
| 289 |
+
"""Format a single result for display"""
|
| 290 |
+
if not result:
|
| 291 |
+
return "No information available"
|
| 292 |
+
|
| 293 |
+
text = result.get('text', 'N/A')
|
| 294 |
+
dest = result.get('dest', 'N/A')
|
| 295 |
+
passport = result.get('pass', 'N/A')
|
| 296 |
+
|
| 297 |
+
return f"{passport.upper()} → {dest.upper()}: {text}"
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
async def main():
|
| 301 |
+
"""Main function to demonstrate usage"""
|
| 302 |
+
print("="*60)
|
| 303 |
+
print(" Passport Index Visa Checker (Playwright)")
|
| 304 |
+
print("="*60)
|
| 305 |
+
|
| 306 |
+
async with PassportIndexVisaScraper(debug=True) as scraper:
|
| 307 |
+
# Initialize session
|
| 308 |
+
if not await scraper.initialize_session():
|
| 309 |
+
print("❌ Failed to initialize session")
|
| 310 |
+
return
|
| 311 |
+
|
| 312 |
+
print("\n" + "="*60)
|
| 313 |
+
print(" Testing visa requirements...")
|
| 314 |
+
print("="*60)
|
| 315 |
+
|
| 316 |
+
# Test single visa requirement
|
| 317 |
+
print("\n📍 Single visa check: US → GB")
|
| 318 |
+
print("-" * 40)
|
| 319 |
+
result = await scraper.check_visa_requirement_browser('us', 'gb')
|
| 320 |
+
if result:
|
| 321 |
+
print(f"Result: {scraper.format_result(result)}")
|
| 322 |
+
else:
|
| 323 |
+
print("Trying interactive method...")
|
| 324 |
+
result = await scraper.check_visa_interactive('us', 'gb')
|
| 325 |
+
if result:
|
| 326 |
+
print(f"Result: {scraper.format_result(result)}")
|
| 327 |
+
|
| 328 |
+
# Test multiple destinations
|
| 329 |
+
print("\n📍 Multiple destinations for US passport:")
|
| 330 |
+
print("-" * 40)
|
| 331 |
+
destinations = ['ca', 'mx', 'jp', 'au'] # Canada, Mexico, Japan, Australia
|
| 332 |
+
results = await scraper.check_multiple_destinations('us', destinations, delay=2.0)
|
| 333 |
+
|
| 334 |
+
print("\n📊 Summary:")
|
| 335 |
+
for dest, result in results.items():
|
| 336 |
+
if result:
|
| 337 |
+
print(f" ✅ {scraper.format_result(result)}")
|
| 338 |
+
else:
|
| 339 |
+
print(f" ❌ US → {dest.upper()}: Failed")
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
COUNTRY_CODES = {
|
| 343 |
+
'af': 'Afghanistan', 'al': 'Albania', 'dz': 'Algeria', 'ad': 'Andorra',
|
| 344 |
+
'ao': 'Angola', 'ag': 'Antigua and Barbuda', 'ar': 'Argentina', 'am': 'Armenia',
|
| 345 |
+
'au': 'Australia', 'at': 'Austria', 'az': 'Azerbaijan', 'bs': 'Bahamas',
|
| 346 |
+
'bh': 'Bahrain', 'bd': 'Bangladesh', 'bb': 'Barbados', 'by': 'Belarus',
|
| 347 |
+
'be': 'Belgium', 'bz': 'Belize', 'bj': 'Benin', 'bt': 'Bhutan',
|
| 348 |
+
'bo': 'Bolivia', 'ba': 'Bosnia and Herzegovina', 'bw': 'Botswana', 'br': 'Brazil',
|
| 349 |
+
'bn': 'Brunei', 'bg': 'Bulgaria', 'bf': 'Burkina Faso', 'bi': 'Burundi',
|
| 350 |
+
'kh': 'Cambodia', 'cm': 'Cameroon', 'ca': 'Canada', 'cv': 'Cape Verde',
|
| 351 |
+
'cf': 'Central African Republic', 'td': 'Chad', 'cl': 'Chile', 'cn': 'China',
|
| 352 |
+
'co': 'Colombia', 'km': 'Comoros', 'cg': 'Congo', 'cr': 'Costa Rica',
|
| 353 |
+
'hr': 'Croatia', 'cu': 'Cuba', 'cy': 'Cyprus', 'cz': 'Czech Republic',
|
| 354 |
+
'dk': 'Denmark', 'dj': 'Djibouti', 'dm': 'Dominica', 'do': 'Dominican Republic',
|
| 355 |
+
'ec': 'Ecuador', 'eg': 'Egypt', 'sv': 'El Salvador', 'gq': 'Equatorial Guinea',
|
| 356 |
+
'er': 'Eritrea', 'ee': 'Estonia', 'et': 'Ethiopia', 'fj': 'Fiji',
|
| 357 |
+
'fi': 'Finland', 'fr': 'France', 'ga': 'Gabon', 'gm': 'Gambia',
|
| 358 |
+
'ge': 'Georgia', 'de': 'Germany', 'gh': 'Ghana', 'gr': 'Greece',
|
| 359 |
+
'gd': 'Grenada', 'gt': 'Guatemala', 'gn': 'Guinea', 'gw': 'Guinea-Bissau',
|
| 360 |
+
'gy': 'Guyana', 'ht': 'Haiti', 'hn': 'Honduras', 'hu': 'Hungary',
|
| 361 |
+
'is': 'Iceland', 'in': 'India', 'id': 'Indonesia', 'ir': 'Iran',
|
| 362 |
+
'iq': 'Iraq', 'ie': 'Ireland', 'il': 'Israel', 'it': 'Italy',
|
| 363 |
+
'jm': 'Jamaica', 'jp': 'Japan', 'jo': 'Jordan', 'kz': 'Kazakhstan',
|
| 364 |
+
'ke': 'Kenya', 'ki': 'Kiribati', 'kp': 'North Korea', 'kr': 'South Korea',
|
| 365 |
+
'kw': 'Kuwait', 'kg': 'Kyrgyzstan', 'la': 'Laos', 'lv': 'Latvia',
|
| 366 |
+
'lb': 'Lebanon', 'ls': 'Lesotho', 'lr': 'Liberia', 'ly': 'Libya',
|
| 367 |
+
'li': 'Liechtenstein', 'lt': 'Lithuania', 'lu': 'Luxembourg', 'mk': 'Macedonia',
|
| 368 |
+
'mg': 'Madagascar', 'mw': 'Malawi', 'my': 'Malaysia', 'mv': 'Maldives',
|
| 369 |
+
'ml': 'Mali', 'mt': 'Malta', 'mh': 'Marshall Islands', 'mr': 'Mauritania',
|
| 370 |
+
'mu': 'Mauritius', 'mx': 'Mexico', 'fm': 'Micronesia', 'md': 'Moldova',
|
| 371 |
+
'mc': 'Monaco', 'mn': 'Mongolia', 'me': 'Montenegro', 'ma': 'Morocco',
|
| 372 |
+
'mz': 'Mozambique', 'mm': 'Myanmar', 'na': 'Namibia', 'nr': 'Nauru',
|
| 373 |
+
'np': 'Nepal', 'nl': 'Netherlands', 'nz': 'New Zealand', 'ni': 'Nicaragua',
|
| 374 |
+
'ne': 'Niger', 'ng': 'Nigeria', 'no': 'Norway', 'om': 'Oman',
|
| 375 |
+
'pk': 'Pakistan', 'pw': 'Palau', 'pa': 'Panama', 'pg': 'Papua New Guinea',
|
| 376 |
+
'py': 'Paraguay', 'pe': 'Peru', 'ph': 'Philippines', 'pl': 'Poland',
|
| 377 |
+
'pt': 'Portugal', 'qa': 'Qatar', 'ro': 'Romania', 'ru': 'Russia',
|
| 378 |
+
'rw': 'Rwanda', 'kn': 'Saint Kitts and Nevis', 'lc': 'Saint Lucia',
|
| 379 |
+
'vc': 'Saint Vincent and the Grenadines', 'ws': 'Samoa', 'sm': 'San Marino',
|
| 380 |
+
'st': 'Sao Tome and Principe', 'sa': 'Saudi Arabia', 'sn': 'Senegal',
|
| 381 |
+
'rs': 'Serbia', 'sc': 'Seychelles', 'sl': 'Sierra Leone', 'sg': 'Singapore',
|
| 382 |
+
'sk': 'Slovakia', 'si': 'Slovenia', 'sb': 'Solomon Islands', 'so': 'Somalia',
|
| 383 |
+
'za': 'South Africa', 'es': 'Spain', 'lk': 'Sri Lanka', 'sd': 'Sudan',
|
| 384 |
+
'sr': 'Suriname', 'sz': 'Swaziland', 'se': 'Sweden', 'ch': 'Switzerland',
|
| 385 |
+
'sy': 'Syria', 'tw': 'Taiwan', 'tj': 'Tajikistan', 'tz': 'Tanzania',
|
| 386 |
+
'th': 'Thailand', 'tl': 'Timor-Leste', 'tg': 'Togo', 'to': 'Tonga',
|
| 387 |
+
'tt': 'Trinidad and Tobago', 'tn': 'Tunisia', 'tr': 'Turkey', 'tm': 'Turkmenistan',
|
| 388 |
+
'tv': 'Tuvalu', 'ug': 'Uganda', 'ua': 'Ukraine', 'ae': 'United Arab Emirates',
|
| 389 |
+
'gb': 'United Kingdom', 'us': 'United States', 'uy': 'Uruguay', 'uz': 'Uzbekistan',
|
| 390 |
+
'vu': 'Vanuatu', 've': 'Venezuela', 'vn': 'Vietnam', 'ye': 'Yemen',
|
| 391 |
+
'zm': 'Zambia', 'zw': 'Zimbabwe'
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
REVERSED_COUNTRY_CODES = {
|
| 395 |
+
'Afghanistan': 'af', 'Albania': 'al', 'Algeria': 'dz', 'Andorra': 'ad',
|
| 396 |
+
'Angola': 'ao', 'Antigua and Barbuda': 'ag', 'Argentina': 'ar', 'Armenia': 'am',
|
| 397 |
+
'Australia': 'au', 'Austria': 'at', 'Azerbaijan': 'az', 'Bahamas': 'bs',
|
| 398 |
+
'Bahrain': 'bh', 'Bangladesh': 'bd', 'Barbados': 'bb', 'Belarus': 'by',
|
| 399 |
+
'Belgium': 'be', 'Belize': 'bz', 'Benin': 'bj', 'Bhutan': 'bt',
|
| 400 |
+
'Bolivia': 'bo', 'Bosnia and Herzegovina': 'ba', 'Botswana': 'bw', 'Brazil': 'br',
|
| 401 |
+
'Brunei': 'bn', 'Bulgaria': 'bg', 'Burkina Faso': 'bf', 'Burundi': 'bi',
|
| 402 |
+
'Cambodia': 'kh', 'Cameroon': 'cm', 'Canada': 'ca', 'Cape Verde': 'cv',
|
| 403 |
+
'Central African Republic': 'cf', 'Chad': 'td', 'Chile': 'cl', 'China': 'cn',
|
| 404 |
+
'Colombia': 'co', 'Comoros': 'km', 'Congo': 'cg', 'Costa Rica': 'cr',
|
| 405 |
+
'Croatia': 'hr', 'Cuba': 'cu', 'Cyprus': 'cy', 'Czech Republic': 'cz',
|
| 406 |
+
'Denmark': 'dk', 'Djibouti': 'dj', 'Dominica': 'dm', 'Dominican Republic': 'do',
|
| 407 |
+
'Ecuador': 'ec', 'Egypt': 'eg', 'El Salvador': 'sv', 'Equatorial Guinea': 'gq',
|
| 408 |
+
'Eritrea': 'er', 'Estonia': 'ee', 'Ethiopia': 'et', 'Fiji': 'fj',
|
| 409 |
+
'Finland': 'fi', 'France': 'fr', 'Gabon': 'ga', 'Gambia': 'gm',
|
| 410 |
+
'Georgia': 'ge', 'Germany': 'de', 'Ghana': 'gh', 'Greece': 'gr',
|
| 411 |
+
'Grenada': 'gd', 'Guatemala': 'gt', 'Guinea': 'gn', 'Guinea-Bissau': 'gw',
|
| 412 |
+
'Guyana': 'gy', 'Haiti': 'ht', 'Honduras': 'hn', 'Hungary': 'hu',
|
| 413 |
+
'Iceland': 'is', 'India': 'in', 'Indonesia': 'id', 'Iran': 'ir',
|
| 414 |
+
'Iraq': 'iq', 'Ireland': 'ie', 'Israel': 'il', 'Italy': 'it',
|
| 415 |
+
'Jamaica': 'jm', 'Japan': 'jp', 'Jordan': 'jo', 'Kazakhstan': 'kz',
|
| 416 |
+
'Kenya': 'ke', 'Kiribati': 'ki', 'North Korea': 'kp', 'South Korea': 'kr',
|
| 417 |
+
'Kuwait': 'kw', 'Kyrgyzstan': 'kg', 'Laos': 'la', 'Latvia': 'lv',
|
| 418 |
+
'Lebanon': 'lb', 'Lesotho': 'ls', 'Liberia': 'lr', 'Libya': 'ly',
|
| 419 |
+
'Liechtenstein': 'li', 'Lithuania': 'lt', 'Luxembourg': 'lu', 'Macedonia': 'mk',
|
| 420 |
+
'Madagascar': 'mg', 'Malawi': 'mw', 'Malaysia': 'my', 'Maldives': 'mv',
|
| 421 |
+
'Mali': 'ml', 'Malta': 'mt', 'Marshall Islands': 'mh', 'Mauritania': 'mr',
|
| 422 |
+
'Mauritius': 'mu', 'Mexico': 'mx', 'Micronesia': 'fm', 'Moldova': 'md',
|
| 423 |
+
'Monaco': 'mc', 'Mongolia': 'mn', 'Montenegro': 'me', 'Morocco': 'ma',
|
| 424 |
+
'Mozambique': 'mz', 'Myanmar': 'mm', 'Namibia': 'na', 'Nauru': 'nr',
|
| 425 |
+
'Nepal': 'np', 'Netherlands': 'nl', 'New Zealand': 'nz', 'Nicaragua': 'ni',
|
| 426 |
+
'Niger': 'ne', 'Nigeria': 'ng', 'Norway': 'no', 'Oman': 'om',
|
| 427 |
+
'Pakistan': 'pk', 'Palau': 'pw', 'Panama': 'pa', 'Papua New Guinea': 'pg',
|
| 428 |
+
'Paraguay': 'py', 'Peru': 'pe', 'Philippines': 'ph', 'Poland': 'pl',
|
| 429 |
+
'Portugal': 'pt', 'Qatar': 'qa', 'Romania': 'ro', 'Russia': 'ru',
|
| 430 |
+
'Rwanda': 'rw', 'Saint Kitts and Nevis': 'kn', 'Saint Lucia': 'lc',
|
| 431 |
+
'Saint Vincent and the Grenadines': 'vc', 'Samoa': 'ws', 'San Marino': 'sm',
|
| 432 |
+
'Sao Tome and Principe': 'st', 'Saudi Arabia': 'sa', 'Senegal': 'sn',
|
| 433 |
+
'Serbia': 'rs', 'Seychelles': 'sc', 'Sierra Leone': 'sl', 'Singapore': 'sg',
|
| 434 |
+
'Slovakia': 'sk', 'Slovenia': 'si', 'Solomon Islands': 'sb', 'Somalia': 'so',
|
| 435 |
+
'South Africa': 'za', 'Spain': 'es', 'Sri Lanka': 'lk', 'Sudan': 'sd',
|
| 436 |
+
'Suriname': 'sr', 'Swaziland': 'sz', 'Sweden': 'se', 'Switzerland': 'ch',
|
| 437 |
+
'Syria': 'sy', 'Taiwan': 'tw', 'Tajikistan': 'tj', 'Tanzania': 'tz',
|
| 438 |
+
'Thailand': 'th', 'Timor-Leste': 'tl', 'Togo': 'tg', 'Tonga': 'to',
|
| 439 |
+
'Trinidad and Tobago': 'tt', 'Tunisia': 'tn', 'Turkey': 'tr', 'Turkmenistan': 'tm',
|
| 440 |
+
'Tuvalu': 'tv', 'Uganda': 'ug', 'Ukraine': 'ua', 'United Arab Emirates': 'ae',
|
| 441 |
+
'United Kingdom': 'gb', 'United States': 'us', 'Uruguay': 'uy', 'Uzbekistan': 'uz',
|
| 442 |
+
'Vanuatu': 'vu', 'Venezuela': 've', 'Vietnam': 'vn', 'Yemen': 'ye',
|
| 443 |
+
'Zambia': 'zm', 'Zimbabwe': 'zw'
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
if __name__ == "__main__":
|
| 447 |
+
asyncio.run(main())
|
server.py
CHANGED
|
@@ -8,7 +8,7 @@ import json
|
|
| 8 |
|
| 9 |
from app.util.gen_ai_base import GenAIBaseClient
|
| 10 |
from app.util.browser_agent import BrowserAgent
|
| 11 |
-
|
| 12 |
import sys
|
| 13 |
sys.stdout.reconfigure(line_buffering=True)
|
| 14 |
|
|
@@ -54,6 +54,27 @@ def create_app() -> Flask:
|
|
| 54 |
import traceback
|
| 55 |
traceback.print_exc()
|
| 56 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.route('/', methods=['GET'])
|
| 58 |
def hello_world():
|
| 59 |
return "Flask server is running.", 200
|
|
|
|
| 8 |
|
| 9 |
from app.util.gen_ai_base import GenAIBaseClient
|
| 10 |
from app.util.browser_agent import BrowserAgent
|
| 11 |
+
from app.util.visa_availability_scraper_playwright import PassportIndexVisaScraper
|
| 12 |
import sys
|
| 13 |
sys.stdout.reconfigure(line_buffering=True)
|
| 14 |
|
|
|
|
| 54 |
import traceback
|
| 55 |
traceback.print_exc()
|
| 56 |
return jsonify({"error": str(e)}), 500
|
| 57 |
+
|
| 58 |
+
@app.route('/visa-checker', methods=['POST'])
|
| 59 |
+
async def visa_checker():
|
| 60 |
+
raw = request.get_data(as_text=True)
|
| 61 |
+
body = json.loads(raw)
|
| 62 |
+
source = body.get('source')
|
| 63 |
+
destination = body.get('destination')
|
| 64 |
+
async with PassportIndexVisaScraper(debug=True) as scraper:
|
| 65 |
+
if not await scraper.initialize_session():
|
| 66 |
+
return jsonify({"error": "Failed to initialize session"}), 500
|
| 67 |
+
result = await scraper.check_visa_requirements(source, destination)
|
| 68 |
+
|
| 69 |
+
if result:
|
| 70 |
+
return jsonify(result), 200
|
| 71 |
+
else:
|
| 72 |
+
result = await scraper.check_visa_interactive(source, destination)
|
| 73 |
+
if result:
|
| 74 |
+
return jsonify(result), 200
|
| 75 |
+
else:
|
| 76 |
+
return jsonify({"error": "Failed to retrieve visa information"}), 500
|
| 77 |
+
|
| 78 |
@app.route('/', methods=['GET'])
|
| 79 |
def hello_world():
|
| 80 |
return "Flask server is running.", 200
|