@@ -24,35 +24,50 @@ const INITIAL_FETCH_PARAMS = {
2424 * Uses this url: "https://www.counter-strike.net/news/"
2525 * @returns {Promise<Post[]> }
2626 * @param params
27+ * @param postsFilter
2728 */
28- export async function getBlogPosts ( params ?: Partial < CSGOFetchParams > ) : Promise < Post [ ] > {
29- return await getPosts ( TYPE_IDS . blog , params )
29+ export async function getBlogPosts ( params ?: Partial < CSGOFetchParams > , postsFilter ?: CSGOPostFilter ) : Promise < Post [ ] > {
30+ return await getPosts ( TYPE_IDS . blog , params , postsFilter )
3031}
3132
3233/**
3334 * Uses this url: https://www.counter-strike.net/news/updates/
3435 * @returns {Promise<Post[]> }
3536 * @param params
37+ * @param postsFilter
3638 */
37- export async function getUpdatePosts ( params ?: Partial < CSGOFetchParams > ) : Promise < Post [ ] > {
38- return await getPosts ( TYPE_IDS . updates , params )
39+ export async function getUpdatePosts ( params ?: Partial < CSGOFetchParams > , postsFilter ?: CSGOPostFilter ) : Promise < Post [ ] > {
40+ return await getPosts ( TYPE_IDS . updates , params , postsFilter )
41+ }
42+
43+ function filterPost ( postsFilter : CSGOPostFilter , post : any , recursionDepth = 1 ) : boolean {
44+ if ( recursionDepth > 5 ) {
45+ return false
46+ }
47+ return Object . keys ( postsFilter ) . every ( key => (
48+ typeof post === 'object' &&
49+ ( typeof postsFilter [ key ] === 'object'
50+ ? filterPost ( postsFilter [ key ] , post [ key ] , recursionDepth + 1 )
51+ : postsFilter [ key ] === post [ key ]
52+ )
53+ ) )
3954}
4055
4156/**
4257 *
4358 * @returns {Promise<Post[]> }
4459 * @param category
4560 * @param params
61+ * @param postsFilter
4662 */
47- export default async function getPosts ( category ?: number , params ?: Partial < CSGOFetchParams > ) : Promise < Post [ ] > {
63+ export default async function getPosts ( category ?: number , params ?: Partial < CSGOFetchParams > , postsFilter : CSGOPostFilter = { } ) : Promise < Post [ ] > {
4864 const res = await fetch ( URL + new URLSearchParams ( { ...INITIAL_FETCH_PARAMS , ...params } as unknown as Record < string , string > ) . toString ( ) , {
4965 headers : {
5066 Accept : 'application/json'
5167 }
5268 } )
5369 const data = await res . json ( )
54- if ( ( params ?. offset ?? 0 ) > 100 ) console . log ( await res . text ( ) )
55- return data . events . filter ( ( post : any ) => ! category || post . event_type === category ) . map ( ( post : any ) => {
70+ return data . events . filter ( ( post : any ) => ( ! category || post . event_type === category ) && filterPost ( postsFilter , post ) ) . map ( ( post : any ) => {
5671 const image : string | undefined = JSON . parse ( post . jsondata ?? '{}' ) . localized_capsule_image [ 0 ]
5772 return {
5873 title : post . announcement_body . headline ,
@@ -73,6 +88,10 @@ export interface CSGOFetchParams {
7388 origin : 'https://www.counter-strike.net' | string
7489}
7590
91+ export interface CSGOPostFilter {
92+ [ key : string ] : any
93+ }
94+
7695export interface Post {
7796 title : string
7897 link : string
@@ -84,22 +103,24 @@ export interface Post {
84103export class UpdatesListener {
85104 private readonly intervalId : NodeJS . Timer
86105 private readonly params ?: Partial < CSGOFetchParams >
106+ private readonly postsFilter : CSGOPostFilter
87107 private readonly callback : ( post : Post ) => any
88108 private readonly callbackError ?: ( e : unknown ) => any
89109 private lastPostTime : number
90- constructor ( callback : ( post : Post ) => any , interval : number = 600000 , params ?: Partial < CSGOFetchParams > , lastPostTime ?: number , callbackError ?: ( e : unknown ) => any ) {
110+ constructor ( callback : ( post : Post ) => any , interval : number = 600000 , params ?: Partial < CSGOFetchParams > , lastPostTime ?: number , callbackError ?: ( e : unknown ) => any , postsFilter : CSGOPostFilter = { } ) {
91111 this . lastPostTime = lastPostTime ?? Date . now ( )
92112 this . params = params
93113 this . callback = callback
94114 this . callbackError = callbackError
115+ this . postsFilter = postsFilter
95116 // eslint-disable-next-line @typescript-eslint/no-misused-promises
96117 this . intervalId = setInterval ( this . fetchPosts . bind ( this ) , interval )
97118 void this . fetchPosts ( )
98119 }
99120
100121 async fetchPosts ( ) : Promise < void > {
101122 try {
102- const posts = await getPosts ( undefined , this . params )
123+ const posts = await getPosts ( undefined , this . params , this . postsFilter )
103124 posts . reverse ( ) . forEach ( post => {
104125 if ( post . date . getTime ( ) <= this . lastPostTime ) {
105126 return
0 commit comments