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