Skip to content

Instantly share code, notes, and snippets.

@quantum5
Created February 4, 2020 04:49
Show Gist options
  • Save quantum5/e950203d73e016ba97622241a883cdb3 to your computer and use it in GitHub Desktop.
Save quantum5/e950203d73e016ba97622241a883cdb3 to your computer and use it in GitHub Desktop.
Wilderness Warbands calendar event generator (for RuneScape)
import argparse
import sys
import time
import uuid
from datetime import datetime, timedelta, timezone
ICAL_TIME = '%Y%m%dT%H%M%SZ'
def generate(f, begin=None, alarms=None, opaque=True, duration=15):
now = datetime.now(timezone.utc)
unixhour = int((begin or now).timestamp()) // 3600
if unixhour % 7 != 0:
unixhour += 7 - unixhour % 7
first = datetime.fromtimestamp(3600 * unixhour, tz=timezone.utc)
def write(line, *args):
print(line % args, file=f)
def write_time(line, time):
print('%s:%s' % (line, time.strftime(ICAL_TIME)), file=f)
write('BEGIN:VCALENDAR')
write('PRODID:-//Quantum//NONSGML Warbands Calendar//EN')
write('VERSION:2.0')
for i in range(24):
start = first + timedelta(hours=7) * i
end = start + timedelta(minutes=duration)
write('BEGIN:VEVENT')
write_time('CREATED', now)
write_time('LAST-MODIFIED', now)
write_time('DTSTAMP', now)
write('UID:%s', uuid.uuid4())
write('SUMMARY:Wilderness Warbands')
write('RRULE:FREQ=WEEKLY')
write_time('DTSTART', start)
write_time('DTEND', end)
write('TRANSP:%s', 'OPAQUE' if opaque else 'TRANSPARENT')
write('SEQUENCE:1')
for alarm in alarms or []:
write('BEGIN:VALARM')
write('ACTION:DISPLAY')
write('TRIGGER;VALUE=DURATION:-PT%dM', alarm)
write('DESCRIPTION:Wilderness Warbands starts in %d minutes!', alarm)
write('END:VALARM')
write('END:VEVENT')
write('END:VCALENDAR')
def main():
parser = argparse.ArgumentParser(description='Wilderness Warbands calendar generator.')
parser.add_argument('output', nargs='?', type=argparse.FileType('w'), default=sys.stdout,
help='file to write calendar to')
parser.add_argument('-a', '--alarm', type=int, action='append', metavar='N',
help='set an alarm N minutes before the event (can be repeated, default: 5 minutes)')
parser.add_argument('-n', '--no-alarm', action='store_true',
help='disable alarms')
parser.add_argument('--opaque', action='store_true', default=True,
help='make events opaque (marked as busy, default)')
parser.add_argument('-t', '--transparent', action='store_false', dest='opaque',
help='make events transparent (not marked as busy)')
parser.add_argument('-d', '--duration', type=int, default=15,
help='duration of warband event in minutes (default: 15)')
args = parser.parse_args()
with args.output:
generate(args.output, alarms=[] if args.no_alarm else args.alarm or [5],
opaque=args.opaque, duration=args.duration)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment