-
Notifications
You must be signed in to change notification settings - Fork 6
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
Feature: Added celsius support for weather #19
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,22 @@ | |
var weather = require("weather-js"); | ||
|
||
module.exports = function getWeather(api, threadID, body) { | ||
const locale = body.substring('@weather '.length); | ||
console.log('Fetching weather for ' + locale + '...'); | ||
weather.find({ search: locale, degreeType: 'F' }, function(err, result) { | ||
var locale = ''; | ||
var currentDType = 'F'; | ||
const infoArray = body.split(' '); | ||
for (var i = 1; i < infoArray.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop seems like a lot of work for just basically checking if the last thing is a C or an F. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I was thinking about extracting the last part first and process it afterwards. |
||
if (i == 1) { | ||
locale += infoArray[i]; | ||
} else { | ||
if (i != infoArray.length - 1 || (i == infoArray.length - 1 && infoArray[infoArray.length - 1] != 'F' && infoArray[infoArray.length - 1] != 'C')) { | ||
locale += ' ' + infoArray[i]; | ||
} else { | ||
currentDType = infoArray[infoArray.length - 1]; | ||
} | ||
} | ||
} | ||
console.log('Fetching weather for ' + locale + ' with degreeType ' + currentDType + '...'); | ||
weather.find({ search: locale, degreeType: currentDType }, function(err, result) { | ||
if (err) { | ||
api.sendMessage(err, threadID); | ||
console.error(err); | ||
|
@@ -65,9 +78,9 @@ module.exports = function getWeather(api, threadID, body) { | |
} else { | ||
emoji = ''; | ||
} | ||
|
||
var displayType = currentDType === 'F' ? '\xB0F\n' : '\xB0C\n'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it can be a const as well right? Considering we aren't changing the display degree type afterwards. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably |
||
let message = | ||
data.current.temperature + '\xB0F\n' + data.location.name + '\n' + data.current.skytext + emoji + '\n' + | ||
data.current.temperature + displayType + data.location.name + '\n' + data.current.skytext + emoji + '\n' + | ||
'Feels like ' + data.current.feelslike + '\xB0. Humidity ' + data.current.humidity + '%.\n'; | ||
|
||
// Concatenate forecast to message | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make the degree type into flags
--unit <C/F>
and-u <C/F>
? And modify the regex accordingly to sanitize the input.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure how to do that? Any tutorials?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or you want a format like
@weather 78701 --unit C
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah like that.
@weather 78701 --unit C
, or@weather 78701 -u F
, or lower cases.If we use flags, we are guaranteed that the characters after the
-
are not part of the address, so we can simply substring the flag out, and use the@weather 78701
part of the body the old way.