Beispiel #1
0
import sys
# Yeah this is a hack
sys.path.append('.')
sys.path.append('../')

from convert import Convert
from datetime import datetime, timedelta

convert = Convert()
convert.CSV_FILE_LOCATION = 'examples/BostonCruiseTerminalSchedule.csv'
convert.SAVE_LOCATION = 'examples/arrive.ics'
convert.HEADER_COLUMNS_TO_SKIP = 2
convert.NAME = 3
convert.START_DATE = 7
convert.END_DATE = 8
convert.DESCRIPTION = 6
convert.LOCATION = 9

convert.read_csv()
i = 0
while i < len(convert.csv_data):
    row = convert.csv_data[i]
    start_date = row[2] + '-'+row[convert.START_DATE]
    try:
        row[convert.START_DATE] = datetime.strptime(start_date, '%m/%d/%y-%H:%M')
        row[convert.END_DATE] = row[convert.START_DATE]+timedelta(hours=1)
        i += 1
    except:
        convert.csv_data.pop(i)
    row[convert.NAME] = 'Arrive '+row[convert.NAME]
    
def ICal_Generator(finallist_Ical, finalname_Ical, chat_type_Ical):
    #open calendar.csv file
    with open("calendar" + str(chat_type_Ical) + ".csv", "w") as c:
        headers_Ical = ("Subject", "StartDate", "StartTime", "EndDate",
                        "EndTime", "AllDayEvent", "Location", "Description")
        writer_Ical = csv.DictWriter(c, fieldnames=headers_Ical)
        writer_Ical.writeheader()
        # counter_Ical is to track the location of module NAME
        counter_Ical = -1
        # For each module added by the user
        for Module_Ical in finallist_Ical:
            counter_Ical += 1

            #write data into calendar.csv file
            for row_Ical in Module_Ical:
                #define variables here
                First_monday_Ical = "14/8/2017"  #supposed to be input value from telegram
                dates_list_Ical = []
                subject_Ical = finalname_Ical[counter_Ical] + " {}".format(
                    row_Ical[1]
                )  #subject located at this current row and first column
                starttime_Ical = row_Ical[
                    4]  #start time located at this current row and column 5th
                endtime_Ical = row_Ical[
                    5]  #end time located at this current row and column 6th
                alldayevent_Ical = "False"  #all day event is set to false
                description_Ical = " Group: %s \n %s" % (
                    row_Ical[2], row_Ical[7]
                )  #type of lession and group number is located at 2nd, 3rd, 8th column
                location_Ical = row_Ical[
                    6]  #location located at this current row and 7th column

                #----assigning values to days of wk and changing date accordingly ----#
                if (row_Ical[3] == str("MON")):
                    b = 0
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                elif (row_Ical[3] == str("TUE")):
                    b = 1
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                elif (row_Ical[3] == str("WED")):
                    b = 2
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                elif (row_Ical[3] == str("THU")):
                    b = 3
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                elif (row_Ical[3] == str("FRI")):
                    b = 4
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                elif (row_Ical[3] == str("SAT")):
                    b = 5
                    date_Ical = datetime.strptime(
                        First_monday_Ical, '%d/%m/%Y') + timedelta(days=b)
                    date_Ical = str(datetime.strftime(date_Ical, '%m/%d/%Y'))

                #----while loop to print/insert all possible dates into array----#
                wk_segment_Ical = row_Ical[7]
                if wk_segment_Ical == "":
                    total_wk_Ical = range(0, 13)
                else:
                    # Recall in this code, value wk 0 is representing actual wk 1 and wk cap is 12
                    total_wk_Ical = [
                    ]  # To input the int value of week with lesson
                    wk_segment_Ical = wk_segment_Ical.replace(
                        "Wk", "")  # Remove the "Wk" in the string
                    wk_segment_Ical = wk_segment_Ical.split(",")
                    for segment_counter_Ical in range(0, len(wk_segment_Ical)):
                        if "-" in wk_segment_Ical[segment_counter_Ical]:
                            segment_Ical = wk_segment_Ical[
                                segment_counter_Ical].split("-")
                            start_segment_Ical = int(segment_Ical[0]) - 1
                            end_segment_Ical = int(segment_Ical[1])
                            range_Ical = range(start_segment_Ical,
                                               end_segment_Ical)
                            for each_wk_Ical in range_Ical:
                                total_wk_Ical.append((each_wk_Ical))
                        else:
                            total_wk_Ical.append(
                                (int(wk_segment_Ical[segment_counter_Ical])) -
                                1)

                print("POTATO")
                print(total_wk_Ical)

                for wk_Ical in range(0, 13):
                    if (wk_Ical != 6):
                        if wk_Ical in total_wk_Ical:
                            dates_list_Ical.append(date_Ical)
                            print(dates_list_Ical)
                        date_Ical = datetime.strptime(
                            date_Ical, '%m/%d/%Y') + timedelta(days=7)
                        date_Ical = str(
                            datetime.strftime(date_Ical, '%m/%d/%Y'))

                    else:
                        if wk_Ical in total_wk_Ical:
                            dates_list_Ical.append(date_Ical)
                            print(dates_list_Ical)
                        date_Ical = datetime.strptime(
                            date_Ical, '%m/%d/%Y') + timedelta(days=14)
                        date_Ical = str(
                            datetime.strftime(date_Ical, '%m/%d/%Y'))

                    #----while loop to print/insert all possible dates into array----#
                for dcol_Ical in range(0, len(total_wk_Ical)):
                    start_end_date_Ical = dates_list_Ical[dcol_Ical]
                    writer_Ical.writerow({"Subject" : subject_Ical, "StartDate" : start_end_date_Ical , "StartTime" : starttime_Ical , \
                                     "EndDate" : start_end_date_Ical , "EndTime" : endtime_Ical , "AllDayEvent" : alldayevent_Ical , \
                                     "Description" : description_Ical, "Location" : location_Ical})

    #Initializing Converter for CSV to ICS file,
    #each variable equivalent to a column in the csv file generated
    convert = Convert()
    convert.CSV_FILE_LOCATION = "calendar" + str(chat_type_Ical) + ".csv"
    convert.SAVE_LOCATION = "calendar" + str(chat_type_Ical) + ".ics"
    convert.HEADER_COLUMNS_TO_SKIP = 0

    convert.NAME = 0
    convert.START_DATE = 1
    convert.END_DATE = 2
    convert.DESCRIPTION = 7
    convert.LOCATION = 6

    CSV_Data = convert.csv_data
    # Create a list for data in csv
    convert.read_csv()
    # convert.csv_data is the returned value(list) of convert.read_csv

    # To remove headings
    i = 1
    while i < (len(CSV_Data)):
        CSV_Row = CSV_Data[i]
        print(CSV_Row)
        if CSV_Row != []:
            # Order should be [EVENTNAME, START_DATE, END_DATE, DESCRIPTION, LOCATION]
            CSVstart_date = str(CSV_Row[1]) + '-' + str(CSV_Row[2])
            CSVend_date = str(CSV_Row[3]) + '-' + str(CSV_Row[4])
            CSV_Data[i][convert.START_DATE] = datetime.strptime(
                CSVstart_date, '%m/%d/%Y-%H:%M')
            CSV_Data[i][convert.END_DATE] = datetime.strptime(
                CSVend_date, '%m/%d/%Y-%H:%M')
            i += 1
            print("added")
        else:
            CSV_Data.pop(i)
            # For Debugging
            #print(CSV_Data)

    convert.csv_data = CSV_Data[1:]
    print(convert.csv_data)
    convert.make_ical()
    #print (convert.cal)                        #Debug check for ical file
    convert.save_ical()