Open andereeicheln0z opened 1 month ago
Try to find out what's going wrong. I found out that loadJSON does not work because no JSON is sent back. "Authentication Failure" is the return value when replacing loadJSON by loadString.
Have you logged into Amazon with your account via Scriptable? Sorry for my long delay. I've been away from GitHub for some time unfortunately
The problem was that the shopping list did not work for the German Amazon site. I revised your script to ensure no duplicate entries, case sensitivity, synchronized entries resolved, no duplicate entries, and optimized performance. The script now looks like this:
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: magic;
async function checkIfUserIsAuthenticated() {
try {
const url = 'https://www.amazon.de/alexashoppinglists/api/getlistitems';
const request = new Request(url);
await request.load();
if (request.response.statusCode === 401 || request.response.statusCode === 403) {
return false;
}
return true;
} catch (error) {
console.error(error);
return false;
}
}
async function makeLogin() {
const url = 'https://www.amazon.de';
const webView = new WebView();
try {
await webView.loadURL(url);
const html = await webView.getHTML();
if (html.includes("Anmelden")) {
await webView.present(false);
return false;
}
return true;
} catch (error) {
console.error(error);
return false;
}
}
async function synchronizeReminders() {
try {
const reminderCalendar = await Calendar.forRemindersByTitle("Einkaufsliste");
const url = 'https://www.amazon.de/alexashoppinglists/api/getlistitems';
const deleteUrl = "https://www.amazon.de/alexashoppinglists/api/deletelistitem";
const json = await new Request(url).loadJSON();
const listItems = json[Object.keys(json)[0]].listItems;
const existingReminders = await Reminder.all([reminderCalendar]);
for (const item of listItems) {
const reminderTitle = item.value.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
const reminderExists = existingReminders.some(reminder => reminder.title === reminderTitle);
if (!reminderExists) {
const reminder = new Reminder();
reminder.title = reminderTitle;
reminder.calendar = reminderCalendar;
await reminder.save();
}
const request = new Request(deleteUrl);
request.method = "DELETE";
request.headers = {
"Content-Type": "application/json"
};
request.body = JSON.stringify(item);
try {
const response = await request.loadString();
} catch (deleteError) {
console.error(deleteError);
}
}
} catch (error) {
console.error(error);
}
}
async function main() {
const isAuthenticated = await checkIfUserIsAuthenticated();
log(isAuthenticated);
if (!isAuthenticated) {
const loggedIn = await makeLogin();
log(loggedIn);
if (!loggedIn) {
console.log('Login failed. Exiting.');
return;
}
}
await synchronizeReminders();
}
main();
Script.complete();
Unfortunately, the script quits with an error. Do you have any tips on what could be wrong?
Log says: