Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wishlist] fetch data from HPD #12

Open
grischard opened this issue Sep 22, 2023 · 1 comment
Open

[wishlist] fetch data from HPD #12

grischard opened this issue Sep 22, 2023 · 1 comment

Comments

@grischard
Copy link

HPD has additional information on buildings.

const TOKEN_URL = 'https://mspwvw-hpdleov3.nyc.gov/authenticationservice/1.0/api/Apim/token';
const DATA_URL = 'https://mspwvw-hpdleov3.nyc.gov/hpdonline.api/1.0/api/building/search';

async function fetchToken() {
    try {
        let response = await fetch(TOKEN_URL, { method: 'POST', body: '' });
        let data = await response.json();
        return data.token;
    } catch (error) {
        console.error('Error fetching token:', error);
        throw error;
    }
}

async function fetchDataWithToken(bin, token) {
    try {
        let response = await fetch(DATA_URL, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'ApiKey': `Bearer ${token}`
            },
            body: JSON.stringify({
                bin: bin,
                isCountRequired: true
            })
        });
        let data = await response.json();
        return data;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

async function fetchInfo(bin) {
    try {
        let token = await fetchToken();
        let data = await fetchDataWithToken(bin, token);
        return data;
    } catch (error) {
        console.error('Error fetching info:', error);
        throw error;
    }
}

// Example usage
fetchInfo(3428938)
    .then(data => {
        console.log('Fetched Data:', data);
    })
    .catch(error => {
        console.error('Error:', error);
    });
@jmapb
Copy link
Owner

jmapb commented Sep 30, 2023

That token URL is not eager to give me a token ;)

image

Looks like HPD's internal APIs will deny access unless you've got an active session going.

Fortunately we can get a lot of the same info from NYC Open Data at https://data.cityofnewyork.us/Housing-Development/Buildings-Subject-to-HPD-Jurisdiction/kj4p-ruqc, eg https://data.cityofnewyork.us/resource/kj4p-ruqc.json?bin=3428938.

Unfortunately some of the fields returned by the internal hpdonline.api are missing from the open dataset: cityOwnedFlag, class, constructionType, mgmtProgOwnership, pin, yearBuilt.

Fortunately the open dataset does provide the building's HPD ID number, which we can use to craft a deep link into the HPD portal (eg https://hpdonline.nyc.gov/hpdonline/building/997424/overview) & find the data that way.

Unfortunately we aren't permitted to scrape this deep link directly, so offering the link to the user might be the best we can do :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants