-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDog Days 2.0.js
197 lines (159 loc) · 7.14 KB
/
Dog Days 2.0.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: paw;
// Set API variables
const API_TOKEN = "<INSERT API KEY HERE>";
const APP_ID = "<INSERT APP ID HERE>";
const TABLE_ID = "<INSERT TABLE ID HERE>";
// Begin functions to get data from API
// Lastest 💛 pee timestamp
async function timeSinceLastPee() {
// Start API call
const url = "https://api.airtable.com/v0/" + APP_ID + "/" + TABLE_ID + "/?maxRecords=1&view=Pee";
const newRequest = new Request(url);
newRequest.headers = { Authorization: "Bearer " + API_TOKEN };
const json = await newRequest.loadJSON();
// Isolate fields and values from API response
const records = json["records"];
const fields = records["0"]["fields"];
const timestamp = fields["Timestamp"];
// Get time difference between current time and response timestamp
let timestampRaw = new Date(timestamp);
let now = new Date();
let timeDiffMS = now - timestampRaw;
let timeDiff = (timeDiffMS / (1000 * 60 * 60)).toFixed(2);
// Function returns the time
return timeDiff;
};
// Lastest 💩 poop timestamp
async function timeSinceLastPoop() {
// Start API call
const url = "https://api.airtable.com/v0/" + APP_ID + "/" + TABLE_ID + "/?maxRecords=1&view=Poop";
const newRequest = new Request(url);
newRequest.headers = { Authorization: "Bearer " + API_TOKEN };
const json = await newRequest.loadJSON();
// Isolate fields and values from API response
const records = json["records"];
const fields = records["0"]["fields"];
const timestamp = fields["Timestamp"];
// Get time difference between current time and response timestamp
let timestampRaw = new Date(timestamp);
let now = new Date();
let timeDiffMS = now - timestampRaw;
let timeDiff = (timeDiffMS / (1000 * 60 * 60)).toFixed(2);
// Function returns the time
return timeDiff;
};
// Latest 🍗 food timestamp
async function timeSinceLastFood() {
// Start API call
const url = "https://api.airtable.com/v0/" + APP_ID + "/" + TABLE_ID + "/?maxRecords=1&view=Food";
const newRequest = new Request(url);
newRequest.headers = { Authorization: "Bearer " + API_TOKEN };
const json = await newRequest.loadJSON();
// Isolate fields and values from API response
const records = json["records"];
const fields = records["0"]["fields"];
const timestamp = fields["Timestamp"];
// Get time difference between current time and response timestamp
let timestampRaw = new Date(timestamp);
let now = new Date();
let timeDiffMS = now - timestampRaw;
let timeDiff = (timeDiffMS / (1000 * 60 * 60)).toFixed(2);
// Function returns the time
return timeDiff;
};
// Lastest 💧 water timestamp
async function timeSinceLastWater() {
// Start API call
const url = "https://api.airtable.com/v0/" + APP_ID + "/" + TABLE_ID + "/?maxRecords=1&view=Water";
const newRequest = new Request(url);
newRequest.headers = { Authorization: "Bearer " + API_TOKEN };
const json = await newRequest.loadJSON();
// Isolate fields and values from API response
const records = json["records"];
const fields = records["0"]["fields"];
const timestamp = fields["Timestamp"];
// Get time difference between current time and response timestamp
let timestampRaw = new Date(timestamp);
let now = new Date();
let timeDiffMS = now - timestampRaw;
let timeDiff = (timeDiffMS / (1000 * 60 * 60)).toFixed(2);
// Function returns the time
return timeDiff;
};
// Latest 🚶♀️ walk timestamp
async function timeSinceLastWalk() {
// Start API call
const url = "https://api.airtable.com/v0/" + APP_ID + "/" + TABLE_ID + "/?maxRecords=1&view=Exercise";
const newRequest = new Request(url);
newRequest.headers = { Authorization: "Bearer " + API_TOKEN };
const json = await newRequest.loadJSON();
// Isolate fields and values from API response
const records = json["records"];
const fields = records["0"]["fields"];
const timestamp = fields["Timestamp"];
// Get time difference between current time and response timestamp
let timestampRaw = new Date(timestamp);
let now = new Date();
let timeDiffMS = now - timestampRaw;
let timeDiff = (timeDiffMS / (1000 * 60 * 60)).toFixed(2);
// Function returns the time
return timeDiff;
};
// Function to create and customize widget UI
async function createWidget() {
const dogDays = new ListWidget();
// Add widget background
dogDays.backgroundColor = new Color("#1C1B1D");
// Add widget heading
const heading = dogDays.addText("Barley 🐶");
heading.centerAlignText();
heading.font = Font.semiboldSystemFont(15);
heading.textColor = new Color("#ffffff");
dogDays.addSpacer();
// Add time since last pee
const itemPee = dogDays.addStack();
const lastPee = await timeSinceLastPee();
let line1 = itemPee.addText("💛 " + lastPee + " hours ago");
line1.font = Font.semiboldSystemFont(12);
line1.leftAlignText();
// Add time since last poop
const itemPoop = dogDays.addStack();
const lastPoop = await timeSinceLastPoop();
let line2 = itemPoop.addText("💩 " + lastPoop + " hours ago");
line2.font = Font.semiboldSystemFont(12);
line2.leftAlignText();
// Add time since last food
const itemFood = dogDays.addStack();
const lastFood = await timeSinceLastFood();
let line3 = itemFood.addText("🍗 " + lastFood + " hours ago");
line3.font = Font.semiboldSystemFont(12);
line3.leftAlignText();
// Add time since last water
const itemWater = dogDays.addStack();
const lastWater = await timeSinceLastWater();
let line4 = itemWater.addText("💧 " + lastWater + " hours ago");
line4.font = Font.semiboldSystemFont(12);
line4.leftAlignText();
// Add time since last walk
const itemWalk = dogDays.addStack();
const lastWalk = await timeSinceLastWalk();
let line5 = itemWalk.addText("🚶♀️ " + lastWalk + " hours ago");
line5.font = Font.semiboldSystemFont(12);
line5.leftAlignText();
dogDays.addSpacer();
// Display widget
return dogDays;
};
// Display widget
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Run inside a widget when added to the home screen
Script.setWidget(widget);
} else {
// Otherwise show the medium widget preview inside the Scriptable app
widget.presentSmall();
}
Script.complete();