Skip to content

Commit

Permalink
restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
guymelef committed Oct 13, 2023
1 parent 562bed0 commit 5074f3c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'

import { MONGODB_URI } from './utils/config.js'
import { fetchAllData } from './utils/update_checker.js'
import { checkRequestKeyHeader } from './utils/middleware.js'
import { MONGODB_URI } from './config/config.js'
import { fetchAllData } from './utils/update.js'
import { checkRequestKeyHeader } from './middleware/middleware.js'
import { indexRouter } from './controllers/index.js'
import { updateRouter } from './controllers/update.js'
import { searchRouter } from './controllers/search.js'
Expand All @@ -14,7 +14,7 @@ import { searchRouter } from './controllers/search.js'
mongoose
.connect(MONGODB_URI)
.then(async () => {
console.log('Ⓜ️ Connected to MongoDB!')
console.log('Ⓜ️ Connected to MongoDB!')
await fetchAllData()
})
.catch(err => console.log('🟥 MONGODB ERROR:', err.message))
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Router } from "express"
import { Redis } from "ioredis"

import { REDIS_URI } from "../utils/config.js"
import { getCardInfo } from "../utils/cardinfo_creator.js"
import { fetchFromYugipedia } from "../utils/card_creator.js"
import { saveToDatabase } from "../utils/database_updater.js"
import { REDIS_URI } from "../config/config.js"
import { getCardInfo } from "../utils/cardinfo.js"
import { fetchFromYugipedia } from "../utils/yugipedia.js"
import { saveToDatabase } from "../utils/database.js"



Expand Down Expand Up @@ -49,6 +49,6 @@ searchRouter.post('/', async (req, res) => {
})
}

console.log("RESULT:", `[ ${card.length} ] card found`)
console.log("🔎 RESULT:", `[ ${card.length} ] card found`)
res.json({ match: card.length === 1, card: cardToSend })
})
14 changes: 7 additions & 7 deletions controllers/update.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Router } from "express"

import { fetchFromYugipedia } from "../utils/card_creator.js"
import { saveToDatabase } from "../utils/database_updater.js"
import { checkYgoprodeck, checkYugipedia } from "../utils/update_checker.js"
import { YUGIPEDIA_PAGE, BOT_RD_URL, botRefreshDataRequestOption } from "../utils/config.js"
import { fetchFromYugipedia } from "../utils/yugipedia.js"
import { saveToDatabase } from "../utils/database.js"
import { checkYgoprodeck, checkYugipedia } from "../utils/update.js"
import { YUGIPEDIA_PAGE, BOT_RD_URL, botRefreshDataRequestOption } from "../config/config.js"



