Guest User

Untitled

a guest
May 16th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. //express is the framework we're going to use to handle requests
  2. const express = require('express');
  3. //Create a new instance of express
  4. const app = express();
  5. const FormData = require("form-data");
  6. const bodyParser = require("body-parser");
  7. const http = require('http');
  8. const async = require('async');
  9.  
  10. //This allows parsing of the body of POST requests, that are encoded in JSON
  11. app.use(bodyParser.json());
  12. var router = express.Router();
  13.  
  14. //AccuWeather API key
  15. const weatherKey = process.env.WEATHER_KEY_TWO;
  16.  
  17.  
  18. cityCode = ""; //City code
  19. cityName = "";
  20.  
  21. //Current Conditions Vars
  22. var ccWeatherText = ""; //Text for weather at location
  23. var ccTemp = 0; //Degrees Farenheit
  24. var ccIcon = 0; //weather icon number https://developer.accuweather.com/weather-icons
  25. var ccURL = "test"; //URL for get
  26. var hourlyData = [];
  27. var fiveDayData = [];
  28.  
  29. router.post('/', (req, res) => {
  30. let lat = req.body['lat'];
  31. let lon = req.body['lon'];
  32. var latLongCityCodeURL = ("http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=" + weatherKey + "&q=" + lat + "," + lon);
  33. //Get city code
  34. const httpGet = url => {
  35. return new Promise((resolve, reject) => {
  36. http.get(url, res => {
  37. let body = '';
  38. res.on('data', chunk => body += chunk);
  39. res.on('end', () => {
  40. try {
  41. body = JSON.parse(body);
  42. } catch (err) {
  43. reject(new Error(err));
  44. }
  45. resolve({
  46. code: body.Key,
  47. name: body.EnglishName
  48. });
  49. });
  50. }).on('error', reject);
  51. });
  52. };
  53.  
  54. //Current Conditions
  55. const ccGet = url => {
  56. return new Promise((resolve, reject) => {
  57. http.get(url, res => {
  58. let body = '';
  59. res.on('data', chunk => body += chunk);
  60. res.on('end', () => {
  61. try {
  62. body = JSON.parse(body);
  63. } catch (err) {
  64. reject(new Error(err));
  65. }
  66. resolve({
  67. text: body[0].WeatherText,
  68. temp: body[0].Temperature.Imperial.Value,
  69. icon: body[0].WeatherIcon
  70. });
  71. });
  72. }).on('error', reject);
  73. });
  74. };
  75.  
  76. //12 hour
  77. const twelveGet = url => {
  78. return new Promise((resolve, reject) => {
  79. http.get(url, res => {
  80. let body = '';
  81. res.on('data', chunk => body += chunk);
  82. res.on('end', () => {
  83. try {
  84. body = JSON.parse(body);
  85. } catch (err) {
  86. reject(new Error(err));
  87. }
  88. resolve({
  89. body: body
  90. });
  91. });
  92. }).on('error', reject);
  93. });
  94. };
  95.  
  96. //5 day
  97. const fiveGet = url => {
  98. return new Promise((resolve, reject) => {
  99. http.get(url, res => {
  100. let body = '';
  101. res.on('data', chunk => body += chunk);
  102. res.on('end', () => {
  103. try {
  104. body = JSON.parse(body);
  105. } catch (err) {
  106. reject(new Error(err));
  107. }
  108. resolve({
  109. body: body
  110. });
  111. });
  112. }).on('error', reject);
  113. });
  114. };
  115.  
  116. //Get city code from lat lon
  117. httpGet(latLongCityCodeURL).then(data => {
  118. cityCode = data.code;
  119. cityName = data.name;
  120. ccURL = ("http://dataservice.accuweather.com/currentconditions/v1/" + cityCode + "?apikey=" + weatherKey);
  121. twelveURL = ("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/" + cityCode + "?apikey=" + weatherKey);
  122. fiveURL = ("http://dataservice.accuweather.com/forecasts/v1/daily/5day/" + cityCode + "?apikey=" + weatherKey);
  123. //Get Current Conditions
  124. ccGet(ccURL).then(dataCC => {
  125. ccTemp = dataCC.temp;
  126. ccWeatherText = dataCC.text;
  127. ccIcon = dataCC.icon;
  128. //Get 12 hour forecast
  129. twelveGet(twelveURL).then(dataTwelve => {
  130. //Generate hourly data
  131. for (i = 0; i < dataTwelve.length; i++) {
  132. hourlyData[i] = {
  133. time: dataTwelve[i].EpochDateTime,
  134. temp: dataTwelve[i].Temperature.Value,
  135. text: dataTwelve[i].IconPhrase,
  136. icon: dataTwelve[i].WeatherIcon
  137. };
  138. }
  139. console.log("Hourly Data: " + hourlyData);
  140. }).catch(err => console.log(err));
  141. fiveGet(fiveURL).then(dataFive => {
  142. //Generate five day data
  143. for (i = 0; i < dataFive.length; i++) {
  144. fiveDayData[i] = {
  145. time: dataFive[i].EpochDate,
  146. min: dataFive[i].Temperature.Minimum.Value,
  147. max: dataFive[i].Temperature.Maximum.Value,
  148. iconDay: dataFive[i].Day.Icon,
  149. iconNight: dataFive[i].Night.Icon,
  150. dayPhrase: dataFive[i].Day.IconPhrase,
  151. nightPhrase: dataFive[i].Night.IconPhrase
  152. };
  153. console.log("5 Day Data:" + fiveDayData);
  154. }
  155. res.send({
  156. success: true,
  157. cityName: cityName,
  158. cityCode: cityCode,
  159. currentConditions: {
  160. temp: ccTemp,
  161. icon: ccIcon,
  162. text: ccWeatherText
  163. },
  164. hourlyData: hourlyData,
  165. fiveDayData: fiveDayData
  166. });
  167. }).catch(err => console.log(err));
  168. }).catch(err => console.log(err));
  169. }).catch(err => console.log('Got error ', err));
  170. });
  171.  
  172.  
  173. module.exports = router;
Add Comment
Please, Sign In to add comment