Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 845ec87

Browse files
committed
Temporal patch
1 parent 318c34a commit 845ec87

File tree

10 files changed

+60
-29
lines changed

10 files changed

+60
-29
lines changed

index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ const router = new Router()
1414

1515
router.all('/', async ctx => ctx.redirect('https://ohys.seia.io'))
1616

17-
utils.database.autofill()
18-
utils.routing.autofill(router, functions)
19-
utils.ohys.automate()
17+
const initFn = async () => {
18+
await utils.database.autofill()
19+
await utils.routing.autofill(router, functions)
20+
utils.ohys.automate()
2021

21-
app
22-
.use(cors(config.app.cors))
23-
.use(router.routes())
24-
.use(router.allowedMethods())
25-
.listen(config.app.port, () => log(`${pkg.name}@v${pkg.version} is listening at port ${config.app.port}.`))
22+
app
23+
.use(cors(config.app.cors))
24+
.use(router.routes())
25+
.use(router.allowedMethods())
26+
.listen(config.app.port, () => log(`${pkg.name}@v${pkg.version} is listening at port ${config.app.port}.`))
27+
}
28+
29+
initFn()

utils/crypto/hash/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.md5 = require('./md5')

utils/crypto/hash/md5.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const crypto = require('crypto')
2+
3+
module.exports = data => crypto.createHash('md5').update(data).digest('hex')

utils/crypto/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.hash = require('./hash')

utils/database/schemas/animes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = async lib => {
22
await lib.schema.createTable('animes', table => {
3-
table.increments() // NOTE: id int unsigned not null auto_increment primary key
3+
table.increments().primary()
4+
5+
table.string('hash', 32).notNullable()
46

57
table.integer('episode').notNullable()
68
table.string('series', 256).notNullable()

utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
module.exports.crypto = require('./crypto')
12
module.exports.database = require('./database')
23
module.exports.ohys = require('./ohys')
34
module.exports.routing = require('./routing')

utils/ohys/automate.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
const config = require('../../config')
22
const log = require('../../log')
3-
const database = require('../database')
4-
const data = require('./data')
5-
const fetch = require('./fetch')
3+
const database = require('./database')
64

75
module.exports = async () => {
8-
if (!config.ohys.passUpdateAtStart) {
9-
const latestFeed = await fetch.allList()
10-
const existingFeed = await database.knex('animes').select('*')
11-
12-
if (latestFeed.length > existingFeed.length) {
13-
await data.insert(latestFeed.slice(0, latestFeed.length - existingFeed.length))
14-
}
15-
}
6+
await database.update(true)
167

178
setInterval(async () => {
18-
log(`updating database: ${Date.now()}`)
19-
20-
const latestFeedUpdate = await fetch.list()
21-
const existingFeedUpdate = await database.knex('animes').select('*')
22-
23-
const newItems = await latestFeedUpdate.serialized.filter(item => {
24-
return !existingFeedUpdate.find(eItem => eItem.original === item.name)
25-
})
9+
log('updating database at ' + Date.now())
2610

27-
await data.insert(newItems)
11+
await database.update()
2812
}, config.ohys.refreshRate)
2913
}

utils/ohys/data/insert.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const log = require('../../../log')
2+
const crypto = require('../../crypto')
23
const database = require('../../database')
34
const patterns = require('../patterns')
45

@@ -16,6 +17,7 @@ module.exports = async items => {
1617
await database.knex('animes')
1718
.insert({
1819
id: null,
20+
hash: await crypto.hash.md5(await JSON.stringify(items[i])),
1921
episode: Number(episode),
2022
series,
2123
link: items[i].url,

utils/ohys/database/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.update = require('./update')

utils/ohys/database/update.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const crypto = require('../../crypto')
2+
const database = require('../../database')
3+
const data = require('../data')
4+
const fetch = require('../fetch')
5+
// const scopes = require('../scopes')
6+
7+
const update = async freshly => {
8+
const existingSources = await database.knex('animes')
9+
.select('*')
10+
11+
let latestFeed = await fetch.list()
12+
13+
if (freshly) {
14+
latestFeed = {
15+
serialized: await fetch.allList()
16+
}
17+
}
18+
19+
for (let k = 0; k < latestFeed.serialized.length; k++) {
20+
const item = latestFeed.serialized[k]
21+
const itemStringified = await JSON.stringify(item)
22+
const itemHash = await crypto.hash.md5(itemStringified)
23+
24+
const existingItem = await existingSources.find(item => item.hash === itemHash)
25+
26+
if (!existingItem) {
27+
await data.insert([item])
28+
}
29+
}
30+
}
31+
32+
module.exports = update

0 commit comments

Comments
 (0)