Expand All @@ -24,13 +24,13 @@ updateRouter.get('/:src', (req, res) => {
let newCards = []
if (source === 'ygoprodeck') newCards = await fetchFromYugipedia(null, cards, null)
else newCards = await fetchFromYugipedia(cards, null, null)
console.log(` 📢 [${newCards.length}] NEW CARD(S) FOUND!\n${updateMark.repeat(updateMarkLength)}\n`)
console.log(`📢 [${newCards.length}] NEW CARD(S) FOUND!\n${updateMark.repeat(updateMarkLength)}\n`)

await saveToDatabase(newCards)
fetch(BOT_RD_URL, botRefreshDataRequestOption)
.then(res => res.json())
.then(json => console.log("🤖 BOT RESPONSE:", json))
.catch(err => console.log(" BOT DATA REFRESH ERROR:", err))
.catch(err => console.log("🟥 BOT DATA REFRESH ERROR:", err))

res.json({
source,
Expand All @@ -42,7 +42,7 @@ updateRouter.get('/:src', (req, res) => {
timestamp: new Date().toLocaleString('en-ph')
})
} else {
console.log(` 💯 CARD DB IS UP TO DATE!\n${updateMark.repeat(updateMarkLength)}\n`)
console.log(`💯 CARD DB IS UP TO DATE!\n${updateMark.repeat(updateMarkLength)}\n`)
res.json({
source,
message: "check finished, no new card(s) found",
Expand Down
2 changes: 1 addition & 1 deletion utils/middleware.js → middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SECRET_KEY } from "./config.js"
import { SECRET_KEY } from "../config/config.js"


export const checkRequestKeyHeader = (req, res, next) => {
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from "http"

import { app } from "./app.js"
import { HOST, PORT } from "./utils/config.js"
import { HOST, PORT } from "./config/config.js"



Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions utils/database_updater.js → utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const saveToDatabase = async (cards) => {
await models[category].findOneAndReplace({ name: card.name }, card)
console.log("♻️ CARD REPLACED IN DATABASE!")
} else {
console.log("🔴 MONGODB SAVE ERROR:", error.message)
console.log("🔷", error.stack)
console.log("🟥 MONGODB SAVE ERROR:", error.message)
console.log("🔷 STACK:", error.stack)
throw new Error(error.message)
}
}
Expand Down
24 changes: 12 additions & 12 deletions utils/update_checker.js → utils/update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requestOptions, YGOPD_API, YUGIPEDIA_RC } from './config.js'
import { requestOptions, YGOPD_API, YUGIPEDIA_RC } from '../config/config.js'
import { OcgCard, RushCard, StrayCard, YgopdCard } from '../models/card.js'
import BotVariable from '../models/variable.js'

Expand All @@ -15,20 +15,20 @@ export const fetchAllData = async () => {
MAIN_CARDS = await OcgCard.find({}, 'name -_id').lean().exec()
RUSH_CARDS = await RushCard.find({}, 'name -_id').lean().exec()
STRAY_CARDS = await StrayCard.find({}, 'name -_id').lean().exec()
console.log("🗃️ OCG/TCG CARDS:", MAIN_CARDS.length.toLocaleString('en-ph'))
console.log("🗃️ RUSH CARDS:", RUSH_CARDS.length.toLocaleString('en-ph'))
console.log("🗃️ STRAY CARDS:", STRAY_CARDS.length.toLocaleString('en-ph'))
console.log("🗃️ OCG/TCG CARDS:", MAIN_CARDS.length.toLocaleString('en-ph'))
console.log("🗃️ RUSH CARDS:", RUSH_CARDS.length.toLocaleString('en-ph'))
console.log("🗃️ STRAY CARDS:", STRAY_CARDS.length.toLocaleString('en-ph'))

const ygoprodeck = await BotVariable.findOne({ name: 'YGOPRODeck' })
YGOPDCOUNT = ygoprodeck.card_count
console.log(`🟩 YGOPRODECK CARD COUNT (${ygoprodeck.last_update}): 💠 ${YGOPDCOUNT.toLocaleString('en-ph')}`)
console.log(`⏺️ YGOPRODECK CARD COUNT (${ygoprodeck.last_update}): 💠 ${YGOPDCOUNT.toLocaleString('en-ph')}`)

const yugipedia = await BotVariable.findOne({ name: 'Yugipedia' })
YUGIPEDIA_LAST_UPDATE = yugipedia.lastUpdate
console.log(`🟩 YUGIPEDIA LAST UPDATE: 💠 ${new Date(YUGIPEDIA_LAST_UPDATE).toLocaleString('en-ph')}`)
console.log(`⏺️ YUGIPEDIA LAST UPDATE: 💠 ${new Date(YUGIPEDIA_LAST_UPDATE).toLocaleString('en-ph')}`)
} catch (err) {
console.log("🔴 CARDS FETCH ERROR:", err.message)
console.log("🔷", err.stack)
console.log("🟥 CARDS FETCH ERROR:", err.message)
console.log("🔷 STACK:", err.stack)
}
}

Expand All @@ -39,8 +39,8 @@ export const checkYgoprodeck = async () => {
ygopdCards = await ygopdCards.json()
ygopdCards = ygopdCards.data

console.log(" MAIN CARDS COUNT: 🔸", MAIN_CARDS.length.toLocaleString('en-ph'))
console.log(" YGOPRODECK COUNT: 🔸", ygopdCards.length.toLocaleString('en-ph'))
console.log("MAIN CARDS COUNT: 🔸", MAIN_CARDS.length.toLocaleString('en-ph'))
console.log("YGOPRODECK COUNT: 🔸", ygopdCards.length.toLocaleString('en-ph'))

if (YGOPDCOUNT === ygopdCards.length) return newCards
else YGOPDCOUNT = ygopdCards.length
Expand Down Expand Up @@ -70,8 +70,8 @@ export const checkYugipedia = async () => {
let rc = await recentChanges.json()
rc = rc.query.recentchanges

console.log(' LAST YUGIPEDIA CARD CREATED: 🕒', new Date(YUGIPEDIA_LAST_UPDATE).toLocaleString('en-ph'))
console.log(' MOST RECENT CHANGE (NEW): 🕒', new Date(rc[0].timestamp).toLocaleString('en-ph'))
console.log('LAST YUGIPEDIA CARD CREATED: 🕒', new Date(YUGIPEDIA_LAST_UPDATE).toLocaleString('en-ph'))
console.log('MOST RECENT CHANGE (NEW): 🕒', new Date(rc[0].timestamp).toLocaleString('en-ph'))

let newCardPages = rc.filter(item => {
const comment = item.comment.toLowerCase()
Expand Down
6 changes: 3 additions & 3 deletions utils/card_creator.js → utils/yugipedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { requestOptions,
YUGIPEDIA_PAGETITLE,
YUGIPEDIA_IMG,
YUGIPEDIA_SEARCH
} from "./config.js"
} from "../config/config.js"



Expand All @@ -13,7 +13,7 @@ export const fetchFromYugipedia = async (cardPageIds, cardPageTitles, cardName)
const CARDS = []

try {
console.log(` 📖 SEARCHING YUGIPEDIA... 【${cardPageIds || cardPageTitles || cardName}】`)
console.log(`📖 SEARCHING YUGIPEDIA... 【${cardPageIds || cardPageTitles || cardName}】`)
let wikiContent

if (cardPageIds) {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const fetchFromYugipedia = async (cardPageIds, cardPageTitles, cardName)

return CARDS
} catch (err) {
console.log(`🔴 YUGIPEDIA LOOKUP ERROR:`, err.message)
console.log(`🟥 YUGIPEDIA LOOKUP ERROR:`, err.message)
console.log("🔷 STACK:", err.stack)
return CARDS
}
Expand Down

0 comments on commit 5074f3c

Please sign in to comment.