UNPKG

3.27 kBJavaScriptView Raw
1const _ = require('underscore')
2const cfg = require('../lib/config')
3const g = require('got')
4const log = console.log
5
6let api = {
7 $url: 'https://www.tukui.org/api.php',
8 $web: 'https://www.tukui.org/download.php',
9
10 $lcl: /\?id=(.*)$/,
11 $fcl: 'tukui',
12 $scl: 'tukui.com',
13
14 info(ad, done) {
15 let id = ad.key.split('-')[0]
16 let mo = cfg.getMode()
17
18 if (mo === '_retail_' && ad.key.match(/0-tukui|0-elvui/i)) {
19 g(`${api.$web}?ui=${ad.key.match(/0-tukui/i) ? 'tukui' : 'elvui'}`)
20 .then(res => {
21 let i = {
22 name: ad.key,
23 author: 'tukui.org',
24 download: 1000000,
25 version: [{}]
26 }
27
28 res.body
29 .replace(/\r/g, '')
30 .split('\n')
31 .forEach(line => {
32 if (line.match(/current version/i)) {
33 let d = line.replace(/</g, '>').split('>')
34 i.update = new Date(d[6]) / 1000
35 i.version[0].name = d[2]
36 }
37
38 if (line.match(/btn-mod/i)) {
39 i.version[0].link = 'https://www.tukui.org' + line.split('"')[1]
40 }
41 })
42
43 done(i)
44 })
45 .catch(x => done())
46 return
47 }
48
49 // log('getting', `${api.$url}/filedetails/${id}.json`)
50 g(`${api.$url}?${mo === '_retail_' ? 'addon' : 'classic-addon'}=${id}`)
51 .then(res => {
52 let x = JSON.parse(res.body)
53
54 ad.key = id + '-' + x.name.replace(/[^a-zA-Z0-9]/g, '')
55 done({
56 name: x.name,
57 author: x.author,
58 update: new Date(x.lastupdate).valueOf() / 1000,
59 download: x.downloads,
60 version: [
61 {
62 name: x.version,
63 game: x.patch,
64 link: x.url
65 }
66 ]
67 })
68 })
69 .catch(x => done())
70 },
71
72 search(ad, done) {
73 let mo = cfg.getMode()
74
75 g(
76 `${api.$url}?${mo === '_retail_' ? 'addons' : 'classic-addons'}=all`
77 ).then(res => {
78 res = JSON.parse(res.body)
79
80 // log(res)
81 // retail version will not show in api results
82 if (mo === '_retail_')
83 res = res.concat([
84 {
85 id: 0,
86 name: 'TukUI',
87 small_desc: 'TukUI',
88 downloads: 1000000,
89 lastupdate: new Date().valueOf(),
90 web_url: api.$web + '?ui=tukui'
91 },
92 {
93 id: 0,
94 name: 'ElvUI',
95 small_desc: 'ElvUI',
96 downloads: 1000000,
97 lastupdate: new Date().valueOf(),
98 web_url: api.$web + '?ui=elvui'
99 }
100 ])
101
102 res = _.filter(
103 res,
104 d =>
105 d.name.toLowerCase().search(ad.key.toLowerCase()) >= 0 ||
106 d.small_desc.toLowerCase().search(ad.key.toLowerCase()) >= 0
107 )
108
109 res.sort((a, b) => b.downloads - a.downloads)
110
111 res = res.slice(0, 15)
112
113 // log(res)
114 done(
115 res.map(x => {
116 return {
117 name: x.name,
118 key: x.id + '-' + x.name.replace(/[^a-zA-Z0-9]/g, ''),
119 download: parseInt(x.downloads),
120 update: new Date(x.lastupdate).valueOf() / 1000,
121 page: x.web_url
122 }
123 })
124 )
125 })
126 }
127}
128
129module.exports = api