Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const os = require('os');
- const path = require('path');
- const request = require('request');
- const mail = require('nodemailer');
- const zipCodes = [
- {from: '03000', to: '03999'},
- {from: '08000', to: '08999'},
- {from: '13000', to: '13999'},
- {from: '17000', to: '17999'},
- {from: '18000', to: '18999'},
- {from: '21000', to: '21999'},
- {from: '25000', to: '25999'},
- {from: '28000', to: '28999'},
- {from: '30000', to: '30009'},
- {from: '35000', to: '35999'},
- {from: '37000', to: '37999'},
- {from: '38000', to: '38999'},
- {from: '41000', to: '41999'},
- {from: '46000', to: '46000'},
- {from: '48000', to: '48000'},
- {from: '51000', to: '51000'}]
- .reduce((codes, range) => {
- let from = Number(range.from);
- let to = Number(range.to);
- for (let i = from; i <= to; i++) {
- let code = i.toString();
- if (i < 10000) code = '0'.concat(code);
- codes.push(code);
- }
- return codes;
- }, []);
- function sendEmail(subject, message){
- const options = {
- host: 'smtp.gmail.com',
- port: 587,
- connectionTimeout: 500,
- auth: {
- user: 'ragnar@creditozen.com',
- pass: 'O9ln8N!2UY9ZBJ7YH&WK!#S0YyCdTLs9z3!*Xt74In@bhtWv*Caer1xnc#p4qREWeqjD3^@QU61r!Fpm3Ezd3#ohB2W$WM*THE07'
- }
- };
- let transporter = mail.createTransport(options);
- transporter.verify((err) => {
- if (err) return console.log('Error:', err);
- transporter.sendMail({
- from: '"Ragnar Laud" <ragnar@creditozen.com>',
- to: 'ragnar@creditozen.com',
- subject: subject,
- html: message,
- }, (err, info) => {
- if (err) return console.log('Error sending message:', err);
- console.log('Message sent:', info.response);
- });
- });
- };
- function useFile(){
- fs.readdir('files', (err, files) => {
- if (err) return console.log(err);
- let _;
- (_ = (index, base) => {
- if (index < files.length) {
- let file = path.join(base, files[index]);
- console.log('Reading file', file);
- fs.readFile(file, (err, contents) => {
- if (err) return console.log(err);
- contents = contents.toString();
- let codes = contents.split(os.EOL).reduce((codes, row) => {
- }, {});
- _(index+1, base);
- });
- }
- })(0, 'files');
- })
- }
- function AddressComponent(longName, shortName, types){
- this.longName = longName;
- this.shortName = shortName;
- this.types = types;
- const typesSet = new Set(types);
- this.isOfType = (type) => {
- return typesSet.has(type);
- }
- }
- function AddressComponentCollection(){
- this.components = [];
- const componentsSet = new Set();
- this.addComponent = (component) => {
- if(component instanceof AddressComponent && !componentsSet.has(component)) {
- this.components.push(component);
- this.componentsSet.add(component);
- }
- return this;
- }
- this.addComponents = (components) => {
- this.components.forEach(this.addComponent);
- return this;
- }
- this.getComponentsByType = (type) => {
- return this.components.reduce((matches, component) => {
- if(component.isOfType(type)) matches.push(component);
- return matches;
- }, []);
- }
- }
- function Geometry(jsonObject){
- this.bounds = new (function(b){
- this.northeast = b['northeast'];
- this.southwest = b['southwest'];
- })(jsonObject['bounds']);
- this.viewport = new (function(v){
- this.northeast = v['northeast'];
- this.southwest = v['southwest'];
- })(jsonObject['viewport']);
- this.location = jsonObject['location'];
- this.locationType = jsonObject['location_type'];
- }
- function Address(jsonObject){
- this.addressComponents = new AddressComponentCollection().addComponents(jsonObject['address_components']);
- this.formattedAddress = jsonObject['formatted_address'];
- this.geometry = new Geometry(jsonObject['geometry']);
- this.types = jsonObject['types'];
- const typesSet = new Set(this.types);
- this.isOfType = function(type){
- return typesSet.has(type);
- }
- }
- function AddressCollection(){
- this.addresses = [];
- const addressesSet = new Set();
- this.addAddress = (address) => {
- if(address instanceof Address && !addressesSet.has(address)){
- this.addresses.push(address);
- addressesSet.add(address);
- }
- return this;
- };
- }
- (function useApi(){
- fs.mkdir('./api', (err) => {if(err) console.log('Directory already exists, skipping...')});
- const log = function(index, message, ...messages){
- messages.forEach((message) => {
- console.log(`[#${index}-${zipCodes[index]}] :: ${message}`);
- });
- };
- let errors = [];
- let collection = new AddressCollection();
- const getStatus = function(){
- return `<h1>Status Update</h1><p>Failed: ${errors.length}</p><br /><p>Successful: ${collections.addresses.length}</p>`;
- };
- // TODO: Split into actions
- // TODO: Write a promise-based mixed (sync-async) pipeline
- // TODO: Un-successful requests are not errors.
- // TODO: Catch any and all errors that are not failed requests and send them
- let _;
- (_ = (i) => {
- if(i > zipCodes.length){
- return sendEmail('ZIP Code Extraction Started', 'The ZIP Code Extraction has ended.');
- }
- // if(i > 100) return;
- if(i === 0) sendEmail('ZIP Code Extraction Started', 'The ZIP Code Extraction has started. You will receive an update about it every 1000 ');
- request(`https://maps.googleapis.com/maps/api/geocode/json?address=${zipCodes[i]}%20Spain`, (err, response, data) => {
- try{
- data = JSON.parse(data);
- }catch(exception){
- return log(`FAILURE Server sent non-json response data`);
- }
- let error = false;
- if(err) {
- error = error || {};
- error.exception = err;
- log(i, `FAILURE Exception ocurred: ${err}`);
- }
- if(response['statusCode'] !== 200) {
- error = error || {};
- error.response = response;
- log(i, `FAILURE Status Code ${response['statusCode']} ocurred`);
- }
- if(data['status'] !== "OK") {
- error = error || {};
- error.data = data;
- log(i, `FAILURE Server sent status ${data['status']}`);
- }
- if(error === false){
- data['results']
- .reduce((addresses, object) => {
- addresses.push(new Address(object));
- return addresses;
- }, [])
- .forEach(collection.addAddress);
- console.log(`SUCCESS`);
- }
- if(i > 0 && i % 1000 === 0) sendEmail('ZIP Code Extraction Status', getStatus());
- _(++i);
- });
- })(0);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement