From 0be12412ce43f962e1e90b6aeae05413f86d1867 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Tue, 13 Apr 2021 16:56:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fixing=20wrong=20date=20and=20wo?= =?UTF-8?q?rk=20without=20redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- package.json | 2 +- src/controllers/AnimeController.ts | 530 +++++++++++++------------ src/controllers/DirectoryController.ts | 288 +++++++------- src/controllers/UtilsController.ts | 328 ++++++++------- src/database/connection.ts | 22 + src/database/models/anime.model.ts | 2 - src/utils/util.ts | 508 +++++++++++++----------- 8 files changed, 909 insertions(+), 773 deletions(-) diff --git a/README.md b/README.md index 277dbe5..cf4f3e6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# **Aruppi API** (v4.0.3) +# **Aruppi API** (v4.0.4) > This API has everything about Japan, from anime, music, radio, images, videos ... to japanese culture > diff --git a/package.json b/package.json index b7f7bfd..63bb4e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aruppi", - "version": "4.0.3", + "version": "4.0.4", "description": "Aruppi is a custom API to obtain data from the Japanese culture for the mobile app", "main": "./src/api/api.ts", "scripts": { diff --git a/src/controllers/AnimeController.ts b/src/controllers/AnimeController.ts index a351160..741fb71 100644 --- a/src/controllers/AnimeController.ts +++ b/src/controllers/AnimeController.ts @@ -84,20 +84,22 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `schedule_${hashStringMd5(day)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `schedule_${hashStringMd5(day)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot(`${urls.BASE_JIKAN}schedule/${day}`, { - parse: true, - scrapy: false, - }); + return res.status(200).json(resultRedis); + } } + + data = await requestGot(`${urls.BASE_JIKAN}schedule/${day}`, { + parse: true, + scrapy: false, + }); } catch (err) { return next(err); } @@ -109,19 +111,21 @@ export default class AnimeController { })); if (animeList.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `schedule_${hashStringMd5(day)}`, - JSON.stringify({ day: animeList }), - ); + redisClient.set( + `schedule_${hashStringMd5(day)}`, + JSON.stringify({ day: animeList }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `schedule_${hashStringMd5(day)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `schedule_${hashStringMd5(day)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ day: animeList, @@ -136,35 +140,37 @@ export default class AnimeController { let data: any; try { - let resultQueryRedis: any; - - if (subtype) { - resultQueryRedis = await redisClient.get( - `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, - ); - } else { - resultQueryRedis = await redisClient.get( - `top_${hashStringMd5(`${type}:${page}`)}`, - ); - } - - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (redisClient.connected) { + let resultQueryRedis: any; - return res.status(200).json(resultRedis); - } else { - if (subtype !== undefined) { - data = await requestGot( - `${urls.BASE_JIKAN}top/${type}/${page}/${subtype}`, - { parse: true, scrapy: false }, + if (subtype) { + resultQueryRedis = await redisClient.get( + `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, ); } else { - data = await requestGot(`${urls.BASE_JIKAN}top/${type}/${page}`, { - parse: true, - scrapy: false, - }); + resultQueryRedis = await redisClient.get( + `top_${hashStringMd5(`${type}:${page}`)}`, + ); + } + + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); + + return res.status(200).json(resultRedis); } } + + if (subtype !== undefined) { + data = await requestGot( + `${urls.BASE_JIKAN}top/${type}/${page}/${subtype}`, + { parse: true, scrapy: false }, + ); + } else { + data = await requestGot(`${urls.BASE_JIKAN}top/${type}/${page}`, { + parse: true, + scrapy: false, + }); + } } catch (err) { return next(err); } @@ -181,31 +187,33 @@ export default class AnimeController { })); if (top.length > 0) { - /* Set the key in the redis cache. */ - if (subtype) { - redisClient.set( - `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, - JSON.stringify({ top }), - ); - } else { - redisClient.set( - `top_${hashStringMd5(`${type}:${page}`)}`, - JSON.stringify({ top }), - ); - } + if (redisClient.connected) { + /* Set the key in the redis cache. */ + if (subtype) { + redisClient.set( + `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, + JSON.stringify({ top }), + ); + } else { + redisClient.set( + `top_${hashStringMd5(`${type}:${page}`)}`, + JSON.stringify({ top }), + ); + } - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - if (subtype) { - redisClient.expireat( - `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); - } else { - redisClient.expireat( - `top_${hashStringMd5(`${type}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + if (subtype) { + redisClient.expireat( + `top_${hashStringMd5(`${type}:${subtype}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } else { + redisClient.expireat( + `top_${hashStringMd5(`${type}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } } return res.status(200).json({ top }); @@ -248,29 +256,31 @@ export default class AnimeController { let animeList: any[] = []; try { - const resultQueryRedis: any = await redisClient.get( - `lastEpisodes_${hashStringMd5('lastEpisodes')}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `lastEpisodes_${hashStringMd5('lastEpisodes')}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - // @ANIMEFLV - // data = await requestGot( - // `${urls.BASE_ANIMEFLV_JELU}LatestEpisodesAdded`, - // { - // parse: true, - // scrapy: false, - // }, - // ); - - $ = await requestGot(`${urls.BASE_MONOSCHINOS}`, { - scrapy: true, - parse: false, - }); + return res.status(200).json(resultRedis); + } } + + // @ANIMEFLV + // data = await requestGot( + // `${urls.BASE_ANIMEFLV_JELU}LatestEpisodesAdded`, + // { + // parse: true, + // scrapy: false, + // }, + // ); + + $ = await requestGot(`${urls.BASE_MONOSCHINOS}`, { + scrapy: true, + parse: false, + }); } catch (err) { return next(err); } @@ -329,19 +339,21 @@ export default class AnimeController { } if (episodes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `lastEpisodes_${hashStringMd5('lastEpisodes')}`, - JSON.stringify({ episodes }), - ); + redisClient.set( + `lastEpisodes_${hashStringMd5('lastEpisodes')}`, + JSON.stringify({ episodes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `lastEpisodes_${hashStringMd5('lastEpisodes')}`, - parseInt(`${+new Date() / 1000}`, 10) + 1800, - ); + redisClient.expireat( + `lastEpisodes_${hashStringMd5('lastEpisodes')}`, + parseInt(`${+new Date() / 1000}`, 10) + 1800, + ); + } res.status(200).json({ episodes, @@ -357,25 +369,27 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `contentTv_${hashStringMd5(`${type}:${page}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `contentTv_${hashStringMd5(`${type}:${page}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_ANIMEFLV_JELU}${ - url.charAt(0).toUpperCase() + url.slice(1) - }/${type}/${page}`, - { - parse: true, - scrapy: false, - }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_ANIMEFLV_JELU}${ + url.charAt(0).toUpperCase() + url.slice(1) + }/${type}/${page}`, + { + parse: true, + scrapy: false, + }, + ); } catch (err) { return next(err); } @@ -397,19 +411,21 @@ export default class AnimeController { }); if (animes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `contentTv_${hashStringMd5(`${type}:${page}`)}`, - JSON.stringify({ animes }), - ); + redisClient.set( + `contentTv_${hashStringMd5(`${type}:${page}`)}`, + JSON.stringify({ animes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `contentTv_${hashStringMd5(`${type}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `contentTv_${hashStringMd5(`${type}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ animes, @@ -425,25 +441,27 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_ANIMEFLV_JELU}${ - url.charAt(0).toUpperCase() + url.slice(1) - }/${type}/${page}`, - { - parse: true, - scrapy: false, - }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_ANIMEFLV_JELU}${ + url.charAt(0).toUpperCase() + url.slice(1) + }/${type}/${page}`, + { + parse: true, + scrapy: false, + }, + ); } catch (err) { return next(err); } @@ -465,19 +483,21 @@ export default class AnimeController { }); if (animes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, - JSON.stringify({ animes }), - ); + redisClient.set( + `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, + JSON.stringify({ animes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `contentSpecial_${hashStringMd5(`${type}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ animes, @@ -493,25 +513,27 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `contentOva_${hashStringMd5(`${type}:${page}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `contentOva_${hashStringMd5(`${type}:${page}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_ANIMEFLV_JELU}${ - url.charAt(0).toUpperCase() + url.slice(1) - }/${type}/${page}`, - { - parse: true, - scrapy: false, - }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_ANIMEFLV_JELU}${ + url.charAt(0).toUpperCase() + url.slice(1) + }/${type}/${page}`, + { + parse: true, + scrapy: false, + }, + ); } catch (err) { return next(err); } @@ -533,19 +555,21 @@ export default class AnimeController { }); if (animes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `contentOva_${hashStringMd5(`${type}:${page}`)}`, - JSON.stringify({ animes }), - ); + redisClient.set( + `contentOva_${hashStringMd5(`${type}:${page}`)}`, + JSON.stringify({ animes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `contentOva_${hashStringMd5(`${type}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `contentOva_${hashStringMd5(`${type}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ animes, @@ -561,25 +585,27 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `contentMovie_${hashStringMd5(`${type}:${page}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `contentMovie_${hashStringMd5(`${type}:${page}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_ANIMEFLV_JELU}${ - url.charAt(0).toUpperCase() + url.slice(1) - }/${type}/${page}`, - { - parse: true, - scrapy: false, - }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_ANIMEFLV_JELU}${ + url.charAt(0).toUpperCase() + url.slice(1) + }/${type}/${page}`, + { + parse: true, + scrapy: false, + }, + ); } catch (err) { return next(err); } @@ -601,19 +627,21 @@ export default class AnimeController { }); if (animes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `contentMovie_${hashStringMd5(`${type}:${page}`)}`, - JSON.stringify({ animes }), - ); + redisClient.set( + `contentMovie_${hashStringMd5(`${type}:${page}`)}`, + JSON.stringify({ animes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `contentMovie_${hashStringMd5(`${type}:${page}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `contentMovie_${hashStringMd5(`${type}:${page}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ animes, @@ -629,19 +657,21 @@ export default class AnimeController { let episodes: any; try { - const resultQueryRedis: any = await redisClient.get( - `episodes_${hashStringMd5(title)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `episodes_${hashStringMd5(title)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - searchAnime = await AnimeModel.findOne({ - $or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }], - }); + return res.status(200).json(resultRedis); + } } + + searchAnime = await AnimeModel.findOne({ + $or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }], + }); } catch (err) { return next(err); } @@ -662,19 +692,21 @@ export default class AnimeController { } if (episodes) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `episodes_${hashStringMd5(title)}`, - JSON.stringify({ episodes }), - ); + redisClient.set( + `episodes_${hashStringMd5(title)}`, + JSON.stringify({ episodes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `episodes_${hashStringMd5(title)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `episodes_${hashStringMd5(title)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ episodes }); } else { @@ -687,31 +719,35 @@ export default class AnimeController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `servers_${hashStringMd5(id)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `servers_${hashStringMd5(id)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - if (isNaN(parseInt(id.split('/')[0]))) { - if (id.split('/')[0] === 'ver') { - data = await videoServersMonosChinos(id); - } else { - data = await videoServersJK(id); - } - } else { - data = await requestGot( - `${urls.BASE_ANIMEFLV_JELU}GetAnimeServers/${id}`, - { parse: true, scrapy: false }, - ); + return res.status(200).json(resultRedis); + } + } - data = await transformUrlServer(data.servers); + if (isNaN(parseInt(id.split('/')[0]))) { + if (id.split('/')[0] === 'ver') { + data = await videoServersMonosChinos(id); + } else { + data = await videoServersJK(id); } + } else { + data = await requestGot( + `${urls.BASE_ANIMEFLV_JELU}GetAnimeServers/${id}`, + { parse: true, scrapy: false }, + ); - if (data) { + data = await transformUrlServer(data.servers); + } + + if (data) { + if (redisClient.connected) { /* Set the key in the redis cache. */ redisClient.set( @@ -725,11 +761,11 @@ export default class AnimeController { `servers_${hashStringMd5(id)}`, parseInt(`${+new Date() / 1000}`, 10) + 7200, ); - - res.status(200).json({ servers: data }); - } else { - res.status(500).json({ message: 'Aruppi lost in the shell' }); } + + res.status(200).json({ servers: data }); + } else { + res.status(500).json({ message: 'Aruppi lost in the shell' }); } } catch (err) { return next(err); @@ -752,7 +788,6 @@ export default class AnimeController { title: animeQuery[0].title || null, poster: animeQuery[0].poster || null, synopsis: animeQuery[0].description || null, - status: animeQuery[0].state || null, type: animeQuery[0].type || null, rating: animeQuery[0].score || null, genres: animeQuery[0].genres || null, @@ -767,7 +802,6 @@ export default class AnimeController { title: animeQuery[0].title || null, poster: animeQuery[0].poster || null, synopsis: animeQuery[0].description || null, - status: animeQuery[0].state || null, type: animeQuery[0].type || null, rating: animeQuery[0].score || null, genres: animeQuery[0].genres || null, diff --git a/src/controllers/DirectoryController.ts b/src/controllers/DirectoryController.ts index 4ea2d2f..78cc48b 100644 --- a/src/controllers/DirectoryController.ts +++ b/src/controllers/DirectoryController.ts @@ -61,7 +61,6 @@ export default class DirectoryController { poster: item.poster, type: item.type, genres: item.genres, - state: item.state, score: item.score, source: item.source, description: item.description, @@ -87,7 +86,6 @@ export default class DirectoryController { poster: item.poster, type: item.type, genres: item.genres, - state: item.state, score: item.score, source: item.source, description: item.description, @@ -111,20 +109,22 @@ export default class DirectoryController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `season_${hashStringMd5(`${year}:${type}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `season_${hashStringMd5(`${year}:${type}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot(`${urls.BASE_JIKAN}season/${year}/${type}`, { - scrapy: false, - parse: true, - }); + return res.status(200).json(resultRedis); + } } + + data = await requestGot(`${urls.BASE_JIKAN}season/${year}/${type}`, { + scrapy: false, + parse: true, + }); } catch (err) { return next(err); } @@ -138,19 +138,21 @@ export default class DirectoryController { }); if (season.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `season_${hashStringMd5(`${year}:${type}`)}`, - JSON.stringify({ season }), - ); + redisClient.set( + `season_${hashStringMd5(`${year}:${type}`)}`, + JSON.stringify({ season }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `season_${hashStringMd5(`${year}:${type}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `season_${hashStringMd5(`${year}:${type}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ season, @@ -164,20 +166,22 @@ export default class DirectoryController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `allSeasons_${hashStringMd5('allSeasons')}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `allSeasons_${hashStringMd5('allSeasons')}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot(`${urls.BASE_JIKAN}season/archive`, { - parse: true, - scrapy: false, - }); + return res.status(200).json(resultRedis); + } } + + data = await requestGot(`${urls.BASE_JIKAN}season/archive`, { + parse: true, + scrapy: false, + }); } catch (err) { return next(err); } @@ -190,19 +194,21 @@ export default class DirectoryController { }); if (archive.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `allSeasons_${hashStringMd5('allSeasons')}`, - JSON.stringify({ archive }), - ); + redisClient.set( + `allSeasons_${hashStringMd5('allSeasons')}`, + JSON.stringify({ archive }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `allSeasons_${hashStringMd5('allSeasons')}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `allSeasons_${hashStringMd5('allSeasons')}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ archive }); } else { @@ -214,20 +220,22 @@ export default class DirectoryController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `laterSeasons_${hashStringMd5('laterSeasons')}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `laterSeasons_${hashStringMd5('laterSeasons')}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot(`${urls.BASE_JIKAN}season/later`, { - parse: true, - scrapy: false, - }); + return res.status(200).json(resultRedis); + } } + + data = await requestGot(`${urls.BASE_JIKAN}season/later`, { + parse: true, + scrapy: false, + }); } catch (err) { return next(err); } @@ -241,19 +249,21 @@ export default class DirectoryController { }); if (future.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `laterSeasons_${hashStringMd5('laterSeasons')}`, - JSON.stringify({ future }), - ); + redisClient.set( + `laterSeasons_${hashStringMd5('laterSeasons')}`, + JSON.stringify({ future }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `laterSeasons_${hashStringMd5('laterSeasons')}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `laterSeasons_${hashStringMd5('laterSeasons')}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ future }); } else { @@ -267,90 +277,94 @@ export default class DirectoryController { let resultAnime: any; try { - const resultQueryRedis: any = await redisClient.get( - `moreInfo_${hashStringMd5(title)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `moreInfo_${hashStringMd5(title)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - resultQuery = await AnimeModel.findOne({ - $or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }], - }); - - const extraInfo: any = await animeExtraInfo(resultQuery!.mal_id); - - switch (resultQuery?.source) { - case 'animeflv': - resultAnime = { - title: resultQuery?.title, - poster: resultQuery?.poster, - synopsis: resultQuery?.description, - status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', - type: resultQuery?.type, - rating: resultQuery?.score, - genres: resultQuery?.genres, - moreInfo: [extraInfo], - promo: await getAnimeVideoPromo(resultQuery!.mal_id), - characters: await getAnimeCharacters(resultQuery!.mal_id), - related: await getRelatedAnimesFLV(resultQuery!.id), - }; - break; - case 'jkanime': - resultAnime = { - title: resultQuery?.title, - poster: resultQuery?.poster, - synopsis: resultQuery?.description, - status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', - type: resultQuery?.type, - rating: resultQuery?.score, - genres: resultQuery?.genres, - moreInfo: [extraInfo], - promo: await getAnimeVideoPromo(resultQuery!.mal_id), - characters: await getAnimeCharacters(resultQuery!.mal_id), - related: await getRelatedAnimesMAL(resultQuery!.mal_id), - }; - break; - case 'monoschinos': - resultAnime = { - title: resultQuery?.title, - poster: resultQuery?.poster, - synopsis: resultQuery?.description, - status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', - type: resultQuery?.type, - rating: resultQuery?.score, - genres: resultQuery?.genres, - moreInfo: [extraInfo], - promo: await getAnimeVideoPromo(resultQuery!.mal_id), - characters: await getAnimeCharacters(resultQuery!.mal_id), - related: await getRelatedAnimesMAL(resultQuery!.mal_id), - }; - break; - default: - resultAnime = undefined; - break; + return res.status(200).json(resultRedis); } } + + resultQuery = await AnimeModel.findOne({ + $or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }], + }); + + const extraInfo: any = await animeExtraInfo(resultQuery!.mal_id); + + switch (resultQuery?.source) { + case 'animeflv': + resultAnime = { + title: resultQuery?.title, + poster: resultQuery?.poster, + synopsis: resultQuery?.description, + status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', + type: resultQuery?.type, + rating: resultQuery?.score, + genres: resultQuery?.genres, + moreInfo: [extraInfo], + promo: await getAnimeVideoPromo(resultQuery!.mal_id), + characters: await getAnimeCharacters(resultQuery!.mal_id), + related: await getRelatedAnimesFLV(resultQuery!.id), + }; + break; + case 'jkanime': + resultAnime = { + title: resultQuery?.title, + poster: resultQuery?.poster, + synopsis: resultQuery?.description, + status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', + type: resultQuery?.type, + rating: resultQuery?.score, + genres: resultQuery?.genres, + moreInfo: [extraInfo], + promo: await getAnimeVideoPromo(resultQuery!.mal_id), + characters: await getAnimeCharacters(resultQuery!.mal_id), + related: await getRelatedAnimesMAL(resultQuery!.mal_id), + }; + break; + case 'monoschinos': + resultAnime = { + title: resultQuery?.title, + poster: resultQuery?.poster, + synopsis: resultQuery?.description, + status: !extraInfo.aired.to ? 'En emisión' : 'Finalizado', + type: resultQuery?.type, + rating: resultQuery?.score, + genres: resultQuery?.genres, + moreInfo: [extraInfo], + promo: await getAnimeVideoPromo(resultQuery!.mal_id), + characters: await getAnimeCharacters(resultQuery!.mal_id), + related: await getRelatedAnimesMAL(resultQuery!.mal_id), + }; + break; + default: + resultAnime = undefined; + break; + } } catch (err) { return next(err); } if (resultAnime) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `moreInfo_${hashStringMd5(title)}`, - JSON.stringify(resultAnime), - ); + redisClient.set( + `moreInfo_${hashStringMd5(title)}`, + JSON.stringify(resultAnime), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `moreInfo_${hashStringMd5(title)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `moreInfo_${hashStringMd5(title)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json(resultAnime); } else { diff --git a/src/controllers/UtilsController.ts b/src/controllers/UtilsController.ts index c7d3808..48754ff 100644 --- a/src/controllers/UtilsController.ts +++ b/src/controllers/UtilsController.ts @@ -69,17 +69,19 @@ export default class UtilsController { let feed: CustomFeed & Parser.Output; try { - const resultQueryRedis: any = await redisClient.get( - `anitakume_${hashStringMd5('anitakume')}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `anitakume_${hashStringMd5('anitakume')}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - feed = await parser.parseURL(urls.BASE_IVOOX); + return res.status(200).json(resultRedis); + } } + + feed = await parser.parseURL(urls.BASE_IVOOX); } catch (err) { return next(err); } @@ -116,19 +118,21 @@ export default class UtilsController { }); if (podcast.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `anitakume_${hashStringMd5('anitakume')}`, - JSON.stringify({ podcast }), - ); + redisClient.set( + `anitakume_${hashStringMd5('anitakume')}`, + JSON.stringify({ podcast }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `anitakume_${hashStringMd5('anitakume')}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `anitakume_${hashStringMd5('anitakume')}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ podcast }); } else { @@ -153,51 +157,55 @@ export default class UtilsController { ]; try { - const resultQueryRedis: any = await redisClient.get( - `news_${hashStringMd5('news')}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `news_${hashStringMd5('news')}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - for (const rssPage of pagesRss) { - const feed = await parser.parseURL(rssPage.url); - - feed.items.forEach((item: any) => { - const formattedObject: News = { - title: item.title, - url: item.link, - author: feed.title?.includes('Crunchyroll') - ? 'Crunchyroll' - : feed.title, - thumbnail: obtainPreviewNews(item['content:encoded']), - content: item['content:encoded'], - }; - - news.push(formattedObject); - }); + return res.status(200).json(resultRedis); } } + + for (const rssPage of pagesRss) { + const feed = await parser.parseURL(rssPage.url); + + feed.items.forEach((item: any) => { + const formattedObject: News = { + title: item.title, + url: item.link, + author: feed.title?.includes('Crunchyroll') + ? 'Crunchyroll' + : feed.title, + thumbnail: obtainPreviewNews(item['content:encoded']), + content: item['content:encoded'], + }; + + news.push(formattedObject); + }); + } } catch (err) { return next(err); } if (news.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `news_${hashStringMd5('news')}`, - JSON.stringify({ news }), - ); + redisClient.set( + `news_${hashStringMd5('news')}`, + JSON.stringify({ news }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `news_${hashStringMd5('news')}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `news_${hashStringMd5('news')}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ news }); } else { @@ -210,20 +218,22 @@ export default class UtilsController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `images_${hashStringMd5(title)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `images_${hashStringMd5(title)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_QWANT}count=51&q=${title}&t=images&safesearch=1&locale=es_ES&uiv=4`, - { scrapy: false, parse: true }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_QWANT}count=51&q=${title}&t=images&safesearch=1&locale=es_ES&uiv=4`, + { scrapy: false, parse: true }, + ); } catch (err) { return next(err); } @@ -237,19 +247,21 @@ export default class UtilsController { }); if (results.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `images_${hashStringMd5(title)}`, - JSON.stringify({ images: results }), - ); + redisClient.set( + `images_${hashStringMd5(title)}`, + JSON.stringify({ images: results }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `images_${hashStringMd5(title)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `images_${hashStringMd5(title)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ images: results }); } else { @@ -262,20 +274,22 @@ export default class UtilsController { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `videos_${hashStringMd5(channelId)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `videos_${hashStringMd5(channelId)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - data = await requestGot( - `${urls.BASE_YOUTUBE}${channelId}&part=snippet,id&order=date&maxResults=50`, - { scrapy: false, parse: true }, - ); + return res.status(200).json(resultRedis); + } } + + data = await requestGot( + `${urls.BASE_YOUTUBE}${channelId}&part=snippet,id&order=date&maxResults=50`, + { scrapy: false, parse: true }, + ); } catch (err) { return next(err); } @@ -291,19 +305,21 @@ export default class UtilsController { }); if (results.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `videos_${hashStringMd5(channelId)}`, - JSON.stringify({ videos: results }), - ); + redisClient.set( + `videos_${hashStringMd5(channelId)}`, + JSON.stringify({ videos: results }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `videos_${hashStringMd5(channelId)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `videos_${hashStringMd5(channelId)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ videos: results }); } else { @@ -442,35 +458,39 @@ export default class UtilsController { let themes: any; try { - const resultQueryRedis: any = await redisClient.get( - `oped_${hashStringMd5(title)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `oped_${hashStringMd5(title)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - themes = await structureThemes(await themeParser.serie(title), true); + return res.status(200).json(resultRedis); + } } + + themes = await structureThemes(await themeParser.serie(title), true); } catch (err) { return next(err); } if (themes) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `oped_${hashStringMd5(title)}`, - JSON.stringify({ themes }), - ); + redisClient.set( + `oped_${hashStringMd5(title)}`, + JSON.stringify({ themes }), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `oped_${hashStringMd5(title)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `oped_${hashStringMd5(title)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } res.status(200).json({ themes }); } else { @@ -484,58 +504,62 @@ export default class UtilsController { let resultQueryRedis: any; try { - if (year) { - resultQueryRedis = await redisClient.get( - `themesyear_${hashStringMd5(year)}`, - ); - } else { - resultQueryRedis = await redisClient.get( - `themesyear_${hashStringMd5('allYear')}`, - ); - } + if (redisClient.connected) { + if (year) { + resultQueryRedis = await redisClient.get( + `themesyear_${hashStringMd5(year)}`, + ); + } else { + resultQueryRedis = await redisClient.get( + `themesyear_${hashStringMd5('allYear')}`, + ); + } - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return res.status(200).json(resultRedis); - } else { - if (year === undefined) { - themes = await themeParser.allYears(); - } else { - themes = await structureThemes(await themeParser.year(year), false); + return res.status(200).json(resultRedis); } } + + if (year === undefined) { + themes = await themeParser.allYears(); + } else { + themes = await structureThemes(await themeParser.year(year), false); + } } catch (err) { return next(err); } if (themes.length > 0) { - /* Set the key in the redis cache. */ - - if (year) { - redisClient.set( - `themesyear_${hashStringMd5(year)}`, - JSON.stringify({ themes }), - ); - } else { - redisClient.set( - `themesyear_${hashStringMd5('allYear')}`, - JSON.stringify({ themes }), - ); - } + if (redisClient.connected) { + /* Set the key in the redis cache. */ + + if (year) { + redisClient.set( + `themesyear_${hashStringMd5(year)}`, + JSON.stringify({ themes }), + ); + } else { + redisClient.set( + `themesyear_${hashStringMd5('allYear')}`, + JSON.stringify({ themes }), + ); + } - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - if (year) { - redisClient.expireat( - `themesyear_${hashStringMd5(year)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); - } else { - redisClient.expireat( - `themesyear_${hashStringMd5('allYear')}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + if (year) { + redisClient.expireat( + `themesyear_${hashStringMd5(year)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } else { + redisClient.expireat( + `themesyear_${hashStringMd5('allYear')}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } } res.status(200).json({ themes }); diff --git a/src/database/connection.ts b/src/database/connection.ts index 6eb1ab2..f9f7680 100644 --- a/src/database/connection.ts +++ b/src/database/connection.ts @@ -40,8 +40,30 @@ export const redisClient: RedisClient = redis.createClient({ host: process.env.REDIS_HOST, port: parseInt(process.env.REDIS_PORT!), password: process.env.REDIS_PASSWORD, + retry_strategy: function (options) { + if (options.error && options.error.code === 'ECONNREFUSED') { + // End reconnecting on a specific error and flush all commands with + // a individual error + return new Error('The server refused the connection'); + } + if (options.total_retry_time > 1000 * 60 * 60) { + // End reconnecting after a specific timeout and flush all commands + // with a individual error + return new Error('Retry time exhausted'); + } + if (options.attempt > 10) { + // End reconnecting with built in error + return undefined; + } + // reconnect after + return Math.min(options.attempt * 100, 3000); + }, }); redisClient.on('connect', () => { console.log('Redis connected: redis.'); }); + +redisClient.on('error', function (err) { + console.log('Redis error: ' + err); +}); diff --git a/src/database/models/anime.model.ts b/src/database/models/anime.model.ts index b3ef5de..5ba67eb 100644 --- a/src/database/models/anime.model.ts +++ b/src/database/models/anime.model.ts @@ -12,7 +12,6 @@ export interface Anime extends Document { poster: string; type: string; genres: Types.Array; - state: string; score: string; source: string; description: string; @@ -26,7 +25,6 @@ const AnimeSchema: Schema = new Schema({ poster: { type: String }, type: { type: String }, genres: [{ type: String }], - state: { type: String }, score: { type: String }, source: { type: String }, description: { type: String }, diff --git a/src/utils/util.ts b/src/utils/util.ts index 2fa57ee..da63c54 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -56,22 +56,24 @@ export const animeExtraInfo = async (mal_id: number) => { }; try { - const resultQueryRedis: any = await redisClient.get( - `extraInfo_${hashStringMd5(`${mal_id}`)}`, - ); - - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `extraInfo_${hashStringMd5(`${mal_id}`)}`, + ); - return resultRedis; - } else { - data = await requestGot(`${urls.BASE_JIKAN}anime/${mal_id}`, { - parse: true, - scrapy: false, - }); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - broadcast = data.broadcast.split('at')[0].trim().toLowerCase() || null; + return resultRedis; + } } + + data = await requestGot(`${urls.BASE_JIKAN}anime/${mal_id}`, { + parse: true, + scrapy: false, + }); + + broadcast = data.broadcast.split('at')[0].trim().toLowerCase() || null; } catch (err) { return err; } @@ -101,19 +103,21 @@ export const animeExtraInfo = async (mal_id: number) => { }; if (formattedObject) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `extraInfo_${hashStringMd5(`${mal_id}`)}`, - JSON.stringify(formattedObject), - ); + redisClient.set( + `extraInfo_${hashStringMd5(`${mal_id}`)}`, + JSON.stringify(formattedObject), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `extraInfo_${hashStringMd5(`${mal_id}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `extraInfo_${hashStringMd5(`${mal_id}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return formattedObject; } else { @@ -125,20 +129,22 @@ export const getAnimeVideoPromo = async (mal_id: number) => { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `promoInfo_${hashStringMd5(`${mal_id}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `promoInfo_${hashStringMd5(`${mal_id}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - data = await requestGot(`${urls.BASE_JIKAN}anime/${mal_id}/videos`, { - parse: true, - scrapy: false, - }); + return resultRedis; + } } + + data = await requestGot(`${urls.BASE_JIKAN}anime/${mal_id}/videos`, { + parse: true, + scrapy: false, + }); } catch (err) { return err; } @@ -152,19 +158,21 @@ export const getAnimeVideoPromo = async (mal_id: number) => { }); if (promo.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `promoInfo_${hashStringMd5(`${mal_id}`)}`, - JSON.stringify(promo), - ); + redisClient.set( + `promoInfo_${hashStringMd5(`${mal_id}`)}`, + JSON.stringify(promo), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `promoInfo_${hashStringMd5(`${mal_id}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `promoInfo_${hashStringMd5(`${mal_id}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return promo; } else { @@ -176,20 +184,22 @@ export const getAnimeCharacters = async (mal_id: number) => { let data: any; try { - const resultQueryRedis: any = await redisClient.get( - `charactersInfo_${hashStringMd5(`${mal_id}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `charactersInfo_${hashStringMd5(`${mal_id}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - data = await requestGot( - `${urls.BASE_JIKAN}anime/${mal_id}/characters_staff`, - { parse: true, scrapy: false }, - ); + return resultRedis; + } } + + data = await requestGot( + `${urls.BASE_JIKAN}anime/${mal_id}/characters_staff`, + { parse: true, scrapy: false }, + ); } catch (err) { return err; } @@ -204,19 +214,21 @@ export const getAnimeCharacters = async (mal_id: number) => { }); if (characters.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `charactersInfo_${hashStringMd5(`${mal_id}`)}`, - JSON.stringify(characters), - ); + redisClient.set( + `charactersInfo_${hashStringMd5(`${mal_id}`)}`, + JSON.stringify(characters), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `charactersInfo_${hashStringMd5(`${mal_id}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `charactersInfo_${hashStringMd5(`${mal_id}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return characters; } else { @@ -251,20 +263,22 @@ export const getRelatedAnimesFLV = async (id: string) => { let $: cheerio.Root; try { - const resultQueryRedis: any = await redisClient.get( - `relatedFLV_${hashStringMd5(id)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `relatedFLV_${hashStringMd5(id)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_ANIMEFLV}/anime/${id}`, { - parse: false, - scrapy: true, - }); + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_ANIMEFLV}/anime/${id}`, { + parse: false, + scrapy: true, + }); } catch (err) { return err; } @@ -292,19 +306,21 @@ export const getRelatedAnimesFLV = async (id: string) => { } if (relatedAnimes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `relatedFLV_${hashStringMd5(id)}`, - JSON.stringify(relatedAnimes), - ); + redisClient.set( + `relatedFLV_${hashStringMd5(id)}`, + JSON.stringify(relatedAnimes), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `relatedFLV_${hashStringMd5(id)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `relatedFLV_${hashStringMd5(id)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return relatedAnimes; } else { @@ -316,20 +332,22 @@ export const getRelatedAnimesMAL = async (mal_id: number) => { let $: cheerio.Root; try { - const resultQueryRedis: any = await redisClient.get( - `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`https://myanimelist.net/anime/${mal_id}`, { - parse: false, - scrapy: true, - }); + return resultRedis; + } } + + $ = await requestGot(`https://myanimelist.net/anime/${mal_id}`, { + parse: false, + scrapy: true, + }); } catch (err) { return err; } @@ -365,19 +383,21 @@ export const getRelatedAnimesMAL = async (mal_id: number) => { } if (relatedAnimes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, - JSON.stringify(relatedAnimes), - ); + redisClient.set( + `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, + JSON.stringify(relatedAnimes), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `getRelatedMAL_${hashStringMd5(`${mal_id}`)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return relatedAnimes; } @@ -394,20 +414,22 @@ export const animeFlvInfo = async (id: string | undefined) => { let episodes: any[] = []; try { - const resultQueryRedis: any = await redisClient.get( - `animeflvInfo_${hashStringMd5(id!)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `animeflvInfo_${hashStringMd5(id!)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_ANIMEFLV}/anime/${id}`, { - scrapy: true, - parse: false, - }); + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_ANIMEFLV}/anime/${id}`, { + scrapy: true, + parse: false, + }); } catch (err) { return err; } @@ -442,19 +464,21 @@ export const animeFlvInfo = async (id: string | undefined) => { } if (episodes.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `animeflvInfo_${hashStringMd5(id!)}`, - JSON.stringify(episodes), - ); + redisClient.set( + `animeflvInfo_${hashStringMd5(id!)}`, + JSON.stringify(episodes), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `animeflvInfo_${hashStringMd5(id!)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `animeflvInfo_${hashStringMd5(id!)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return episodes; } else { @@ -470,20 +494,22 @@ export const jkanimeInfo = async (id: string | undefined) => { let countEpisodes: string[] = []; try { - const resultQueryRedis: any = await redisClient.get( - `jkanimeInfo_${hashStringMd5(id!)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `jkanimeInfo_${hashStringMd5(id!)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_JKANIME}${id}`, { - scrapy: true, - parse: false, - }); + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_JKANIME}${id}`, { + scrapy: true, + parse: false, + }); } catch (err) { return err; } @@ -510,19 +536,21 @@ export const jkanimeInfo = async (id: string | undefined) => { } if (episodesList.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `jkanimeInfo_${hashStringMd5(id!)}`, - JSON.stringify(episodesList), - ); + redisClient.set( + `jkanimeInfo_${hashStringMd5(id!)}`, + JSON.stringify(episodesList), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `jkanimeInfo_${hashStringMd5(id!)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `jkanimeInfo_${hashStringMd5(id!)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return episodesList; } else { @@ -542,20 +570,22 @@ export const monoschinosInfo = async ( /* Extra info of the anime */ extraInfo = await animeExtraInfo(mal_id); - const resultQueryRedis: any = await redisClient.get( - `monoschinosInfo_${hashStringMd5(id!)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `monoschinosInfo_${hashStringMd5(id!)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); - console.log(extraInfo); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_MONOSCHINOS}anime/${id}`, { - scrapy: true, - parse: false, - }); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); + + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_MONOSCHINOS}anime/${id}`, { + scrapy: true, + parse: false, + }); } catch (err) { return err; } @@ -607,7 +637,7 @@ export const monoschinosInfo = async ( yyyy = broadCastDate.getFullYear(); episodeList.push({ - nextEpisodeDate: `${yyyy}/${mm}/${dd}/`, + nextEpisodeDate: `${yyyy}/${mm}/${dd}`, }); } } @@ -633,19 +663,21 @@ export const monoschinosInfo = async ( }); if (episodeList.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `monoschinosInfo_${hashStringMd5(id!)}`, - JSON.stringify(episodeList), - ); + redisClient.set( + `monoschinosInfo_${hashStringMd5(id!)}`, + JSON.stringify(episodeList), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `monoschinosInfo_${hashStringMd5(id!)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `monoschinosInfo_${hashStringMd5(id!)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return episodeList; } else { @@ -658,20 +690,22 @@ export const videoServersMonosChinos = async (id: string) => { let videoServers: any[] = []; try { - const resultQueryRedis: any = await redisClient.get( - `videoServersMonosChinos_${hashStringMd5(id)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `videoServersMonosChinos_${hashStringMd5(id)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_MONOSCHINOS}${id}`, { - scrapy: true, - parse: false, - }); + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_MONOSCHINOS}${id}`, { + scrapy: true, + parse: false, + }); } catch (err) { return err; } @@ -715,19 +749,21 @@ export const videoServersMonosChinos = async (id: string) => { }); if (videoServers.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `videoServersMonosChinos_${hashStringMd5(id)}`, - JSON.stringify(videoServers), - ); + redisClient.set( + `videoServersMonosChinos_${hashStringMd5(id)}`, + JSON.stringify(videoServers), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `videoServersMonosChinos_${hashStringMd5(id)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `videoServersMonosChinos_${hashStringMd5(id)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return videoServers; } else { @@ -741,20 +777,22 @@ export const videoServersJK = async (id: string) => { let script: string | null = ''; try { - const resultQueryRedis: any = await redisClient.get( - `videoServersJK_${hashStringMd5(id)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `videoServersJK_${hashStringMd5(id)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(`${urls.BASE_JKANIME}${id}`, { - scrapy: true, - parse: false, - }); + return resultRedis; + } } + + $ = await requestGot(`${urls.BASE_JKANIME}${id}`, { + scrapy: true, + parse: false, + }); } catch (err) { return err; } @@ -807,19 +845,21 @@ export const videoServersJK = async (id: string) => { serverList = serverList.filter(x => x.id !== 'xtreme s' && x.id !== 'desuka'); if (serverList.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `videoServersJK_${hashStringMd5(id!)}`, - JSON.stringify(serverList), - ); + redisClient.set( + `videoServersJK_${hashStringMd5(id!)}`, + JSON.stringify(serverList), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `videoServersJK_${hashStringMd5(id!)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `videoServersJK_${hashStringMd5(id!)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return serverList; } else { @@ -831,17 +871,19 @@ async function desuServerUrl(url: string) { let $: cheerio.Root; try { - const resultQueryRedis: any = await redisClient.get( - `desuServerUrl_${hashStringMd5(url)}`, - ); + if (redisClient.connected) { + const resultQueryRedis: any = await redisClient.get( + `desuServerUrl_${hashStringMd5(url)}`, + ); - if (resultQueryRedis) { - const resultRedis: any = JSON.parse(resultQueryRedis); + if (resultQueryRedis) { + const resultRedis: any = JSON.parse(resultQueryRedis); - return resultRedis; - } else { - $ = await requestGot(url, { scrapy: true, parse: false }); + return resultRedis; + } } + + $ = await requestGot(url, { scrapy: true, parse: false }); } catch (err) { return err; } @@ -864,19 +906,21 @@ async function desuServerUrl(url: string) { .split("'")[1]; if (result.length > 0) { - /* Set the key in the redis cache. */ + if (redisClient.connected) { + /* Set the key in the redis cache. */ - redisClient.set( - `desuServerUrl_${hashStringMd5(url)}`, - JSON.stringify(result), - ); + redisClient.set( + `desuServerUrl_${hashStringMd5(url)}`, + JSON.stringify(result), + ); - /* After 24hrs expire the key. */ + /* After 24hrs expire the key. */ - redisClient.expireat( - `desuServerUrl_${hashStringMd5(url)}`, - parseInt(`${+new Date() / 1000}`, 10) + 7200, - ); + redisClient.expireat( + `desuServerUrl_${hashStringMd5(url)}`, + parseInt(`${+new Date() / 1000}`, 10) + 7200, + ); + } return result; } else {