Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automating CRM Data Retrieval with Node.js HTTP Requests

Tech Nov 1 1

config.js

export const sessionCookie =
    'WTSESSIONID=155f7788cc3dcbae54a007a35e91.crm-web-wt-g2-srv15; BOMC_PROBE_ID=FB4D90E3F77ADE943B223DB764052EBE222EB5EEA83909F3638B33CAE7BAE24A16F0279702DAE09C10437EF951E404453592C33E34F5FC6E502D555F2A522423; currentBusiHref=; currentBusiName=; jumpCurrentBusiHref=; jumpCurrentBusiName=; operator=51811568; operorg=¹óÖÝά´´Ã³Ò×ÓÐÏÞ¹«Ë¾; operpost=¹óÖÝά´´Ã³Ò×ÓÐÏÞ¹«Ë¾-·Ç×Ô°ìÌü¸Úλ; crmCity=¹óÑô; crmCounty=ÄÏÃ÷Çø; moduleName=yhhdjycx; bussinessStep=21; sstcms=54549.36505.57775.0000; UBmobile=15285910184; scanEquipmentProvider=; AIPortal_SessionId=DATAcbe0795b-4bf0-427e-88be-5c0bf35a1e95; AIPortal_Oper_LastAccessedTime=509b07479765844476b1612561c5cf50; AIPortal_Oper_ActiveUrl=9b639d8e726a75a11ac8bad9de83ff4c0e5b0663fc25db5310eb2faf1abb70a2; AIPortal_HostIp=797ebb1dbeadc2a0d2a0082250895e40; AIPortal_Port=e31d5367c32b3700; AIPortal_Oper_OpId=3485eb4a193ffe9037feea805bed6525; AIPortal_Oper_OrgId=537d5b2b91ea711937feea805bed6525; AIPortal_Oper_OpName=f6032b4a1737c499; AIPortal_Oper_LogName=3485eb4a193ffe9037feea805bed6525; AIPortal_Oper_HostIp=c6f2c566ec25cee528faa51c22201a74; AIPortal_Result=c0012bc3eb72ee8a; AIPortal_Context_Path=37feea805bed6525; AIPortal_MAC=37feea805bed6525; AIPortal_CNAME=37feea805bed6525; AIPortal_4A_Session_Id=97bbe3d82a172311d94e254e3f9de22f; AIPortal_Region_Id=6678d3f66677bafa; AIPortal_Res_Id=fed357c6bcf1dce5; AIPortal_Station_Id=37feea805bed6525; cname=37feea805bed6525; mac=37feea805bed6525; ngboss_options=75fe019adbfd06c5a8ced660faae92beaab1ba62a2271775925bdbe50596b43d; WT_FPC=id=2a484f612d3a6160fc11647046935270:lv=1653316671923:ss=1653309241177; Distributed_OperId=51811568; WTSESSIONID=c422c52c4b234739782d49d52e5a.crm-web-wt-g2-srv8; AIPortal_SessionId=DATAcbe0795b-4bf0-427e-88be-5c0bf35a1e95; AIPortal_4A_Session_Id=e56e7cfd8c3f77553521b07937a3c089';
export const timestampToken =
    'FB4D90E3F77ADE943B223DB764052EBE71292EC8DC3C13940BDB41A6840B99282A3B05F0E04B517D53C899375FC1CCF0246DDF4B7F1FEBCC5B69F889262572B03013D1D2761B39A4';

file.js

const fs = require('fs');

Date.prototype.toCustomFormat = function (format) {
    const replacements = {
        'M+': this.getMonth() + 1,
        'd+': this.getDate(),
        'h+': this.getHours(),
        'm+': this.getMinutes(),
        's+': this.getSeconds(),
        'q+': Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds()
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substring(4 - RegExp.$1.length));
    }

    for (const key in replacements) {
        if (new RegExp('(' + key + ')').test(format)) {
            format = format.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? replacements[key] : ('00' + replacements[key]).substr(-2)
            );
        }
    }
    return format;
};

