Skip to content

Commit d98fbbe

Browse files
committed
feat: added the news
1 parent 8678b20 commit d98fbbe

File tree

5 files changed

+80
-49
lines changed

5 files changed

+80
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ subjects.json
140140
timetable.json
141141
timetable.json
142142
attendance.json
143+
news.json

examples/news.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
2+
import readline from "readline";
3+
import fs from "fs";
4+
import dotenv from "dotenv";
5+
import { LoginWithToken } from "../src";
6+
7+
dotenv.config();
8+
9+
const rl = readline.createInterface({
10+
input: process.stdin,
11+
output: process.stdout
12+
});
13+
14+
const askQuestion = (query: string): Promise<string> => {
15+
return new Promise(resolve => rl.question(query, resolve));
16+
};
17+
18+
const main = async () => {
19+
try {
20+
const url = process.env.INSTANCE_URL ?? "";
21+
const refreshToken = process.env.REFRESH_TOKEN ?? "";
22+
const userId = process.env.USER_ID ?? "";
23+
const deviceId = process.env.DEVICE_ID ?? "";
24+
25+
// Refresh token and get device ID
26+
console.log("Refreshing token...");
27+
const smartschool = await LoginWithToken(url, refreshToken, deviceId);
28+
console.log("Token refreshed successfully.");
29+
30+
// Fetch news
31+
console.log("\nFetching news...");
32+
const news = await smartschool.GetNews();
33+
34+
fs.writeFileSync("news.json", JSON.stringify(news, null, 2));
35+
console.log("\nNews fetched successfully:");
36+
console.log(`Found ${news.length} news items.`);
37+
console.log(JSON.stringify(news, null, 2));
38+
} catch (error) {
39+
console.error("\nAn error occurred:");
40+
console.error(error);
41+
} finally {
42+
rl.close();
43+
}
44+
};
45+
46+
main();

src/rest/endpoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export const BASE_URL = () => "https://api.skolengo.com/api/v1/bff-sko-app";
33

44
export const SEARCH_SCHOOLS = () => "schools";
5-
export const SCHOOL_NEWS = () => "schools-info";
5+
export const SCHOOL_NEWS = () => "Homepage/Pnsrpc/request?method=getPnsRecentNews";
66

77
export const USER_INFO = () => "mobile/accountinfo";
88
export const USER_ASSIGNMENT = (id: string, userId: string, completed: string) => "planner/api/v1/planned-assignments/"+ userId.split("_")[0] + "/" + id + "/" + completed;

src/routes/School.ts

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,41 @@ import { NewsAttributes } from "../types/News";
66
import { BaseDataResponse, BaseResponse, fileIncluded } from "../types/RequestHandler";
77
import { schoolIncluded } from "../types/School";
88
import { getSingleRelation } from "../util/Relations";
9-
10-
const manager = new RestManager(BASE_URL());
11-
12-
export const GetSchoolNews = async (accessToken: string): Promise<Array<News>> => {
13-
const response = await manager.get<BaseResponse>(SCHOOL_NEWS(), {
14-
"include": "illustration,school,author,author.person,author.technicalUser",
15-
"fields[schoolInfo]": "illustration,school,author,publicationDateTime,title,shortContent,content,linkedWebSiteUrl",
16-
"fields[announcement]": "level",
17-
"fields[schoolInfoFile]": "url,alternativeText",
18-
"fields[school]": "name",
19-
"fields[schoolInfoAuthor]": "person,technicalUser,additionalInfo",
20-
"fields[person]": "firstName,lastName,title,photoUrl",
21-
"fields[schoolInfoTechnicalUser]": "label,logoUrl",
22-
"page[limit]": 50
23-
}, {
24-
Authorization: `Bearer ${accessToken}`
25-
});
26-
27-
const includedMap = new Map<string, unknown>();
28-
for (const item of response.included ?? []) {
29-
includedMap.set(`${item.type}:${item.id}`, item);
30-
}
31-
32-
return (Array.isArray(response.data) ? response.data : [])
33-
.filter((item): item is BaseDataResponse<"news", NewsAttributes> => item.type === "news")
9+
import { extractBaseUrl } from "../util/URL";
10+
11+
12+
export const GetSchoolNews = async (url: string, accessToken: string, mobileId: string): Promise<Array<News>> => {
13+
const [base] = extractBaseUrl(url);
14+
const manager = new RestManager(base);
15+
16+
const responsetext = await manager.post<any>(SCHOOL_NEWS(),
17+
undefined,
18+
undefined,
19+
{
20+
headers: {
21+
"Authorization": `Bearer ${accessToken}`,
22+
"Accept-Language": "fr",
23+
"SmscMobileId": mobileId
24+
}
25+
},
26+
true
27+
);
28+
const response = JSON.parse(responsetext);
29+
30+
return (Array.isArray(response) ? response : [])
3431
.map(news => {
35-
const { relationships, attributes } = news;
36-
37-
const illustrationId = getSingleRelation(relationships.illustration)?.id;
38-
const authorData = getSingleRelation(relationships.school);
39-
40-
const illustration = illustrationId
41-
? includedMap.get(`schoolInfoFile:${illustrationId}`) as fileIncluded
42-
: null;
43-
44-
const author = authorData
45-
? includedMap.get(`${authorData.type}:${authorData.id}`) as schoolIncluded
46-
: null;
47-
4832
return new News(
49-
news.id,
50-
new Date(attributes?.publicationDateTime ?? ""),
51-
attributes?.title ?? "",
52-
attributes?.shortContent ?? "",
53-
attributes?.content ?? "",
33+
news.newsItem.newsID,
34+
new Date(news.newsItem.date_published ?? ""),
35+
news.newsItem.title ?? "",
36+
news.newsItem.message ?? "",
37+
news.newsItem.message ?? "",
5438
{
55-
id: authorData?.id ?? "",
56-
name: author?.attributes?.name ?? ""
39+
id: news.newsItem.author ?? "",
40+
name: news.newsItem.name ?? ""
5741
},
58-
attributes?.linkedWebSiteUrl ?? "",
59-
new Attachment(accessToken, illustration?.id ?? "", illustration?.attributes?.url ?? "")
42+
"",
43+
new Attachment(accessToken, news.icon ?? "", `${base}/smsc/svg/${news.icon}/${news.icon}_24x24.svg`)
6044
);
6145
});
6246
};

src/structures/Smartschool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class SmartSchool {
8686
}
8787
async GetNews(): Promise<Array<News>> {
8888
await this.refreshAccessToken();
89-
return GetSchoolNews(this.accessToken);
89+
return GetSchoolNews(this.refreshURL, this.accessToken, this.SMSCMobileID);
9090
}
9191
async GetTimetable(periodStart?: Date, periodEnd?: Date): Promise<Array<TimetableDay>> {
9292
await this.refreshAccessToken();

0 commit comments

Comments
 (0)