Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ea24154

Browse filesBrowse files
committed
dev: add WeatherService
- adds weather service - updates IdentificationService to add ip_user property
1 parent 29dbf3a commit ea24154
Copy full SHA for ea24154

File tree

3 files changed

+98
-0
lines changed
Filter options

3 files changed

+98
-0
lines changed

‎src/backend/src/modules/external-extras/ExternalExtrasModule.js

Copy file name to clipboardExpand all lines: src/backend/src/modules/external-extras/ExternalExtrasModule.js
+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class ExternalExtrasModule extends AdvancedBase {
1313
const { NewsDataService } = require('./NewsDataService');
1414
services.registerService('newsdata', NewsDataService);
1515
}
16+
if ( !! config?.services?.weather ) {
17+
const { WeatherService } = require('./WeatherService');
18+
services.registerService('weather', WeatherService);
19+
}
1620
}
1721
}
1822

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const BaseService = require('../../services/BaseService');
2+
const { Context } = require('../../util/context');
3+
4+
class WeatherService extends BaseService {
5+
async ['__on_driver.register.interfaces'] () {
6+
const svc_registry = this.services.get('registry');
7+
const col_interfaces = svc_registry.get('interfaces');
8+
9+
const common = {
10+
default_parameter: 'q',
11+
parameters: {
12+
'*': {
13+
type: 'json',
14+
},
15+
},
16+
result: {
17+
type: 'json'
18+
},
19+
};
20+
21+
col_interfaces.set('weather', {
22+
description: 'weatherapi.com',
23+
methods: {
24+
weather: {
25+
description: 'Report current weather in the specified location',
26+
...common,
27+
},
28+
forecast: {
29+
description: 'Report the weather forecast for the specified location',
30+
...common,
31+
},
32+
}
33+
});
34+
}
35+
async _init () {
36+
this.baseURL = 'https://api.weatherapi.com/v1';
37+
}
38+
static IMPLEMENTS = {
39+
weather: {
40+
// We call this "weather" instead of "current" so that it can be
41+
// the default method for the weather driver.
42+
async weather (parameters) {
43+
return await this.general('current.json', parameters);
44+
},
45+
async forecast (parameters) {
46+
// Okay this is kinda dumb but the default behavior for forecast
47+
// is that it forecasts 1 day - the current day. I'm going to make
48+
// the default 5 days here because I think that's what most people
49+
// will expect.
50+
51+
if ( parameters.days === undefined ) {
52+
parameters.days = 5;
53+
}
54+
55+
return await this.general('forecast.json', parameters);
56+
}
57+
}
58+
}
59+
60+
async general (component, parameters) {
61+
const require = this.require;
62+
63+
const axios = require('axios');
64+
const querystring = require('querystring');
65+
66+
if ( ! parameters.q ) {
67+
const requester = Context.get('requester');
68+
parameters.q = requester.ip_user ??
69+
'-77.6776746,165.2019492'; // McMurdo Station, Antarctica
70+
}
71+
72+
const qstr = querystring.stringify({
73+
...parameters,
74+
key: this.config.apiKey,
75+
});
76+
77+
const req_options = {
78+
method: 'GET',
79+
url: this.baseURL + `/${component}?` + qstr,
80+
};
81+
82+
console.log('debug the request', req_options);
83+
84+
const resp = await axios.request(req_options);
85+
86+
return resp.data;
87+
}
88+
}
89+
90+
module.exports = {
91+
WeatherService,
92+
};

‎src/backend/src/services/abuse-prevention/IdentificationService.js

Copy file name to clipboardExpand all lines: src/backend/src/services/abuse-prevention/IdentificationService.js
+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Requester {
5757
ua: req.headers['user-agent'],
5858
ip: req.connection.remoteAddress,
5959
ip_forwarded: req.headers['x-forwarded-for'],
60+
ip_user: req.headers['x-forwarded-for'] ||
61+
req.connection.remoteAddress,
6062
origin: req.headers['origin'],
6163
referer: req.headers['referer'],
6264
referer_origin,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.