dustMason/resto

where to eat

★ 0Forks 0SvelteGitHub ↗Compare

README

resto

silly thing to help decide where to eat. powered by a google sheet with the following columns:

  • Restaurant
  • Category
  • Rating
  • Order Link
  • Price
  • Website
  • Phone
  • Address
  • Map
  • Monday
  • Tuesday
  • Wednesday
  • Thursday
  • Friday
  • Saturday
  • Sunday

the google sheet itself has this helper AppScript

const apiKey = 'my-api-key';
const na = "";

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
  range.sort({column: 1, ascending: true});
}

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('Business Info')
    .addItem('Fetch Business Details', 'fetchBusinessDetails')
    .addToUi();
}

function fetchBusinessDetails() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const range = sheet.getActiveRange();
  const offsetColumns = 4; // Adjust this to set how many columns to offset the output

  range.getValues().forEach((row, rowIndex) => {
    const placeName = row[0] + ", Alameda, CA"; // Use the value from the first column of the selected row
    const placeId = getPlaceIdFromName(placeName, apiKey);
    if (placeId) {
      const businessInfo = getBusinessInfo(placeId, apiKey);
      if (businessInfo) {
        const targetRange = sheet.getRange(range.getRow() + rowIndex, 1 + offsetColumns, 1, 12);
        targetRange.setValues([[
          businessInfo.priceLevel || 'N/A',
          businessInfo.website || 'N/A',
          businessInfo.phone || 'N/A',
          businessInfo.address || 'N/A',
          businessInfo.mapLink || 'N/A',
          ...parseBusinessHours(businessInfo.hours)
        ]]);
      }
    }
  });
}

function getPlaceIdFromName(placeName, apiKey) {
  const response = UrlFetchApp.fetch(`https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=${encodeURIComponent(placeName)}&inputtype=textquery&fields=place_id&key=${apiKey}`);
  const data = JSON.parse(response.getContentText());

  if (data.status === 'OK' && data.candidates.length > 0) {
    return data.candidates[0].place_id;
  }
  return null;
}

function getBusinessInfo(placeId, apiKey) {
  const url = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&fields=website,formatted_phone_number,opening_hours,formatted_address,url,price_level&key=${apiKey}`;
  const response = UrlFetchApp.fetch(url);
  const data = JSON.parse(response.getContentText());

  if (data.status === 'OK') {
    const result = data.result;
    return {
      website: result.website,
      phone: result.formatted_phone_number,
      address: result.formatted_address,
      mapLink: result.url,
      priceLevel: result.price_level,
      hours: result.opening_hours ? result.opening_hours.weekday_text : []
    };
  }
  return null;
}

function parseBusinessHours(hoursArray) {
  const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  const hoursMap = {};

  // Initialize all days as 'Closed'
  daysOfWeek.forEach(day => {
    hoursMap[day] = 'Closed';
  });

  // Check if hoursArray is an array
  if (Array.isArray(hoursArray)) {
    // Parse the hours
    hoursArray.forEach(entry => {
      const [day, hours] = entry.split(': ');
      if (day && hours) {
        hoursMap[day] = hours;
      }
    });
  }

  // Return the hours in the order of daysOfWeek
  return daysOfWeek.map(day => hoursMap[day]);
}
function parseBusinessHours(hoursArray) {
  const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  const hoursMap = {};

  // Initialize all days as 'Closed'
  daysOfWeek.forEach(day => {
    hoursMap[day] = 'Closed';
  });

  // Check if hoursArray is an array
  if (Array.isArray(hoursArray)) {
    // Parse the hours
    hoursArray.forEach(entry => {
      const [day, hours] = entry.split(': ');
      if (day && hours) {
        hoursMap[day] = hours;
      }
    });
  }

  // Return the hours in the order of daysOfWeek
  return daysOfWeek.map(day => hoursMap[day]);
}

Contributors

dustMason

Issues