summaryrefslogtreecommitdiff
path: root/python-i3-agenda.spec
blob: 10fcabb6530f7e2c7f58bc9fd80be92359d9fe36 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
%global _empty_manifest_terminate_build 0
Name:		python-i3-agenda
Version:	1.7
Release:	1
Summary:	Show your next google calendar event in polybar or i3-bar
License:	Unlicense
URL:		https://github.com/rosenpin/i3-agenda
Source0:	https://mirrors.aliyun.com/pypi/web/packages/62/46/c8b2d84d4fd286bbaf395ec84a845ec80efc8ad760d3fd441cc055551136/i3-agenda-1.7.tar.gz
BuildArch:	noarch

Requires:	python3-bidi
Requires:	python3-google-api-python-client
Requires:	python3-google-auth-httplib2
Requires:	python3-google-auth-oauthlib
Requires:	python3-aiohttp

%description
[![AUR version](https://img.shields.io/aur/version/i3-agenda?style=flat-square&logo=arch-linux)](https://aur.archlinux.org/packages/i3-agenda/)
[![PyPI](https://img.shields.io/pypi/v/i3-agenda?style=flat-square&logo=python)](https://pypi.org/project/i3-agenda/) <a href="https://github.com/rosenpin/i3-agenda/releases"><img src="https://img.shields.io/github/commits-since/rosenpin/i3-agenda/latest.svg?color=green" alt="Version Badge"></a> 
<a target="_blank" href="https://www.paypal.com/donate/?hosted_button_id=9M8LL9GJXELBN" title="Donate using PayPal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" /></a>

# What is this?
It's a script that communicates with Google's calendar API, it will go through your calendars and print the next calendar event details.\
You can take this output and show it on your i3-bar or polybar

![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)

## How does it work
It will read your next 10 events from each of your calendars, then go through them all and figure out which one is closest.\
It will print the time and title of the closest event.

# Setup

## Google API
https://developers.google.com/calendar/quickstart/python

1. You need to create a Google API project and download your OAuth 2.0 credentials json file.\
You first need to create a project [here](https://console.developers.google.com/apis/credentials), then add Google Calendar support, then download the credentials.json file.\
**Alternatively, you can just use [this link](https://developers.google.com/calendar/quickstart/python) and click "Enable the Google Calendar API". This will create a project, add Google Calendar support, and let you download the file in 1 click**.\
If you're having trouble, you can use this tutorial for more information [https://developers.google.com/calendar/auth](https://developers.google.com/calendar/auth).\
Another great guide can be found here if you're still having trouble: [https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile](https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile).
2. Download the credentials file to somewhere on your computer.
3. Proceed to installation phase.

## Installation
After downloading the credentials file, install the package.

### Pip
1. `sudo pip install i3-agenda`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Arch Linux (AUR)
1. `yay -S i3-agenda-git`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Manual
#### Dependencies
You need to install some python libraries first.\
Make sure python3 is your default python.\
Run `sudo pip3 install python-bidi google-api-python-client google-auth-httplib2 google-auth-oauthlib`

1. Clone the repo to a local directory `cd ~/ && git clone https://github.com/rosenpin/i3-agenda && cd i3-agenda`
3. Run the script `python3 i3_agenda/__init__.py -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step. If configured correctly, it will prompt you to log in in your browser, accept everything. It should print your next event.
4. Optional: you can run `sudo python setup.py install` to add the script to your path so you can run `i3-agenda` anywhere.
5. Add configuration to your bar (examples in the Examples section below).

# Usage
```
  -h, --help            show this help message and exit
  --credentials CREDENTIALS, -c CREDENTIALS
                        path to your credentials.json file
  --conf CONF, -cd CONF
                        path to the i3agenda configuration and cache folder
  --cachettl CACHETTL, -ttl CACHETTL
                        time for cache to be kept in minutes
  --update, -u          when using this flag it will not load previous results from cache, it will however save new results to cache.
                        You can use this flag to refresh all the cache forcefully
  --ids IDS [IDS ...], -i IDS [IDS ...]
                        list of calendar ids to fetch, space separated. If  none is specified all calendars will be fetched
  --maxres MAXRES, -r MAXRES
                        max number of events to query Google's API for each of your calendars.
                        Increase this number if you have lot of events in your google calendar
  --today, -d           print only today events
  --no-event-text TEXT  text to display when there are no events
  --hide-event-after HIDE_EVENT_AFTER
                        minutes to show events after they start before showing the next event.
                        If not specified, the current event will be shown until it ends
  --show-event-before SHOW_EVENT_BEFORE
                        minutes to show events before they start. If not specified, the next event will be shown regardless of when it starts
  --date-format DATE_FORMAT
                        the date format like %d/%m/%y. Default is %d/%m
  --limchar LIMCHAR, -l LIMCHAR
                        the max characters that the displayed event can contain
  --skip SKIP, -s SKIP  the number of events to skip from the most recent
```

### Filter displayed calendars
To display events only from certain calendars use the `--ids` parameter and pass a list of calendar id, space separated.\
To obtain the calendar id you can check the settings page of the calendar on Google (usually is the owner email, if it's not shared).\
Leaving the list empty will fetch all calendars (default behavior).

## Notes
### Known issues
It might not work properly if you have more than 10 all day events, this can be fixed by increasing the maxResults variable.

### RTL support
If you use RTL or some of your events contain RTL languages, you will need to pipe [pybidi](https://pypi.org/project/python-bidi/) with the script. Example:  
`i3-agenda -c ~/.google_credentials.json -ttl 60 | pybidi`

### Caching
It uses a caching mechanism so you won't have to contact Google servers every minute, to set the cache TTL use the -ttl flag.\
Example: `i3-agenda --ttl 60` to set the TTL to 60 (meaning it will contact Google again every hour).\
This means that if you create a new event, it might take an hour for the script to recognize it.

### Multi account support
Multi account support is not officialy supported, but you can use the workaround from this issue: https://github.com/rosenpin/i3-agenda/issues/35#issuecomment-923976482

## Examples
### Example [polybar](https://github.com/polybar/polybar) configuration:
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
``` ini
modules-center = agenda
....
[module/agenda]
type = custom/script
; Show the next event and forget cache automatically every 60 minutes
exec = i3-agenda -c ~/.google_credentials.json -ttl 60
; left click to launch Google Calendar
click-left = chromium https://calendar.google.com/calendar/r/day
; right click force update the cache, if for example you just added a new event
click-right = notify-send "syncing..." && i3-agenda -c ~/.google_credentials.json --update && notify-send -t 2000 "sync finished"
interval = 60
```

### Example [SwiftBar](https://github.com/swiftbar/SwiftBar) configuration
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)
This will show your next event as the menu bar title, when you press it you will see a dropdown with all your today events  
You can call the file `agenda.2m.sh` to make it refresh every 2 minutes
``` bash
#!/bin/bash
# <swiftbar.hideAbout>true</swiftbar.hideAbout>

i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30

echo "---"
href="href='https://calendar.google.com/calendar/u/0/r/'"

i=1
while :; do 
    event=$(i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30 --skip $i --today)
    ((i++))
    if [[ "$event" == "No events" ]];then
        exit 0
    fi
    echo "$event | $href"
done
```

### Example i3block configuration:
```ini
[i3-agenda]
command=i3-agenda -c ~/.google_credentials.json -ttl 60
interval=60
```


Example output of the script:\
```10:55 Grocery shopping```

### How to use the `skip` flag to scroll events

Edit the polybar configuration creating two modules:

```ini
[module/agenda-ipc]
type = custom/ipc

hook-0 = i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-1 = ~/.config/polybar/scripts/i3agenda-onscroll.sh down && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-2 = ~/.config/polybar/scripts/i3agenda-onscroll.sh up && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)

format = %{F#61afef}%{F-} <output>

; left click to launch Google Calendar
click-left = firefox https://calendar.google.com/calendar/u/0/r
; right click force update the cache, if for example you just added a new event
click-right = rm ~/.config/i3-agenda/i3-agenda-skip.tmp; i3-agenda -c ~/.config/i3-agenda/client_secret.json --update && notify-send "i3-agenda" "Sync completed"

; show the previous event
scroll-down = polybar-msg hook agenda-ipc 2
; show the next event
scroll-up = polybar-msg hook agenda-ipc 3

[module/agenda]
type = custom/script
interval = 900
exec = polybar-msg hook agenda-ipc 1
label =
```

Add both modules to the bar, for example:

```ini
modules-center = agenda agenda-ipc
```

In the polybar scripts folder add the file `i3agenda-onscroll.sh`:

```bash
#!/usr/bin/env bash
if [ -n "${1}" ]; then
  file=~/.config/i3-agenda/i3-agenda-skip.tmp
  typeset -i skip=$(cat $file || echo 0)
  if [[ "${1}" == "up" ]]; then
    skip+=1
  elif [[ "${1}" == "down" && $skip -gt 0 ]]; then
    skip=$(( skip - 1))
  fi
  echo $skip > $file
fi
```


%package -n python3-i3-agenda
Summary:	Show your next google calendar event in polybar or i3-bar
Provides:	python-i3-agenda
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-i3-agenda
[![AUR version](https://img.shields.io/aur/version/i3-agenda?style=flat-square&logo=arch-linux)](https://aur.archlinux.org/packages/i3-agenda/)
[![PyPI](https://img.shields.io/pypi/v/i3-agenda?style=flat-square&logo=python)](https://pypi.org/project/i3-agenda/) <a href="https://github.com/rosenpin/i3-agenda/releases"><img src="https://img.shields.io/github/commits-since/rosenpin/i3-agenda/latest.svg?color=green" alt="Version Badge"></a> 
<a target="_blank" href="https://www.paypal.com/donate/?hosted_button_id=9M8LL9GJXELBN" title="Donate using PayPal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" /></a>

# What is this?
It's a script that communicates with Google's calendar API, it will go through your calendars and print the next calendar event details.\
You can take this output and show it on your i3-bar or polybar

![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)

## How does it work
It will read your next 10 events from each of your calendars, then go through them all and figure out which one is closest.\
It will print the time and title of the closest event.

# Setup

## Google API
https://developers.google.com/calendar/quickstart/python

1. You need to create a Google API project and download your OAuth 2.0 credentials json file.\
You first need to create a project [here](https://console.developers.google.com/apis/credentials), then add Google Calendar support, then download the credentials.json file.\
**Alternatively, you can just use [this link](https://developers.google.com/calendar/quickstart/python) and click "Enable the Google Calendar API". This will create a project, add Google Calendar support, and let you download the file in 1 click**.\
If you're having trouble, you can use this tutorial for more information [https://developers.google.com/calendar/auth](https://developers.google.com/calendar/auth).\
Another great guide can be found here if you're still having trouble: [https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile](https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile).
2. Download the credentials file to somewhere on your computer.
3. Proceed to installation phase.

## Installation
After downloading the credentials file, install the package.

### Pip
1. `sudo pip install i3-agenda`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Arch Linux (AUR)
1. `yay -S i3-agenda-git`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Manual
#### Dependencies
You need to install some python libraries first.\
Make sure python3 is your default python.\
Run `sudo pip3 install python-bidi google-api-python-client google-auth-httplib2 google-auth-oauthlib`

1. Clone the repo to a local directory `cd ~/ && git clone https://github.com/rosenpin/i3-agenda && cd i3-agenda`
3. Run the script `python3 i3_agenda/__init__.py -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step. If configured correctly, it will prompt you to log in in your browser, accept everything. It should print your next event.
4. Optional: you can run `sudo python setup.py install` to add the script to your path so you can run `i3-agenda` anywhere.
5. Add configuration to your bar (examples in the Examples section below).

# Usage
```
  -h, --help            show this help message and exit
  --credentials CREDENTIALS, -c CREDENTIALS
                        path to your credentials.json file
  --conf CONF, -cd CONF
                        path to the i3agenda configuration and cache folder
  --cachettl CACHETTL, -ttl CACHETTL
                        time for cache to be kept in minutes
  --update, -u          when using this flag it will not load previous results from cache, it will however save new results to cache.
                        You can use this flag to refresh all the cache forcefully
  --ids IDS [IDS ...], -i IDS [IDS ...]
                        list of calendar ids to fetch, space separated. If  none is specified all calendars will be fetched
  --maxres MAXRES, -r MAXRES
                        max number of events to query Google's API for each of your calendars.
                        Increase this number if you have lot of events in your google calendar
  --today, -d           print only today events
  --no-event-text TEXT  text to display when there are no events
  --hide-event-after HIDE_EVENT_AFTER
                        minutes to show events after they start before showing the next event.
                        If not specified, the current event will be shown until it ends
  --show-event-before SHOW_EVENT_BEFORE
                        minutes to show events before they start. If not specified, the next event will be shown regardless of when it starts
  --date-format DATE_FORMAT
                        the date format like %d/%m/%y. Default is %d/%m
  --limchar LIMCHAR, -l LIMCHAR
                        the max characters that the displayed event can contain
  --skip SKIP, -s SKIP  the number of events to skip from the most recent
```

### Filter displayed calendars
To display events only from certain calendars use the `--ids` parameter and pass a list of calendar id, space separated.\
To obtain the calendar id you can check the settings page of the calendar on Google (usually is the owner email, if it's not shared).\
Leaving the list empty will fetch all calendars (default behavior).

## Notes
### Known issues
It might not work properly if you have more than 10 all day events, this can be fixed by increasing the maxResults variable.

### RTL support
If you use RTL or some of your events contain RTL languages, you will need to pipe [pybidi](https://pypi.org/project/python-bidi/) with the script. Example:  
`i3-agenda -c ~/.google_credentials.json -ttl 60 | pybidi`

### Caching
It uses a caching mechanism so you won't have to contact Google servers every minute, to set the cache TTL use the -ttl flag.\
Example: `i3-agenda --ttl 60` to set the TTL to 60 (meaning it will contact Google again every hour).\
This means that if you create a new event, it might take an hour for the script to recognize it.

### Multi account support
Multi account support is not officialy supported, but you can use the workaround from this issue: https://github.com/rosenpin/i3-agenda/issues/35#issuecomment-923976482

## Examples
### Example [polybar](https://github.com/polybar/polybar) configuration:
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
``` ini
modules-center = agenda
....
[module/agenda]
type = custom/script
; Show the next event and forget cache automatically every 60 minutes
exec = i3-agenda -c ~/.google_credentials.json -ttl 60
; left click to launch Google Calendar
click-left = chromium https://calendar.google.com/calendar/r/day
; right click force update the cache, if for example you just added a new event
click-right = notify-send "syncing..." && i3-agenda -c ~/.google_credentials.json --update && notify-send -t 2000 "sync finished"
interval = 60
```

### Example [SwiftBar](https://github.com/swiftbar/SwiftBar) configuration
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)
This will show your next event as the menu bar title, when you press it you will see a dropdown with all your today events  
You can call the file `agenda.2m.sh` to make it refresh every 2 minutes
``` bash
#!/bin/bash
# <swiftbar.hideAbout>true</swiftbar.hideAbout>

i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30

echo "---"
href="href='https://calendar.google.com/calendar/u/0/r/'"

i=1
while :; do 
    event=$(i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30 --skip $i --today)
    ((i++))
    if [[ "$event" == "No events" ]];then
        exit 0
    fi
    echo "$event | $href"
done
```

### Example i3block configuration:
```ini
[i3-agenda]
command=i3-agenda -c ~/.google_credentials.json -ttl 60
interval=60
```


Example output of the script:\
```10:55 Grocery shopping```

### How to use the `skip` flag to scroll events

Edit the polybar configuration creating two modules:

```ini
[module/agenda-ipc]
type = custom/ipc

hook-0 = i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-1 = ~/.config/polybar/scripts/i3agenda-onscroll.sh down && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-2 = ~/.config/polybar/scripts/i3agenda-onscroll.sh up && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)

format = %{F#61afef}%{F-} <output>

; left click to launch Google Calendar
click-left = firefox https://calendar.google.com/calendar/u/0/r
; right click force update the cache, if for example you just added a new event
click-right = rm ~/.config/i3-agenda/i3-agenda-skip.tmp; i3-agenda -c ~/.config/i3-agenda/client_secret.json --update && notify-send "i3-agenda" "Sync completed"

; show the previous event
scroll-down = polybar-msg hook agenda-ipc 2
; show the next event
scroll-up = polybar-msg hook agenda-ipc 3

[module/agenda]
type = custom/script
interval = 900
exec = polybar-msg hook agenda-ipc 1
label =
```

Add both modules to the bar, for example:

```ini
modules-center = agenda agenda-ipc
```

In the polybar scripts folder add the file `i3agenda-onscroll.sh`:

```bash
#!/usr/bin/env bash
if [ -n "${1}" ]; then
  file=~/.config/i3-agenda/i3-agenda-skip.tmp
  typeset -i skip=$(cat $file || echo 0)
  if [[ "${1}" == "up" ]]; then
    skip+=1
  elif [[ "${1}" == "down" && $skip -gt 0 ]]; then
    skip=$(( skip - 1))
  fi
  echo $skip > $file
fi
```


%package help
Summary:	Development documents and examples for i3-agenda
Provides:	python3-i3-agenda-doc
%description help
[![AUR version](https://img.shields.io/aur/version/i3-agenda?style=flat-square&logo=arch-linux)](https://aur.archlinux.org/packages/i3-agenda/)
[![PyPI](https://img.shields.io/pypi/v/i3-agenda?style=flat-square&logo=python)](https://pypi.org/project/i3-agenda/) <a href="https://github.com/rosenpin/i3-agenda/releases"><img src="https://img.shields.io/github/commits-since/rosenpin/i3-agenda/latest.svg?color=green" alt="Version Badge"></a> 
<a target="_blank" href="https://www.paypal.com/donate/?hosted_button_id=9M8LL9GJXELBN" title="Donate using PayPal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" /></a>

# What is this?
It's a script that communicates with Google's calendar API, it will go through your calendars and print the next calendar event details.\
You can take this output and show it on your i3-bar or polybar

![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)

## How does it work
It will read your next 10 events from each of your calendars, then go through them all and figure out which one is closest.\
It will print the time and title of the closest event.

# Setup

## Google API
https://developers.google.com/calendar/quickstart/python

1. You need to create a Google API project and download your OAuth 2.0 credentials json file.\
You first need to create a project [here](https://console.developers.google.com/apis/credentials), then add Google Calendar support, then download the credentials.json file.\
**Alternatively, you can just use [this link](https://developers.google.com/calendar/quickstart/python) and click "Enable the Google Calendar API". This will create a project, add Google Calendar support, and let you download the file in 1 click**.\
If you're having trouble, you can use this tutorial for more information [https://developers.google.com/calendar/auth](https://developers.google.com/calendar/auth).\
Another great guide can be found here if you're still having trouble: [https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile](https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile).
2. Download the credentials file to somewhere on your computer.
3. Proceed to installation phase.

## Installation
After downloading the credentials file, install the package.

### Pip
1. `sudo pip install i3-agenda`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Arch Linux (AUR)
1. `yay -S i3-agenda-git`
2. Try running `i3-agenda -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step.
3. Add configuration to your bar (examples in the Examples section below).

### Manual
#### Dependencies
You need to install some python libraries first.\
Make sure python3 is your default python.\
Run `sudo pip3 install python-bidi google-api-python-client google-auth-httplib2 google-auth-oauthlib`

1. Clone the repo to a local directory `cd ~/ && git clone https://github.com/rosenpin/i3-agenda && cd i3-agenda`
3. Run the script `python3 i3_agenda/__init__.py -c $CREDENTIALS_FILE_PATH` with "$CREDENTIALS_FILE_PATH" replaced with the path to the credentials.json file you downloaded in the previous step. If configured correctly, it will prompt you to log in in your browser, accept everything. It should print your next event.
4. Optional: you can run `sudo python setup.py install` to add the script to your path so you can run `i3-agenda` anywhere.
5. Add configuration to your bar (examples in the Examples section below).

# Usage
```
  -h, --help            show this help message and exit
  --credentials CREDENTIALS, -c CREDENTIALS
                        path to your credentials.json file
  --conf CONF, -cd CONF
                        path to the i3agenda configuration and cache folder
  --cachettl CACHETTL, -ttl CACHETTL
                        time for cache to be kept in minutes
  --update, -u          when using this flag it will not load previous results from cache, it will however save new results to cache.
                        You can use this flag to refresh all the cache forcefully
  --ids IDS [IDS ...], -i IDS [IDS ...]
                        list of calendar ids to fetch, space separated. If  none is specified all calendars will be fetched
  --maxres MAXRES, -r MAXRES
                        max number of events to query Google's API for each of your calendars.
                        Increase this number if you have lot of events in your google calendar
  --today, -d           print only today events
  --no-event-text TEXT  text to display when there are no events
  --hide-event-after HIDE_EVENT_AFTER
                        minutes to show events after they start before showing the next event.
                        If not specified, the current event will be shown until it ends
  --show-event-before SHOW_EVENT_BEFORE
                        minutes to show events before they start. If not specified, the next event will be shown regardless of when it starts
  --date-format DATE_FORMAT
                        the date format like %d/%m/%y. Default is %d/%m
  --limchar LIMCHAR, -l LIMCHAR
                        the max characters that the displayed event can contain
  --skip SKIP, -s SKIP  the number of events to skip from the most recent
```

### Filter displayed calendars
To display events only from certain calendars use the `--ids` parameter and pass a list of calendar id, space separated.\
To obtain the calendar id you can check the settings page of the calendar on Google (usually is the owner email, if it's not shared).\
Leaving the list empty will fetch all calendars (default behavior).

## Notes
### Known issues
It might not work properly if you have more than 10 all day events, this can be fixed by increasing the maxResults variable.

### RTL support
If you use RTL or some of your events contain RTL languages, you will need to pipe [pybidi](https://pypi.org/project/python-bidi/) with the script. Example:  
`i3-agenda -c ~/.google_credentials.json -ttl 60 | pybidi`

### Caching
It uses a caching mechanism so you won't have to contact Google servers every minute, to set the cache TTL use the -ttl flag.\
Example: `i3-agenda --ttl 60` to set the TTL to 60 (meaning it will contact Google again every hour).\
This means that if you create a new event, it might take an hour for the script to recognize it.

### Multi account support
Multi account support is not officialy supported, but you can use the workaround from this issue: https://github.com/rosenpin/i3-agenda/issues/35#issuecomment-923976482

## Examples
### Example [polybar](https://github.com/polybar/polybar) configuration:
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/screenshot.png)
``` ini
modules-center = agenda
....
[module/agenda]
type = custom/script
; Show the next event and forget cache automatically every 60 minutes
exec = i3-agenda -c ~/.google_credentials.json -ttl 60
; left click to launch Google Calendar
click-left = chromium https://calendar.google.com/calendar/r/day
; right click force update the cache, if for example you just added a new event
click-right = notify-send "syncing..." && i3-agenda -c ~/.google_credentials.json --update && notify-send -t 2000 "sync finished"
interval = 60
```

### Example [SwiftBar](https://github.com/swiftbar/SwiftBar) configuration
![example](https://raw.githubusercontent.com/rosenpin/i3-agenda/master/art/mac_screenshot.png)
This will show your next event as the menu bar title, when you press it you will see a dropdown with all your today events  
You can call the file `agenda.2m.sh` to make it refresh every 2 minutes
``` bash
#!/bin/bash
# <swiftbar.hideAbout>true</swiftbar.hideAbout>

i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30

echo "---"
href="href='https://calendar.google.com/calendar/u/0/r/'"

i=1
while :; do 
    event=$(i3-agenda -c ~/.google_credentials.json -ttl 60 --limchar 30 --skip $i --today)
    ((i++))
    if [[ "$event" == "No events" ]];then
        exit 0
    fi
    echo "$event | $href"
done
```

### Example i3block configuration:
```ini
[i3-agenda]
command=i3-agenda -c ~/.google_credentials.json -ttl 60
interval=60
```


Example output of the script:\
```10:55 Grocery shopping```

### How to use the `skip` flag to scroll events

Edit the polybar configuration creating two modules:

```ini
[module/agenda-ipc]
type = custom/ipc

hook-0 = i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-1 = ~/.config/polybar/scripts/i3agenda-onscroll.sh down && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)
hook-2 = ~/.config/polybar/scripts/i3agenda-onscroll.sh up && i3-agenda -c ~/.google_credentials.json --skip $(cat ~/.config/i3-agenda/i3-agenda-skip.tmp || echo 0)

format = %{F#61afef}%{F-} <output>

; left click to launch Google Calendar
click-left = firefox https://calendar.google.com/calendar/u/0/r
; right click force update the cache, if for example you just added a new event
click-right = rm ~/.config/i3-agenda/i3-agenda-skip.tmp; i3-agenda -c ~/.config/i3-agenda/client_secret.json --update && notify-send "i3-agenda" "Sync completed"

; show the previous event
scroll-down = polybar-msg hook agenda-ipc 2
; show the next event
scroll-up = polybar-msg hook agenda-ipc 3

[module/agenda]
type = custom/script
interval = 900
exec = polybar-msg hook agenda-ipc 1
label =
```

Add both modules to the bar, for example:

```ini
modules-center = agenda agenda-ipc
```

In the polybar scripts folder add the file `i3agenda-onscroll.sh`:

```bash
#!/usr/bin/env bash
if [ -n "${1}" ]; then
  file=~/.config/i3-agenda/i3-agenda-skip.tmp
  typeset -i skip=$(cat $file || echo 0)
  if [[ "${1}" == "up" ]]; then
    skip+=1
  elif [[ "${1}" == "down" && $skip -gt 0 ]]; then
    skip=$(( skip - 1))
  fi
  echo $skip > $file
fi
```


%prep
%autosetup -n i3-agenda-1.7

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-i3-agenda -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 1.7-1
- Package Spec generated