-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.gs
118 lines (109 loc) · 4.77 KB
/
Main.gs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
spreadsheet=SpreadsheetApp.getActive()
settings=spreadsheet.getSheetByName("settings")
function parseSettings(){//parse the settings
if (settings.getRange("B12").getValue()==false){return false}//switch
keywords=settings.getRange("B10").getValue().split(",")//keywords
times=eval(settings.getRange("B2:B8").getValues())//times
user_days=settings.getRange("D2:D8").getValues().flat()//days of the week
return [keywords,user_days,times]
}
function fetchLastTweet(){
settings.getRange("C12").setValue("")
nitter_instance=settings.getRange("D14").getValue()
url=nitter_instance+settings.getRange("C11").getValue()// for public access to twitter with no login
xpathText=settings.getRange("D15").getValue() //xpath to last tweet
settings.getRange("C12").setFormula('=REGEXREPLACE(CHOOSECOLS(IMPORTXML("'+url+'";"'+xpathText+'");1);"\n";"")')
tweet=settings.getRange("C12").getValue()
return tweet
}
function includes_kw(tweet){//look for keywords presence in tweet
for (k in keywords){
if (tweet.includes(keywords[k])){return true}
}
return false
}
function check_dates(start,end){ //check whether any of the chosen dates from dates_TCL are wanted
d=start
while (d<=end){
if (user_days.includes(d.getDay())){return true}
d.setDate(d.getDate()+1) //might fail if daylight savings occur during the period
}
return false
}
function check_time(tweet_time){ //to check one single time. DO NOT USE DIRECTLY IN RUN
day=times[(startTime.getDay()-1)+7*(startTime.getDay()==0)][0].split(',')//times for the current day of the week
for (t in day){
start=day[t].split('-')[0]
start=new Date(0,0,1,start.split(":")[0],start.split(":")[1])//start of t-th period of the day in js date format
end=day[t].split('-')[1]
end=new Date(0,0,1,end.split(":")[0],end.split(":")[1])//end of t-th period of the day in js date format
if ((tweet_time>=start) && (tweet_time<=end)){
if (tweet_time.getHours()*60+tweet_time.getMinutes()<now.getHours()*60+now.getMinutes()){ //in case they tweet after the disurption has happened (this happens lol), so the notification is still sent
startTime.setHours(now.getHours(),now.getMinutes())
}else{
startTime.setHours(tweet_time.getHours(),tweet_time.getMinutes())
}
endTime.setHours(end.getHours(),end.getMinutes(),0)//end time is the end of the time monitored time period in which the current time/tweet time (depending on the tweet content) falls in
return true}
}
return false
}
function check_multi_times(tweet_time){ //to check 1+ times. USE THIS ONE IN RUN
if (Array.isArray(tweet_time)){
for (t in tweet_time){
if (check_time(tweet_time[t])){return true}
}
return false
}
return check_time(tweet_time)
}
function avoidRedundancy(tweet){ //very important: to not get spammed with the same event until next tweet
logs=spreadsheet.getSheetByName("logs")
if (logs.getRange("C2").getValue()==tweet){return false}
return true
}
function event(){// create the calendar event and send notification
cal=CalendarApp.getCalendarById(settings.getRange('D12').getValue())
ev=cal.createEvent(tweet,startTime,endTime)
ev.addPopupReminder(5) //notification cannot be set to later than 5mins before the event (in the script), but will show anyway
}
function run(){
debug='OFF'
debug_counter=0
if (parseSettings()){
[keywords,user_days,times]=parseSettings()
tweet=fetchLastTweet()
console.log(tweet)
debug='ON'
debug_counter++
if (avoidRedundancy(tweet)){
debug+='-no redundancy'
debug_counter++
if (includes_kw(tweet)){
debug+='-keywords included'
debug_counter++
startTime=dates_TCL(tweet)[0]
endTime=dates_TCL(tweet)[1]
//change here for a custom date retrieving function (beware of the output format!)
if (check_dates(startTime,endTime)){
debug+='-dates match'
debug_counter++
tweet_time=time_TCL(tweet)//change here for a custom time retrieving function (beware of the output format!)
if (check_multi_times(tweet_time)){
debug+='-times match'
debug_counter++
event()
logs.insertRowBefore(2)
logs.getRange("A2:C2").setValues([[startTime,endTime,tweet]])
console.log([startTime,endTime,tweet])
debug_counter=0
return true
}
}
}
}
}
debug_phrase=["","-redundancy","-no matches for specified keywords","-no matches for specified dates","-no matches for specified times"]
console.log(debug+debug_phrase[debug_counter])// complete debug sentence: 'ON-no redundancy-keywords included-dates match-times match'
return false
}