From d5d5e5d59c1fdcf7e3568df57052d3d450a30442 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Wed, 16 Feb 2022 00:17:19 +0300 Subject: [PATCH] Output to file --- .gitignore | 1 + Output.js | 23 +++++++++++++++++++++++ getPrice.js | 6 ------ index.js | 7 +++++-- items/Item.js | 4 ++-- items/Part.js | 5 +++-- items/Primary.js | 11 +++++++++++ items/Secondary.js | 11 +++++++++++ items/index.js | 29 ++++++++++++++++++++++++++--- 9 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 Output.js delete mode 100644 getPrice.js create mode 100644 items/Primary.js create mode 100644 items/Secondary.js diff --git a/.gitignore b/.gitignore index 76efb07..e971d13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules .vscode +index.txt diff --git a/Output.js b/Output.js new file mode 100644 index 0000000..b52eada --- /dev/null +++ b/Output.js @@ -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) + } +} diff --git a/getPrice.js b/getPrice.js deleted file mode 100644 index 58528f2..0000000 --- a/getPrice.js +++ /dev/null @@ -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) -} diff --git a/index.js b/index.js index c999a79..0010842 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ const puppeteer = require('puppeteer') -const items = require('./items'); +const items = require('./items') +const Output = require('./Output') +const output = new Output(); (async () => { const browser = await puppeteer.launch({ headless: false }) @@ -13,9 +15,10 @@ const items = require('./items'); const setPrice = await item.set.getPrice(page) if (partsPrice < setPrice) { - console.log(item.name, partsPrice, setPrice) + output.addLine(`${item.name} prime: ${partsPrice}/${setPrice} выгода ${setPrice - partsPrice}`) } } await browser.close() + output.submit() })() diff --git a/items/Item.js b/items/Item.js index 07eb6a3..494f226 100644 --- a/items/Item.js +++ b/items/Item.js @@ -9,7 +9,7 @@ module.exports = class Item { this.addPart('blueprint') } - addPart (part) { - this.parts.push(new Part(this.name, part)) + addPart (part, amount) { + this.parts.push(new Part(this.name, part, amount)) } } diff --git a/items/Part.js b/items/Part.js index 3dc8111..87fbc67 100644 --- a/items/Part.js +++ b/items/Part.js @@ -1,7 +1,8 @@ module.exports = class Part { - constructor (set, part) { + constructor (set, part, amount) { this.part = part this.url = `${set}_prime_${part}` + this.amount = amount ?? 1 } async getPrice (page) { @@ -10,6 +11,6 @@ module.exports = class Part { await page.goto(marketUrl + this.url) const element = await page.waitForSelector('b.price') const value = await element.evaluate(el => el.textContent) - return Number(value) + return Number(value) * this.amount } } diff --git a/items/Primary.js b/items/Primary.js new file mode 100644 index 0000000..e9d8b8e --- /dev/null +++ b/items/Primary.js @@ -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') + } +} diff --git a/items/Secondary.js b/items/Secondary.js new file mode 100644 index 0000000..1a9f514 --- /dev/null +++ b/items/Secondary.js @@ -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') + } +} diff --git a/items/index.js b/items/index.js index 932d8c3..97940d2 100644 --- a/items/index.js +++ b/items/index.js @@ -1,6 +1,29 @@ const Warframe = require('./Warframe') +const Primary = require('./Primary') +// const Secondary = require('./Secondary') -module.exports = [ - new Warframe('ash'), - new Warframe('atlas') +const warframes = [ + 'ash', 'atlas', 'banshee', 'chroma', 'ember', + '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