forked from technoherder/UtilityBelt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1079 lines (919 loc) · 31.7 KB
/
Program.cs
File metadata and controls
1079 lines (919 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text.Json;
using System.Web;
using UtilityBelt.Helpers;
using UtilityBelt.Models;
namespace UtilityBelt
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(Properties.Resources.ASCIIart);
Console.WriteLine("Loading...");
IServiceProvider services = ServiceProviderBuilder.GetServiceProvider(args);
IOptions<SecretsModel> options = services.GetRequiredService<IOptions<SecretsModel>>();
bool showMenu = true;
do
{
MenuOptions(options);
showMenu = RecursiveOptions();
} while (showMenu);
}
#region Choice Handler
static bool RecursiveOptions()
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Green;
string testUserInput = null;
do
{
Console.Write("Would you like to run another option?: ");
testUserInput = Console.ReadLine();
if (int.TryParse(testUserInput, out _)) //If user input was a number...
{
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.\n");
testUserInput = null;
}
} while (testUserInput == null);
try
{
return FromString(testUserInput);
}
catch
{
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.");
return RecursiveOptions();
}
}
static void MenuOptions(IOptions<SecretsModel> options)
{
Console.WriteLine("");
Console.WriteLine("Select the Tool");
Console.WriteLine("1) Port Scanner");
Console.WriteLine("2) Text Message");
Console.WriteLine("3) Random Chuck Norris Joke");
Console.WriteLine("4) Random Cat Fact");
Console.WriteLine("5) Bitcoin Prices");
Console.WriteLine("6) Who is in Space");
Console.WriteLine("7) Weather forecast");
Console.WriteLine("8) Country Information");
Console.WriteLine("9) Discord sender");
Console.WriteLine("10) Random Quote");
Console.WriteLine("11) Random Insult");
Console.WriteLine("12) Who Stole the Cookie");
Console.WriteLine("13) Random Taco Recipe");
Console.WriteLine("14) COVID-19 Statistics");
Console.WriteLine("15) Geek jokes");
Console.WriteLine("16) Number Fact");
Console.WriteLine("17) Random Advice");
Console.WriteLine("18) International Space Station Location");
Console.WriteLine("19) Random Quote 2");
Console.WriteLine("20) Console Calculator");
Console.WriteLine("21) Gender from name");
Console.WriteLine("22) Random Dad Joke");
Console.WriteLine("23) Breaking Bad");
Console.WriteLine("24) Fun Ghost Game");
Console.WriteLine("25) Dns Hostname to IP Address");
Console.WriteLine("26) Panda Fact");
Console.WriteLine("");
Console.Write("Your choice:");
string optionPicked = Console.ReadLine().ToLower();
switch (optionPicked)
{
case "1":
case "port":
case "port scanner":
portScanner();
break;
case "2":
case "ssms":
case "text":
case "text message":
TextMessage(options);
break;
case "3":
case "random chuck norris joke":
case "chuck norris joke":
case "chuck norris":
case "joke":
RandomChuckNorrisJoke();
break;
case "4":
case "cat fact":
case "cat":
CatFact();
break;
case "5":
case "bitcoin prices":
case "bitcoin":
BitcoinPrices();
break;
case "6":
case "who is in space":
case "space":
Space();
break;
case "7":
case "weather":
case "wf":
case "weather forecast":
WeatherForecast(options);
break;
case "8":
case "Country":
CountryInformation();
break;
case "9":
case "discord":
case "ds":
case "webhook":
case "wh":
DiscordWebhook(options);
break;
case "10":
case "quote":
RandomQuote();
break;
case "11":
case "insult":
RandomInsult();
break;
case "12":
case "cookie":
CookieAccusation();
break;
case "13":
case "taco":
RandomTaco();
break;
case "14":
case "covid":
case "covid19":
case "covid-19":
Covid19();
break;
case "15":
case "geek":
GeekJokes();
break;
case "16":
case "numberfact":
NumberFact();
break;
case "17":
case "advice":
case "random advice":
RandomAdvice();
break;
case "18":
case "space station location":
case "internation space station":
SpaceStationLocation();
break;
case "19":
case "quote2":
RandomQuoteGarden();
break;
case "20":
ConsoleCalculator();
break;
case "21":
case "gender from name":
GenderFromName();
break;
case "22":
case "dadjoke":
DadJoke();
break;
case "23":
case "breaking bad":
BreakingBadQuotes();
break;
case "24":
GhostGame();
break;
case "25":
case "hosttoip":
HostToIp();
break;
case "26":
case "panda fact":
case "panda":
RandomPandaFact();
break;
default:
Console.WriteLine("Please make a valid option");
MenuOptions(options);
break;
}
}
#endregion
#region Choice Processors
#region Weather
static void WeatherForecast(IOptions<SecretsModel> options)
{
var openWeatherMapApiKey = options.Value.OpenWeatherMapApiKey;
if (String.IsNullOrEmpty(openWeatherMapApiKey))
{
Console.WriteLine("Whoops! API key is not defined.");
return;
}
Console.Write("Enter your town name:");
string town = Console.ReadLine();
string resp = "";
using (var wc = new WebClient())
{
resp = wc.DownloadString($"http://api.openweathermap.org/data/2.5/weather?q={town}&appid={openWeatherMapApiKey}");
}
WeatherRoot wr = JsonSerializer.Deserialize<WeatherRoot>(resp);
Console.WriteLine();
Console.WriteLine("Temperature: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C. Feels like: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C");
Console.WriteLine("Wind speed: " + wr.Wind.Speed + " m/s. Air pressure is " + wr.Main.Pressure + "mmHg or " + Math.Round(wr.Main.Pressure * 133.322, 1) + " Pascals.");
long currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
bool SunWhat = currentTime > wr.Sys.Sunrise;
long whatNext = SunWhat ? wr.Sys.Sunset : wr.Sys.Sunrise;
long diff = whatNext - currentTime;
var dto = DateTimeOffset.FromUnixTimeSeconds(diff);
if (SunWhat) //If sun should be setting...
{
Console.WriteLine("It's day right now. The sun will set in " + dto.ToString("HH:mm:ss"));
}
else
{
Console.WriteLine("It's night right now. The sun will rise in " + dto.ToString("HH:mm:ss"));
}
}
#endregion
#region Port Scanner
static void portScanner()
{
Console.Write("Please enter a domain:");
string domain = Console.ReadLine().ToLower();
Console.Write("Please enter a starting Port Number:");
int lowPort = int.Parse(Console.ReadLine());
Console.Write("Please enter an ending Port Number:");
int highPort = int.Parse(Console.ReadLine());
PortScanner.Scanner(domain, lowPort, highPort);
}
#endregion
#region Text Message
static void TextMessage(IOptions<SecretsModel> options)
{
Console.WriteLine();
Console.Write("Input number to receive message:");
string send2Number = Console.ReadLine().ToLower();
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(options.Value.Email, options.Value.EmailPassword),
EnableSsl = true
};
MailMessage message = new MailMessage();
message.From = new MailAddress(options.Value.Email);
var carrierType = MenuEnum<CarrierType>("Carrier");
var meta = EnumHelper.GetAttributeOfType<CarrierMetaAttribute>(carrierType);
message.To.Add(new MailAddress(send2Number + $"@{meta?.Domain}"));
message.Subject = "This is my subject";
message.Body = "This is the content";
try
{
client.Send(message);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Sent successfully");
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Text Message failed");
}
message.Dispose();
client.Dispose();
}
static T MenuEnum<T>(string subject)
where T : struct, Enum
{
Console.WriteLine("");
Console.WriteLine($"Select the {subject}");
Array values = Enum.GetValues(typeof(T));
for (int i = 0; i < values.Length; i++)
{
var current = values.GetValue(i) as Enum;
var meta = EnumHelper.GetAttributeOfType<CarrierMetaAttribute>(current);
string description = meta?.Name ?? Enum.GetName(typeof(T), current);
Console.WriteLine($"{i + 1}) {description}");
}
Console.WriteLine("");
Console.Write("Your choice:");
string optionPicked = Console.ReadLine().ToLower();
bool isValid = Enum.TryParse(optionPicked, true, out T result);
if (!isValid || !Enum.IsDefined(typeof(T), result))
{
Console.WriteLine("Please select a valid option");
return MenuEnum<T>(subject);
}
return result;
}
#endregion
#region Chuck Norris Jokes
static void RandomChuckNorrisJoke()
{
string content = string.Empty;
string url = "https://api.chucknorris.io/jokes/random";
using (var wc = new WebClient())
{
content = wc.DownloadString(url);
}
ChuckJokeModel chuckJoke = JsonSerializer.Deserialize<ChuckJokeModel>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(chuckJoke.Value);
Console.WriteLine();
}
#endregion
#region Bitcoin Prices
static void BitcoinPrices()
{
string content = string.Empty;
string bitUrl = "https://api.coindesk.com/v1/bpi/currentprice.json";
using (var wc = new WebClient())
{
content = wc.DownloadString(bitUrl);
}
BitcoinPrice bitFact = JsonSerializer.Deserialize<BitcoinPrice>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("As Of - " + bitFact.Time.Updated);
Console.WriteLine("USD - $ " + bitFact.Bpi.USD.Rate);
Console.WriteLine();
}
#endregion
#region Cat facts
static void CatFact()
{
string content = string.Empty;
string catUrl = "https://cat-fact.herokuapp.com/facts/random";
using (var wc = new WebClient())
{
content = wc.DownloadString(catUrl);
}
CatFactModel catFact = JsonSerializer.Deserialize<CatFactModel>(content);
Console.WriteLine();
if (catFact.Status != null && catFact.Status.Verified)
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(catFact.Text);
Console.WriteLine();
}
#endregion
#region People in space
static void Space()
{
string content = string.Empty;
string spacePeopleUrl = "http://api.open-notify.org/astros.json";
using (var wc = new WebClient())
{
content = wc.DownloadString(spacePeopleUrl);
}
SpacePersonModel spacePeopleFact = JsonSerializer.Deserialize<SpacePersonModel>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("There are " + spacePeopleFact.People.Count + " in space right now!");
foreach (SpacePerson spacePerson in spacePeopleFact.People)
{
Console.WriteLine(spacePerson.Name + " is in " + spacePerson.Craft);
}
Console.WriteLine();
}
#endregion
#region Country information
static void CountryInformation()
{
string content = string.Empty;
Console.WriteLine("");
Console.WriteLine("Please enter a country name:");
string countryName = Console.ReadLine().ToLower();
string url = $"https://restcountries.eu/rest/v2/name/{countryName}";
try
{
using (var wc = new WebClient())
{
content = wc.DownloadString(url);
}
var countryInformationResult = JsonSerializer.Deserialize<List<CountryInformation>>(content);
foreach (var item in countryInformationResult)
{
Console.WriteLine("===============================================");
Console.WriteLine($"Country Name: {item.Name}");
Console.WriteLine($"Capital: {item.Capital}");
Console.WriteLine($"Region: {item.Region}");
Console.WriteLine($"Population: {item.Population.ToString("N1")}");
Console.WriteLine($"Area: {item.Area.ToString("N1")} km²");
Console.WriteLine("Currencies");
foreach (var moneda in item.Currencies)
{
Console.WriteLine($"*Code:\t\t{moneda.Code}");
Console.WriteLine($"*Name:\t\t{moneda.Name}");
Console.WriteLine($"*Symbol:\t{moneda.Symbol}");
}
Console.WriteLine("Languages");
foreach (var language in item.Languages)
{
Console.WriteLine($"* Name:\t\t{language.Name} / {language.NativeName}");
}
Console.WriteLine("===============================================");
}
}
catch (WebException)
{
Console.WriteLine("Country not found");
}
}
#endregion
#region Discord Sender
static void DiscordWebhook(IOptions<SecretsModel> options)
{
string whook = options.Value.DiscordWebhook;
if (String.IsNullOrEmpty(whook))
{
Console.WriteLine("Whoops! You dont have a webhook defined in your config!"); return;
}
Console.Write("Enter the message:");
string msg = Console.ReadLine();
WebHookContent cont = new WebHookContent()
{
content = msg
};
string json = JsonSerializer.Serialize(cont);
using (var www = new HttpClient())
{
var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var task = www.PostAsync(whook, content);
task.Wait();
}
Console.WriteLine("Message sent!");
}
#endregion
#region Random quote
static void RandomQuote()
{
string content = string.Empty;
string quoteUrl = "https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(quoteUrl);
}
QuoteModel quote = JsonSerializer.Deserialize<QuoteModel>(content);
Console.WriteLine();
Console.WriteLine(quote.QuoteText);
Console.WriteLine($"--{quote.QuoteAuthor}");
Console.WriteLine();
}
#endregion
#region Random insult
static void RandomInsult()
{
string content = string.Empty;
string apiUrl = "https://evilinsult.com/generate_insult.php?lang=en&type=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(apiUrl);
}
EvilInsultModel insultResponse = JsonSerializer.Deserialize<EvilInsultModel>(content);
Console.WriteLine();
Console.WriteLine(HttpUtility.HtmlDecode(insultResponse.Insult));
Console.WriteLine();
}
#endregion
#region Who stole the cookie
static void CookieAccusation()
{
string content = string.Empty;
string suspectUrl = "https://randomuser.me/api/?inc=name&format=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(suspectUrl);
}
CookieSuspectModel cookieSuspect = JsonSerializer.Deserialize<CookieSuspectModel>(content);
string suspectFullName = cookieSuspect.results[0].name.first + " " + cookieSuspect.results[0].name.last;
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: " + suspectFullName + " stole the cookie from the cookie jar.");
Console.WriteLine("");
Console.WriteLine(suspectFullName + ": Who, me?");
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: Yes, you!");
Console.WriteLine("");
Console.WriteLine(suspectFullName + ": Couldn't be!");
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: Then who?");
Console.WriteLine();
}
#endregion
#region Random taco recipe
static void RandomTaco()
{
string content = string.Empty;
string tacoRandomizerUrl = "http://taco-randomizer.herokuapp.com/random/";
using (var wc = new WebClient())
{
content = wc.DownloadString(tacoRandomizerUrl);
}
TacoRecipeModel recipe = JsonSerializer.Deserialize<TacoRecipeModel>(content);
Console.WriteLine();
Console.WriteLine(@$"Why not try {recipe.BaseLayer.Name} with {recipe.Mixin.Name},");
Console.WriteLine(@$"seasoned with {recipe.Seasoning.Name}, topped off with {recipe.Condiment.Name}");
Console.WriteLine(@$"and wrapped in delicious {recipe.Shell.Name}.");
Console.WriteLine();
}
#endregion
#region Covid-19
static void Covid19()
{
Console.WriteLine("");
Console.WriteLine("Enter the country name to get the information. If you want to see the global information, type \"Global\". For the list of all countries type \"List\": ");
string userInput = Console.ReadLine();
Console.WriteLine("");
string content = string.Empty;
using (var wc = new WebClient())
{
content = wc.DownloadString("https://api.covid19api.com/summary");
}
CovidRoot summary = JsonSerializer.Deserialize<CovidRoot>(content);
if (userInput.StartsWith("List", StringComparison.InvariantCultureIgnoreCase))
{
userInput = userInput.Substring(4).TrimStart();
Console.WriteLine("Found countries: ");
foreach (var countryJson in summary.Countries)
{
if (countryJson.Country.StartsWith(userInput, StringComparison.InvariantCultureIgnoreCase))
Console.WriteLine(countryJson.Country);
}
return;
}
else if (userInput.Equals("Global", StringComparison.InvariantCultureIgnoreCase))
{
ShowCovidInfo("Global", summary.Global.NewConfirmed, summary.Global.TotalConfirmed, summary.Global.NewDeaths, summary.Global.TotalDeaths, summary.Global.NewRecovered, summary.Global.TotalRecovered);
return;
}
else
{
foreach (var countryJson in summary.Countries)
{
if (userInput.Equals(countryJson.Country, StringComparison.InvariantCultureIgnoreCase))
{
ShowCovidInfo(countryJson.Country, countryJson.NewConfirmed, countryJson.TotalConfirmed, countryJson.NewDeaths, countryJson.TotalDeaths, countryJson.NewRecovered, countryJson.TotalRecovered);
return;
}
}
}
Console.WriteLine("Country not found");
}
static void ShowCovidInfo(string countryName, long newConfirmed, long totalConfirmed, long newDeaths, long totalDeaths, long newRecovered, long totalRecovered)
{
Console.WriteLine("Statistics: " + countryName);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("New Confirmed: " + newConfirmed);
Console.WriteLine("Total Confirmed: " + totalConfirmed);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("New Deaths: " + newDeaths);
Console.WriteLine("Total Deaths: " + totalDeaths);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("New Recovered: " + newRecovered);
Console.WriteLine("Total Recovered: " + totalRecovered);
}
#endregion
#region GeekJokes
static void GeekJokes()
{
IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
string content = string.Empty;
string geekJokeUrl = "https://geek-jokes.sameerkumar.website/api?format=json";
using (var wc = new WebClient() { Proxy = defaultWebProxy })
{
content = wc.DownloadString(geekJokeUrl);
}
GeekJokeModel joke = JsonSerializer.Deserialize<GeekJokeModel>(content);
Console.WriteLine();
Console.WriteLine(@$"The Geek says -- {joke.Joke}");
Console.WriteLine();
}
#endregion
#region NumberFact
static void NumberFact()
{
string content = string.Empty;
int numberEntered;
Console.WriteLine("Please enter an integer number");
String userInput = Console.ReadLine();
if (int.TryParse(userInput, out numberEntered))
{
string numberFactURL = $"http://numbersapi.com/{userInput}?format=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(numberFactURL);
}
Console.WriteLine("Random fact for number " + userInput + ":");
Console.WriteLine(content);
}
else
{
Console.WriteLine("Input was not an integer");
}
}
#endregion
#region Random Advice
static void RandomAdvice()
{
string content = string.Empty;
string adviceUrl = "https://api.adviceslip.com/advice";
using (var wc = new WebClient())
{
content = wc.DownloadString(adviceUrl);
}
AdviceModel randomAdvice = JsonSerializer.Deserialize<AdviceModel>(content);
Console.WriteLine("Here's your advice:");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(randomAdvice.Slip.Advice);
Console.WriteLine();
}
#endregion
#region Space Station Location
static void SpaceStationLocation()
{
string content = string.Empty;
string adviceUrl = "http://api.open-notify.org/iss-now.json";
using (var wc = new WebClient())
{
content = wc.DownloadString(adviceUrl);
}
SpaceStationLocation spaceStation = JsonSerializer.Deserialize<SpaceStationLocation>(content);
Console.WriteLine("Woah! The International Space Station is currently at:");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($@"{spaceStation.Location.Latitude} Latitude and {spaceStation.Location.Longitude} Longitude");
Console.Write("That's so cool!");
Console.WriteLine();
}
#endregion
#region Random quote
static void RandomQuoteGarden()
{
IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
string content = string.Empty;
string randomQuoteUrl = "https://quote-garden.herokuapp.com/api/v2/quotes/random";
using (var wc = new WebClient() { Proxy = defaultWebProxy })
{
content = wc.DownloadString(randomQuoteUrl);
}
RandomQuoteModel quote = JsonSerializer.Deserialize<RandomQuoteModel>(content);
Console.WriteLine();
Console.WriteLine(@$"Quote -- {quote.Quote.Text}");
Console.WriteLine(@$"From -- {quote.Quote.Author}");
Console.WriteLine();
}
#endregion
#region Console Calculator
static void ConsoleCalculator()
{
// Declare variables and then initialize to zero.
int num1 = 0; int num2 = 0;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
// Ask the user to type the first number.
Console.WriteLine("Type a number, and then press Enter");
num1 = Convert.ToInt32(Console.ReadLine());
// Ask the user to type the second number.
Console.WriteLine("Type another number, and then press Enter");
num2 = Convert.ToInt32(Console.ReadLine());
// Ask the user to choose an option.
Console.WriteLine("Choose an option from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
// Use a switch statement to do the math.
switch (Console.ReadLine())
{
case "a":
Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
break;
case "s":
Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
break;
case "m":
Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
break;
case "d":
Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
break;
}
// Wait for the user to respond before closing.
Console.Write("Press any key to close the Calculator console app...");
Console.ReadKey();
}
#endregion
#region Gender from name
static void GenderFromName()
{
Console.Write("Type the name, and then press Enter: ");
string enteredName = Console.ReadLine().ToLower();
if (string.IsNullOrEmpty(enteredName))
{
Console.WriteLine("No name provided!");
Console.WriteLine();
return;
}
IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
string content = string.Empty;
string genderizatorUrl = $"https://api.genderize.io?name={enteredName}";
using (var wc = new WebClient() { Proxy = defaultWebProxy })
{
content = wc.DownloadString(genderizatorUrl);
}
GenderizatorModel genderResult = JsonSerializer.Deserialize<GenderizatorModel>(content);
Console.WriteLine();
Console.WriteLine(@$"The gender is {genderResult.Gender} with a probability of {genderResult.Probability}");
Console.WriteLine();
}
#endregion
#region Dad Joke
static void DadJoke()
{
WebRequest request = WebRequest.Create("https://icanhazdadjoke.com");
request.Headers.Add("User-Agent", "GitHub library https://github.com/aherd2985/UtilityBelt");
request.Headers.Add("Accept", "application/json");
// Get the response.
WebResponse response = request.GetResponse();
var status = ((HttpWebResponse)response).StatusCode;
if (status != HttpStatusCode.OK)
{
Console.WriteLine(@"There was an error retrieving the joke. Please try again later.");
}
else
{
using Stream dataStream = response.GetResponseStream();
if (dataStream != null)
{
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
DadJokeModel dadJoke = JsonSerializer.Deserialize<DadJokeModel>(responseFromServer);
Console.WriteLine(@"Random Dad Joke:");
Console.WriteLine(dadJoke.joke);
}
else
{
Console.WriteLine(@"The joke data is null. This is probably bad.");
}
}
// Close the response.
response.Close();
}
#endregion
#region Random pada fact
static void RandomPandaFact()
{
string content;
string url = "https://some-random-api.ml/facts/panda";
using (var wc = new WebClient())
{
content = wc.DownloadString(url);
}
PandaFactModel pandaFact = JsonSerializer.Deserialize<PandaFactModel>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(pandaFact.Fact);
Console.WriteLine();
}
#endregion
#region HostToIp
private static void HostToIp()
{
while (true)
{
Console.Write("Please enter a hostname: ");
var hostname = Console.ReadLine();
if (string.IsNullOrEmpty(hostname) && Uri.CheckHostName(hostname) != UriHostNameType.Unknown)
{
continue;
}
Console.WriteLine($"Hostname: {hostname}");
try
{
var ipAddresses = Dns.GetHostAddresses(hostname);
for (var i = 0; i < ipAddresses.Length; i++)
{
Console.WriteLine($"IP[{i + 1}]: {ipAddresses[i]}");
}
break;
}
catch (SocketException e)
{
Console.WriteLine("Invalid hostname - " + e.Message);
break;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
}
#endregion
#region Ghost Game
private static void GhostGame()
{
Random rNumber = new Random();
int score = 0;
int randomNum = rNumber.Next(0, 4);
int numberInput;