yahooWeather.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function convertTemp (temp, unit) {
  2. if (unit === 'fahrenheit') {
  3. return parseInt((temp - 273.15) * 9 / 5 + 32)
  4. } else {
  5. return parseInt(temp - 273.15)
  6. }
  7. }
  8. function convertSpeed (speed, unit) {
  9. speed = speed * 3.6 // m/s -> km/h
  10. print(speed)
  11. if (unit === 'metric') {
  12. return parseInt(speed)
  13. } else {
  14. return parseInt(speed / 1.60934) // km/h -> mph
  15. }
  16. }
  17. function getYahooWeather (location, tempUnit, distanceUnit) {
  18. if (tempUnit === undefined) tempUnit = 'celsius'
  19. if (distanceUnit === undefined) distanceUnit = 'metric'
  20. print('getting weather');
  21. var result = httpRequest({
  22. method: 'GET',
  23. url: 'https://api.getoboo.com/v1/weather?q=' + encodeURIComponent(location)
  24. });
  25. if (result) {
  26. var jsonResult;
  27. try {
  28. jsonResult = JSON.parse(result);
  29. } catch(e) {
  30. print(e); // error in the above string!
  31. return null;
  32. }
  33. print(JSON.stringify(jsonResult))
  34. var weatherObj = {
  35. 'temperature': convertTemp(jsonResult.main.temp, tempUnit),
  36. 'condition': jsonResult.weather[0].icon,
  37. 'wind': convertSpeed(jsonResult.wind.speed, distanceUnit),
  38. 'secondary': [
  39. {
  40. type: 'wind-speed',
  41. unit: (distanceUnit === 'metric' ? 'km/h' : 'mph'),
  42. value: convertSpeed(jsonResult.wind.speed, distanceUnit)
  43. },
  44. {
  45. type: 'humidity',
  46. unit: '%',
  47. value: jsonResult.main.humidity
  48. },
  49. {
  50. type: 'temp-hi-lo',
  51. unit: convertTemp(jsonResult.main.temp_min, tempUnit) + tempUnit[0],
  52. value: convertTemp(jsonResult.main.temp_max, tempUnit)
  53. }
  54. ]
  55. }
  56. return weatherObj;
  57. }
  58. else {
  59. return null;
  60. }
  61. }