forked from tommyp/shorts-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorts.js
101 lines (92 loc) · 3.97 KB
/
shorts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(function () {
"use strict";
var ShortsWeather = {
init: function (coordinates) {
ShortsWeather.getWeather(coordinates.lat, coordinates.long);
},
getWeather: function (lat, long) {
$.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + escape(lat) + "&lon=" + escape(long), function(response) {
var code, current_temp, error, high_temp, line, locale, result;
try {
console.log(response);
code = response.weather[0].id;
locale = response.name;
current_temp = parseInt(response.main.temp - 273.15);
high_temp = parseInt(response.main.temp_max - 273.15);
ShortsWeather.buildText(current_temp, high_temp, locale, code);
} catch (_error) {
console.log(error);
error = _error;
line = "Sorry!";
result = "It looks like something is wrong.";
ShortsWeather.render(line, result);
}
});
},
buildText: function (current, high, locale, code) {
var coldLine,
coldLines,
coldWord,
coldWords,
result,
soonLine,
soonLines,
trigger,
triggerCodes,
warmLine,
warmLines,
warmWord,
warmWords;
warmLines = ["Hell yeah", "Of course", "Get the legs out", "Totes", "Flat out", "No Doubt", "It bloody well is"];
warmWords = ["lovely", "great", "warm", "hot", "sweltering", "sunny"];
coldLines = ["No way", "Hell no", "Are you not wise?", "Jeans flat out", "Fraid not", "Way on", "Away on"];
coldWords = ["baltic", "freezing", "chilly", "cold", "crappy", "shitty"];
soonLines = ["Give it a chance", "Houl yer horses", "Relax yer kacks", "Don't worry", "Not yet"];
warmWord = warmWords[Math.floor(Math.random() * warmWords.length)];
coldWord = coldWords[Math.floor(Math.random() * coldWords.length)];
trigger = 13;
triggerCodes = [701, 800, 801, 802, 803];
if (current >= trigger && triggerCodes.indexOf(code) !== -1) {
warmLine = warmLines[Math.floor(Math.random() * warmLines.length)];
result = "It's a " + warmWord + " " + current + " Degrees";
ShortsWeather.render(warmLine, result);
ShortsWeather.setTweet(warmLine, warmWord, current, locale);
} else if (high >= trigger && triggerCodes.indexOf(code) !== -1) {
soonLine = soonLines[Math.floor(Math.random() * soonLines.length)];
result = "It's a " + coldWord + " " + current + " degrees right now,<br/> but it'll be a " + warmWord + " " + high + " degrees later";
ShortsWeather.render(soonLine, result);
ShortsWeather.setTweet(soonLine, coldWord, current, locale);
} else {
coldLine = coldLines[Math.floor(Math.random() * coldLines.length)];
result = "It's a " + coldWord + " " + current + " Degrees";
ShortsWeather.render(coldLine, result);
ShortsWeather.setTweet(coldLine, coldWord, current, locale);
}
},
setTweet: function (line, word, temp, locale) {
var tweetHtml, tweetText;
tweetText = "Is it Shorts Weather today? " + line + ". It\'s a " + word + " " + temp + " degrees in " + locale + ".";
tweetHtml = '<a href="https://twitter.com/share" class="twitter-share-button" data-text="' + tweetText + '" data-hashtags="shortsweather">Tweet</a>';
$('#twitter-widget-0').remove();
$('#tweet').append(tweetHtml);
twttr.widgets.load();
$('#tweet').toggle();
},
render: function (line, result) {
$('#line').html(line);
$('#result').html(result).toggle();
}
}
$(document).ready(function() {
if (navigator && navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(
function (position) {
var coordinates = {lat: position.coords.latitude, lon: position.coords.longitude};
ShortsWeather.init(coordinates);
}
);
} else {
return error("Geolocale is not supported.");
}
});
}());