@@ -3,51 +3,73 @@ const iconv = require('iconv-lite')
3
3
const path = require ( 'path' )
4
4
const SrcDir = path . join ( '../../' , './src/' )
5
5
const db = require ( SrcDir + 'db' )
6
-
7
- async function hitokoto ( ctx , next ) {
8
- // Connect Database
6
+ let Hitokoto
7
+ async function syncHitokotoList ( ) {
8
+ const result = { }
9
9
const hitokoto = await db . registerModel ( 'hitokoto' )
10
+ // Fetch All Data
11
+ result . all = await hitokoto . find ( {
12
+ attributes : {
13
+ exclude : [ 'from_who' , 'creator_uid' , 'assessor' , 'owner' ]
14
+ }
15
+ } )
16
+ // Generate Categroy List
17
+ result . categroy = { }
18
+ for ( let sentence of result . all ) {
19
+ if ( ! result . categroy [ sentence . type ] ) {
20
+ // Init Categroy List
21
+ result . categroy [ sentence . type ] = [ ]
22
+ }
23
+ result . categroy [ sentence . type ] . push ( sentence )
24
+ }
25
+ // fill TS
26
+ result . lastUpdate = Date . now ( )
27
+ // Update Data
28
+ Hitokoto = result
29
+ }
30
+ async function hitokoto ( ctx , next ) {
31
+ // judge whether data is exist
32
+ if ( ! Hitokoto ) {
33
+ // Sync Data
34
+ await syncHitokotoList ( )
35
+ } else if ( ( Date . now ( ) - Hitokoto . lastUpdate ) > 1000 * 60 * 60 * 2 ) {
36
+ // Data is outdate. async update.
37
+ syncHitokotoList ( )
38
+ }
10
39
if ( ctx . query && ctx . query . c ) {
11
40
// exist params c
12
- const ret = await hitokoto . findOne ( {
13
- where : {
14
- type : ctx . query . c
15
- } ,
16
- attributes : {
17
- exclude : [ 'from_who' , 'creator_uid' , 'assessor' , 'owner' ]
18
- } ,
19
- order : db . sequelize . random ( )
20
- } )
21
- if ( ! ret ) {
41
+ if ( ! Hitokoto . categroy [ ctx . query . c ] ) {
22
42
ctx . status = 404
23
43
ctx . body = {
24
44
status : 404 ,
25
45
message : '很抱歉,该分类下尚无条目'
26
46
}
27
47
return
28
48
}
49
+ // Random Sentence
50
+ const sentence = Hitokoto . categroy [ ctx . query . c ] [ Math . floor ( Math . random ( ) * Hitokoto . categroy [ ctx . query . c ] . length ) ]
29
51
// CheckEncoding
30
52
const encode = ctx . query . encode
31
53
const gbk = ( ctx . query && ctx . query . charset && ctx . query . charset . toLocaleLowerCase ( ) === 'gbk' ) ? ! ! 'gbk' : false
32
54
switch ( encode ) {
33
55
case 'json' :
34
56
if ( gbk ) {
35
57
ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
36
- ctx . body = iconv . encode ( JSON . stringify ( ret ) , 'GBK' )
58
+ ctx . body = iconv . encode ( JSON . stringify ( sentence ) , 'GBK' )
37
59
} else {
38
- ctx . body = ret
60
+ ctx . body = sentence
39
61
}
40
62
break
41
63
case 'text' :
42
64
if ( gbk ) {
43
65
ctx . set ( 'Content-Type' , 'text/plain; charset=gbk' )
44
- ctx . body = iconv . encode ( ret . hitokoto , 'GBK' )
66
+ ctx . body = iconv . encode ( sentence . hitokoto , 'GBK' )
45
67
}
46
- ctx . body = ret . hitokoto
68
+ ctx . body = sentence . hitokoto
47
69
break
48
70
case 'js' :
49
71
const select = ctx . query . select ? ctx . query . select : '.hitokoto'
50
- const response = `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
72
+ const response = `(function hitokoto(){var hitokoto="${ sentence . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
51
73
if ( gbk ) {
52
74
ctx . set ( 'Content-Type' , 'text/javascript; charset=gbk' )
53
75
ctx . body = iconv . encode ( response , 'GBK' )
@@ -59,43 +81,38 @@ async function hitokoto (ctx, next) {
59
81
default :
60
82
if ( gbk ) {
61
83
ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
62
- ctx . body = iconv . encode ( JSON . stringify ( ret ) , 'GBK' )
84
+ ctx . body = iconv . encode ( JSON . stringify ( sentence ) , 'GBK' )
63
85
} else {
64
- ctx . body = ret
86
+ ctx . body = sentence
65
87
}
66
88
break
67
89
}
68
90
} else {
69
91
// Not Params or just has callback
70
- const ret = await hitokoto . findOne ( {
71
- attributes : {
72
- exclude : [ 'from_who' , 'creator_uid' , 'assessor' , 'owner' ]
73
- } ,
74
- order : db . sequelize . random ( )
75
- } )
76
-
92
+ // Random Sentence
93
+ const sentence = Hitokoto . all [ Math . floor ( Math . random ( ) * Hitokoto . all . length ) ]
77
94
// CheckEncoding
78
95
const encode = ctx . query . encode
79
96
const gbk = ( ctx . query && ctx . query . charset && ctx . query . charset . toLocaleLowerCase ( ) === 'gbk' ) ? ! ! 'gbk' : false
80
97
switch ( encode ) {
81
98
case 'json' :
82
99
if ( gbk ) {
83
100
ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
84
- ctx . body = iconv . encode ( JSON . stringify ( ret ) , 'GBK' )
101
+ ctx . body = iconv . encode ( JSON . stringify ( sentence ) , 'GBK' )
85
102
} else {
86
- ctx . body = ret
103
+ ctx . body = sentence
87
104
}
88
105
break
89
106
case 'text' :
90
107
if ( gbk ) {
91
108
ctx . set ( 'Content-Type' , 'text/plain; charset=gbk' )
92
- ctx . body = iconv . encode ( ret . hitokoto , 'GBK' )
109
+ ctx . body = iconv . encode ( sentence . hitokoto , 'GBK' )
93
110
}
94
- ctx . body = ret . hitokoto
111
+ ctx . body = sentence . hitokoto
95
112
break
96
113
case 'js' :
97
114
const select = ctx . query . select ? ctx . query . select : '.hitokoto'
98
- const response = `(function hitokoto(){var hitokoto="${ ret . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
115
+ const response = `(function hitokoto(){var hitokoto="${ sentence . hitokoto } ";var dom=document.querySelector('${ select } ');Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;})()`
99
116
if ( gbk ) {
100
117
ctx . set ( 'Content-Type' , 'text/javascript; charset=gbk' )
101
118
ctx . body = iconv . encode ( response , 'GBK' )
@@ -107,9 +124,9 @@ async function hitokoto (ctx, next) {
107
124
default :
108
125
if ( gbk ) {
109
126
ctx . set ( 'Content-Type' , 'application/json; charset=gbk' )
110
- ctx . body = iconv . encode ( JSON . stringify ( ret ) , 'GBK' )
127
+ ctx . body = iconv . encode ( JSON . stringify ( sentence ) , 'GBK' )
111
128
} else {
112
- ctx . body = ret
129
+ ctx . body = sentence
113
130
}
114
131
break
115
132
}
0 commit comments