diff --git a/README.md b/README.md index 2f656e7..ec6cb23 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ const alerts = require('google-alerts-api'); ## Configuration -#### IMPORTANT: Due to the latest changes in Google, authentication with disabled JavaScript is permited. Still, you can generate cookies on your own and reuse it later on ([see how to get cookies](#generate-cookies)) +#### IMPORTANT: Due to the latest changes in Google, authentication with disabled JavaScript is permited. Still, you can generate cookies on your own and reuse it later on ([see how to get cookies](#generate-cookies)) ```js alerts.configure({ @@ -154,7 +154,30 @@ alerts.sync((err) => { alerts.sync((err) => { const syncedAlertsList = alerts.getAlerts(); //alertToRemove does not exists here. }); - }); + }); +}); +``` + +#### Preview alert: + +Alert preview returns Google provided HTML preview without generating the alert. + +```js +alerts.sync(() => { + const alertToPreview = { + howOften: HOW_OFTEN.AT_MOST_ONCE_A_DAY, + sources: SOURCE_TYPE.AUTOMATIC, // default one + lang: 'en', + name: 'NodeJS AND "Chrome V8"', + region: 'PL', // or "any", if you want "All Regions" + howMany: HOW_MANY.BEST, + deliverTo: DELIVER_TO.RSS, + deliverToData: '' + }; + + alerts.preview(alertToPreview, (err, alert) => { + console.log(alert.preview); //HTML data + }); }); ``` @@ -174,7 +197,7 @@ You can authenticate once, and then use your cookies. Unfortunatelly it requires 2. Navigate **Application** tab, select **Cookies** preview for http://myaccount.google.com domain 3. Copy **SID**, **HSID** and **SSID** cookie values -![copy SID, HSID, SSID cookie values](https://cdn.steemitimages.com/DQmbMvsdTvVpwukxMSXss57wq28gxXmLUNqkEgzYREHcLtZ/image.png) +![copy SID, HSID, SSID cookie values](https://cdn.steemitimages.com/DQmbMvsdTvVpwukxMSXss57wq28gxXmLUNqkEgzYREHcLtZ/image.png) #### STEP 3: Prepare your auth cookie string @@ -228,7 +251,7 @@ alerts.sync((err) => { - https://accounts.google.com/b/1/DisplayUnlockCaptcha (make sure you are editing settings for proper user...) - https://myaccount.google.com/lesssecureapps - review [auth issues] labeled issues, hope you will find an answer - + [auth issues]: [tests]: diff --git a/src/api.js b/src/api.js index 4eff646..2f06835 100644 --- a/src/api.js +++ b/src/api.js @@ -15,7 +15,7 @@ const ERROR = { function getCookies() { const str = JSON.stringify(reqHandler.getCookies()); - return new Buffer(str).toString('base64'); + return Buffer.from(str).toString('base64'); } function generateCookies(mail, password, cb) { @@ -40,7 +40,7 @@ function configure({mail, password, cookies}) { } function parseCookies(cookies) { - const str = new Buffer(cookies, 'base64').toString('ascii'); + const str = Buffer.from(cookies, 'base64').toString('ascii'); return JSON.parse(str); } @@ -127,6 +127,19 @@ function create(createData, cb) { }); } +function preview(previewData, cb) { + const previewParams = alerts.create(previewData); + + reqHandler.preview(previewParams, (err, resp, preview) => { + if (err) return cb(err); + try { + cb(null, { ...previewData, preview}); + } catch (e) { + cb(e); + } + }); +} + function generateCookiesBySID(SID, HSID, SSID) { const str = JSON.stringify( [ @@ -135,7 +148,7 @@ function generateCookiesBySID(SID, HSID, SSID) { { key: 'SSID', value: SSID, domain: 'google.com' }, ] ); - return new Buffer(str).toString('base64'); + return Buffer.from(str).toString('base64'); } module.exports = { @@ -145,6 +158,7 @@ module.exports = { generateCookiesBySID, sync, getAlerts, + preview, create, remove, modify, diff --git a/src/request-handler.js b/src/request-handler.js index 158ce8d..938f329 100644 --- a/src/request-handler.js +++ b/src/request-handler.js @@ -11,6 +11,7 @@ const ALERTS_URL = 'https://www.google.com/alerts'; const ALERTS_MODIFY_URL = 'https://www.google.com/alerts/modify?x={requestX}'; const ALERTS_CREATE_URL = 'https://www.google.com/alerts/create?x={requestX}'; const ALERTS_DELETE_URL = 'https://www.google.com/alerts/delete?x={requestX}'; +const ALERTS_PREVIEW_URL = 'https://www.google.com/alerts/preview?params={requestParams}'; const COOKIES_URL = 'https://accounts.google.com'; @@ -160,6 +161,11 @@ function modify(requestX, form, cb) { }, cb); } +function preview(requestParams, cb) { + const url = ALERTS_PREVIEW_URL.replace('{requestParams}', encodeURIComponent(JSON.stringify(requestParams))); + request(url, cb); +} + function create(requestX, form, cb) { const url = ALERTS_CREATE_URL.replace('{requestX}', requestX); request({ @@ -201,6 +207,7 @@ function checkRssSource(url, cb) { module.exports = { login, + preview, create, modify, delete: remove, diff --git a/tests/test.js b/tests/test.js index 9aa147e..56fe3fb 100644 --- a/tests/test.js +++ b/tests/test.js @@ -61,6 +61,28 @@ describe('google', function () { }); }); + it('previews RSS', (done) => { + api.sync(() => { + const alertToPreview = { + name: NAME, + howOften: HOW_OFTEN.AT_MOST_ONCE_A_DAY, + sources: SOURCE_TYPE.AUTOMATIC, + lang: 'en', + region: 'PL', + howMany: HOW_MANY.BEST, + deliverTo: DELIVER_TO.RSS, + deliverToData: '', + }; + + api.preview(alertToPreview, (err, alert) => { + expect(err).to.be(null); + expect(alert.name).to.be(alertToPreview.name); + expect(alert).to.have.property('preview'); + done(); + }); + }); + }); + describe('creates with region', () => { it('as "any"', (done) => { api.sync(() => { @@ -75,7 +97,7 @@ describe('google', function () { deliverTo: DELIVER_TO.RSS, deliverToData: '', }; - + api.create(alertToCreate, (err, alert) => { expect(err).to.be(null); api.sync(() => { @@ -99,7 +121,7 @@ describe('google', function () { deliverTo: DELIVER_TO.RSS, deliverToData: '', }; - + api.create(alertToCreate, (err) => { expect(err).to.be(null); api.sync(() => { @@ -107,12 +129,12 @@ describe('google', function () { expect(alert).to.eql({ ...alert, region: 'any' }); done(); }); - + }); }); }); }) - + it('edit name for RSS', done => { api.sync((err) => { expect(err).to.be(null);