Skip to content

Commit

Permalink
feat(iCloud): Send notifications to user!
Browse files Browse the repository at this point in the history
  • Loading branch information
EntraptaJ committed Mar 13, 2023
1 parent 5aebf94 commit 4916f8e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"test": "ts-estest ./src",
"prepublishOnly": "npm run build",
"build": "tsc -p ./tsconfig.build.json",
"start:debug": "node --loader @k-foss/ts-esnode --experimental-modules --experimental-specifier-resolution=node --harmony-top-level-await ./",
"start:debug": "node --loader @k-foss/ts-esnode --experimental-modules --experimental-specifier-resolution=node ./",
"dev": "nodemon",
"prettier": "prettier --config .prettierrc --check \"src/**/*ts\"",
"lint": "eslint ./src --ext .js,.ts",
"runBin": "node --loader @k-foss/ts-esnode --experimental-modules --experimental-specifier-resolution=node --harmony-top-level-await ./src/Utils/runScript.ts"
"runBin": "node --loader @k-foss/ts-esnode --experimental-modules --experimental-specifier-resolution=node ./src/Utils/runScript.ts"
},
"devDependencies": {
"@k-foss/ts-esnode": "^2.0.3",
Expand Down
57 changes: 56 additions & 1 deletion src/Library/iCloud.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
// src/Library/iCloud.ts
import iCloud from 'icloudjs';
import { CONFIG } from './Config';
import got from 'got';
import { logger, LogMode } from './Logger';
import readline from 'readline/promises';


export const iCloudAPI = new iCloud({
...CONFIG.iCloud,
trustDevice: true,
saveCredentials: true,
});

export async function initiCloud(): Promise<void> {
logger.log(LogMode.INFO, `Logging into iCloud`);

await iCloudAPI.authenticate();

logger.log(LogMode.DEBUG, `iCloud Status`, iCloudAPI.status);

if (iCloudAPI.status === 'MfaRequested') {
logger.log(LogMode.WARN, `iCloud needs multifactor`);

const consoleInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const mfaCode = await consoleInterface.question('Please provide MFA code\n');
logger.log(LogMode.DEBUG, `Receieved code ${mfaCode}`);

logger.log(LogMode.INFO, `Providing MFA to iCloud`);
await iCloudAPI.provideMfaCode(mfaCode);
}

await iCloudAPI.awaitReady;
}

export async function sendAlert(device: string, {
subject = 'Find My iPhone Alert',
text = 'Hello World!'
}): Promise<any> {
if (iCloudAPI.accountInfo) {
const { findme: { url: prefixUrl } } = iCloudAPI.accountInfo.webservices

logger.log(LogMode.INFO, `Attempting to send alert`, prefixUrl)

const api = got.extend({
headers: iCloudAPI.authStore.getHeaders(),
})

return api.post(`${prefixUrl}/fmipservice/client/web/sendMessage`, {
json: {
device,
subject,
sounds: true,
userText: true,
text
}
})
} else {
throw new Error('iCloud Not Connected')
}
}
27 changes: 25 additions & 2 deletions src/Modules/User/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
// src/Modules/User/index.ts
import { iCloudAPI, sendAlert } from "../../Library/iCloud";
import { logger, LogMode } from "../../Library/Logger";

export class User {
public name: string;

public deviceID: string;


public async findUserNotifyDevice(deviceName: string): Promise<void> {
const findMyAPI = iCloudAPI.getService('findme');

await findMyAPI.refresh()

for (const [deviceID, device] of findMyAPI.devices) {
logger.log(LogMode.DEBUG, `Looping over user Devices`, deviceID, device);

if (device.deviceInfo.name === deviceName) {
logger.log(LogMode.DEBUG, `Found device ${deviceName} with the ID of ${deviceID}`);

this.deviceID = deviceID;
}
}
}

/**
* Sends notification to user via her Apple Watch Ultra
*/
public async notifyUser() {

public async notifyUser(options: {
subject?: string,
text?: string
}) {
await sendAlert(this.deviceID, options)
}
}
71 changes: 33 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
// src/index.ts
import 'reflect-metadata';
import { CONFIG } from './Library/Config';
import { iCloudAPI } from './Library/iCloud';
import { initiCloud } from './Library/iCloud';
import { logger, LogMode } from './Library/Logger';
import { GoTransit } from './Modules/GoTransit';
import { User } from './Modules/User';
import { sayHello } from './Utils/sayHello';

import readline from 'readline/promises';

logger.log(LogMode.INFO, `Starting TransitRouting`);

logger.log(LogMode.INFO, `Logging into iCloud`);
logger.log(LogMode.INFO, `Starting TransitRouting`);

await iCloudAPI.authenticate();

logger.log(LogMode.INFO, `iCloud Status`, iCloudAPI.status);
logger.log(LogMode.INFO, `Init iCloud Library`);
await initiCloud();

if (iCloudAPI.status === 'MfaRequested') {
logger.log(LogMode.INFO, `iCloud needs multifactor`);
logger.log(LogMode.INFO, `Creating Kristine Entity`);

const consoleInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const kristine = new User();

const mfaCode = await consoleInterface.question('Please provide MFA code');
logger.log(LogMode.DEBUG, `Receieved code ${mfaCode}`);
await kristine.findUserNotifyDevice(`Kristine’s Apple Watch`);

logger.log(LogMode.INFO, `Providing MFA to iCloud`);
await iCloudAPI.provideMfaCode(mfaCode);
}
await kristine.notifyUser({
subject: 'Platform Assignment',
text: 'Platform 6 & 7'
})

const goAPI = new GoTransit({
apiURL: CONFIG.apiURL,
Expand All @@ -39,27 +34,27 @@ const goAPI = new GoTransit({

logger.log(LogMode.INFO, `Making request`);

const unionDepartures = await goAPI.unionDepartures();

for (const departure of unionDepartures) {
if (departure.Service === 'Kitchener') {
if (departure.Platform !== '-') {
logger.log(
LogMode.DEBUG,
`Union Departure to Kitchener at platform`,
departure.Platform,
);
}
}
}

const trains = await goAPI.getAllTrains();

for (const train of trains) {
if (train.LineCode === 'GT') {
logger.log(LogMode.DEBUG, `Trains`, train);
}
}
// const unionDepartures = await goAPI.unionDepartures();

// for (const departure of unionDepartures) {
// if (departure.Service === 'Kitchener') {
// if (departure.Platform !== '-') {
// logger.log(
// LogMode.DEBUG,
// `Union Departure to Kitchener at platform`,
// departure.Platform,
// );
// }
// }
// }

// const trains = await goAPI.getAllTrains();

// for (const train of trains) {
// if (train.LineCode === 'GT') {
// logger.log(LogMode.DEBUG, `Trains`, train);
// }
// }

await sayHello('K-FOSS');

Expand Down

0 comments on commit 4916f8e

Please sign in to comment.