Monday, November 26, 2018

Google Alerts API



$ npm i -S google-alerts-api
const alerts = require('google-alerts-api');

if using email and password, configure like below

alerts.configure({
    mail: 'your_mail@gmail.com',
    password: '**********'
});

if using cookies, to be configured like below

alerts.configure({
    cookies: 'W3sia2V5IjoiR0FQUyIsInZhbHVlIjoiMTpCRXRtZEpjc...saGRasC==',
});

This is how to fetch alerts

const alerts = require('google-alerts-api');
const { HOW_OFTEN, DELIVER_TO, HOW_MANY, SOURCE_TYPE } = alerts;

alerts.configure({
    mail: 'your_mail@gmail.com',
    password: '**********'
});

alerts.sync((err) => {
    if(err) return console.log(err);
    const alertList = alerts.getAlerts();
    alertList.forEach(alert => printAlertInfo);
});

function printAlertInfo(alert) {
    console.log('name:', alert.name);
    //'How Many' property information:
    if (alert.howMany === HOW_MANY.BEST) {
    console.log('How many: Only the best results');
    } else if (alert.howMany === HOW_MANY.ALL) {
    console.log('How many: All Results');
    }
}

a sample alert object is like below

{
    name: '"Donald Trump * ISIS"',
    id: '4f94515ec736ef62:ade5b03803caa237:com:en:PL:R',
    howOften: 2, //use HOW_OFTEN enum to find out proper meaning
    sources: '...', // some of SOURCE_TYPE enum property, SOURCE_TYPE.AUTOMATIC by default
    lang: 'en',
    region: 'PL',
    howMany: 3, //use HOW_MANY enum to find out proper meaning
    deliverTo: 2, //use DELIVER_TO enum to find out proper meaning
    deliverToData: '', //email address, available when deliverTo === DELIVER_TO.MAIL
    rss: 'https://google.com/alerts/feeds/00357582442749620569/11537740808718742679' //field available, when deliverTo === DELIVER_TO.RSS
}


references:
https://github.com/adasq/google-alerts-api

No comments:

Post a Comment