mvan231 / Scriptable

MIT License
114 stars 39 forks source link

Alexa to Reminders Access.js quits with an error #25

Open andereeicheln0z opened 1 month ago

andereeicheln0z commented 1 month ago

Unfortunately, the script quits with an error. Do you have any tips on what could be wrong?

IMG_3842

Log says:

2024-05-18 13:59:23: index of "Sign in"
is -1
2024-05-18 13:59:23: logged in
2024-05-18 13:59:23: /private/var/mobile/
¡ Library/Mobile Documents/
iCloud~dk~simonbs~Scriptable/Documents/ lastCreatedDateTime.txt
2024-05-18 13:59:23: lastDate is 0
2024-05-18 13:59:26: Error: Die Daten konnten nicht gelesen werden, da sie nicht das korrekte Format haben.
andereeicheln0z commented 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.

mvan231 commented 3 weeks ago

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

andereeicheln0z commented 3 weeks ago

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();