Skip to content

Commit

Permalink
Deprecated selenium support, updated README and added documentation t…
Browse files Browse the repository at this point in the history
…o code (#9)

* Removed selenium and updated README

* Updated Dockerfile
  • Loading branch information
aditeyabaral authored Jul 28, 2024
1 parent 7e751c1 commit 7a060cb
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 334 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Docker Image Build

on: [push, pull_request]
on: [ push, pull_request ]

jobs:

Expand All @@ -9,8 +9,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --tag pesu-auth
- name: Spawn a container
run: docker run --name pesu-auth -d -p 5000:5000 pesu-auth
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --tag pesu-auth
- name: Spawn a container
run: docker run --name pesu-auth -d -p 5000:5000 pesu-auth
34 changes: 17 additions & 17 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
name: Python Version Compatibility

on: [push, pull_request]
on: [ push, pull_request ]

jobs:
build-linux:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ images/
*.log
README.html
README_tmp*
.venv
.venv
pyrightconfig.json
*.bru
bruno.json
14 changes: 0 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
FROM python:3.10.11-bullseye

RUN apt update -y && apt upgrade -y
RUN apt install wget unzip

ARG DEBIAN_FRONTEND=noninteractive
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt install ./google-chrome-stable_current_amd64.deb -y
RUN rm ./google-chrome-stable_current_amd64.deb

RUN pip install get-chrome-driver
RUN get-chrome-driver --auto-download --extract
RUN mv chromedriver/*/bin/chromedriver /usr/bin/chromedriver
RUN chmod +x /usr/bin/chromedriver
RUN rm -rf chromedriver/

COPY app /app
COPY README.md /README.md
COPY requirements.txt /requirements.txt
Expand Down
102 changes: 62 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,53 @@ returns the user's profile information.
> :warning: **Warning:** The live version is hosted on a free tier server, so you might experience some latency on the
> first request since the server might not be awake. Subsequent requests will be faster.
# How to use pesu-auth
## How to run pesu-auth locally

Running the PESUAuth API locally is simple. Clone the repository and follow the steps below to get started.

### Running with Docker

This is the easiest and recommended way to run the API locally. Ensure you have Docker installed on your system. Run the
following commands to start the API.

1. Build the Docker image

```bash
docker build . --tag pesu-auth
```

2. Run the Docker container

```bash
docker run --name pesu-auth -d -p 5000:5000 pesu-auth
```

3. Access the API at `http://localhost:5000/`

## Non-Interactive Mode
### Running without Docker

This is the most common and recommended way to use the API. You can send a request to `/authenticate` endpoint with the
user's credentials and the API will return a JSON object, with the user's profile information if requested.
If you don't have Docker installed, you can run the API using Python. Ensure you have Python 3.8 or higher installed on
your system.

1. Create a virtual environment using `conda` or any other virtual environment manager of your choice and activate it.
Then, install the dependencies using the following command.

```bash
pip install -r requirements.txt
```

2. Run the API using the following command.

```bash
python app/app.py
```

3. Access the API at `http://localhost:5000/`

# How to use pesu-auth

You can send a request to the `/authenticate` endpoint with the user's credentials and the API will return a JSON object,
with the user's profile information if requested.

### Request Parameters

Expand Down Expand Up @@ -84,7 +125,7 @@ import requests
data = {
'username': 'your SRN or PRN here',
'password': 'your password here',
'profile': False # Optional, defaults to False
'profile': True # Optional, defaults to False
# Set to True if you want to retrieve the user's profile information
}

Expand All @@ -104,47 +145,28 @@ print(response.json())
"program": "Bachelor of Technology",
"branch_short_code": "CSE",
"branch": "Computer Science and Engineering",
"semester": "Sem-1",
"section": "Section A",
"semester": "NA",
"section": "NA",
"email": "[email protected]",
"phone": "1234567890",
"campus_code": 1,
"campus": "RR"
},
"know_your_class_and_section": {
"prn": "PES1201800001",
"srn": "PES1201800001",
"name": "Johnny Blaze",
"class": "Sem-1",
"section": "Section A",
"cycle": "NA",
"department": "CSE (RR Campus)",
"branch": "CSE",
"institute_name": "PES University (Ring Road)"
},
"message": "Login successful.",
"timestamp": "2023-06-18 20:57:59.979374+05:30"
"know_your_class_and_section": {
"prn": "PES1201800001",
"srn": "PES1201800001",
"name": "JOHNNY BLAZE",
"class": "",
"section": "",
"cycle": "NA",
"department": "",
"branch": "CSE",
"institute_name": ""
},
"timestamp": "2024-07-28 22:30:10.103368+05:30"
}
```

</details>

## Interactive Mode

You can also use interactive mode which will spawn a browser window and allow the user to sign in to PESU Academy. Send
a request to the `authenticateInteractive` endpoint with the `profile` query parameter set to `true` and the API will
return
a JSON object with the user's profile information.

> :warning: **Warning:** This will only work if the API is running on the same server as the client.
<details><summary>Here is an example using Python</summary>

#### Request

```python
import requests

response = requests.post("http://localhost:5000/authenticateInteractive?profile=true")
print(response.json())
```

</details>
28 changes: 13 additions & 15 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@


def convert_readme_to_html():
"""
Convert the README.md file to HTML and save it as README.html so that it can be rendered on the home page.
"""
readme_content = open("README.md").read().strip()
readme_content = re.sub(r":\w+: ", "", readme_content)
with open("README_tmp.md", "w") as f:
Expand All @@ -34,6 +37,9 @@ def convert_readme_to_html():

@app.route("/")
def index():
"""
Render the home page with the README.md content.
"""
try:
if "README.html" not in os.listdir():
convert_readme_to_html()
Expand All @@ -48,6 +54,9 @@ def index():

@app.route("/authenticate", methods=["POST"])
def authenticate():
"""
Authenticate the user with the provided username and password.
"""
username = request.json.get("username")
password = request.json.get("password")
profile = request.json.get("profile", False)
Expand All @@ -62,21 +71,10 @@ def authenticate():
return json.dumps(authentication_result), 200

# if either username or password is not provided, we return an error
return json.dumps({
"status": False,
"message": "Username or password not provided."
}), 400


@app.route("/authenticateInteractive", methods=["GET", "POST"])
def authenticate_interactive():
profile = request.args.get("profile", False)
if isinstance(profile, str):
profile = profile.lower() == "true"
current_time = datetime.datetime.now(IST)
authentication_result = pesu_academy.authenticate_selenium_interactive(profile)
authentication_result["timestamp"] = str(current_time)
return json.dumps(authentication_result), 200
return (
json.dumps({"status": False, "message": "Username or password not provided."}),
400,
)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 7a060cb

Please sign in to comment.