Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Options:
- // --proxy-address Proxy data in format ip:port [required]
- // --proxy-auth Authentication data in format login:password [required]
- // --vk-auth Authentication data in format login:password [required]
- const {Builder, By, Key, until} = require('selenium-webdriver');
- const firefox = require('selenium-webdriver/firefox');
- const proxy = require('selenium-webdriver/proxy');
- const { getExtension, getAddHeaderUrl } = require('firefox-modheader');
- const yargs = require('yargs');
- const fs = require('fs');
- const argv = yargs
- .demandOption(['proxy-address', 'proxy-auth', 'vk-auth'])
- .option('proxy-address', { description: 'Proxy data in format ip:port' })
- .option('proxy-auth', { description: 'Authentication data in format login:password' })
- .option('vk-auth', { description: 'Authentication data in format login:password' })
- .help(false)
- .version(false)
- .argv;
- (async function example() {
- const options = new firefox.Options();
- options.addExtensions(getExtension());
- options.headless();
- const driver = await new Builder()
- .forBrowser('firefox')
- .setFirefoxOptions(options)
- .setProxy(proxy.manual({
- http: argv['proxy-address'],
- https: argv['proxy-address']
- }))
- .build();
- try {
- await driver.get(getAddHeaderUrl('Proxy-Authorization', 'Basic ' + Buffer.from(argv['proxy-auth']).toString('base64')));
- await driver.sleep(1000);
- console.debug('Заголовок Proxy-Authorization для прокси установлен')
- await driver.get('https://vk.com');
- console.debug('Страница vk.com загружена')
- await driver.findElement(By.id('index_email')).sendKeys(argv['vk-auth'].split(':')[0]);
- await driver.sleep(1000);
- await driver.findElement(By.id('index_pass')).sendKeys(argv['vk-auth'].split(':')[1], Key.ENTER);
- await driver.sleep(1000);
- console.debug('Логин/пароль на странице vk.com введен')
- const currentUrl = await driver.getCurrentUrl();
- console.log(currentUrl);
- if (currentUrl.indexOf('vk.com/login') != -1) {
- console.warn('Логин/пароль не верны');
- return;
- }else {
- // TODO: Save cookies
- // const cookies = await driver.manage().getCookies();
- // saveCookie(cookies);
- }
- // Получаем список всех постов (У активных постов статус 3)
- await driver.get('https://vk.com/adsmarket?act=overview&status=-1');
- console.debug('Страница vk.com/adsmarket загружена')
- const rows = await driver.findElements(By.xpath("//table[@id='exchange_requests_list_table']/tbody/tr"));
- let posts = [];
- // Пропускаем первую строку, т.к. это заголовок таблицы
- for (let i = 1; i < rows.length; i++) {
- const row = rows[i];
- const data = await getPostData(row);
- posts.push(data);
- }
- console.log(posts);
- } catch (err) {
- console.error(err);
- }
- })();
- /**
- * Получаем данные ркалимного поста
- */
- const getPostData = function(th) {
- return new Promise(async function(resolve, reject) {
- const name = await th.findElement(By.className('title')).getText();
- const html = await th.findElement(By.className('exchange_request_status')).getAttribute("innerHTML");
- const result = /post_stats([0-9\-]+)_([0-9]+)/.exec(html);
- const group_id = result[1];
- const post_id = result[2];
- resolve({ name, group_id, post_id })
- })
- }
- //const saveCookie = function(cookies) {
- // const file = 'cookies/' + argv['vk-auth'].split(':')[0] + '@' + argv['proxy-address'];
- // fs.writeFileSync(file, JSON.stringify(cookies));
- //}
- //const loadCookie = function(driver) {
- // const file = 'cookies/' + argv['vk-auth'].split(':')[0] + '@' + argv['proxy-address'];
- // const cookies = fs.readFileSync(file);
- //
- // for (const cookie of cookies) {
- // driver.manage().addCookie(cookie)
- // }
- //}