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

Add south korea #1090

Merged
merged 17 commits into from
Mar 19, 2024
5 changes: 3 additions & 2 deletions src/adapters/acumar.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ async function getPollutionData(
let results = [];

try {
const response = await client(station.url);
const $ = load(response.body);
const response = await client({ url: station.url, responseType: 'text'});

const $ = load(response);

if (dateLuxon && hourLuxon) {
const firstDataRowIndex = $('table')
Expand Down
11 changes: 3 additions & 8 deletions src/adapters/adairquality.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,9 @@ const stations = [
export function fetchData(source, cb) {
const requests = stations.map((station) => {
return (done) => {
client(`${source.url}${station.slug}`)
.then((response) => {
if (response.statusCode !== 200) {
return done({
message: `Failure to load data url (${source.url}${station.slug})`,
});
}
let data = Object.assign(station, { body: response.body });
client({ url: `${source.url}${station.slug}` })
.then((body) => {
let data = Object.assign(station, { body });
return done(null, data);
})
.catch((err) => {
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/air4thai.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export const name = 'air4thai';
*/

export function fetchData (source, cb) {
client(source.url)
.then((response) => {
const data = JSON.parse(response.body);
client({ url: source.url })
.then((data) => {

const formattedData = formatData(data);

if (formattedData === undefined) {
Expand Down
11 changes: 5 additions & 6 deletions src/adapters/airnow-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ async function getLocations(url) {
const locationsUrl = `${url}airnow/today/monitoring_site_locations.dat`;
log.verbose(`Fetching AirNow locations from "${locationsUrl}"`);

const locationsData = await client(locationsUrl, {
responseType: 'text',
});
_locationsStream[url] = StringStream.from(locationsData.body)
const locationsData = await client({ url: locationsUrl, responseType: 'text' });

_locationsStream[url] = StringStream.from(locationsData)
.lines('\n')
.parse((s) => {
s = s.split('|');
Expand Down Expand Up @@ -91,9 +90,9 @@ export async function fetchStream(source) {

log.info(`Fetching AirNow measurements from "${url}"`);

const response = await client(url, { responseType: 'text' });
const body = await client({ url, responseType: 'text' });

return StringStream.from(response.body)
return StringStream.from(body)
.lines('\n')
.map(async (m) => {
try {
Expand Down
66 changes: 36 additions & 30 deletions src/adapters/arpalazio.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DateTime } from 'luxon';
const { StringStream, MultiStream } = sj;

const getter = got.extend({ timeout: { request: REQUEST_TIMEOUT } });
import client from '../lib/requests.js';

const timezone = 'Europe/Rome';

Expand All @@ -34,18 +35,25 @@ const hourlyParameters = difference(

export const name = 'arpalazio';

export async function fetchStream(source) {
const response = await getter.get(source.url, {
responseType: 'text',
});

if (response.statusCode !== 200) {
throw new FetchError(DATA_URL_ERROR, source, null);
export async function fetchData (source, cb) {
try {
const stream = await fetchStream(source, cb);
const measurements = await stream.toArray();
cb(null, { name: stream.name, measurements });
} catch (e) {
cb({ message: `fetchData error: ${e.message}` });
}
}


async function fetchStream(source, cb) {

const body = await client({ url: source.url, responseType: 'text' });

let $;
try {
$ = cheerio.load(response.body);
$ = cheerio.load(body);
} catch (e) {
throw new FetchError(DATA_PARSE_ERROR, source, e);
}
Expand Down Expand Up @@ -73,12 +81,12 @@ export async function fetchStream(source) {
)
);
out.add(
await handleProvince(
province.name,
provinceDailyURL,
dailyAvgPeriod,
source
)
await handleProvince(
province.name,
provinceDailyURL,
dailyAvgPeriod,
source
)
);
});

Expand All @@ -91,24 +99,22 @@ const handleProvince = async function (
averagingPeriod,
source
) {
const response = await getter.get(url, { responseType: 'text' });

if (response.statusCode !== 200) {
throw new FetchError(DATA_PARSE_ERROR, source, null);
}
log.verbose(`Loading province information from ${url}`);

const $ = cheerio.load(response.body);
const pollutantURLs = $('a')
.map(function () {
const pollutant = $(this).text().toLowerCase().replace('.', '');
const currentParameters = getParameters(averagingPeriod);
if (currentParameters.indexOf(pollutant) >= 0) {
const href = $(this).attr('href');
return `${baseUrl}${href}`;
}
})
.get();
const body = await client({ url, responseType: 'text' });

const $ = cheerio.load(body);
const pollutantURLs = $('a')
.map(function () {
const pollutant = $(this).text().toLowerCase().replace('.', '');
const currentParameters = getParameters(averagingPeriod);
if (currentParameters.indexOf(pollutant) >= 0) {
const href = $(this).attr('href');
return `${baseUrl}${href}`;
} else {
return null;
}
})
.get();

const arrayOfPromises = pollutantURLs.map((dataUrl) =>
getStream(name, dataUrl, averagingPeriod, source, url)
Expand Down
15 changes: 7 additions & 8 deletions src/adapters/au_act.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ export function fetchData(source, cb) {
.minus({ days: 1 })
.toFormat("yyyy-LL-dd'T'HH:mm:ss");

client(source.url, {
searchParams: {
$query: `select *, :id where (\`datetime\` > '${timeAgo}') order by \`datetime\` desc limit 1000`,
},
})
.then((res) => {
const body = res.body;

const params = {
$query: `select *, :id where (\`datetime\` > '${timeAgo}') order by \`datetime\` desc limit 1000`,
};

client({ url: source.url, params })
.then((body) => {
try {
const data = formatData(JSON.parse(body), source);
const data = formatData(body, source);
if (data === undefined) {
return cb({ message: 'Failure to parse data.' });
}
Expand Down
10 changes: 4 additions & 6 deletions src/adapters/buenos-aires.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const timezone = 'America/Argentina/Buenos_Aires';
export const name = 'buenos-aires';

export function fetchData(source, callback) {
client(source.url)
.then((response) => {
const body = response.body;
client({ url: source.url, responseType: 'text' })
.then((body) => {
let tasks = [];
let $ = load(body);

Expand Down Expand Up @@ -100,9 +99,8 @@ const makeStationQuery = (sourceUrl, station, parameter, date) => {

const handleStation = (url, station, parameter, today) => {
return (done) => {
client(url)
.then((response) => {
const body = response.body;
client({ url, responseType: 'text'})
.then((body) => {
const results = formatData(body, station, parameter, today);
return done(null, results);
})
Expand Down
5 changes: 2 additions & 3 deletions src/adapters/canterbury.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ export function fetchData(source, cb) {
.replace('$date', date);

return function (cb) {
client(url)
.then((response) => {
let body = JSON.parse(response.body);
client({ url })
.then((body) => {
body = body.data.item[body.data.item.length - 1]; // get the last item in the array
cb(null, [body, stations[key]]);
})
Expand Down
15 changes: 2 additions & 13 deletions src/adapters/catalonia.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ export async function fetchData(source, cb) {
const fetchURL = source.url;

try {
const response = await client(fetchURL);
const body = response.body;

if (response.statusCode !== 200) {
return cb({ message: 'Failure to load data url.' });
}
const body = await client({ url: fetchURL });

const data = formatData(body);
if (data === undefined) {
Expand All @@ -29,17 +24,11 @@ export async function fetchData(source, cb) {

cb(null, data);
} catch (error) {
console.error('Error fetching data:', error);
return cb({ message: 'Unknown adapter error.' });
cb({ message: `fetchData error: ${error.message}` });
}
}

function formatData(data) {
try {
data = JSON.parse(data);
} catch (e) {
return undefined;
}

const aqRepack = (item) => {
let aq = [];
Expand Down
17 changes: 7 additions & 10 deletions src/adapters/chile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ export function fetchData (source, cb) {
var sources = [source.url, 'http://sinca.mma.gob.cl/index.php/json/listado'];
var tasks = [];

_.forEach(sources, function (e) {
_.forEach(sources, function (url) {
var task = function (cb) {
client(e)
.then((response) => {
if (response.statusCode !== 200) {
return cb(response);
}
cb(null, response.body);
client({ url })
.then((body) => {
cb(null, body);
})
.catch((error) => {
cb(error);
});
};

tasks.push(task);
});

Expand Down Expand Up @@ -75,8 +72,8 @@ export function fetchData (source, cb) {
*/
const formatData = function (results) {
try {
var data = JSON.parse(results[0]);
var meta = JSON.parse(results[1]);
var data = results[0];
var meta = results[1];
} catch (e) {
return undefined;
}
Expand Down
5 changes: 2 additions & 3 deletions src/adapters/cyprus.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ export const name = 'cyprus';
*/

export function fetchData(source, cb) {
client(source.url)
.then((response) => {
const data = JSON.parse(response.body);
client({ url: source.url })
.then((data) => {
const formattedData = formatData(data);

if (formattedData === undefined) {
Expand Down
19 changes: 9 additions & 10 deletions src/adapters/defra.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import log from '../lib/logger.js';
import client from '../lib/requests.js';
export const name = 'defra';

export function fetchData(source, cb) {
client(source, cb).then((response) => {
export async function fetchData(source, cb) {
try {
const data = formatData(source, response.body);
if (data === undefined) {
return cb({ message: 'Failure to parse data.' });
} else {
return cb(null, data);
}
const body = await client({ url: source.url, responseType: 'text'});
const data = formatData(source, body);
if (data === undefined) {
return cb({ message: 'Failure to parse data.' });
} else {
return cb(null, data);
}
} catch (e) {
return cb({ message: 'Unknown adapter error.' });
return cb({ message: e.message });
}
});
}

let formatData = function (source, data) {
Expand Down
Loading
Loading