Exemplo n.º 1
0
def main():

    # Get your timetable
    with open(INPUT_FILENAME) as data_file:
        data = json.load(data_file)
    # Get subjects code and their respective name
    with open('subjects.json') as data_file:
        subjects = json.load(data_file)
    for day in data:
        startDates = [next_weekday(x[0], days[day]) for x in WORKING_DAYS]

        for time in data[day]:
            # parsing time from time_table dict
            # currently we only parse the starting time
            # duration of the event is rounded off to the closest hour
            # i.e 17:00 - 17:55 will be shown as 17:00 - 18:00

            parse_results = timetable_dict_parser.findall(time)[0]

            lectureBeginsStamps = [get_stamp(parse_results[:3], start) \
                                                        for start in startDates]

            durationInHours = data[day][time][2]

            # Find the name of this course
            # Use subject name if available, else ask the user for the subject
            # name and use that
            # TODO: Add labs to `subjects.json`
            subject_code = data[day][time][0]
            summary = subject_code
            description = subject_code
            if (subject_code in subjects.keys()):
                summary = subjects[subject_code].title()
            else:
                print('ERROR: Our subjects database does not have %s in it.' %
                      subject_code)
                summary = input(
                    'INPUT: Please input the name of the course %s: ' %
                    subject_code)

                subjects[subject_code] = str(summary)

                summary = summary.title()

            # Find location of this class
            location = data[day][time][1]

            for lectureBegin, [periodBegin, periodEnd] in \
                    zip(lectureBeginsStamps, WORKING_DAYS):

                event = build_event.build_event_duration(
                    summary, description, lectureBegin, durationInHours,
                    location, "weekly", periodEnd)

                cal.add_component(event)

            if (DEBUG):
                print(event)

    with open(OUTPUT_FILENAME, 'wb') as f:
        f.write(cal.to_ical())
        print("INFO: Your timetable has been written to %s" % OUTPUT_FILENAME)
