Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Alert Preview #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
});
});
```

Expand All @@ -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
Expand Down Expand Up @@ -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]: <https://github.com/adasq/google-alerts-api/issues?q=label%3Aauth-issues+>
[tests]: <https://github.com/adasq/google-alerts-api/blob/master/tests/test.js>
Expand Down
20 changes: 17 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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(
[
Expand All @@ -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 = {
Expand All @@ -145,6 +158,7 @@ module.exports = {
generateCookiesBySID,
sync,
getAlerts,
preview,
create,
remove,
modify,
Expand Down
7 changes: 7 additions & 0 deletions src/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -201,6 +207,7 @@ function checkRssSource(url, cb) {

module.exports = {
login,
preview,
create,
modify,
delete: remove,
Expand Down
30 changes: 26 additions & 4 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -75,7 +97,7 @@ describe('google', function () {
deliverTo: DELIVER_TO.RSS,
deliverToData: '',
};

api.create(alertToCreate, (err, alert) => {
expect(err).to.be(null);
api.sync(() => {
Expand All @@ -99,20 +121,20 @@ describe('google', function () {
deliverTo: DELIVER_TO.RSS,
deliverToData: '',
};

api.create(alertToCreate, (err) => {
expect(err).to.be(null);
api.sync(() => {
const alert = findAlertByName(api.getAlerts(), NEW_NAME);
expect(alert).to.eql({ ...alert, region: 'any' });
done();
});

});
});
});
})

it('edit name for RSS', done => {
api.sync((err) => {
expect(err).to.be(null);
Expand Down