Gistrec

Selenium VK Authorization

Oct 25th, 2020 (edited)
198
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Options:
  2. //  --proxy-address  Proxy data in format ip:port                       [required]
  3. //  --proxy-auth     Authentication data in format login:password       [required]
  4. //  --vk-auth        Authentication data in format login:password       [required]
  5.  
  6. const {Builder, By, Key, until} = require('selenium-webdriver');
  7. const firefox = require('selenium-webdriver/firefox');
  8. const proxy = require('selenium-webdriver/proxy');
  9.  
  10. const { getExtension, getAddHeaderUrl } = require('firefox-modheader');
  11.  
  12. const yargs = require('yargs');
  13. const fs    = require('fs');
  14.  
  15. const argv = yargs
  16.     .demandOption(['proxy-address', 'proxy-auth', 'vk-auth'])
  17.     .option('proxy-address', { description: 'Proxy data in format ip:port' })
  18.     .option('proxy-auth',    { description: 'Authentication data in format login:password' })
  19.     .option('vk-auth',       { description: 'Authentication data in format login:password' })
  20.     .help(false)
  21.     .version(false)
  22.     .argv;
  23.  
  24. (async function example() {
  25.     const options = new firefox.Options();
  26.     options.addExtensions(getExtension());
  27.     options.headless();
  28.  
  29.     const driver = await new Builder()
  30.         .forBrowser('firefox')
  31.         .setFirefoxOptions(options)
  32.         .setProxy(proxy.manual({
  33.             http:  argv['proxy-address'],
  34.             https: argv['proxy-address']
  35.         }))
  36.         .build();
  37.  
  38.     try {
  39.         await driver.get(getAddHeaderUrl('Proxy-Authorization', 'Basic ' + Buffer.from(argv['proxy-auth']).toString('base64')));
  40.         await driver.sleep(1000);
  41.         console.debug('Заголовок Proxy-Authorization для прокси установлен')
  42.  
  43.         await driver.get('https://vk.com');
  44.         console.debug('Страница vk.com загружена')
  45.  
  46.         await driver.findElement(By.id('index_email')).sendKeys(argv['vk-auth'].split(':')[0]);
  47.         await driver.sleep(1000);
  48.  
  49.         await driver.findElement(By.id('index_pass')).sendKeys(argv['vk-auth'].split(':')[1], Key.ENTER);
  50.         await driver.sleep(1000);
  51.         console.debug('Логин/пароль на странице vk.com введен')
  52.  
  53.         const currentUrl = await driver.getCurrentUrl();
  54.         console.log(currentUrl);
  55.         if (currentUrl.indexOf('vk.com/login') != -1) {
  56.             console.warn('Логин/пароль не верны');
  57.             return;
  58.         }else {
  59.             // TODO: Save cookies
  60.             // const cookies = await driver.manage().getCookies();
  61.             // saveCookie(cookies);
  62.         }
  63.  
  64.         // Получаем список всех постов (У активных постов статус 3)
  65.         await driver.get('https://vk.com/adsmarket?act=overview&status=-1');
  66.         console.debug('Страница vk.com/adsmarket загружена')
  67.  
  68.         const rows = await driver.findElements(By.xpath("//table[@id='exchange_requests_list_table']/tbody/tr"));
  69.  
  70.         let posts = [];
  71.         // Пропускаем первую строку, т.к. это заголовок таблицы
  72.         for (let i = 1; i < rows.length; i++) {
  73.             const row = rows[i];
  74.             const data = await getPostData(row);
  75.  
  76.             posts.push(data);
  77.         }
  78.         console.log(posts);
  79.     } catch (err) {
  80.         console.error(err);
  81.     }
  82. })();
  83.  
  84. /**
  85.  * Получаем данные ркалимного поста
  86.  */
  87. const getPostData = function(th) {
  88.     return new Promise(async function(resolve, reject) {
  89.         const name = await th.findElement(By.className('title')).getText();
  90.  
  91.         const html = await th.findElement(By.className('exchange_request_status')).getAttribute("innerHTML");
  92.         const result = /post_stats([0-9\-]+)_([0-9]+)/.exec(html);
  93.         const group_id = result[1];
  94.         const post_id  = result[2];
  95.  
  96.         resolve({ name, group_id, post_id })
  97.     })
  98. }
  99.  
  100. //const saveCookie = function(cookies) {
  101. //    const file = 'cookies/' + argv['vk-auth'].split(':')[0] + '@' + argv['proxy-address'];
  102. //    fs.writeFileSync(file, JSON.stringify(cookies));
  103. //}
  104.  
  105. //const loadCookie = function(driver) {
  106. //    const file = 'cookies/' + argv['vk-auth'].split(':')[0] + '@' + argv['proxy-address'];
  107. //    const cookies = fs.readFileSync(file);
  108. //
  109. //    for (const cookie of cookies) {
  110. //        driver.manage().addCookie(cookie)
  111. //    }
  112. //}
Comments
  • User was banned
Add Comment
Please, Sign In to add comment