What were the Supreme Court’s decisions for today?

What is this post about?

This marks my third installment as I delve into automating the web crawling process for retrieving daily updates from the Supreme Court’s website. The previous ones were:  
Court Crawler Tool
What cases were added to the Supreme Court’s docket today?

The initial one necessitated user input to navigate through decisions from Nepal’s Courts. Through automating the data collection process, the tool significantly minimizes the time needed to compile court decisions. Researchers can now channel their efforts into analysis and interpretation rather than data gathering. The tool’s web-based interface ensures accessibility from any location, making it a versatile resource across platforms. Users can customize their searches based on specific courts and dates, guaranteeing the tool provides precisely the information they seek.

The second tool is a daily email digest feature, automating the delivery of a daily email to the user’s preferred email account. This email contains a list of cases newly added to the court’s docket for the day.

Can you not find this data on the Supreme Court’s website?

Of course you can find this on the Supreme Court’s website in this link – Decided Case Report but there is no dedicated method for this to have this sent as daily email digest everyday.  

How does this work?

This Python script automates the process of retrieving daily updates on decisions made by the Supreme Court from its website. The code consists of two main functions. The first function extracts a specific table element from the HTML response of a web request to the Supreme Court’s website. The second function sends an email with the extracted table element as an attachment. The email includes details about decisions made by the Supreme Court on the current date.

The script begins by obtaining the current Nepali date using a web request to a specific URL. It then makes a second web request to the Supreme Court’s website, extracting relevant information through query strings and form data. The extracted table element is then included in an email, which is sent automatically to a specified recipient. Additionally, the script creates an HTML file containing the table element, attaches it to the email, and deletes the file after sending the email. The output includes information about the Nepali date and a confirmation message indicating that the email has been sent.

Python and Task Scheduler

The Python Script Itself

            import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import requests
from lxml import html
import re
import os

# Function to extract the table element from the HTML response
def extract_table_element(response_content):
    tree = html.fromstring(response_content)
    return tree.xpath("/html/body/div[3]/div/table[2]")[0]

# Function to send an email with the extracted table element as an attachment
def send_email(table_element, year, month, day):
    # Email settings
    sender_email = "automation@sushilparajuli.com"
    recipient_email = "parajulishusil@gmail.com"
    subject = f"Decisions that were made today by Supreme Court {year}/{month}/{day}"

    # SMTP settings
    smtp_server = 'mail.yoursmtpurl.com' # your smtp url goes here
    smtp_port = 587
    smtp_username = sender_email
    smtp_password = 'yourpassword' # your smtp password goes here

    # Create the email message
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = recipient_email
    msg['Subject'] = subject

    # Attach the table element as HTML to the email body
    html_content = html.tostring(table_element, encoding="unicode")
    msg.attach(MIMEText(html_content, 'html'))

    # Save the table element as an HTML file
    html_filename = 'table_element.html'
    with open(html_filename, 'w', encoding='utf-8') as html_file:

    # Attach the HTML file to the email
    with open(html_filename, 'rb') as file:
        attachment = MIMEApplication(file.read(), _subtype="html")
        attachment.add_header('Content-Disposition', f'attachment; filename={html_filename}')

    # Connect to the SMTP server and send the email
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        server.login(smtp_username, smtp_password)
        server.sendmail(sender_email, recipient_email, msg.as_string())

    # Delete the HTML file after sending the email

    print("email has been sent")

# First part to get Nepali date
dateresponse = requests.get("https://taxpayerportal.ird.gov.np/Handlers/Common/DateHandler.ashx?method=GetCurrentDate")
match = re.search(r'"NepaliDate":"(\d{4})\.(\d{2})\.(\d{2})"', dateresponse.text)
year, month, day = match.groups()
nepali_date = f"{year}.{month}.{day}"
print("Nepali Date:", nepali_date)
print("Year:", year)
print("Month:", month)
print("Day:", day)

# Second part to make a request to the second URL
url = "https://supremecourt.gov.np/lic/sys.php"
query_strings = {
    'd': 'reports',
    'f': 'case_p'

form_data = {
    'syy': '',
    'smm': '',
    'sdd': '',
    'mode': 'showx',
    'tyy': year,
    'tmm': month,
    'tdd': day,
    'regno': '',
    'imageField.x': 48,
    'imageField.y': 10

response = requests.post(url, params=query_strings, data=form_data)

# Extract the specified table element
table_element = extract_table_element(response.content)

# Send an email with the extracted table element and attachment
send_email(table_element, year, month, day)


Automation through Windows Task Scheduler

To schedule the above Python script through Task Scheduler in Windows: 

Press Win + S and type “Task Scheduler,” then press Enter. In the Actions pane, click “Create Basic Task…” Follow the wizard to set a name and description. Select a trigger (e.g., daily, weekly) for when you want the script to run. Specify the start date and time based on your preference.

Choose Action: Select “Start a program” and click Next. Browse and select the Python executable (python.exe) as the program. You can find your python executable location by typing “where python” in the command prompt. In “Add arguments,” provide the name of your Python script (e.g. script.py). In “Start in,” provide the path of your script folder (e.g., C:\Path\to\your\script\folder) Review your settings and click Finish.

Now, your Python script will run automatically based on the scheduled triggers you configured in Task Scheduler and every day at the specified time you will receive a daily digest of all the cases that got decided by the Supreme Court on that particular day.