Skip to content
Snippets Groups Projects
Select Git revision
  • 4566ac18e00da5b34ac60c02a2f42bf0278c6e3c
  • master default protected
2 results

cal.py

Blame
  • cal.py 3.47 KiB
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    import calendar
    import datetime
    from datetime import date, timedelta, datetime, time
    import dateutil.parser
    
    year = 2017
    first_day = date(year, 1, 1)
    last_day = date(year, 12, 31)
    
    def get_moon(f):
        # https://www.calendar-12.com/moon_phases/2017
        # writer->calc->cleanup->export csv ";"
        with open(f) as csv:
            for line in csv:
                phase_str, date_str, time_str = line.split(";")
                date_str = date_str.split(",", 1)[0]
                date = dateutil.parser.parse(date_str)
                hour_str, min_str = time_str.split(":")
                hour, minute = int(hour_str), int(min_str)
                moon_time = time(hour, minute)
                timestamp = datetime.combine(date, moon_time)
                phase = {"e": "", "i": "", "u": "", "a": ""}[phase_str[1]]
                yield timestamp.date(), (phase, timestamp)
    
    
    def get_holidays(f):
        # http://svatky.centrum.cz/svatky/statni-svatky/2017/
        # writer->calc->cleanup->export csv ";"
        with open(f) as csv:
            for line in csv:
                date_str, holiday_str = line.split(";")
                date = dateutil.parser.parse(date_str, dayfirst=True)
                holiday = holiday_str.strip()
                yield date.date(), holiday
    
    
    def get_happydays(f):
        with open(f) as csv:
            for line in csv:
                if line.startswith("exec"):
                    continue
                date_str, holiday_str, cat_str, name_str = line.split(":")
                date_str, holiday_str, cat_str, name_str = date_str.strip(), holiday_str.strip(), cat_str.strip(), name_str.strip()
                if not "VIP" in cat_str:
                    continue
                day, month, year = date_str.split(".")
                if not(year):
                    age_str = ""
                    year = datetime.now().year
                else:
                    year = int(year)
                    age = datetime.now().year - year
                    age_str = "(%d)" % age
                dt = date(year, int(month), int(day))
                yield dt, " ".join([name_str, age_str])
    
    
    def gen(year, moon, holidays, happydays):
        first_day = date(year, 1, 1)
        last_day = date(year, 12, 31)
        first_week_day = first_day - timedelta(days=first_day.weekday())
        last_week_day = last_day + timedelta(days=6-first_day.weekday())
        calendar_range = last_week_day - first_week_day
        week_count = (calendar_range.days+1) / 7
        for week in range(week_count):
            month_name = (first_week_day + timedelta(days = 7*week)).strftime("%B")
            wl = [str(week+1), month_name]  # week num, month name
            for day_of_week in range(7):
                cur = first_week_day + timedelta(days = 7*week + day_of_week)
                wl.append(cur.strftime("%A"))           # day name
                wl.append(str(cur.timetuple().tm_yday)) # day num in year
                wl.append(str(cur.day))                 # day in month
                moon_str = "%s %s" % (moon[cur][0], moon[cur][1].strftime("%H:%M")) if cur in moon else ""
                wl.append(moon_str)                     # moon phase
                holiday = holidays[cur] if cur in holidays else ""
                wl.append(holiday)                      # holiday
                happyday = happydays[cur] if cur in happydays else ""
                wl.append(happyday)                     # happyday
            print ";".join(wl)
    
    
    def main():
        moon = dict(get_moon("moon.csv"))
        holidays = dict(get_holidays("holidays.csv"))
        happydays = dict(get_happydays("../../etc/happydays.txt"))
        gen(2017, moon, holidays, happydays)
    
    main()