-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
56 lines (47 loc) · 2.06 KB
/
code.gs
File metadata and controls
56 lines (47 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function updatePrayerTime() {
const calenderId = 'mail.muhammed2002@gmail.com';
const city = 'Kerala';
const country = 'India';
const method = 1;
const url = `http://api.aladhan.com/v1/timingsByCity?city=${city}&country=${country}&method=${method}`;
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText());
console.log("data = ",data);
const timings = data.data.timings;
const calendar = CalendarApp.getCalendarById(calenderId);
function addOrUpdateEvent(summary,timeStr) {
const date = new Date();
const [hours,minutes] = timeStr.split(':').map(Number);
let startTime;
if(summary == 'Fajr prayer') {
const sunriseTime = new Date(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);
startTime = new Date(sunriseTime.getTime() - 50 * 60000);
} else if (summary == 'Maghrib prayer'){
startTime = new Date(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);
} else {
startTime = new Date(date.getFullYear(),date.getMonth(),date.getDate(),hours,minutes);
startTime = new Date(startTime.getTime()+ 10 * 60000);
}
const endTime = new Date(startTime.getTime()+ 20 * 60000) // 20 * 60 seconds * 1000 milliseconds
console.log(summary,hours,minutes);
// Check for existing events with the same summary
const events = calendar.getEventsForDay(startTime, { search: summary });
if (events.length > 0) {
const event = events[0];
if (event.isRecurringEvent()) {
const series = event.getEventSeries();
series.deleteEventSeries();
} else {
event.deleteEvent();
}
}
// Create a new recurring event
const recurrence = 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);
}