Exemplo n.º 2
0
def user():
    user_id = request.form['user_id']
    token = request.form['sessionToken']
    login_details = {
        'user_id': user_id,
        'password': request.form['password'],
        'answer': request.form['security_answer'],
        'sessionToken': token,
        'requestedUrl': 'https://erp.iitkgp.ernet.in/IIT_ERP3',
    }
    s = session_dict[token]
    r = s.post(ERP_LOGIN_URL, data=login_details, headers=headers)
    print(r)
    ssoToken = re.search(r'\?ssoToken=(.+)$',
                         r.history[1].headers['Location']).group(1)

    ERP_TIMETABLE_URL = "https://erp.iitkgp.ernet.in/Acad/student/view_stud_time_table.jsp"

    timetable_details = {
        'ssoToken': ssoToken,
        'module_id': '16',
        'menu_id': '40',
    }

    # This is just a hack to get cookies. TODO: do the standard thing here
    abc = s.post(
        'https://erp.iitkgp.ernet.in/Acad/student/view_stud_time_table.jsp',
        headers=headers,
        data=timetable_details)
    cookie_val = None
    for a in s.cookies:
        if (a.path == "/Acad/"):
            cookie_val = a.value

    cookie = {
        'JSESSIONID': cookie_val,
    }
    r = s.post(
        'https://erp.iitkgp.ernet.in/Acad/student/view_stud_time_table.jsp',
        cookies=cookie,
        headers=headers,
        data=timetable_details)

    soup = bs(r.text, 'html.parser')
    rows_head = soup.findAll('table')[2]
    rows = rows_head.findAll('tr')
    times = []

    for a in rows[0].findAll('td'):
        if ('AM' in a.text or 'PM' in a.text):
            times.append(a.text)

    #### For timings end
    days = {}
    #### For day
    days[1] = "Monday"
    days[2] = "Tuesday"
    days[3] = "Wednesday"
    days[4] = "Thursday"
    days[5] = "Friday"
    days[6] = "Saturday"
    #### For day end

    timetable_dict = {}

    for i in range(1, len(rows)):
        timetable_dict[days[i]] = {}
        tds = rows[i].findAll('td')
        time = 0
        for a in range(1, len(tds)):
            txt = tds[a].find('b').text.strip()
            if (len(txt) >= 7):
                timetable_dict[days[i]][times[time]] = list(
                    (tds[a].find('b').text[:7], tds[a].find('b').text[7:],
                     int(tds[a]._attr_value_as_string('colspan'))))
            time = time + int(tds[a]._attr_value_as_string('colspan'))

    def merge_slots(in_dict):
        for a in in_dict:
            in_dict[a] = sorted(in_dict[a])
            for i in range(len(in_dict[a]) - 1, 0, -1):
                if (in_dict[a][i][0] == in_dict[a][i - 1][0] +
                        in_dict[a][i - 1][1]):
                    in_dict[a][i -
                               1][1] = in_dict[a][i][1] + in_dict[a][i - 1][1]
                    in_dict[a].remove(in_dict[a][i])
            in_dict[a] = in_dict[a][0]
        return (in_dict)

    for day in timetable_dict.keys():
        subject_timings = {}
        for time in timetable_dict[day]:
            flattened_time = int(time[:time.find(':')])
            if (flattened_time < 6):
                flattened_time += 12
            if (not timetable_dict[day][time][0] in subject_timings.keys()):
                subject_timings[timetable_dict[day][time][0]] = []
            subject_timings[timetable_dict[day][time][0]].append(
                [flattened_time, timetable_dict[day][time][2]])
        subject_timings = merge_slots(subject_timings)
        for time in list(timetable_dict[day].keys()):
            flattened_time = int(time[:time.find(':')])
            if (flattened_time < 6):
                flattened_time += 12
            if (not flattened_time
                    == subject_timings[timetable_dict[day][time][0]][0]):
                del (timetable_dict[day][time])
            else:
                timetable_dict[day][time][2] = subject_timings[
                    timetable_dict[day][time][0]][1]

    print(timetable_dict)

    del session_dict[token]

    cal = Calendar()
    cal.add('prodid', '-//Your Timetable generated by GYFT//mxm.dk//')
    cal.add('version', '1.0')
    data = timetable_dict

    days = {}
    days["Monday"] = 0
    days["Tuesday"] = 1
    days["Wednesday"] = 2
    days["Thursday"] = 3
    days["Friday"] = 4
    days["Saturday"] = 5
    ###
    # Get subjects code and         their respective name
    for day in data:
        startDates = [next_weekday(x[0], days[day]) for x in WORKING_DAYS]

        for time in data[day]:
            # parsing time from time_table dict
            # currently we only parse the starting time
            # duration of the event is rounded off to the closest hour
            # i.e 17:00 - 17:55 will be shown as 17:00 - 18:00

            parse_results = timetable_dict_parser.findall(time)[0]

            lectureBeginsStamps = [get_stamp(parse_results[:3], start) \
                                                        for start in startDates]

            durationInHours = data[day][time][2]

            # Find the name of this course
            # Use subject name if available, else ask the user for the subject
            # name and use that
            # TODO: Add labs to `subjects.json`
            subject_code = data[day][time][0]
            summary = subject_code
            description = subject_code
            if (subject_code in subjects.keys()):
                summary = subjects[subject_code].title()
            else:
                print('ERROR: Our subjects database does not have %s in it.' %
                      subject_code)
                # summary = input('INPUT: Please input the name of the course %s: ' %
                #         subject_code)

                subjects[subject_code] = str(subject_code)

                summary = summary.title()

            # Find location of this class
            location = data[day][time][1]

            for lectureBegin, [periodBegin, periodEnd] in \
                    zip(lectureBeginsStamps, WORKING_DAYS):

                event = build_event.build_event_duration(
                    summary, description, lectureBegin, durationInHours,
                    location, "weekly", periodEnd)

                cal.add_component(event)

            if (DEBUG):
                print(event)

    return str(cal.to_ical())
