const getSchools = () => { const { readFileSync } = require('fs'); const input = readFileSync('teams.csv', 'utf8'); const schoolsAsZip = []; return new Promise(res => { const csv = require('fast-csv'); csv.fromString(input, { headers: true }) .on('data', d => { schoolsAsZip.push({ team: d.team_name }); }) .on('end', () => { res(schoolsAsZip) }); }); }; const addCity = async () => { const searchUrl = 'https://mapi.petersons.com/api/v2/School'; const fetch = require('node-fetch'); const result = await fetch(searchUrl, { headers: { 'Content-Type': 'application/json' , 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', 'Referrer': 'https://www.petersons.com/search/schools?searchtype=12&page=1&result=15&searchterm=creighton', 'Petersons-Subscription-key': 'rZYXDqtcS2fkdItzl4o0eVNjunxQpZLPiZ8+LcFNi//OJVljl2pYsFx9gflQMURGNfhh4UwzoNokWX9ETjm/TuAGiUPJBCX+l/rgOtXvG2WUYRkfXFOYGZNYHkgndGC8zYszcSw5VRnzyqnAN7BidfmjsenSe/TdgboBo6EqWE+dzLQHd42ovQv3GxQGieAj' }, method: 'POST', body: JSON.stringify({ pageNumber: 1, resultsPerPage: 15, criteria: [], searchTerm: null, enhancedProfilesOnly: false, checkSpelling: false, userIpAddress: '174.70.57.3', searchterm: 'creighton', sessionId: 'fkaryk2ku2tiq3ndsxdrubcz', searchType: 12 }) // {"pageNumber":1,"resultsPerPage":15,"criteria":[],"searchTerm":null,"enhancedProfilesOnly":false,"checkSpelling":false,"searchType":"12","userEmail":"","userIpAddress":"174.70.57.3","sessionId":"fkaryk2ku2tiq3ndsxdrubcz","searchterm":"creighton"} }); console.log(result); //const asJson = result.json(); } const getCity = async (college) => { const collegeAsUri = college.replace('\'', '').replace('(', '').replace(')', ''); console.log('getting college', collegeAsUri); const cmd = `curl 'https://mapi.petersons.com/api/v2/School' -H 'Pragma: no-cache' -H 'Origin: https://www.petersons.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'Referer: https://www.petersons.com/search/schools?searchtype=12&page=1&result=15&searchterm=vermont&criteria=1-collegeTypes:traditional;2-degreeTypes:bachelors;6-studentPopulation:large,verylarge,midsize,small' -H 'Connection: keep-alive' -H 'Petersons-Subscription-key: rZYXDqtcS2fkdItzl4o0eVNjunxQpZLPiZ8+LcFNi//OJVljl2pYsFx9gflQMURGNfhh4UwzoNokWX9ETjm/TuAGiUPJBCX+l/rgOtXvG2WUYRkfXFOYGZNYHkgndGC8zYszcSw5VRnzyqnAN7BidfmjsenSe/TdgboBo6EqWE+dzLQHd42ovQv3GxQGieAj' --data-binary '{"pageNumber":1,"resultsPerPage":15,"criteria":[{"filterType":1,"searchCriteria":"includeMissing:false|collegeTypes:traditional"},{"filterType":2,"searchCriteria":"includeMissing:false|degreeTypes:bachelors"},{"filterType":6,"searchCriteria":"includeMissing:false|studentPopulation:large,verylarge,midsize,small"}],"searchTerm":null,"enhancedProfilesOnly":false,"checkSpelling":false,"searchType":"12","userEmail":"","userIpAddress":"174.70.57.3","sessionId":"fkaryk2ku2tiq3ndsxdrubcz","searchterm":"${collegeAsUri}"}' --compressed` const { exec } = require('child_process'); return new Promise((res, rej) => { exec(cmd, {}, (err, stdout) => { if (err) { rej(err); } else { const results = JSON.parse(stdout); if (results.Results.length > 0) { const firstResult = results.Results[0]; const geoData = { id: college, fullSchoolName: firstResult.SchoolName, city: firstResult.SchoolCity, state: firstResult.StateCode, stateName: firstResult.StateName, zip: firstResult.ZipCode }; res(geoData); } else { res({ id: college }); } } }); }); }; const getFips = async({ city, stateName, zip }) => { const mappings = { 'alabama': 'US01', 'alaska': 'US02', 'arizona': 'US04', 'arkansas': 'US05', 'california': 'US06', 'colorado': 'US08', 'connecticut': 'US09', 'delaware': 'US10', 'district of columbia': 'US11', 'florida': 'US12', 'georgia': 'US13', 'guantanamo bay': 'US', 'hawaii': 'US15', 'idaho': 'US16', 'illinois': 'US17', 'indiana': 'US18', 'iowa': 'US19', 'kansas': 'US20', 'kentucky': 'US21', 'louisiana': 'US22', 'maine': 'US23', 'maryland': 'US24', 'massachusetts': 'US25', 'michigan': 'US26', 'minnesota': 'US27', 'mississippi': 'US28', 'missouri': 'US29', 'montana': 'US30', 'nebraska': 'US31', 'nevada': 'US32', 'new hampshire': 'US33', 'new jersey': 'US34', 'new mexico': 'US35', 'new york': 'US36', 'north carolina': 'US37', 'north dakota': 'US38', 'ohio': 'US39', 'oklahoma': 'US40', 'oregon': 'US41', 'pennsylvania': 'US42', 'rhode island': 'US44', 'south carolina': 'US45', 'south dakota': 'US46', 'tennessee': 'US47', 'texas': 'US48', 'utah': 'US49', 'vermont': 'US50', 'virginia': 'US51', 'washington': 'US53', 'west virginia': 'US54', 'wisconsin': 'US55', 'wyoming': 'US56' }; if (!city) { return null; } const fetch = require('node-fetch'); const FormData = require('form-data'); const fipsSearchUrl = 'http://gis.pongworld.com/lookup.php?searchtype=usfips'; const form = new FormData(); const stateKey = mappings[stateName.toLowerCase()]; console.log('state key', stateKey); form.append('searchtype', 'usplace'); form.append('search', 'Go'); form.append('placename', city); form.append('admin', stateKey); const result = await fetch(fipsSearchUrl, { method: 'POST', body: form}); const txt = await result.text(); const cheerio = require('cheerio'); const $ = cheerio.load(txt); const fips = $('table[align] tr:nth-of-type(2) td:nth-of-type(2)'); if (fips.length == 1) { const leftPad = require('left-pad'); const paddedFips = `${stateKey.substring(2)}${leftPad(fips.text(), 3, '0')}`; return paddedFips; } else { return null; } }; const run = async() => { const schools = await getSchools(); const schoolsWithFips = []; for (const school of schools) { const city = await getCity(school.team); const fips = await getFips(city); console.log(school.team, fips) schoolsWithFips.push({ team: school.team, city: city.city, state: city.stateName, fips }); } const csv = require('fast-csv'); return new Promise(res => { csv.writeToPath('schools-with-fips.csv', schoolsWithFips, { headers: true }) .on('finish', () => { res(); }); }); }; run();