Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using Newtonsoft.Json.Linq;
- namespace ConsoleApp1 {
- class Program {
- static Regex rXKCDAddress = new Regex(@"https://xkcd\.com/\d+", RegexOptions.Compiled);
- static Random r = new Random();
- static HttpClient c = new HttpClient();
- static int l = JObject.Parse(c.GetStringAsync("https://xkcd.com/info.0.json").Result).GetValue("num").Value<int>();
- static void Main() {
- while (true)
- Console.WriteLine(XKCD(Console.ReadLine()));
- }
- static string XKCD(string s) {
- if (int.TryParse(s, out int n) && n <= l)
- return "https://xkcd.com/" + n;
- if (string.IsNullOrWhiteSpace(s))
- return "https://xkcd.com/" + r.Next(1, l + 1);
- return rXKCDAddress.Match(c.GetStringAsync("https://www.google.com/search?source=hp&q=site%3Axkcd.com+-site%3Aforums.xkcd.com+-site%3Ablog.xkcd.com+-site%3Afora.xkcd.com+" + HttpUtility.HtmlEncode(s.Replace(' ', '+'))).Result).Value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment