|
| 1 | +// |
| 2 | +// CampusController.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Denys Danyliuk on 02.06.2022. |
| 6 | +// |
| 7 | + |
| 8 | +import Vapor |
| 9 | +import KPIHubParser |
| 10 | +import Routes |
| 11 | + |
| 12 | +struct StudySheetResponse: Content { |
| 13 | + let studySheet: [StudySheetItem] |
| 14 | +} |
| 15 | + |
| 16 | +struct StudySheetItem: Content { |
| 17 | + let lesson: StudySheetLesson |
| 18 | + let activities: [StudySheetActivity] |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +final class CampusController { |
| 23 | + |
| 24 | + func userInfo( |
| 25 | + request: Request, |
| 26 | + loginQuery: CampusLoginQuery |
| 27 | + ) async throws -> UserInfo { |
| 28 | + let oauthResponse = try await oauth(request: request, loginQuery: loginQuery) |
| 29 | + let campusAPICredentials = try oauthResponse.content.decode(CampusAPICredentials.self) |
| 30 | + |
| 31 | + let accountInfoResponse: ClientResponse = try await request.client.get( |
| 32 | + "https://api.campus.kpi.ua/Account/Info", |
| 33 | + beforeSend: { clientRequest in |
| 34 | + let auth = BearerAuthorization(token: campusAPICredentials.accessToken) |
| 35 | + clientRequest.headers.bearerAuthorization = auth |
| 36 | + } |
| 37 | + ) |
| 38 | + return try accountInfoResponse.content.decode(UserInfo.self) |
| 39 | + } |
| 40 | + |
| 41 | + func studySheet( |
| 42 | + request: Request, |
| 43 | + loginQuery: CampusLoginQuery |
| 44 | + ) async throws -> StudySheetResponse { |
| 45 | + |
| 46 | + let oauthResponse = try await oauth(request: request, loginQuery: loginQuery) |
| 47 | + let campusAPICredentials = try oauthResponse.content.decode(CampusAPICredentials.self) |
| 48 | + |
| 49 | + let authPHPResponse: ClientResponse = try await request.client.get( |
| 50 | + "https://campus.kpi.ua/auth.php", |
| 51 | + beforeSend: { clientRequest in |
| 52 | + clientRequest.headers.cookie = oauthResponse.headers.setCookie |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + let studySheetResponse: ClientResponse = try await request.client.get( |
| 57 | + "https://campus.kpi.ua/student/index.php?mode=studysheet", |
| 58 | + beforeSend: { clientRequest in |
| 59 | + let auth = BearerAuthorization(token: campusAPICredentials.accessToken) |
| 60 | + clientRequest.headers.bearerAuthorization = auth |
| 61 | + if let phpSessionId = authPHPResponse.headers.setCookie?.all["PHPSESSID"] { |
| 62 | + clientRequest.headers.cookie = HTTPCookies( |
| 63 | + dictionaryLiteral: ("PHPSESSID", phpSessionId) |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + let html = try (studySheetResponse.body).htmlString(encoding: .windowsCP1251) |
| 70 | + |
| 71 | + let studySheetItems = try await StudySheetLessonsParser().parse(html) |
| 72 | + .asyncMap { lesson -> StudySheetItem in |
| 73 | + let response: ClientResponse = try await request.client.post( |
| 74 | + "https://campus.kpi.ua\(lesson.link)", |
| 75 | + beforeSend: { clientRequest in |
| 76 | + let auth = BearerAuthorization(token: campusAPICredentials.accessToken) |
| 77 | + clientRequest.headers.bearerAuthorization = auth |
| 78 | + if let phpSessionId = authPHPResponse.headers.setCookie?.all["PHPSESSID"] { |
| 79 | + clientRequest.headers.cookie = HTTPCookies( |
| 80 | + dictionaryLiteral: ("PHPSESSID", phpSessionId) |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + ) |
| 85 | + let html = try (response.body).htmlString(encoding: .windowsCP1251) |
| 86 | + let activities = try StudySheetActivitiesParser().parse(html) |
| 87 | + return StudySheetItem(lesson: lesson, activities: activities) |
| 88 | + } |
| 89 | + |
| 90 | + return StudySheetResponse(studySheet: studySheetItems) |
| 91 | + } |
| 92 | + |
| 93 | + // MARK: - Helpers |
| 94 | + |
| 95 | + func oauth( |
| 96 | + request: Request, |
| 97 | + loginQuery: CampusLoginQuery |
| 98 | + ) async throws -> ClientResponse { |
| 99 | + try await request.client.post( |
| 100 | + "https://api.campus.kpi.ua/oauth/token", |
| 101 | + beforeSend: { clientRequest in |
| 102 | + try clientRequest.query.encode(loginQuery) |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments