How I Automated The Google Form Filling For My College Attendance Using Python

To fight Corona Virus, our Prime Minister has declared lockdown for 21 days in all over the country. And so, our college decided to take online lectures and also, sent us a google form which is to be filled every day for each lecture to fill attendance. Filling form 4 times a day with the subject name and corresponding time and professor details is a tedious task. But also, Attendance is Engineer's Only Motivation to attend lectures.

And So, I decided to use Python and to write a script to automate this tedious task.

As usual, I started by searching on google to see how can we submit the google form using python script. And I came to know that Every Google Form Input element has a "name" attribute attached to it and we can use it to send the data for the corresponding field.

So, I opened the google form in chrome and Started inspecting Input element in which I will be filling the data, and I found a bunch of ss-form-entry objects with name attribute like " name = entry.<id> "

I copied every name attribute value of the field.

Now, In Python I created a dictionary with the value of name attributes that we copied from the browser as keys and the data that I want to fill in the corresponding field as it's value.

values = {
            # Email Address
            "emailAddress": str(sys.argv[1]),
            # Enrollment Number
            "entry.33987362": str(sys.argv[2]),
            # Course
            "entry.363926033": "BE",
            # Branch
            "entry.733518766": "IT",
            # Semester
            "entry.114626584": "Sem-6",
            # Subject
            "entry.609979780": i[2],
            # Date
            "entry.1916623197_year": date[0],
            "entry.1916623197_month": date[1],
            "entry.1916623197_day": date[2][0:2],
            # Time
            "entry.125609755_hour": i[0],
            "entry.125609755_minute": i[1],
        }

Then I created a dictionary with Day Name(Monday, Tuesday, etc.) as keys and the lectures' details as list as values.

subjects_time = {
        "Monday": [["10", "30", "AJ(HC)"], ["11", "30", "DCDR(PC)"], ["1", "00", "WT(AM)"], ["2", "00", "SE(KD)"]],
        "Tuesday": [["11", "30", "SE(KD)"], ["1", "00", "AJ(HC)"]],
        "Wednesday": [["11", "30", ".NET(PC)"], ["1", "00", "SE(KD)"], ["2", "00", ".NET(PC)"]],
        "Thursday": [["10", "30", "WT(AM)"], ["11", "30", "DCDR(PC)"], ["1", "00", "AJ(HC)"]],
        "Friday": [["10", "30", "AJ(HC)"], ["11", "30", ".NET(PC)"], ["1", "00", "SE(KD)"], ["2", "00", ".NET(PC)"]],
    }

And put both the dictionaries in a function that returns the list of different data dictionaries of the current day.

def get_values():
    """It returns a list of different form data to be submitted by send_attendance method.
    subjects_time is a dictionary with Day as keys and time and subjects in a list as values.
    value_list is a list of lectures' subject and time of current_day."""

    values_list = []
    now = datetime.datetime.now()
    day_name = now.strftime("%A")

    subjects_time = {
        "Monday": [["10", "30", "AJ(HC)"], ["11", "30", "DCDR(PC)"], ["1", "00", "WT(AM)"], ["2", "00", "SE(KD)"]],
        "Tuesday": [["11", "30", "SE(KD)"], ["1", "00", "AJ(HC)"]],
        "Wednesday": [["11", "30", ".NET(PC)"], ["1", "00", "SE(KD)"], ["2", "00", ".NET(PC)"]],
        "Thursday": [["10", "30", "WT(AM)"], ["11", "30", "DCDR(PC)"], ["1", "00", "AJ(HC)"]],
        "Friday": [["10", "30", "AJ(HC)"], ["11", "30", ".NET(PC)"], ["1", "00", "SE(KD)"], ["2", "00", ".NET(PC)"]],
    }

    date = str(now).split('-')

    for i in subjects_time[day_name]:
        '''keys are the value of 'name' element of the '''
        values = {
            # Email Address
            "emailAddress": str(sys.argv[1]),
            # Enrollment Number
            "entry.33987362": str(sys.argv[2]),
            # Course
            "entry.363926033": "BE",
            # Branch
            "entry.733518766": "IT",
            # Semester
            "entry.114626584": "Sem-6",
            # Subject
            "entry.609979780": i[2],
            # Date
            "entry.1916623197_year": date[0],
            "entry.1916623197_month": date[1],
            "entry.1916623197_day": date[2][0:2],
            # Time
            "entry.125609755_hour": i[0],
            "entry.125609755_minute": i[1],
        }

        values_list.append(values)

    return values_list

And, finally the function for sending the form response using POST method of requests library. It takes two arguments - URL and data(here, data_dictionary), respectively.

def send_attendance(url, data):
    """It takes google form url which is to be submitted and also data which is a list of data to be submitted in the form iteratively."""

    for d in data:
        try:
            requests.post(url, data=d)
            print("Form Submitted.")
            time.sleep(5)
        except:
            print("Error Occured!")

Note: Change the URL of google form by replacing '/viewform' at the end of the URL with 'formResponse'. Now, your URL should look like this https://docs.google.com/forms/d/<form_id>/formResponse

And that's it, now I just have to run this script once a day and it will fill my attendance for all the lectures of that day. I can also deploy it on a server like PythonAnyWhere or DigitalOcean and it can run by itself every day, but as I'm home quarantine for the 21 Days, I have more than enough time to run it manually every day.🤓🤓

You can find the full code here : AutoFill Google Form


Thank Your Reading.#HappyCoding👩‍💻 👨‍💻