Skip to content

Instantly share code, notes, and snippets.

@alireza-ahmadi
Last active July 24, 2018 10:54
Show Gist options
  • Save alireza-ahmadi/d22cd330f52bb71f5989b134cbf60245 to your computer and use it in GitHub Desktop.
Save alireza-ahmadi/d22cd330f52bb71f5989b134cbf60245 to your computer and use it in GitHub Desktop.
// # Usage
//
// Save this file and replace username and password on the last line of the file with
// your Snapp username and password then run the following commands:
//
// yarn init && yarn add request
// node SnappTotalPriceCalculator.js
//
const request = require('request');
let totalPrice = 0;
let token = '';
const fetchPage = (page) => {
const options = {
method: 'GET',
url: 'https://web-api.snapp.ir/api/v1/ride/history',
qs: {
page: `${page}`
},
headers: {
'content-type': 'application/json',
authorization: token,
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
referer: 'https://app.snapp.ir/',
accept: 'application/json',
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
const rides = JSON.parse(body).rides;
if (rides.length === 0) {
console.log(totalPrice)
return
}
totalPrice += rides.reduce((sum, item) => {
if (item.latest_ride_status === 6 || item.latest_ride_status === 7) {
return sum;
}
return sum + parseInt(item.price, 10)
}, 0);
fetchPage(page + 1);
});
};
const login = (username, password) => {
const options = {
method: 'POST',
url: 'https://web-api.snapp.ir/api/v1/auth/login',
headers:
{
'content-type': 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
origin: 'https://app.snapp.ir',
referer: 'https://app.snapp.ir/',
accept: 'application/json'
},
body: { password, username },
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
token = body.token;
fetchPage(1);
});
}
login('PutYourEmailHere', 'PutYourPasswordHere');
@Pourghannad
Copy link

yarn install or yarn add ?

@alireza-ahmadi
Copy link
Author

@Pourghannad of course yarn add, my bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment