2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
|
|
|
|
export class OpenWeatherMap implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'OpenWeatherMap',
|
|
|
|
name: 'openWeatherMap',
|
|
|
|
icon: 'fa:sun',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Gets current and future weather information',
|
2019-06-23 03:35:23 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'OpenWeatherMap',
|
|
|
|
color: '#554455',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'openWeatherMapApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Current Weather',
|
|
|
|
value: 'currentWeather',
|
|
|
|
description: 'Returns the current weather data',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return current weather data',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: '5 Day Forecast',
|
2019-06-23 03:35:23 -07:00
|
|
|
value: '5DayForecast',
|
|
|
|
description: 'Returns the weather data for the next 5 days',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Return weather data for the next 5 days',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'currentWeather',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Format',
|
|
|
|
name: 'format',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Imperial',
|
|
|
|
value: 'imperial',
|
|
|
|
description: 'Fahrenheit | miles/hour',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Metric',
|
|
|
|
value: 'metric',
|
|
|
|
description: 'Celsius | meter/sec',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Scientific',
|
|
|
|
value: 'standard',
|
|
|
|
description: 'Kelvin | meter/sec',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'metric',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The format in which format the data should be returned',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// Location Information
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Location Selection',
|
|
|
|
name: 'locationSelection',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'City Name',
|
|
|
|
value: 'cityName',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'City ID',
|
|
|
|
value: 'cityId',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Coordinates',
|
|
|
|
value: 'coordinates',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Zip Code',
|
|
|
|
value: 'zipCode',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'cityName',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'How to define the location for which to return the weather',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'City',
|
|
|
|
name: 'cityName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'berlin,de',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
locationSelection: [
|
|
|
|
'cityName',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The name of the city to return the weather of',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'City ID',
|
|
|
|
name: 'cityId',
|
|
|
|
type: 'number',
|
|
|
|
default: 160001123,
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
locationSelection: [
|
|
|
|
'cityId',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The ID of city to return the weather of. List can be downloaded here: http://bulk.openweathermap.org/sample/.',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'Latitude',
|
|
|
|
name: 'latitude',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '13.39',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
locationSelection: [
|
|
|
|
'coordinates',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The latitude of the location to return the weather of',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'Longitude',
|
|
|
|
name: 'longitude',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '52.52',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
locationSelection: [
|
|
|
|
'coordinates',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The longitude of the location to return the weather of',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
displayName: 'Zip Code',
|
|
|
|
name: 'zipCode',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: '10115,de',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
locationSelection: [
|
|
|
|
'zipCode',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The ID of city to return the weather of. List can be downloaded here: http://bulk.openweathermap.org/sample/.',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
2020-10-07 23:40:22 -07:00
|
|
|
{
|
|
|
|
displayName: 'Language',
|
|
|
|
name: 'language',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'en',
|
|
|
|
description: 'The two letter language code to get your output in (eg. en, de, ...).',
|
|
|
|
},
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('openWeatherMapApi');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
let endpoint = '';
|
|
|
|
let locationSelection;
|
2020-10-07 23:40:22 -07:00
|
|
|
let language;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
let qs: IDataObject;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Set base data
|
|
|
|
qs = {
|
|
|
|
APPID: credentials.accessToken,
|
|
|
|
units: this.getNodeParameter('format', i) as string,
|
|
|
|
};
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Get the location
|
|
|
|
locationSelection = this.getNodeParameter('locationSelection', i) as string;
|
|
|
|
if (locationSelection === 'cityName') {
|
|
|
|
qs.q = this.getNodeParameter('cityName', i) as string;
|
|
|
|
} else if (locationSelection === 'cityId') {
|
|
|
|
qs.id = this.getNodeParameter('cityId', i) as number;
|
|
|
|
} else if (locationSelection === 'coordinates') {
|
|
|
|
qs.lat = this.getNodeParameter('latitude', i) as string;
|
|
|
|
qs.lon = this.getNodeParameter('longitude', i) as string;
|
|
|
|
} else if (locationSelection === 'zipCode') {
|
|
|
|
qs.zip = this.getNodeParameter('zipCode', i) as string;
|
|
|
|
} else {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The locationSelection "${locationSelection}" is not known!`, { itemIndex: i });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Get the language
|
|
|
|
language = this.getNodeParameter('language', i) as string;
|
|
|
|
if (language) {
|
|
|
|
qs.lang = language;
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'currentWeather') {
|
|
|
|
// ----------------------------------
|
|
|
|
// currentWeather
|
|
|
|
// ----------------------------------
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = 'weather';
|
|
|
|
} else if (operation === '5DayForecast') {
|
|
|
|
// ----------------------------------
|
|
|
|
// 5DayForecast
|
|
|
|
// ----------------------------------
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = 'forecast';
|
|
|
|
} else {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`, { itemIndex: i });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method: 'GET',
|
|
|
|
qs,
|
|
|
|
uri: `https://api.openweathermap.org/data/2.5/${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
try {
|
|
|
|
responseData = await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
2021-04-16 09:33:36 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({json:{ error: error.message }});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|