コード例 #1
0
ファイル: Program.cs プロジェクト: hunterjw/XkcdBrowser
        static void ComicExamples()
        {
            // Get the first comic
            Comic first = Xkcd.GetFirstComic();

            // Get a random comic
            Comic random = Xkcd.GetRandomComic();

            // Get the latest comic
            Comic latest = Xkcd.GetLatestComic();

            // Get a specific comic (by ID)
            Comic tenTwentyFour = Xkcd.GetComic(1024);

            // Get the next comic
            Comic tenTwentyFive = tenTwentyFour.Next();

            // Get the previous comic
            Comic tenTwentyThree = tenTwentyFour.Previous();

            // There's nothing before the first comic
            Comic badFirst = first.Previous();

            // ...and nothing after the latest
            Comic badLast = latest.Next();

            // Get the dictionary of comic archive entries, keyed on comic ID
            Dictionary <int, ComicArchiveEntry> comicDict = Xkcd.ComicDictionary;

            // Refresh the comic dictionary
            Xkcd.RefreshComicDictionary();
        }
コード例 #2
0
        /// <summary>
        /// Gets the previous comic
        /// </summary>
        /// <returns>Previous comic, or null if no previous comic</returns>
        public Comic Previous()
        {
            ComicArchiveEntry nextArchiveEntry = Xkcd.ComicDictionary.OrderByDescending(x => x.Key).SkipWhile(x => x.Key >= Id).FirstOrDefault().Value;

            if (nextArchiveEntry != null)
            {
                return(Xkcd.GetComic(nextArchiveEntry));
            }
            return(null);
        }
コード例 #3
0
        public static IMessageActivity HandleXkcdCommands(string text, IDialogContext context)
        {
            string xkcdUrlStart   = "https://xkcd.com/";
            string latestXkcdUrl  = xkcdUrlStart + "info.0.json";
            string completeString = "";
            var    replyMessage   = context.MakeMessage();

            WebClient wc = new WebClient();

            if (text.StartsWith("random"))
            {
                string json = wc.DownloadString(latestXkcdUrl);
                Xkcd   deserializedXkcdJson = JsonConvert.DeserializeObject <Xkcd>(json);
                int    maxNum = deserializedXkcdJson.Num;
                Random rnd    = new Random();
                int    rndNum = rnd.Next(1, maxNum);

                completeString = xkcdUrlStart + "/" + rndNum + "/info.0.json";
            }
            if (text.StartsWith("new"))
            {
                completeString += xkcdUrlStart + "info.0.json";
            }

            if (completeString.Length > 0)
            {
                string json = wc.DownloadString(completeString);
                Xkcd   deserializedXkcdJson = JsonConvert.DeserializeObject <Xkcd>(json);


                Attachment attachment = new Attachment
                {
                    ContentType = "image/jpg",
                    ContentUrl  = deserializedXkcdJson.Img,
                    Name        = deserializedXkcdJson.Title,
                };
                replyMessage.Attachments.Add(attachment);
                replyMessage.Text = String.Format("Title: {0} \r\nAlt: {1} \r\nUrl: {2}", deserializedXkcdJson.Title, deserializedXkcdJson.Alt, xkcdUrlStart +
                                                  deserializedXkcdJson.Num);
            }
            return(replyMessage);
        }
コード例 #4
0
 /// <summary>
 /// Comic for this archive entry
 /// </summary>
 public Comic Comic() => Xkcd.GetComic(this);