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
import { fetch } from "https://esm.town/v/std/fetch";
import process from "node:process";
export async function GetPelotonWorkoutsAndSaveToAirTable(
req: express.Request,
res: express.Response,
) {
let response = await fetch("https://api.onepeloton.com/auth/login", {
method: "POST",
mode: "no-cors",
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
credentials: "include",
body: JSON.stringify({
username_or_email: process.env.peloLogin,
password: process.env.peloPass,
}),
});
const session = await response.json();
let workoutResponse = await fetch(
`https://api.onepeloton.com/api/user/${session["user_id"]}/workouts`,
{
method: "GET",
mode: "no-cors",
credentials: "include",
headers: {
"Content-Type": "application/json",
cookie: `peloton_session_id=${session["session_id"]};`,
"peloton-platform": "web",
},
},
);
const workouts = await workoutResponse.json();
const { Airtable } = await import(
"https://deno.land/x/airtable@v1.1.1/mod.ts"
);
const airtable = new Airtable({
apiKey: process.env.airTablePelo,
baseId: process.env.airTableBase,
tableName: "Table 1",
});
let ids = new Array();
let errors = new Array();
for (const key in workouts) {
airtable.create(
{
"fields": {
Name: key["name"],
Id: key["id"],
metrics_type: key["metrics_type"],
start_time: key["start_time"],
status: key["status"],
title: key["title"],
total_work: ["total_work"],
},
},
{ typecast: true },
function (err, record) {
if (err) {
console.error(err);
errors.push(err);
res.send(errors);
}
console.log(record.getId());
ids.push(record.getId());
},
);
}
res.send(`Records added ids: ${ids}`);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023