Student List Integration With Taxonomy

by ADMIN 39 views

Introduction

In this article, we will explore the implementation of a Python script to integrate the student list with taxonomy using the Portal's API endpoint. The API endpoint is GET https://portal.ce.pdn.ac.lk/api/taxonomy/v1/students, and the destination file is located at https://github.com/cepdnaclk/www.ce.pdn.ac.lk/blob/main/_data/students.json. We will also update the destination file format to match the desired structure.

Understanding the API Endpoint

The API endpoint GET https://portal.ce.pdn.ac.lk/api/taxonomy/v1/students returns a list of students in JSON format. The response is expected to contain the following information:

  • name: The name of the student
  • code: The code of the student
  • profiles_page: The profiles page of the student
  • start_date: The start date of the student (optional)
  • end_date: The end date of the student (optional)

Destination File Format

The destination file is expected to be in JSON format, with the following structure:

{
    "undergraduate": {
        "name": "Undergraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk",
        "students": [
            {
                "name": "E22 Batch",
                "code": "e22",
                "profiles_page": "/students/e22/",
                "start_date": "<optional, default is null>",
                "end_date": "<optional, default is null>"
            }
        ]
    },
    "postgraduate": {
        "name": "Postgraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk/students/postgraduate/",
        "students": []
    }
}

Python Script Implementation

To implement the student list integration with taxonomy, we will use the requests library to call the API endpoint and the json library to parse the response. We will also use the json library to update the destination file format.

Step 1: Install Required Libraries

To start, we need to install the required libraries. We will use the requests library to call the API endpoint and the json library to parse and update the destination file.

pip install requests

Step 2: Call the API Endpoint

Next, we will call the API endpoint using the requests library. We will use the GET method to retrieve the list of students.

import requests

url = "https://portal.ce.pdn.ac.lk/api/taxonomy/v1/students"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()
else:
    print("Failed to retrieve data")
    exit(1)

Step 3: Parse the Response

Once we have the response, we will parse it using the json library. We will create a dictionary to store the parsed data.

import json

data = response.json()

students = []
for student in data:
    students.append({
        "name": student["name"],
        "code": student["code"],
        "profiles_page": student["profiles_page"],
        "start_date": student.get("start_date"),
        "end_date": student.get("end_date")
    })

Step 4: Update the Destination File Format

Next, we will update the destination file format to match the desired structure. We will create a dictionary to store the updated data.

updated_data = {
    "undergraduate": {
        "name": "Undergraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk",
        "students": students
    },
    "postgraduate": {
        "name": "Postgraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk/students/postgraduate/",
        "students": []
    }
}

Step 5: Write the Updated Data to the Destination File

Finally, we will write the updated data to the destination file using the json library.

with open("students.json", "w") as file:
    json.dump(updated_data, file, indent=4)

Conclusion

In this article, we have implemented a Python script to integrate the student list with taxonomy using the Portal's API endpoint. We have called the API endpoint, parsed the response, updated the destination file format, and written the updated data to the destination file. The updated destination file format matches the desired structure, with the undergraduate and postgraduate students listed separately.

Future Work

In the future, we can improve the script by adding error handling and logging mechanisms. We can also add more features, such as filtering and sorting the students by name or code.

Code

The complete code for the script is as follows:

import requests
import json

url = "https://portal.ce.pdn.ac.lk/api/taxonomy/v1/students"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()
else:
    print("Failed to retrieve data")
    exit(1)

students = []
for student in data:
    students.append({
        "name": student["name"],
        "code": student["code"],
        "profiles_page": student["profiles_page"],
        "start_date": student.get("start_date"),
        "end_date": student.get("end_date")
    })

updated_data = {
    "undergraduate": {
        "name": "Undergraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk",
        "students": students
    },
    "postgraduate": {
        "name": "Postgraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk/students/postgraduate/",
        "students": []
    }
}

with open("students.json", "w") as file:
    json.dump(updated_data, file, indent=4)

Introduction

In our previous article, we implemented a Python script to integrate the student list with taxonomy using the Portal's API endpoint. In this article, we will answer some frequently asked questions (FAQs) related to the implementation.

Q: What is the purpose of the API endpoint?

A: The API endpoint GET https://portal.ce.pdn.ac.lk/api/taxonomy/v1/students is used to retrieve a list of students in JSON format. The response contains the student's name, code, profiles page, start date, and end date.

Q: What is the destination file format?

A: The destination file format is a JSON file that contains the updated student list in the desired structure. The file has the following structure:

{
    "undergraduate": {
        "name": "Undergraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk",
        "students": [
            {
                "name": "E22 Batch",
                "code": "e22",
                "profiles_page": "/students/e22/",
                "start_date": "<optional, default is null>",
                "end_date": "<optional, default is null>"
            }
        ]
    },
    "postgraduate": {
        "name": "Postgraduate Students",
        "profiles_page": "https://people.ce.pdn.ac.lk/students/postgraduate/",
        "students": []
    }
}

Q: How does the script handle errors?

A: The script uses try-except blocks to handle errors. If the API endpoint returns an error, the script will print an error message and exit.

Q: Can I customize the script to fit my needs?

A: Yes, you can customize the script to fit your needs. You can modify the API endpoint, destination file format, and error handling mechanisms to suit your requirements.

Q: What are the benefits of using this script?

A: The benefits of using this script include:

  • Automating the process of integrating the student list with taxonomy
  • Reducing the risk of human error
  • Improving the accuracy and consistency of the data
  • Enhancing the user experience by providing a seamless and efficient experience

Q: Can I use this script with other APIs?

A: Yes, you can use this script with other APIs by modifying the API endpoint and destination file format to match the requirements of the new API.

Q: How do I troubleshoot issues with the script?

A: To troubleshoot issues with the script, you can:

  • Check the API endpoint and destination file format for errors
  • Verify that the script is correctly configured and installed
  • Use debugging tools to identify the source of the issue
  • Consult the documentation and online resources for help

Conclusion

In this article, we have answered some frequently asked questions related to the implementation of the student list integration with taxonomy using the Portal's API endpoint. We hope that this article has provided valuable insights and information to help you understand the implementation and troubleshoot any issues that arise.

Additional Resources

For more information on the implementation, please refer to the following resources:

Note: This article assumes that the reader has a basic understanding of Python and API integration. If you are new to these topics, please consult the resources provided above for more information.