|
| 1 | +export type { CVToScan, CVToScanPersonalDetails, CVToScanEducation, CVToScanLanguage, CVToScanWorkExperience } from './cv.ts' |
| 2 | +export { newCvToScan, LangLevel } from './cv.ts' |
| 3 | + |
| 4 | +import { envT, readEnv, serverT } from './env.ts' |
| 5 | +import { CVToScan } from './cv.ts' |
| 6 | + |
| 7 | +import { Sha512 } from "https://deno.land/[email protected]/hash/sha512.ts" |
| 8 | + |
| 9 | +export enum LoginUsersRestriction { |
| 10 | + None, // This scraper doesn't have any domain login users |
| 11 | + One, // This scraper expects one user to login with |
| 12 | + OneOrMore, // This scraper expects at least one user to login with but can have more |
| 13 | +} |
| 14 | + |
| 15 | +export class RTCVScraperClient { |
| 16 | + private env: envT |
| 17 | + private servers: Array<ServerConn> |
| 18 | + private referenceCache: { [reference: string | number]: Date } = {} |
| 19 | + dummyMode = false |
| 20 | + |
| 21 | + constructor(loginUsersResitrction: LoginUsersRestriction) { |
| 22 | + this.env = readEnv() |
| 23 | + |
| 24 | + const loginUsersCount = this.env.login_users?.length |
| 25 | + if (loginUsersResitrction == LoginUsersRestriction.One) { |
| 26 | + if (!loginUsersCount) throw 'Expected exactly one login user but got none' |
| 27 | + if (loginUsersCount != 1) throw `Expected exactly one login user but got ${loginUsersCount}` |
| 28 | + } else if (loginUsersResitrction == LoginUsersRestriction.OneOrMore) { |
| 29 | + if (!loginUsersCount) throw 'Expected exactly one ore more login users but got none' |
| 30 | + } |
| 31 | + |
| 32 | + this.servers = [ |
| 33 | + new ServerConn(this.env.primary_server), |
| 34 | + ...(this.env.alternative_servers?.map(s => new ServerConn(s)) || []), |
| 35 | + ] |
| 36 | + } |
| 37 | + |
| 38 | + async authenticate(): Promise<this> { |
| 39 | + if (this.dummyMode) return this |
| 40 | + |
| 41 | + await Promise.all(this.servers.map(s => s.checkHasScraperRole())) |
| 42 | + return this |
| 43 | + } |
| 44 | + |
| 45 | + // Get the user that is used to login to the site we scrape |
| 46 | + get loginUser() { |
| 47 | + return this.loginUsers[0]! |
| 48 | + } |
| 49 | + |
| 50 | + // Get the users that are used to login to the site we scrape |
| 51 | + get loginUsers() { |
| 52 | + return this.env.login_users! |
| 53 | + } |
| 54 | + |
| 55 | + async sendCV(cv: CVToScan) { |
| 56 | + if (this.hasCachedReference(cv.referenceNumber)) return |
| 57 | + this.setCachedReference(cv.referenceNumber) |
| 58 | + |
| 59 | + if (this.dummyMode) return |
| 60 | + |
| 61 | + await Promise.all([ |
| 62 | + // We only care if the cv was accepted by the primary server |
| 63 | + this.servers[0].sendCV(cv), |
| 64 | + // From the other servers we will ignore the errors |
| 65 | + ...this.servers.slice(1).map(s => s.sendCV(cv).catch(_ => {/* Ignore errors */ })) |
| 66 | + ]) |
| 67 | + } |
| 68 | + |
| 69 | + setCachedReference(referenceNr: string | number, options?: { ttlHours?: 12 | 24 | 72 }) { |
| 70 | + if (referenceNr === '') throw 'Reference number cannot be empty' |
| 71 | + if (this.hasCachedReference(referenceNr)) return |
| 72 | + const expireDate = new Date() |
| 73 | + expireDate.setHours(expireDate.getHours() + (options?.ttlHours ?? 72)) |
| 74 | + this.referenceCache[referenceNr] = expireDate |
| 75 | + } |
| 76 | + |
| 77 | + hasCachedReference(referenceNr: string | number): boolean { |
| 78 | + const expireDate = this.referenceCache[referenceNr] |
| 79 | + if (!expireDate) return false |
| 80 | + |
| 81 | + if (expireDate < new Date()) { |
| 82 | + // This reference number has been expired |
| 83 | + delete this.referenceCache[referenceNr] |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + return true |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +class ServerConn { |
| 92 | + server_location: string |
| 93 | + authHeader: string |
| 94 | + |
| 95 | + constructor(data: serverT) { |
| 96 | + this.server_location = data.server_location |
| 97 | + |
| 98 | + const hashedApiKey = new Sha512().update(data.api_key).hex() |
| 99 | + this.authHeader = `Basic ${data.api_key_id}:${hashedApiKey}` |
| 100 | + } |
| 101 | + |
| 102 | + private async doRequest(method: 'GET' | 'POST', path: string, body?: unknown) { |
| 103 | + const url = this.server_location + path |
| 104 | + const options = { |
| 105 | + method, |
| 106 | + headers: { |
| 107 | + 'Content-Type': 'application/json', |
| 108 | + 'Authorization': this.authHeader, |
| 109 | + }, |
| 110 | + body: body ? JSON.stringify(body) : undefined, |
| 111 | + } |
| 112 | + |
| 113 | + const req = await fetch(url, options) |
| 114 | + return await req.json() |
| 115 | + } |
| 116 | + |
| 117 | + async sendCV(cv: CVToScan) { |
| 118 | + await this.doRequest('POST', '/api/v1/scraper/scanCV', { cv: cv }) |
| 119 | + } |
| 120 | + |
| 121 | + async checkHasScraperRole() { |
| 122 | + const keyInfo: { roles: Array<{ role: number }> } = await this.doRequest('GET', '/api/v1/auth/keyinfo') |
| 123 | + const hasScraperRole = keyInfo.roles.some(r => r.role == 1) |
| 124 | + if (!hasScraperRole) throw `api key for ${this.server_location} does not have the scraper role` |
| 125 | + } |
| 126 | +} |
0 commit comments