Exemplo n.º 3
0
def main():
    """
    Creates an ICS file `timetable.ics` with the timetable data
    present inside the input file `data.txt`
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input")
    parser.add_argument("-o", "--output")
    args = parser.parse_args()

    INPUT_FILENAME = args.input if args.input else "data.txt"
    if not os.path.exists(INPUT_FILENAME):
        print("Input file", INPUT_FILENAME, "does not exist.")
        os._exit(1)

    OUTPUT_FILENAME = "timetable.ics" if args.output is None else args.output

    # Get your timetable
    with open(INPUT_FILENAME) as data_file:
        data = json.load(data_file)
    # Get subjects code and their respective name
    with open("subjects.json") as data_file:
        subjects = json.load(data_file)
    for day in data:
        startDates = [next_weekday(x[0], days[day]) for x in WORKING_DAYS]

        for time in data[day]:
            # parsing time from time_table dict
            # currently we only parse the starting time
            # duration of the event is rounded off to the closest hour
            # i.e 17:00 - 17:55 will be shown as 17:00 - 18:00

            parse_results = timetable_dict_parser.findall(time)[0]

            lectureBeginsStamps = [
                get_stamp(parse_results[:3], start) for start in startDates
            ]

            durationInHours = data[day][time][2]

            # Find the name of this course
            # Use subject name if available, else ask the user for the subject
            # name and use that
            # TODO: Add labs to `subjects.json`
            subject_code = data[day][time][0]
            summary = subject_code
            description = subject_code
            if subject_code in subjects.keys():
                summary = subjects[subject_code].title()
            else:
                print("\n :( Our subjects database does not have %s in it." %
                      subject_code)
                summary = input(
                    "\t  Please input the name of the course %s: " %
                    subject_code)

                subjects[subject_code] = str(summary)

                summary = summary.title()

            # Find location of this class
            location = data[day][time][1]

            for lectureBegin, [_, periodEnd] in zip(lectureBeginsStamps,
                                                    WORKING_DAYS):

                event = build_event.build_event_duration(
                    summary,
                    description,
                    lectureBegin,
                    durationInHours,
                    location,
                    "weekly",
                    periodEnd,
                )

                cal.add_component(event)

            if DEBUG:
                print(event)

    with open(OUTPUT_FILENAME, "wb") as f:
        f.write(cal.to_ical())
        print("\n:) Your timetable has been written to %s" % OUTPUT_FILENAME)
        print("You can now add this file to your Calendar")
        print("To add it to google Calendar visit tutorial at :")
        print("     https://goo.gl/WvdUsP \n")
Exemplo n.º 4
0
def main():

    # Get your timetable
    with open(INPUT_FILENAME) as data_file:
        data = json.load(data_file)
    # Get subjects code and their respective name
    with open('subjects.json') as data_file:
        subjects = json.load(data_file)

    found_missing_sub = False
    for day in data:
        startDates = [next_weekday(x[0], days[day]) for x in WORKING_DAYS]

        for time in data[day]:
            # parsing time from time_table dict
            # currently we only parse the starting time
            # duration of the event is rounded off to the closest hour
            # i.e 17:00 - 17:55 will be shown as 17:00 - 18:00

            parse_results = timetable_dict_parser.findall(time)[0]

            lectureBeginsStamps = [get_stamp(parse_results[:3], start) \
                                                        for start in startDates]

            durationInHours = data[day][time][2]
            
            # Find the name of this course
            # Use subject name if available, else ask the user for the subject
            # name and use that
            # TODO: Add labs to `subjects.json`
            subject_code = data[day][time][0]
            summary = subject_code
            description = subject_code
            if (subject_code in subjects.keys()):
                summary = subjects[subject_code].title()
            else:
                print('ERROR: Our subjects database does not have %s in it.' %
                        subject_code);
                summary = input('INPUT: Please input the name of the course %s: ' %
                        subject_code)

                subjects[subject_code] = str(summary)
                update_sub_list(subject_code, summary)

                summary = summary.title()
                found_missing_sub = True


            # Find location of this class
            location = data[day][time][1]

            for lectureBegin, [periodBegin, periodEnd] in \
                    zip(lectureBeginsStamps, WORKING_DAYS):

                event = build_event.build_event_duration(summary,
                        description,
                        lectureBegin,
                        durationInHours,
                        location,
                        "weekly",
                        periodEnd)

                cal.add_component(event)

            if (DEBUG):
                print (event)

    if found_missing_sub:
        print('Subject list has been updated. Please commit, push and raise a pull request at github.com/metakgp/gyft.')

    with open(OUTPUT_FILENAME, 'wb') as f:
        f.write(cal.to_ical())
        print("INFO: Your timetable has been written to %s" % OUTPUT_FILENAME)