Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mysql2 = require('mysql2/promise');
- let connection;
- class Parser {
- constructor() {
- this.defaultConf = {
- version: '0.1.38',
- results: {
- flat: [
- ['title', 'HTML title'],
- ]
- },
- results_format: '$query: $title\\n',
- parsecodes: {
- 200: 1,
- },
- max_size: 200 * 1024,
- };
- }
- async init(){
- connection = await mysql2.createConnection({
- host: '127.0.0.1',
- port: '3306',
- user: 'workflow',
- password: '12312312',
- database: 'app'
- });
- }
- async *parse(set, results) {
- this.logger.put("Start scraping query: " + set.query);
- let ins = yield* this.getAdInfo(set.query, {});
- return results;
- }
- * getAdInfo(set, results) {
- let response = yield this.request('GET', set.query, {}, {
- //'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
- check_content: ['<\/html>'],
- decode: 'auto-html',
- });
- if (response.success) {
- results.price = parseInt(this.getPrice(response.data).replace(/\s/g,''));
- results.model = parseInt(this.getModel(response.data).replace(/\s/g,''));
- this.utils.updateResultsData(results, response.data);
- results.success = response.success;
- }
- return results;
- }
- getPrice(data){
- let price = data.match(/<meta.*property="product:price:amount".*content="(.*)".*\/>/);
- let res = price ? price[1] : '0';
- if(res == ' '){
- return '0';
- }
- return res;
- }
- getModel(data){
- let model = data.match(/<div.*class="col-sm-4 col-xs-4">(.*)".*\<\/div>/);
- return res;
- }
- /*Проверяем есть ли записи в базе и если есть ничего не делаем, если нет то добавляем*/
- async CheckDB(key,price){
- try {
- let sql = `select (sid) from sb_product where model = "${key}"`;
- let myl = await connection.query(sql);
- if(myl[0].length == 0){
- let sql = `insert into sb_product(model) values("${key}")`;
- let [row,fields] = await connection.execute(sql);
- for(let i = 0 ; i < price.length; i ++){
- sql = `insert into sb_pricelog(pprice,sid) values("${price[i]}",${myl['sid']})`;
- let [row1,fields1] = await connection.execute(sql);
- }
- }
- else{
- this.logger.put('Keyword already exists');
- }
- } catch(e) {
- this.logger.put(JSON.stringify(e));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement