-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
86 lines (77 loc) · 3.11 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Handler, Context} from 'aws-lambda';
import { DynamoDBClient, ListTablesCommand, GetItemCommand, BatchGetItemCommand, BatchGetItemInput, KeysAndAttributes, BatchGetItemCommandOutput, BatchGetItemCommandInput} from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient, BatchGetCommand, BatchGetCommandInput, BatchGetCommandOutput, Query } from "@aws-sdk/lib-dynamodb"; // ES6 import
import { SQS, SendMessageBatchCommand, SendMessageCommand, SendMessageBatchCommandInput } from "@aws-sdk/client-sqs"
import { ClientRequest } from 'http';
import { articleSeenBefore } from './libs/articleSeenBefore'
import { makeGetRequest } from './libs/makeGetRequest'
import { Article } from './libs/article'
import { enqueArticles } from './libs/enqueArticles'
const https = require('https');
const { XMLParser } = require('fast-xml-parser');
const { parse } = require('path');
exports.handler = async (event, context, callback) => {
const rawServerChannelMap = event["serverIdToChannelMap"]["M"]
const serverChannelMap = parseServerChannelMap(rawServerChannelMap)
const url = event['sort-key']["S"]
const httpResponse = await makeGetRequest(url)
const articlesArr: Article[] = parseXML(httpResponse)
const articlesSeenBeforeMap = await articleSeenBefore(articlesArr)
// filter out the articles which have been seen before
const unqueuedArticlesArr = articlesArr.filter((article: Article) => {
return articlesSeenBeforeMap[article.sortKey] != true
})
const discordEmbedsArr = []
unqueuedArticlesArr.forEach((article) => {
discordEmbedsArr.push(article.toEmbed())
})
const enqueRes = await enqueArticles(discordEmbedsArr, serverChannelMap)
return enqueRes
}
// async function enqueArticles(articlesArr, serverChannelMap){
// return new Promise((resolve, reject) => {
// const length = articlesArr.length
// let i: number = 0
// while(i < length){
// let j = i + 9
// let slice = articlesArr.slice(i,j)
// let sendMessageString = ""
// slice.forEach((item: Article) => { sendMessageString += item })
// console.log(slice)
// i = j
// }
// resolve("complete")
// })
// }
// this function is necessary to eliminate the cumbersome "AttributeValue"
// keys (i.e. types).
// E.g. here's how "subscribers" looks before being parsed by this function:
// "31049823059721":{"S":"23023094823"},"3405920983094":{"S":"340235"}
// and after:
// "31049823059721":"23023094823","3405920983094":"340235"}
function parseServerChannelMap(rawSubscribers){
for(const key in rawSubscribers){
rawSubscribers[key] = rawSubscribers[key]["S"]
}
return rawSubscribers
}
function parseXML(data): Article[]{
const options = {
//preserveOrder:true,
ignoreAttributes:true,
ignoreDeclaration:true,
ignorePiTags:true,
}
const parser = new XMLParser(options)
let resArr = []
const fullParsedData = parser.parse(data)
//return fullParsedData[0].rss[0]
const rawArticlesArr = fullParsedData.rss.channel.item
rawArticlesArr.forEach((a) => {
let article = new Article(a)
if(article.sortKey !== undefined){
resArr.push(article)
}
})
return resArr
}