-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsimple-weather-widget.js
More file actions
159 lines (125 loc) · 3.91 KB
/
simple-weather-widget.js
File metadata and controls
159 lines (125 loc) · 3.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: cloud;
/* **********************************************
Name : simple-weather-widget.js
Author : @supermamon
Version : 1.0.1
Desc : Example widget using the openweathermap.js module
// https://github.com/supermamon/scriptable-scripts/blob/master/docs/openweathermap.md
Changelog:
-------------------------------------------------
v1.0.0 | 02 Nov 2020
* Initial release
-------------------------------------------------
v1.0.1 | 02 Nov 2020
* (fix) API_KEY requires an unshared module
-------------------------------------------------
v1.0.2 | 20 Sep 2022
* (fix) location_name does not override when revgeocode=true
* (doc) in-code documentation
********************************************** */
// REQUIRED!!
// you need an Open Weather API key.
// Get one for free at: https://home.openweathermap.org/api_keys
let API_KEY = "your-api-key"
// Alternatively, store API keys in a module
// Example:
// const secrets = importModule('secrets.js')
// API_KEY = secrets.openweathermap
// ALSO REQUIRED, openweathermap module
const owm = importModule('openweathermap.js')
// by not providing lat and lon, script will
// run automatic location detection
// reverse geocoding `true` will identify the
// location name
const options = {
appid: API_KEY,
revgeocode: true,
exclude: 'hourly,daily'
}
// all other options
/*
const options = {
appid: API_KEY,
// auto-find the location name
revgeocode: true,
// units standard | metric | imperial
units: 'imperial',
// provide coordinates to disable location
// detection
lat: 35.6725687,
lon: 139.7526493,
// provide location name instead of geo-coding
location_name: 'Tokyo',
// alternative language. provide 2-letter code
lang: 'fr',
}
*/
// load weather data
const wttr = await owm(options)
// create the widget
const widget = new ListWidget()
widget.backgroundColor = Color.white()
// text shadows
const shadowOffset = new Point(1, 1)
const shadowColor = Color.lightGray()
const shadowRadius = 0
// change the gradient color depending if it's
// night or day
const gdcolor = wttr.current.is_night ? '0a0a0a' : '277bae'
widget.backgroundGradient = newLinearGradient([`#${gdcolor}ee`, `#${gdcolor}aa`], [0, .8])
// content
// location
let loc_name;
if (wttr.args.location_name) {
// if location is manually provided
loc_name = wttr.args.location_name
} else if (wttr.geo) {
// if reverse-geocoded is performed
loc_name = wttr.geo.locality ?? wttr.geo.name ?? wttr.geo.postalAddress.city
}
// add location name on widget if exists
if (loc_name) {
const location = widget.addText(loc_name)
location.font = Font.boldSystemFont(14)
location.leftAlignText()
widget.addSpacer(1)
}
// temparature
const temp = addText(widget, `${Math.round(wttr.current.temp)}°`)
temp.font = Font.ultraLightSystemFont(46)
temp.leftAlignText()
// icon
const symbol = SFSymbol.named(wttr.current.weather[0].sfsymbol)
const icon = widget.addImage(symbol.image)
icon.leftAlignImage()
icon.size = new Size(8, 8)
widget.addSpacer(5)
// conditions
const conditions = addText(widget, `${wttr.current.weather[0].description}`)
conditions.font = Font.systemFont(12)
// feels like
const fl = addText(widget, `feels like ${Math.round(wttr.current.feels_like)}°${wttr.units.temp}`)
fl.font = Font.systemFont(12)
if (config.runsInWidget) {
Script.setWidget(widget)
} else {
await widget.presentSmall()
}
Script.complete()
function addText(container, text) {
let oText = container.addText(text)
oText.shadowColor = Color.lightGray()
oText.shadowOffset = new Point(1, 1)
oText.shadowRadius = 1
return oText
}
//------------------------------------------------
function newLinearGradient(hexcolors, locations) {
let gradient = new LinearGradient()
gradient.locations = locations
gradient.colors = hexcolors
.map(color => new Color(color))
return gradient
}