- Notifications
You must be signed in to change notification settings - Fork0
This repository contains a Google Apps Script that updates prayer times in a Google Calendar based on data fetched from an external API. The script retrieves daily prayer timings for a specified location using the Aladhan API.
Muhammed770/calendar-prayer-time-api-google-script-
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This Google Apps Script automates the update of prayer times in your Google Calendar using data fetched from the Aladhan API. The script utilizes the University of Islamic Sciences, Karachi method for calculating prayer times.
To use this script, you'll need:
- Google Account: To access Google Calendar and Google Apps Script.
- Calendar ID: Obtain your Google Calendar ID where you want to add the prayer times.
- City and Country: Update the
city
andcountry
variables in the script to match your location.
Google Calendar Setup:
- Create a new Google Calendar if you haven't already.
- Note down the Calendar ID (
calenderId
in the script) which can be found in Calendar settings.
Google Apps Script Setup:
- Open Google Apps Script (script.google.com).
- Create a new script file and paste the provided
updatePrayerTime
function into it.
Configure Trigger:
- Set up a trigger to execute the
updatePrayerTime
function daily.- In the Google Apps Script editor, go toTriggers (clock icon) >Add Trigger.
- Choose
updatePrayerTime
as the function to run. - Set event source to Time-driven, and select day timer with specific time.
- Set up a trigger to execute the
Aladhan API Method:
- The script fetches prayer times from the Aladhan API using the University of Islamic Sciences, Karachi method. You can explore other methods by visitingAladhan API Methods.
TheupdatePrayerTime
function does the following:
- Fetches current prayer times using the Aladhan API based on your location.
- Calculates the start and end times for each prayer, adjusting them as needed.
- Deletes existing events for the same prayer on the current day to prevent duplicates.
- Creates a new recurring event series for each prayer time in your Google Calendar.
functionupdatePrayerTime(){constcalenderId='mail.muhammed2002@gmail.com';constcity='Kerala';constcountry='India';constmethod=1;consturl=`http://api.aladhan.com/v1/timingsByCity?city=${city}&country=${country}&method=${method}`;constresponse=UrlFetchApp.fetch(url);constdata=JSON.parse(response.getContentText());console.log("data = ",data);consttimings=data.data.timings;constcalendar=CalendarApp.getCalendarById(calenderId);functionaddOrUpdateEvent(summary,timeStr){constdate=newDate();const[hours,minutes]=timeStr.split(':').map(Number);letstartTime;if(summary=='Fajr prayer'){constsunriseTime=newDate(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);startTime=newDate(sunriseTime.getTime()-50*60000);}elseif(summary=='Maghrib prayer'){startTime=newDate(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);}else{startTime=newDate(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);startTime=newDate(startTime.getTime()+10*60000);}constendTime=newDate(startTime.getTime()+20*60000)// 20 * 60 seconds * 1000 millisecondsconsole.log(summary,hours,minutes);// Check for existing events with the same summaryconstevents=calendar.getEventsForDay(startTime,{search:summary});if(events.length>0){constevent=events[0];if(event.isRecurringEvent()){constseries=event.getEventSeries();series.deleteEventSeries();}else{event.deleteEvent();}}// Create a new recurring eventconstrecurrence=CalendarApp.newRecurrence().addDailyRule();calendar.createEventSeries(summary,startTime,endTime,recurrence);}addOrUpdateEvent('Fajr prayer',timings.Sunrise);addOrUpdateEvent('Dhuhr prayer',timings.Dhuhr);addOrUpdateEvent('Asr prayer',timings.Asr);addOrUpdateEvent('Maghrib prayer',timings.Maghrib);addOrUpdateEvent('Isha prayer',timings.Isha);}
This project is licensed under the MIT License
About
This repository contains a Google Apps Script that updates prayer times in a Google Calendar based on data fetched from an external API. The script retrieves daily prayer timings for a specified location using the Aladhan API.
Resources
Uh oh!
There was an error while loading.Please reload this page.