Skip to content

Commit

Permalink
js v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoxia committed Jun 24, 2019
1 parent 3338b31 commit 160b304
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 68 deletions.
40 changes: 26 additions & 14 deletions spider/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ router.get('/apis/related', async (ctx) => {
ctx.body = []
return
}
const sql = 'SELECT id FROM hash WHERE MATCH(?) LIMIT 0,? OPTION max_matches=1000, max_query_time=50'

const results = await ctx.mdb.query(sql, [words, 1*(query.count||10)])
const ids = results.map((x) => x.id)
const items = await ctx.torrentdb.collection('hash').find({_id: {$in: ids}}).toArray()
for(const x of items){
x.id = x._id
delete x._id
}
ctx.body = {
code: 0,
items: items
try{
const sql = 'SELECT id FROM hash WHERE MATCH(?) LIMIT 0,? OPTION max_matches=1000, max_query_time=50'

const results = await ctx.mdb.query(sql, [words, 1*(query.count||10)])
const ids = results.map((x) => x.id)
const items = await ctx.torrentdb.collection('hash').find({_id: {$in: ids}}).toArray()
for(const x of items){
x.id = x._id
delete x._id
}
ctx.body = {
code: 0,
items: items
}
}catch(e){
console.error(new Date(), e)
return []
}
})

Expand Down Expand Up @@ -111,7 +116,14 @@ router.get('/apis/search', async (ctx) => {
})

async function fetchItems(ctx, ids) {
const items = await ctx.torrentdb.collection('hash').find({_id: {$in: ids}}).toArray()
if(ids.length == 0)
return []
let items = null
if(ids[0].toString().length == 40) {
items = await ctx.torrentdb.collection('hash').find({hash: {$in: ids}}).toArray()
}else{
items = await ctx.torrentdb.collection('hash').find({_id: {$in: ids.map((x)=>parseInt(x))}}).toArray()
}
for(const x of items){
x.id = x._id
delete x._id
Expand All @@ -130,7 +142,7 @@ async function fetchItems(ctx, ids) {

router.get('/apis/info', async (ctx) => {
ctx.assert(ctx.query.ids, 400)
const ids = ctx.query.ids.split('-').map((x) => parseInt(x))
const ids = ctx.query.ids.split('-')
ctx.body = {
items: await fetchItems(ctx, ids),
code: 0
Expand Down
2 changes: 1 addition & 1 deletion web/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
]
},
env: {
baseUrl: process.env.BASE_URL || 'https://www.shousibaocai.com'
},
/*
** Customize the progress-bar color
Expand Down Expand Up @@ -48,6 +47,7 @@ module.exports = {
** See https://axios.nuxtjs.org/options
*/
axios: {
baseURL: process.env.BASE_URL || 'https://www.shousibaocai.net'
},
/*
** Build configuration
Expand Down
32 changes: 17 additions & 15 deletions web/pages/h/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,27 @@
</template>

<script type="text/javascript">
import axios from '@/plugins/axios'
export default {
layout: 'search',
async asyncData({params}) {
const res = await axios.get('/apis/info', {params: {ids: params.id}})
const data = {
item: res.data.items[0],
activeNames: ['1', '2', '3', '4', '5', '6']
}
const res2 = await axios.get('https://www.shousibaocai.net/apis/related', {params: {keyword: data.item.name, count: 11}})
data.related = res2.data.items.filter((x) => x.id != params.id)
async asyncData({params, $axios}) {
try{
const res = await $axios.$get('/apis/info', {params: {ids: params.id}})
const data = {
item: res.items[0],
activeNames: ['1', '2', '3', '4', '5', '6']
}
const res2 = await $axios.$get('/apis/related', {params: {keyword: data.item.name, count: 11}})
data.related = res2.items.filter((x) => x.id != params.id)
if(!data.item.files) {
data.item.files = [{path: data.item.name, length: data.item.len}]
}
data.magnetLink = 'magnet:?xt=urn:btih:' + data.item.hash + '&dn=' + data.item.name
return data
if(!data.item.files) {
data.item.files = [{path: data.item.name, length: data.item.len}]
}
data.magnetLink = 'magnet:?xt=urn:btih:' + data.item.hash + '&dn=' + data.item.name
return data
}catch(e) {
console.error(new Date(), params, e)
}
},
methods: {
Expand Down
68 changes: 35 additions & 33 deletions web/pages/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,44 @@


<script type="text/javascript">
import axios from '@/plugins/axios'
export default {
layout: 'search',
async asyncData({query}) {
const page = parseInt(query.p || 1)
const params = {
keyword: query.q,
detail: 1,
start: (page - 1) * 10,
count: 10
}
const res = await axios.get('/apis/search', {params: params})
const data = {
items: res.data.items,
meta: res.data.meta,
keyword: query.q,
currentPage: page,
words: query.q.replace(/||,|||!|||<|>|\"|'|:|||\?||\||||||||||·|\(|\)| |\.|||||@|&|%|\^|\*|\+|\||<|>|~|`|\[|\]/g, ' ').split(' ').filter((x) => x!='')
}
data.items.forEach((v) => {
if(!v.files) {
v.files = [{path: v.name, length: v.len}]
}
v.files.sort((a, b) => b.length - a.length)
v.files = v.files.slice(0, 5)
v.files.forEach((s) => {
for(const w of data.words) {
s.path = s.path.replace(new RegExp(w, 'ig'), (p1) => {
return '<span class="highlight">' + p1 + '</span>'
})
}
})
})
return data
async asyncData({query, $axios}) {
try{
const page = parseInt(query.p || 1)
const params = {
keyword: query.q,
detail: 1,
start: (page - 1) * 10,
count: 10
}
const res = await $axios.$get('/apis/search', {params: params})
const data = {
items: res.items,
meta: res.meta,
keyword: query.q,
currentPage: page,
words: query.q.replace(/||,|||!|||<|>|\"|'|:|||\?||\||||||||||·|\(|\)| |\.|||||@|&|%|\^|\*|\+|\||<|>|~|`|\[|\]/g, ' ').split(' ').filter((x) => x!='')
}
data.items.forEach((v) => {
if(!v.files) {
v.files = [{path: v.name, length: v.len}]
}
v.files.sort((a, b) => b.length - a.length)
v.files = v.files.slice(0, 5)
v.files.forEach((s) => {
for(const w of data.words) {
s.path = s.path.replace(new RegExp(w, 'ig'), (p1) => {
return '<span class="highlight">' + p1 + '</span>'
})
}
})
})
return data
}catch(e){
console.error(new Date(), query, e)
}
},
methods: {
Expand Down
5 changes: 0 additions & 5 deletions web/plugins/axios.js

This file was deleted.

0 comments on commit 160b304

Please sign in to comment.