Skip to content

Instantly share code, notes, and snippets.

@curran
Created August 12, 2024 15:07
Show Gist options
  • Save curran/5e8282318578da1465b02519b3119b84 to your computer and use it in GitHub Desktop.
Save curran/5e8282318578da1465b02519b3119b84 to your computer and use it in GitHub Desktop.
BLS Unemployment Scraper
const fetch = require('node-fetch');
const fs = require('fs');
const { PrettyTable } = require('prettytable');
// Sample series IDs and years
const seriesIds = ['CUUR0000SA0', 'SUUR0000SA0'];
const startYear = '2011';
const endYear = '2014';
async function fetchBLSData() {
const url = 'https://api.bls.gov/publicAPI/v1/timeseries/data/';
const headers = { 'Content-Type': 'application/json' };
const body = JSON.stringify({
seriesid: seriesIds,
startyear: startYear,
endyear: endYear,
});
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: body,
});
const jsonData = await response.json();
return jsonData.Results.series;
} catch (error) {
console.error('Error fetching data from BLS API:', error);
return [];
}
}
function writeToFile(seriesId, table) {
const output = table.toString();
fs.writeFileSync(`${seriesId}.txt`, output, 'utf8');
}
function createPrettyTable(series) {
const table = new PrettyTable({
fieldNames: ['series id', 'year', 'period', 'value', 'footnotes'],
});
series.data.forEach((item) => {
const { year, period, value, footnotes } = item;
let footnotesText = '';
footnotes.forEach((footnote) => {
if (footnote) {
footnotesText += footnote.text + ',';
}
});
if ('M01' <= period && period <= 'M12') {
table.addRow({
'series id': series.seriesID,
year,
period,
value,
footnotes: footnotesText.slice(0, -1),
});
}
});
return table;
}
async function main() {
const seriesData = await fetchBLSData();
seriesData.forEach((series) => {
const table = createPrettyTable(series);
writeToFile(series.seriesID, table);
});
console.log('Files created successfully.');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment