Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
BurakGurbuz97 committed Nov 3, 2019
1 parent 7f534bb commit 45abe99
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 77 deletions.
45 changes: 42 additions & 3 deletions src/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,47 @@ import Todos from "./components/todo.js"
import Weather from './components/weather';


const city2offset = {
"Amsterdam": 1,
"Barcelona": 1,
"Tokyo": 9,
"Dubai": 4,
"New York": -5,
"Mumbai": 5.5,
"Prague": 1,
"Mecca": 3,
"Miami": -5,
"Seoul": 9,
"Shanghai": 8,
"Taipei": 8,
"Hong Kong": 8,
"London": 1,
"Paris": 1,
"Singapore": 8,
"Delhi": 5.5,
"Istanbul": 3,
"Rome": 1,
"Guangzhou": 8,
"Bangkok": 7,
"Los Angeles": -8
}

function calcTime(city) {
if (city) {
var offset = city2offset[city]
var d = new Date();
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = new Date(utc + (3600000*offset));
return nd
} else {
return new Date()
}

}


const default_state = {
date: new Date(),
date: calcTime(null),
day: 0,
contentBody: "",
contentVisible: false,
Expand Down Expand Up @@ -70,7 +109,7 @@ class Welcome extends React.Component {

tick() {
this.setState({
date: new Date()
date: calcTime(this.state.currentLocation.split("/")[0])
});

}
Expand Down Expand Up @@ -424,7 +463,7 @@ class Welcome extends React.Component {
<option onClick={(e) => { this.changeLocation(e, "Barcelona/Spain") }}>Barcelona</option> <option onClick={(e) => { this.changeLocation(e, "Hong Kong") }}>Hong Kong</option>
<option onClick={(e) => { this.changeLocation(e, "Tokyo/Japan") }}>Tokyo</option> <option onClick={(e) => { this.changeLocation(e, "London/United Kingdom") }}>London</option>
<option onClick={(e) => { this.changeLocation(e, "Dubai/United Arab Emirates") }}>Dubai</option> <option onClick={(e) => { this.changeLocation(e, "Paris/France") }}>Paris</option>
<option onClick={(e) => { this.changeLocation(e, "New York City/USA") }}>New York City</option> <option onClick={(e) => { this.changeLocation(e, "Singapore") }}>Singapore</option>
<option onClick={(e) => { this.changeLocation(e, "New York/USA") }}>New York City</option> <option onClick={(e) => { this.changeLocation(e, "Singapore") }}>Singapore</option>
<option onClick={(e) => { this.changeLocation(e, "Mumbai/India") }}>Mumbai</option> <option onClick={(e) => { this.changeLocation(e, "Delhi/India") }}>Delhi</option>
<option onClick={(e) => { this.changeLocation(e, "Prague/Czechia") }}>Prague</option> <option onClick={(e) => { this.changeLocation(e, "Istanbul/Turkey") }}>Istanbul</option>
<option onClick={(e) => { this.changeLocation(e, "Mecca/Saudi Arabia") }}>Mecca</option> <option onClick={(e) => { this.changeLocation(e, "Rome/Italy") }}>Rome</option>
Expand Down
104 changes: 30 additions & 74 deletions test_weather.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,33 @@
const Axios = require("axios")

function getRandomItem(set) {
let items = Array.from(set);
return items[Math.floor(Math.random() * items.length)];
city2offset = {
"Amsterdam": 1,
"Barcelona": 1,
"Tokyo": 9,
"Dubai": 4,
"New York": -5,
"Mumbai": 5.5,
"Prague": 1,
"Mecca": 3,
"Miami": -5,
"Seoul": 9,
"Shanghai": 8,
"Taipei": 8,
"Hong Kong": 8,
"London": 1,
"Paris": 1,
"Singapore": 8,
"Delhi": 5.5,
"Istanbul": 3,
"Rome": 1,
"Guangzhou": 8,
"Bangkok": 7,
"Los Angeles": -8
}

country2iso = {
"Netherlands": "nl", "Spain": "es", "Japan": "jp", "United Arab Emirates": "ae",
"USA": "us", "India": "in", "Czechia": "cz", "Saudi Arabia": "sa", "South Korea": "KR",
"China": "cn", "Taiwan": "tw", "Hong Kong": "hk", "United Kingdom": "gb", "France": "fr",
"Singapore": "sg", "Turkey": "tr", "Italy": "it", "Thailand": "th"
}

num2day = {
1 : "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday",
5: "Friday", 6: "Saturday", 0: "Sunday"
}

function fetch_weekly_forecast(city, country, callback) {
day2weather = {}
apiId = "58f46e0fda1c1d4da743943c656bcfe3"
city = city
code = country2iso[country]
mode = "json"
url = `http://api.openweathermap.org/data/2.5/forecast?appid=${apiId}&q=${city},${code}&mode=${mode}`
Axios.get(url).then( (res, err) => {
if (err) {
callback(null , err)
} else {
var time_reference = res.data.list[0].dt_txt.split(" ")[1]
var average = 0
var weather_set = new Set()
res.data.list.map((data) => {
if (data.dt_txt.split(" ")[1] === time_reference) {
var date = new Date(data.dt_txt)
var min = Math.round(data.main["temp_min"] - 273.15)
var max = Math.round(data.main["temp_max"] - 273.15)
var temp = Math.round(data.main["temp"] - 273.15)
var weather = data.weather[0]["main"]
var day = num2day[date.getDay()]
day2weather[day] = {
"min": min,
"max": max,
"temp": temp,
"weather": weather,
}
average = average + temp
weather_set.add(weather)
}
})
average = Math.round(average / 5)
Object.keys(num2day).forEach((key) => {
key = num2day[key]
if(day2weather[key] === undefined ) {
randomize_average = average //can add some staff
day2weather[key] = {
"min": Math.round(randomize_average),
"max": Math.round(randomize_average),
"temp": Math.round(randomize_average),
"weather": getRandomItem(weather_set)
}
}
})
callback(day2weather, null)
}
})
}


fetch_weekly_forecast("Istanbul", "Turkey", (day2weather, err) => {
if (err) {
console.log(err)
} else {
console.log(day2weather)
}
})
function calcTime(city) {
var offset = city2offset[city]
var d = new Date();
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var nd = new Date(utc + (3600000*offset));
return nd
}

0 comments on commit 45abe99

Please sign in to comment.