예제 #1
0
        public static async Task <FicLink> SearchFor(string fic)
        {
            if (fic.Length < 4)
            {
                return(null);
            }

            try
            {
                var res = await MakeRequest(fic);

                for (int i = 0; i < 10; i++)
                {
                    var searchResult = ResultAt(res, i);

                    var url = await GetFinalRedirect(searchResult["url"].Value <string>());

                    var link = FicLink.From(url);

                    if (link != null)
                    {
                        return(link);
                    }
                }

                return(null);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error when search for " + fic);
                Console.WriteLine(e.ToString());
                return(null);
            }
        }
예제 #2
0
        private static async Task <Discord.Embed> GetStory(FicLink link, bool alreadyLinked, string forUser)
        {
            try
            {
                Story story;

                if (link.Network == FicNetwork.FFN)
                {
                    story = await FFNCrawler.CrawlStory(link.ID);
                }
                else if (link.Network == FicNetwork.AO3)
                {
                    story = await AO3Crawler.CrawlStory(link.ID);
                }
                else
                {
                    return(null);
                }

                string statusLine = "Published " + story.Published.ToShortDateString();
                if (story.Updated != null)
                {
                    statusLine += ", Updated " + ((DateTime)story.Updated).ToShortDateString();
                }

                statusLine += "  -  " + story.NumReviews + " Reviews, " + story.NumFavorites + " Favorites";

                string numWords = story.NumWords.ToString("N0", CultureInfo.InvariantCulture).Replace(",", "'");


                var numRecs = RecCounter.GetNumRecommendations(story);

                if (forUser != null)
                {
                    numRecs = RecCounter.Recommend(story, forUser);
                }

                var desc = story.Description;

                List <string> descriptionLines = new List <string>();

                while (desc.Length > 100)
                {
                    var lastSpaceRight = desc.LastIndexOf(' ', 100);
                    if (lastSpaceRight == -1)
                    {
                        lastSpaceRight = desc.IndexOf(' ');
                        if (lastSpaceRight == -1)
                        {
                            descriptionLines.Add(desc);
                            break;
                        }
                    }
                    descriptionLines.Add(desc.Substring(0, lastSpaceRight));
                    desc = desc.Substring(lastSpaceRight + 1);
                }

                if (desc.Length > 0)
                {
                    descriptionLines.Add(desc);
                }

                string description = Clean(story.Description);
                var    updated     = story.Updated ?? story.Published;

                var builder = new Discord.EmbedBuilder();
                builder.WithAuthor(story.Author.PenName, url: story.Author.Url);
                builder.Title       = story.Title;
                builder.Url         = story.Url;
                builder.Description = story.Description;

                var book = "";
                if (story.IsComplete || DateTime.Now.Subtract(updated).TotalDays < 14)
                {
                    book = ":green_book:";
                }
                else if (DateTime.Now.Subtract(updated).TotalDays < 180)
                {
                    book = ":orange_book:";
                }
                else
                {
                    book = ":closed_book:";
                }

                var updateString = AgoString(updated) + (story.IsComplete ? " - Complete!" : "");

                builder.AddInlineField(book + " Last Updated", updateString);

                builder.AddInlineField(":book: Length", $"{FormatBigNumber(story.NumWords)} words in {story.NumChapters} chapters");

                return(builder.Build());
            }
            catch (Exception e)
            {
                Console.WriteLine("Error when crawling story " + link.ID + " at " + link.Network);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(null);
            }
        }
예제 #3
0
        private async static Task <Discord.Embed> GetResponse(string message, string user)
        {
            var networkRegex = new Regex(@"^`([^`]+)`|[^`]+`([^`]+)`|!l\(([^)]+)\)|link\(([^)]+)\)", RegexOptions.IgnoreCase);
            var ffnRegex     = new Regex(@"linkffn\(([^)]+)\)", RegexOptions.IgnoreCase);
            var ao3Regex     = new Regex(@"linkao3\(([^)]+)\)", RegexOptions.IgnoreCase);

            var linkRegex = new Regex(@"https?:\/\/[^\s]+", RegexOptions.IgnoreCase);

            // First, let's look at directly linked stories:
            var ficLink = FicLink.From(message);

            if (ficLink != null)
            {
                return(await GetStory(ficLink, true, user));
            }

            // Now, let's look for linkffn(search) and the others.
            if (networkRegex.IsMatch(message))
            {
                var match = GetValue(networkRegex.Match(message));
                var fic   = await Search.SearchFor(match + " fanfiction");

                if (fic != null)
                {
                    return(await GetStory(fic, false, user));
                }
            }

            if (ffnRegex.IsMatch(message))
            {
                var match = GetValue(ffnRegex.Match(message));

                int tmp;
                if (int.TryParse(match, out tmp))
                {
                    return(await GetStory(new FicLink
                    {
                        ID = match,
                        Network = FicNetwork.FFN
                    }, false, user));
                }

                var fic = await Search.SearchFor(match + " site:fanfiction.net");

                if (fic != null)
                {
                    return(await GetStory(fic, false, user));
                }
            }

            if (ao3Regex.IsMatch(message))
            {
                var match = GetValue(ao3Regex.Match(message));

                int tmp;
                if (int.TryParse(match, out tmp))
                {
                    return(await GetStory(new FicLink
                    {
                        ID = match,
                        Network = FicNetwork.AO3
                    }, false, user));
                }

                var fic = await Search.SearchFor(match + " site:archiveofourown.org");

                if (fic != null)
                {
                    return(await GetStory(fic, false, user));
                }
            }

            return(null);
        }