-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract-all-data.js
More file actions
169 lines (141 loc) · 4.97 KB
/
extract-all-data.js
File metadata and controls
169 lines (141 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const https = require('https');
const http = require('http');
const { parse } = require('node-html-parser');
// Function to fetch HTML
function fetchHTML(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
}).on('error', reject);
});
}
// Function to extract options from select element
function extractOptions(html, selectName) {
const root = parse(html);
const select = root.querySelector(`select[name="${selectName}"]`);
if (!select) return [];
const options = select.querySelectorAll('option');
return options
.filter(opt => opt.getAttribute('value') !== '0' && opt.getAttribute('value') !== 'All')
.map(opt => ({
value: opt.getAttribute('value'),
label: opt.text.trim()
}));
}
// Function to submit form and get cascading dropdown options
async function getCascadingOptions(districtValue, talukValue = null, hobliValue = null) {
try {
// First, get the initial page to get ViewState
const initialHTML = await fetchHTML('https://landrecords.karnataka.gov.in/service3/');
const root = parse(initialHTML);
const viewState = root.querySelector('input[name="__VIEWSTATE"]')?.getAttribute('value') || '';
const eventValidation = root.querySelector('input[name="__EVENTVALIDATION"]')?.getAttribute('value') || '';
// Build form data
const formData = new URLSearchParams();
formData.append('__VIEWSTATE', viewState);
formData.append('__EVENTVALIDATION', eventValidation);
formData.append('__VIEWSTATEGENERATOR', '');
if (districtValue) {
formData.append('ddl_district', districtValue);
formData.append('__EVENTTARGET', 'ddl_district');
}
if (talukValue) {
formData.append('ddl_taluk', talukValue);
formData.append('__EVENTTARGET', 'ddl_taluk');
}
if (hobliValue) {
formData.append('ddl_hobli', hobliValue);
formData.append('__EVENTTARGET', 'ddl_hobli');
}
// Post request
const postData = formData.toString();
const options = {
hostname: 'landrecords.karnataka.gov.in',
path: '/service3/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
'User-Agent': 'Mozilla/5.0',
'Cookie': 'ASP.NET_SessionId=test'
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
});
req.on('error', reject);
req.write(postData);
req.end();
});
} catch (error) {
console.error('Error:', error);
return null;
}
}
// Main function to extract all data
async function extractAllData() {
console.log('Fetching initial page...');
const initialHTML = await fetchHTML('https://landrecords.karnataka.gov.in/service3/');
const districts = extractOptions(initialHTML, 'ddl_district');
console.log(`Found ${districts.length} districts`);
const allData = [];
for (const district of districts) {
console.log(`\nProcessing district: ${district.label} (${district.value})`);
const districtHTML = await getCascadingOptions(district.value);
if (!districtHTML) continue;
const taluks = extractOptions(districtHTML, 'ddl_taluk');
console.log(` Found ${taluks.length} taluks`);
const districtData = {
value: district.value,
label: district.label,
taluks: []
};
for (const taluk of taluks) {
console.log(` Processing taluk: ${taluk.label} (${taluk.value})`);
const talukHTML = await getCascadingOptions(district.value, taluk.value);
if (!talukHTML) continue;
const hoblis = extractOptions(talukHTML, 'ddl_hobli');
console.log(` Found ${hoblis.length} hoblis`);
const talukData = {
value: taluk.value,
label: taluk.label,
hoblis: hoblis.map(h => ({
value: h.value,
label: h.label,
villages: [] // Villages are text input, not dropdown
}))
};
districtData.taluks.push(talukData);
// Add delay to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 500));
}
allData.push(districtData);
// Add delay between districts
await new Promise(resolve => setTimeout(resolve, 1000));
}
return allData;
}
// Run extraction
extractAllData()
.then(data => {
const fs = require('fs');
fs.writeFileSync('extracted-data.json', JSON.stringify(data, null, 2));
console.log('\n✅ Data extraction complete! Saved to extracted-data.json');
console.log(`Total districts: ${data.length}`);
})
.catch(error => {
console.error('Error during extraction:', error);
});