Advertisement
Guest User

Untitled

a guest
Nov 18th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const os = require('os');
  3. const path = require('path');
  4. const request = require('request');
  5. const mail = require('nodemailer');
  6.  
  7. const zipCodes = [
  8.     {from: '03000', to: '03999'},
  9.     {from: '08000', to: '08999'},
  10.     {from: '13000', to: '13999'},
  11.     {from: '17000', to: '17999'},
  12.     {from: '18000', to: '18999'},
  13.     {from: '21000', to: '21999'},
  14.     {from: '25000', to: '25999'},
  15.     {from: '28000', to: '28999'},
  16.     {from: '30000', to: '30009'},
  17.     {from: '35000', to: '35999'},
  18.     {from: '37000', to: '37999'},
  19.     {from: '38000', to: '38999'},
  20.     {from: '41000', to: '41999'},
  21.     {from: '46000', to: '46000'},
  22.     {from: '48000', to: '48000'},
  23.     {from: '51000', to: '51000'}]
  24. .reduce((codes, range) => {
  25.  
  26.     let from = Number(range.from);
  27.     let to = Number(range.to);
  28.  
  29.     for (let i = from; i <= to; i++) {
  30.         let code = i.toString();
  31.         if (i < 10000) code = '0'.concat(code);
  32.         codes.push(code);
  33.     }
  34.  
  35.     return codes;
  36.  
  37. }, []);
  38.  
  39. function sendEmail(subject, message){
  40.     const options = {
  41.         host: 'smtp.gmail.com',
  42.         port: 587,
  43.         connectionTimeout: 500,
  44.         auth: {
  45.             user: 'ragnar@creditozen.com',
  46.             pass: 'O9ln8N!2UY9ZBJ7YH&WK!#S0YyCdTLs9z3!*Xt74In@bhtWv*Caer1xnc#p4qREWeqjD3^@QU61r!Fpm3Ezd3#ohB2W$WM*THE07'
  47.         }
  48.     };
  49.  
  50.     let transporter = mail.createTransport(options);
  51.  
  52.     transporter.verify((err) => {
  53.         if (err) return console.log('Error:', err);
  54.  
  55.         transporter.sendMail({
  56.             from: '"Ragnar Laud" <ragnar@creditozen.com>',
  57.             to: 'ragnar@creditozen.com',
  58.             subject: subject,
  59.             html: message,
  60.         }, (err, info) => {
  61.             if (err) return console.log('Error sending message:', err);
  62.             console.log('Message sent:', info.response);
  63.         });
  64.     });
  65. };
  66.  
  67. function useFile(){
  68.     fs.readdir('files', (err, files) => {
  69.         if (err) return console.log(err);
  70.  
  71.         let _;
  72.         (_ = (index, base) => {
  73.             if (index < files.length) {
  74.                 let file = path.join(base, files[index]);
  75.  
  76.                 console.log('Reading file', file);
  77.  
  78.                 fs.readFile(file, (err, contents) => {
  79.                     if (err) return console.log(err);
  80.                     contents = contents.toString();
  81.  
  82.                     let codes = contents.split(os.EOL).reduce((codes, row) => {
  83.  
  84.                     }, {});
  85.  
  86.                     _(index+1, base);
  87.                 });
  88.             }
  89.         })(0, 'files');
  90.     })
  91. }
  92. function AddressComponent(longName, shortName, types){
  93.   this.longName = longName;
  94.   this.shortName = shortName;
  95.   this.types = types;
  96.  
  97.   const typesSet = new Set(types);
  98.  
  99.   this.isOfType = (type) => {
  100.     return typesSet.has(type);
  101.   }
  102. }
  103. function AddressComponentCollection(){
  104.   this.components = [];
  105.  
  106.   const componentsSet = new Set();
  107.  
  108.   this.addComponent = (component) => {
  109.     if(component instanceof AddressComponent && !componentsSet.has(component)) {
  110.       this.components.push(component);
  111.       this.componentsSet.add(component);
  112.     }
  113.  
  114.     return this;
  115.   }
  116.  
  117.   this.addComponents = (components) => {
  118.     this.components.forEach(this.addComponent);
  119.     return this;
  120.   }
  121.  
  122.   this.getComponentsByType = (type) => {
  123.     return this.components.reduce((matches, component) => {
  124.       if(component.isOfType(type)) matches.push(component);
  125.  
  126.       return matches;
  127.     }, []);
  128.   }
  129. }
  130. function Geometry(jsonObject){
  131.   this.bounds = new (function(b){
  132.     this.northeast = b['northeast'];
  133.     this.southwest = b['southwest'];
  134.   })(jsonObject['bounds']);
  135.   this.viewport = new (function(v){
  136.     this.northeast = v['northeast'];
  137.     this.southwest = v['southwest'];
  138.   })(jsonObject['viewport']);
  139.   this.location = jsonObject['location'];
  140.   this.locationType = jsonObject['location_type'];
  141. }
  142. function Address(jsonObject){
  143.   this.addressComponents = new AddressComponentCollection().addComponents(jsonObject['address_components']);
  144.   this.formattedAddress = jsonObject['formatted_address'];
  145.   this.geometry = new Geometry(jsonObject['geometry']);
  146.   this.types = jsonObject['types'];
  147.  
  148.   const typesSet = new Set(this.types);
  149.  
  150.   this.isOfType = function(type){
  151.     return typesSet.has(type);
  152.   }
  153. }
  154. function AddressCollection(){
  155.   this.addresses = [];
  156.  
  157.   const addressesSet = new Set();
  158.  
  159.   this.addAddress = (address) => {
  160.     if(address instanceof Address && !addressesSet.has(address)){
  161.       this.addresses.push(address);
  162.       addressesSet.add(address);
  163.     }
  164.  
  165.     return this;
  166.   };
  167. }
  168.  
  169. (function useApi(){
  170.   fs.mkdir('./api', (err) => {if(err) console.log('Directory already exists, skipping...')});
  171.  
  172.   const log = function(index, message, ...messages){
  173.     messages.forEach((message) => {
  174.       console.log(`[#${index}-${zipCodes[index]}] :: ${message}`);
  175.     });
  176.   };
  177.  
  178.   let errors = [];
  179.   let collection = new AddressCollection();
  180.  
  181.   const getStatus = function(){
  182.     return `<h1>Status Update</h1><p>Failed: ${errors.length}</p><br /><p>Successful: ${collections.addresses.length}</p>`;
  183.   };
  184.  
  185. // TODO: Split into actions
  186. // TODO: Write a promise-based mixed (sync-async) pipeline
  187. // TODO: Un-successful requests are not errors.
  188. // TODO: Catch any and all errors that are not failed requests and send them
  189.   let _;
  190.   (_ = (i) => {
  191.     if(i > zipCodes.length){
  192.       return sendEmail('ZIP Code Extraction Started', 'The ZIP Code Extraction has ended.');
  193.     }
  194.     // if(i > 100) return;
  195.  
  196.     if(i === 0) sendEmail('ZIP Code Extraction Started', 'The ZIP Code Extraction has started. You will receive an update about it every 1000 ');
  197.  
  198.     request(`https://maps.googleapis.com/maps/api/geocode/json?address=${zipCodes[i]}%20Spain`, (err, response, data) => {
  199.       try{
  200.         data = JSON.parse(data);
  201.       }catch(exception){
  202.         return log(`FAILURE Server sent non-json response data`);
  203.       }
  204.  
  205.       let error = false;
  206.  
  207.       if(err) {
  208.         error = error || {};
  209.         error.exception = err;
  210.         log(i, `FAILURE Exception ocurred: ${err}`);
  211.       }
  212.       if(response['statusCode'] !== 200) {
  213.         error = error || {};
  214.         error.response = response;
  215.         log(i, `FAILURE Status Code ${response['statusCode']} ocurred`);
  216.       }
  217.       if(data['status'] !== "OK") {
  218.         error = error || {};
  219.         error.data = data;
  220.         log(i, `FAILURE Server sent status ${data['status']}`);
  221.       }
  222.  
  223.       if(error === false){
  224.         data['results']
  225.           .reduce((addresses, object) => {
  226.             addresses.push(new Address(object));
  227.             return addresses;
  228.           }, [])
  229.           .forEach(collection.addAddress);
  230.  
  231.         console.log(`SUCCESS`);
  232.       }
  233.  
  234.       if(i > 0 && i % 1000 === 0) sendEmail('ZIP Code Extraction Status', getStatus());
  235.  
  236.       _(++i);
  237.     });
  238.   })(0);
  239. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement