Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to School 21 API #43

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions parser/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
FROM python:3.10.2

RUN apt-get update && apt-get install -y \
chromium \
chromium-driver \
iputils-ping

RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /parser
COPY . .

RUN pip install --no-cache-dir -r requirements.txt

RUN cp /usr/bin/chromedriver /parser/

ENV PYTHONUNBUFFERED=1
ENV DISPLAY=:99

Expand Down
50 changes: 48 additions & 2 deletions parser/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Parser service

In parser container
In parser container we gets data about peers from School 21 API.

## Endpoints

Expand Down Expand Up @@ -29,4 +29,50 @@ data_to_send =
.
}
}
```
```

## TO DO


✅ AUTH 2.0
How to authenticate your application

1. To get an access token (JWT) you need to make a POST request to the Access Token URL (https://auth.sberclass.ru/auth/realms/EduPowerKeycloak/protocol/openid-connect/token). In the request body parameters you need to specify the values for parameters - "username" (=$login), "password" (=$password), "grant_type" (="password") and "client_id" (="s21-open-api")
2. In case of successful authentication, the response will include an access token and a refresh token
3. Use the access token to send requests to the API. Place it in the Authorization header (Bearer $token) to authenticate your application
- Take all campuses `/v1/campuses`
- Take all clusters of KZN `7c293c9c-f28c-4b10-be29-560e4b000a34`
- Take all clusters for all MSK timezon campuses, MSK, KZN, Белгород, Великий Новгород, Ярославль

```
clusters = {
"Kazan": {
"34734": "et",
"34735": "ev",
"34736": "ge",
"34737": "pr",
"34738": "si",
"34739": "un",
"34740": "va",
},
"Moscow": {
"34715": "at",
"36799": "du",
"34717": "ga",
"34718": "il",
"34719": "mi",
"34720": "oa",
"34721": "ox",
"34723": "su",
"34724": "vo",
},
}
```

- Take all peers online
```
curl -X 'GET' \
'https://edu-api.21-school.ru/services/21-school/api/v1/clusters/34734/map?limit=101&offset=0' \
-H 'accept: application/json' \
-H 'Authorization: Bearer token...'
```
32 changes: 32 additions & 0 deletions parser/get_auth_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
## Using AUTH 2.0 to getting token to edu platform
"""

import logging
import os
import requests


def get_token():
"""
### Auth and return token: `Bearer $token`
"""
request = requests.post(
url="https://auth.sberclass.ru/auth/realms/EduPowerKeycloak/protocol/openid-connect/token",
data={
"username": os.getenv("EDU_SCHOOL_LOGIN"),
"password": os.getenv("EDU_SCHOOL_PASSWORD"),
"grant_type": "password",
"client_id": "s21-open-api",
},
timeout=5,
)

if request.status_code != 200:
logging.error(
"Failed to getting token! %d Error: %s", request.status_code, request.json()
)

token = "Bearer " + request.json().get("access_token", "")

return token
28 changes: 7 additions & 21 deletions parser/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@
## Parse and send data to DB API
"""

import sys
import logging
from selenium.common.exceptions import (
NoSuchElementException,
ElementNotInteractableException,
)
from parse_edu import login_and_parse_campus_map
from parse_raw_from_html import convert_to_json
from parser_sender import update_peers
import time
import parse_by_api
import parser_sender

logging.basicConfig(level=logging.INFO, stream=sys.stdout)

if __name__ == "__main__":
try:
temp_json = convert_to_json(login_and_parse_campus_map())
logging.info("All raw data converted")
logging.info("len json: %s", len(temp_json["peers"]))
if temp_json["peers"]:
update_peers(temp_json)
logging.info("Post sended to API")
else:
logging.warning("json empty, not sended to API")
except (NoSuchElementException, ElementNotInteractableException) as all_ex:
logging.error("Parse failed, starting next try. ERROR: %s", all_ex)
while True:
all_peers = parse_by_api.parse_clusters()
parser_sender.update_peers(all_peers)
time.sleep(15)
60 changes: 60 additions & 0 deletions parser/parse_by_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
## Using School 21 API to get peers data.
"""

import logging
from datetime import datetime, timedelta, timezone
import requests
import get_auth_token


def parse_clusters():
"""
### Getting info about online peers on campuses maps
"""
kzn_clusters = {
"34734": "et",
"34735": "ev",
"34736": "ge",
"34737": "pr",
"34738": "si",
"34739": "un",
"34740": "va",
}
all_peers = {"peers": {}}

token = get_auth_token.get_token()
url_endpoint = "https://edu-api.21-school.ru/services/21-school/api/v1/clusters/"

for cluster_id, cluster_name in kzn_clusters.items():
request = requests.get(
url=f"{url_endpoint}{cluster_id}/map",
params={"limit":"101", "offset":"0", "occupied": "true"},
headers={"accept": "application/json", "Authorization": token},
timeout=5,
)
if request.status_code > 200:
logging.error("Failed parse cluster %s", cluster_name)
else:
moscow_time = datetime.now().astimezone(
timezone(timedelta(hours=3), "Moscow")
)
moscow_time = moscow_time.strftime("%Y-%m-%d %H:%M:%S")

for raw_peer in request.json()["clusterMap"]:
clear_peer_data = {
"status": "1",
"cluster": cluster_name,
"row": raw_peer["row"],
"col": str(raw_peer["number"]),
"time": moscow_time,
}
all_peers["peers"][raw_peer["login"].split("@")[0]] = clear_peer_data

print(all_peers)
return all_peers


if __name__ == "__main__":

parse_clusters()
140 changes: 0 additions & 140 deletions parser/parse_edu.py

This file was deleted.

Loading
Loading