Advertisement
Guest User

index.js for proj2

a guest
Apr 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('request');
  2. const argv = require('yargs').argv;
  3. const metric = 'metric';
  4. const imperial = 'imperial';
  5. const kelvin = 'kelvin';
  6.  
  7.  
  8. let apiKey = '379abae147e4f1138f4eb06d4191bc00';
  9. let city = argv.c || 'portland';
  10. let country = argv.l || 'US';
  11. let zip = argv.z || '24142';
  12. let unit = argv.u || 'c';
  13. let help = argv.h || '';
  14.  
  15.  
  16. let url;
  17.  
  18. if(argv.h)
  19. {
  20.     switch(argv.h)
  21.     {
  22.         case 'c':
  23.         {
  24.             console.log("the argument [-c] denotes a city name, it is incompatable with [-z], if not specified it defaults to \"portland\"");
  25.             break;
  26.         }
  27.         case 'l':
  28.         {
  29.             console.log("the argument [-l] denotes the two-letter country code, if not specified it defaults to US");
  30.             break;
  31.         }
  32.         case 'z':
  33.         {
  34.             console.log("the argument [-z] denotes a city zip code, it is incompatable with [-c], if not specified it defaults to \"24142\"");
  35.             break;
  36.         }
  37.         case 'u':
  38.         {
  39.             console.log("the argument [-u] denotes the unit of temp returned, defaults to celsius, supports celsius (c),fahrenheit (f), and kelvin (k)");
  40.             break;
  41.         }
  42.         default:
  43.         {
  44.             console.log("This script gets the tempature of the specified place in the specified degrees. [-c] [-l] [-z] [-u] [-h]\n\nOptions:\n\tc\tdenotes a city name, it is incompatable with [-z], if not specified it defaults to \"portland\"\n\tl\tdenotes the two-letter country code, if not specified it defaults to US\n\tz\tdenotes a city zip code, it is incompatable with [-c], if not specified it defaults to \"24142\"\n\tu\tdenotes the unit of temp returned, defaults to celsius, supports celsius (c),fahrenheit (f), and kelvin (k)\n\th\tlists details and argument options, if this is passed at all, the script wont return data, can take the other argument letters, but only one, else this prints again :P\n\n\n");
  45.         }
  46.     }
  47. }
  48. else
  49. {
  50.     if(argv.u)
  51.     {
  52.         switch(argv.u)
  53.         {
  54.             case 'c':
  55.                 unit = metric; break;
  56.             case 'f':
  57.                 unit = imperial; break;
  58.             case 'k':
  59.                 unit = kelvin; break;
  60.             default:
  61.                 unit = metric;
  62.         }
  63.     }
  64.     else
  65.     {
  66.         unit = metric;
  67.     }
  68.  
  69.     if (!argv.c && argv.z)
  70.     {
  71.             url = `http://api.openweathermap.org/data/2.5/weather?q=${zip},${country}&units=${unit}&appid=${apiKey}`;
  72.     }
  73.     else
  74.     {
  75.             url = `http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&units=${unit}&appid=${apiKey}`;
  76.     }
  77.  
  78.  
  79.     request(url, function(err, response, body)
  80.     {
  81.             if(err)
  82.             {
  83.                 console.log('error:', error);
  84.             }
  85.         else
  86.             {
  87.                 let weather = JSON.parse(body);
  88.                 let message = `It's ${weather.main.temp}${getUnit()} in ${weather.name}!`;
  89.                 console.log(message);
  90.         }
  91.     });
  92.  
  93. }
  94.  
  95. function getUnit()
  96. {
  97.     if(unit === metric)
  98.         return "°C";
  99.     else if(unit === imperial)
  100.         return "°F";
  101.     else if(unit === kelvin)
  102.         return "°K";
  103.     else
  104.         return "°C";
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement