Skip to content
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

sd/feat/homework_4 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 03/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
58 changes: 58 additions & 0 deletions 03/safonov_daniil/darksky.api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const token = require('./tokenStorage');
const request = require('request');

getWeatherByCity = (cityInfo, callback) => {

const longitude = cityInfo.sityCoords[0];
const latitude = cityInfo.sityCoords[1];
const url = `https://api.darksky.net/forecast/${token.getTokenDarkSky()}/${longitude},${latitude}?units=si`;
request({ url: url, json: true },
function (error, response, body) {
if (error) {
console.log(`Error darksky: ${error}`);
callback(error);
} else if (response.statusCode !== 200) {
console.log(`Sataus code: ${response.statusCode}`);
callback(response.statusCode);
} else {
const dailyWeather = body.daily;
const dailyWeatherInfo = {
city: cityInfo.sityName,
summary: dailyWeather.summary,
temperature: (dailyWeather.data[0].temperatureMax + dailyWeather.data[0].temperatureMin) / 2,
windSpeed: dailyWeather.data[0].windSpeed,
}
callback(false, dailyWeatherInfo);
}
});
}

getWeatherByCoords = (coords, callback) => {
const longitude = coords[0];
const latitude = coords[1];
const url = `https://api.darksky.net/forecast/${token.getTokenDarkSky()}/${longitude},${latitude}?units=si`;
request({ url: url, json: true },
function (error, response, body) {
if (error) {
console.log(`Error darksky: ${error}`);
callback(error);
} else if (response.statusCode !== 200) {
console.log(`Sataus code: ${response.statusCode}`);
callback(response.statusCode);
} else {
const dailyWeather = body.daily;
const dailyWeatherInfo = {
coords: coords,
summary: dailyWeather.summary,
temperature: (dailyWeather.data[0].temperatureMax + dailyWeather.data[0].temperatureMin) / 2,
windSpeed: dailyWeather.data[0].windSpeed,
}
callback(false, dailyWeatherInfo);
}
});
}

module.exports = {
getWeatherByCity: getWeatherByCity,
getWeatherByCoords: getWeatherByCoords,
}
44 changes: 44 additions & 0 deletions 03/safonov_daniil/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mapbox = require('./mapbox.api');
const term = require('terminal-kit').terminal;
const darksky = require('./darksky.api');
const weather = require('./weatherLogger');

const menuItem = ['Show weather by location', 'Show weather by city name'];


term.gridMenu(menuItem, (error, response) => {
switch (response.selectedIndex) {
case 0:
term('Please enter your longitude and latitude(comma separate): ');
term.inputField(function (error, input) {
const coords = input.split(",");
darksky.getWeatherByCoords(coords, (error, data) => {
if (error) {
console.log("Error with darksky api!");
process.exit();
}
weather.logByCoords(data);
process.exit();
});
});
break;
case 1:
term('Please enter your city name: ');
term.inputField(function (error, input) {
mapbox.getLocationByCity(input, (error, data) => {
if (error) {
console.log("Error with mapbox api!");
process.exit();
}
darksky.getWeatherByCity(data, (error, data) => {
if (error) {
console.log("Error with darksky api!");
process.exit();
}
weather.logByCity(data);
process.exit();
});
})
});
}
})
25 changes: 25 additions & 0 deletions 03/safonov_daniil/mapbox.api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const token = require('./tokenStorage');
const request = require('request');

getLocationByCity = (cityName, callback) => {
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${cityName}.json?access_token=${token.getTokenMapBox()}`;
request({ url: url, json: true },
function (error, response, body) {
if(error){
console.log(`Error mapbox: ${error}`);
callback(error);
} else if(response.statusCode !== 200){
console.log(`Sataus code: ${response.statusCode}`);
callback(response.statusCode);
} else {
const sityCoords = [body.features[0].center[1], body.features[0].center[0]];
const sity = body.features[0].text;
const sityInfo = { sityCoords: sityCoords, sityName: sity};
callback(false, sityInfo);
}
});
}

module.exports = {
getLocationByCity: getLocationByCity,
}
Loading