function exportToXLS(dataArray) {
    const outputFile = new Date().toCustomFormat('yyyyMMddhhmmss') + '.xls';
    const stream = fs.createWriteStream(outputFile, { encoding: 'utf8' });

    try {
        if (dataArray.length > 0) {
            dataArray.forEach(row => {
                stream.write(row.join('\t') + '\n');
            });
        }
        console.log('Excel file generated successfully');
    } catch (err) {
        console.error('Error generating Excel file:', err);
    } finally {
        stream.close();
    }
}

helper.js

function isPlainObject(obj) {
    return Object.prototype.toString.call(obj) === '[object Object]';
}

function isArray(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]';
}

export function serializeParams(params, prefix) {
    let result = [];

    for (const key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
            const value = params[key];
            const fullKey = prefix ? `${prefix}[${key}]` : key;

            if (isPlainObject(value) || isArray(value)) {
                result.push(serializeParams(value, fullKey));
            } else {
                const encodedValue = encodeURIComponent(value);
                result.push(`${fullKey}=${encodedValue}`);
            }
        }
    }
    return result.join('&');
}

export async function readPhoneNumbers() {
    return new Promise((resolve) => {
        require('fs').readFile('phone.txt', 'utf8', (err, content) => {
            if (err) {
                console.error('Error reading phone numbers:', err);
                resolve([]);
            } else {
                resolve([...new Set(content.trim().split(/\s+/))]);
            }
        });
    });
}

endex.js

(async () => {
    const http = require('http');
    const { serializeParams, readPhoneNumbers } = require('./helper');
    const { sessionCookie, timestampToken } = require('./config');

    const targetURL = 'http://wt.crm.gz.cmcc/ecrm/qryactivityplentyinfouser/QryActivityPlentyinfoUserAction/qryBussinessInfoTable.menu';

    async function fetchCRMData(phoneNumber) {
        const requestBody = {
            _buttonId: 'infoList-query',
            _dateTimeToken: timestampToken,
            _menuId: 110040000014,
            _tmpDate: Date.now(),
            _zoneId: 'list-container',
            BillId: phoneNumber,
            is_first_render: true
        };

        const encodedBody = serializeParams(requestBody);
        const headers = {
            Referer: 'http://wt.crm.gz.cmcc/crm/yhhdjycx.jsp?_busi_tab_iframe=true',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)',
            Cookie: sessionCookie,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(encodedBody)
        };

        const options = {
            host: '127.0.0.1',
            port: 8888,
            path: targetURL,
            method: 'POST',
            headers: headers
        };

        return new Promise((resolve, reject) => {
            const request = http.request(options, (response) => {
                let responseData = '';
                response.setEncoding('utf8');

                response.on('data', (chunk) => {
                    responseData += chunk;
                });

                response.on('end', () => {
                    resolve(responseData);
                });
            });

            request.on('error', (err) => {
                reject(err);
            });

            request.write(encodedBody);
            request.end();
        });
    }

    const phoneNumbers = await readPhoneNumbers();
    if (phoneNumbers.length === 0) {
        console.log('No phone numbers found');
        return;
    }

    for (const number of phoneNumbers) {
        if (number.trim()) {
            const result = await fetchCRMData(number.trim());
            console.log(`Response for ${number}:`, result);
        }
    }
})();

HTML Parsing Example

const { parse } = require('node-html-parser');

const sampleHTML = `<!-- HTML structure omitted for brevity -->`;

const root = parse(sampleHTML);
const activityNames = root.querySelectorAll('tr td:nth-child(1)').map(el => el.text);
console.log('Activities:', activityNames.join(', '));

const paginationInfo = root.querySelector('.ui-paging-bold').text;
const totalPages = paginationInfo.includes('/') ? parseInt(paginationInfo.split('/')[1]) : 1;
console.log('Total pages:', totalPages);
Tags: Node.js

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.