-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (84 loc) · 2.53 KB
/
Copy pathindex.js
File metadata and controls
92 lines (84 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const config = require('./config/config')
const Xray = require('x-ray')
const winston = require('winston')
const magnetLink = require('magnet-link')
const redis = require('redis')
function getMagnetLink (item) {
return new Promise((resolve, reject) => {
magnetLink(item.torrentUri, (err, link) => {
if (err) {
winston.log('error', 'couldn\'t get magnet link')
reject(err)
}
resolve(link)
})
})
}
function mergeMagnetArrayToResults (magnetArray, results) {
let lResults = results
magnetArray.forEach((magnetUri, index) => {
lResults[index].magnetUri = magnetUri
})
return lResults
}
function getOCReMixData () {
return new Promise((resolve, reject) => {
const xray = new Xray()
xray('http://bt.ocremix.org/index.php?order=date&sort=descending', '.trkInner tr:not(:first-child)',
[{
name: '.colName a',
torrentUri: '.colName a@href',
size: '.colSize',
info: '.colName .torrentTag:last-child a@href',
added: '.colAdded',
category: '.colCategory'
}])((err, results) => {
if (err) {
winston.error('no results from ocremix', err)
}
results = results
.filter((item) => item.torrentUri.length !== 0)
let promises = results
.map(getMagnetLink)
Promise.all(promises)
.then((magnets) => {
const resJSON = mergeMagnetArrayToResults(magnets, results)
const resString = JSON.stringify(resJSON, null, '\t')
resolve(resString)
})
.catch((err) => {
reject(err)
})
})
})
}
function getCache (redisClient, key) {
return new Promise((resolve, reject) => {
redisClient.get(key, (err, reply) => {
if (err) {
reject(err)
} else {
resolve(reply)
}
})
})
}
async function readOCReMix (environmentName) {
let configuration = config.loadConfiguration(environmentName || 'development')
let payload = await getOCReMixData()
let redisClient = redis.createClient(configuration.redis.PORT, configuration.redis.HOSTNAME)
redisClient.auth(configuration.redis.PASSWORD, (err) => {
if (err) {
throw err
}
})
let cachedData = await getCache(redisClient, configuration.redis.KEY)
if (cachedData === null || cachedData !== payload) {
winston.log('info', 'New OCReMix Payload')
redisClient.set(configuration.redis.KEY, payload)
} else {
winston.log('warning', 'No New OCReMix Payload')
}
redisClient.quit()
}
readOCReMix()