fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Http;
  7. using System.Xml;
  8.  
  9. namespace GoogleRandomSuggestions
  10. {
  11. static class GoogleSuggestions
  12. {
  13. private const string url = "http://google.com/complete/search?q={0}&output=toolbar";
  14. static HttpClient http;
  15.  
  16. static GoogleSuggestions()
  17. {
  18. http = new HttpClient();
  19. }
  20.  
  21. public static IEnumerable<string> GetSuggestions(string search)
  22. {
  23. string xml = http.GetAsync(String.Format(url, Uri.EscapeUriString(search))).Result.Content.ReadAsStringAsync().Result;
  24. XmlDocument xdoc = new XmlDocument();
  25. bool stop = false;
  26. int attemptsLeft = 10;
  27. List<string> suggestions = new List<string>();
  28. while (!stop) {
  29. try {
  30. xdoc.LoadXml(xml);
  31. foreach (XmlNode node in xdoc.DocumentElement.ChildNodes) {
  32. XmlNode suggestion = node.ChildNodes[0];
  33. suggestions.Add(suggestion.Attributes["data"].Value);
  34. }
  35. stop = true;
  36. } catch (XmlException ex) {
  37. if (xml.Contains("automated")) {
  38. attemptsLeft--;
  39. if (attemptsLeft == 0) stop = true;
  40. Console.ForegroundColor = ConsoleColor.Red;
  41. Console.WriteLine(String.Format("Automated query error - {0} attempts remaining", attemptsLeft));
  42. Console.ForegroundColor = ConsoleColor.Gray;
  43. http.Dispose();
  44. System.Threading.Thread.Sleep(500);
  45. http = new HttpClient();
  46. } else {
  47. stop = true;
  48. throw ex;
  49. }
  50. }
  51. }
  52. return suggestions;
  53. }
  54. }
  55. }
  56.  
  57. namespace GoogleRandomSuggestions
  58. {
  59. class Program
  60. {
  61. static Random rng;
  62.  
  63. static Program()
  64. {
  65. rng = new Random();
  66. }
  67.  
  68. static bool StringBeginsWith(string str, string beginning)
  69. {
  70. if (str.Length < beginning.Length) return false;
  71. return str.Substring(0, beginning.Length).Equals(beginning);
  72. }
  73.  
  74. static string RandomPhrase(string start)
  75. {
  76. string phrase = start;
  77. bool stop = false;
  78. while (!stop) {
  79. string[] suggestions = GoogleSuggestions.GetSuggestions(phrase).Where(s => StringBeginsWith(s, phrase) && !s.Equals(phrase)).ToArray();
  80.  
  81. if (suggestions.Length == 0) {
  82. stop = true;
  83. } else if (suggestions.Length == 1) {
  84. phrase = suggestions[0];
  85. stop = true;
  86. } else {
  87. string choice = suggestions[rng.Next(suggestions.Length)];
  88. if (phrase.Length + 1 > choice.Length) {
  89. phrase = choice;
  90. stop = true;
  91. } else {
  92. phrase = choice.Substring(0, phrase.Length + 1);
  93. Console.ForegroundColor = ConsoleColor.White;
  94. Console.Write(phrase);
  95. Console.ForegroundColor = ConsoleColor.Gray;
  96. Console.WriteLine(choice.Substring(phrase.Length));
  97. }
  98. }
  99. }
  100. return phrase;
  101. }
  102.  
  103. static string RandomPhrase()
  104. {
  105. return RandomPhrase(Char.ConvertFromUtf32(rng.Next(65, 91)));
  106. }
  107.  
  108. static void Main(string[] args)
  109. {
  110. string input = "";
  111. while (true) {
  112. Console.Write("> ");
  113. string last = input;
  114. input = Console.ReadLine();
  115. if (input.Length == 0) input = last;
  116. Console.WriteLine();
  117. string phrase = RandomPhrase(input);
  118. Console.ForegroundColor = ConsoleColor.White;
  119. Console.WriteLine(phrase);
  120. Console.ForegroundColor = ConsoleColor.Gray;
  121. Console.WriteLine();
  122. }
  123. }
  124. }
  125. }
  126.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,24): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Are you missing an assembly reference?
prog.cs(6,18): error CS0234: The type or namespace name `Http' does not exist in the namespace `System.Net'. Are you missing an assembly reference?
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty