Output to file

This commit is contained in:
2022-02-16 00:17:19 +03:00
parent d47a4c634e
commit d5d5e5d59c
9 changed files with 82 additions and 15 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules node_modules
.vscode .vscode
index.txt

23
Output.js Normal file
View File

@@ -0,0 +1,23 @@
const fs = require('fs')
module.exports = class Output {
constructor () {
this.content = ''
}
addLine (text) {
this.content += text + '\n'
}
submit () {
const filename = 'index.txt'
try {
fs.unlinkSync(filename)
} catch (error) {
console.error(error)
console.log('File probably doesnt exist')
}
fs.writeFileSync(filename, this.content)
}
}

View File

@@ -1,6 +0,0 @@
module.exports = async function (page, url) {
await page.goto(url)
const element = await page.waitForSelector('b.price')
const value = await element.evaluate(el => el.textContent)
return Number(value)
}

View File

@@ -1,5 +1,7 @@
const puppeteer = require('puppeteer') const puppeteer = require('puppeteer')
const items = require('./items'); const items = require('./items')
const Output = require('./Output')
const output = new Output();
(async () => { (async () => {
const browser = await puppeteer.launch({ headless: false }) const browser = await puppeteer.launch({ headless: false })
@@ -13,9 +15,10 @@ const items = require('./items');
const setPrice = await item.set.getPrice(page) const setPrice = await item.set.getPrice(page)
if (partsPrice < setPrice) { if (partsPrice < setPrice) {
console.log(item.name, partsPrice, setPrice) output.addLine(`${item.name} prime: ${partsPrice}/${setPrice} выгода ${setPrice - partsPrice}`)
} }
} }
await browser.close() await browser.close()
output.submit()
})() })()

View File

@@ -9,7 +9,7 @@ module.exports = class Item {
this.addPart('blueprint') this.addPart('blueprint')
} }
addPart (part) { addPart (part, amount) {
this.parts.push(new Part(this.name, part)) this.parts.push(new Part(this.name, part, amount))
} }
} }

View File

@@ -1,7 +1,8 @@
module.exports = class Part { module.exports = class Part {
constructor (set, part) { constructor (set, part, amount) {
this.part = part this.part = part
this.url = `${set}_prime_${part}` this.url = `${set}_prime_${part}`
this.amount = amount ?? 1
} }
async getPrice (page) { async getPrice (page) {
@@ -10,6 +11,6 @@ module.exports = class Part {
await page.goto(marketUrl + this.url) await page.goto(marketUrl + this.url)
const element = await page.waitForSelector('b.price') const element = await page.waitForSelector('b.price')
const value = await element.evaluate(el => el.textContent) const value = await element.evaluate(el => el.textContent)
return Number(value) return Number(value) * this.amount
} }
} }

11
items/Primary.js Normal file
View File

@@ -0,0 +1,11 @@
const Item = require('./Item')
module.exports = class Primary extends Item {
constructor (name) {
super(name)
this.addPart('barrel')
this.addPart('receiver')
this.addPart('stock')
}
}

11
items/Secondary.js Normal file
View File

@@ -0,0 +1,11 @@
const Item = require('./Item')
module.exports = class Secondary extends Item {
constructor (name) {
super(name)
this.addPart('barrel', 2)
this.addPart('receiver', 2)
this.addPart('link')
}
}

View File

@@ -1,6 +1,29 @@
const Warframe = require('./Warframe') const Warframe = require('./Warframe')
const Primary = require('./Primary')
// const Secondary = require('./Secondary')
module.exports = [ const warframes = [
new Warframe('ash'), 'ash', 'atlas', 'banshee', 'chroma', 'ember',
new Warframe('atlas') 'equinox', 'frost', 'gara', 'harrow', 'hydroid',
'inaros', 'ivara', 'limbo', 'loki', 'mag',
'mesa', 'mirage', 'nekros', 'nezha', 'nidus',
'nova', 'nyx', 'oberon', 'octavia', 'rhino',
'saryn', 'titania', 'trinity', 'valkyr', 'vauban',
'volt', 'wukong', 'zephyr'
] ]
const primaries = [
'astilla', 'baza', 'boar', 'boltor', 'braton'
]
const items = []
warframes.forEach((name) => {
items.push(new Warframe(name))
})
primaries.forEach((name) => {
items.push(new Primary(name))
})
module.exports = items