This repository was archived by the owner on Dec 25, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 10 files changed +60
-29
lines changed Expand file tree Collapse file tree 10 files changed +60
-29
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,16 @@ const router = new Router()
14
14
15
15
router . all ( '/' , async ctx => ctx . redirect ( 'https://ohys.seia.io' ) )
16
16
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 ( )
20
21
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 ( )
Original file line number Diff line number Diff line change
1
+ module . exports . md5 = require ( './md5' )
Original file line number Diff line number Diff line change
1
+ const crypto = require ( 'crypto' )
2
+
3
+ module . exports = data => crypto . createHash ( 'md5' ) . update ( data ) . digest ( 'hex' )
Original file line number Diff line number Diff line change
1
+ module . exports . hash = require ( './hash' )
Original file line number Diff line number Diff line change 1
1
module . exports = async lib => {
2
2
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 ( )
4
6
5
7
table . integer ( 'episode' ) . notNullable ( )
6
8
table . string ( 'series' , 256 ) . notNullable ( )
Original file line number Diff line number Diff line change
1
+ module . exports . crypto = require ( './crypto' )
1
2
module . exports . database = require ( './database' )
2
3
module . exports . ohys = require ( './ohys' )
3
4
module . exports . routing = require ( './routing' )
Original file line number Diff line number Diff line change 1
1
const config = require ( '../../config' )
2
2
const log = require ( '../../log' )
3
- const database = require ( '../database' )
4
- const data = require ( './data' )
5
- const fetch = require ( './fetch' )
3
+ const database = require ( './database' )
6
4
7
5
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 )
16
7
17
8
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 ( ) )
26
10
27
- await data . insert ( newItems )
11
+ await database . update ( )
28
12
} , config . ohys . refreshRate )
29
13
}
Original file line number Diff line number Diff line change 1
1
const log = require ( '../../../log' )
2
+ const crypto = require ( '../../crypto' )
2
3
const database = require ( '../../database' )
3
4
const patterns = require ( '../patterns' )
4
5
@@ -16,6 +17,7 @@ module.exports = async items => {
16
17
await database . knex ( 'animes' )
17
18
. insert ( {
18
19
id : null ,
20
+ hash : await crypto . hash . md5 ( await JSON . stringify ( items [ i ] ) ) ,
19
21
episode : Number ( episode ) ,
20
22
series,
21
23
link : items [ i ] . url ,
Original file line number Diff line number Diff line change
1
+ module . exports . update = require ( './update' )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments