예제 #1
0
        public void Generate(FunBookDbContext context)
        {
            if (!context.Users.Any())
            {
                throw new ArgumentException("Register at least one user first !");
            }

            var userId = context.Users.FirstOrDefault().Id;

            System.Net.WebClient client = new System.Net.WebClient();
            var endpoints = new string[] { "popular-jokes", "latest-jokes", "joke-of-the-day", "animal-jokes", "blonde-jokes",
                                           "boycott-these-jokes", "clean-jokes", "family-jokes", "holiday-jokes", "how-to-be-insulting", "insult-jokes", "miscellaneous-jokes",
                                           "national-jokes", "office-jokes", "political-jokes", "pop-culture-jokes", "racist-jokes", "relationship-jokes",
                                           "religious-jokes", "school-jokes", "science-jokes", "sex-jokes", "sexist-jokes", "sports-jokes", "technology-jokes", "word-play-jokes", "yo-momma-jokes" };

            var tagDict = new System.Collections.Generic.Dictionary <string, System.Collections.Generic.List <Joke> >();

            for (int j = 0; j < endpoints.Length; j++)
            {
                var    jokeCategory   = endpoints[j].Replace("-jokes", "");
                string downloadString = client.DownloadString(string.Format("http://www.laughfactory.com/jokes/{0}", endpoints[j]));
                var    jokes          = System.Text.RegularExpressions.Regex.Split(downloadString, @"<p id=""joke_\d+"">");

                context.Categories.Add(new Category()
                {
                    Name = jokeCategory
                });
                context.SaveChanges();

                var categoryId = context.Categories.FirstOrDefault(c => c.Name == jokeCategory).Id;

                for (int i = 1; i < jokes.Length; i++)
                {
                    var joke = jokes[i].TrimStart();
                    joke = joke.Substring(0, joke.IndexOf("      "));

                    var tags = System.Text.RegularExpressions.Regex.Split(joke, @"\W+");

                    var jokeObj = new Joke()
                    {
                        CategoryId  = categoryId,
                        UserId      = userId,
                        IsAnonymous = true,
                        Title       = joke.Split(' ').FirstOrDefault(t => t.Length > 2),
                        Text        = joke.Replace("<br>", ""),
                    };

                    foreach (var tag in tags)
                    {
                        if (!tagDict.ContainsKey(tag.ToLower()))
                        {
                            tagDict[tag.ToLower()] = new System.Collections.Generic.List <Joke>();
                        }

                        tagDict[tag.ToLower()].Add(jokeObj);
                    }

                    context.Jokes.Add(jokeObj);
                }

                context.SaveChanges();
            }

            var counter = 0;

            foreach (var kvPair in tagDict)
            {
                if (kvPair.Key.Length > 2)
                {
                    context.Tags.Add(new Tag()
                    {
                        Name  = kvPair.Key,
                        Jokes = kvPair.Value
                    });
                    counter++;
                    if (counter > 10)
                    {
                        counter = 0;
                        context.SaveChanges();
                    }
                }
            }

            context.SaveChanges();

            context.Categories.Add(new Category()
            {
                Name = "Other"
            });

            context.SaveChanges();
        }
예제 #2
0
        public void Generate(FunBookDbContext context)
        {
            if (!context.Users.Any())
            {
                throw new ArgumentException("Register at least one user first !");
            }

            if (!context.Jokes.Any() || !context.Categories.Any() || !context.Tags.Any())
            {
                throw new ArgumentException("Method needs already added jokes, categories and tags!");
            }

            var userId = context.Users.FirstOrDefault().Id;

            var uploader = new TelerikBackendServicesImageUpload();
            var allFiles = uploader.GetUploadedFiles();

            foreach (var file in allFiles)
            {
                var categoryName = file.Item1.Substring(0, file.Item1.IndexOf("#"));
                if (context.Categories.FirstOrDefault(c => c.Name == categoryName) == null)
                {
                    context.Categories.Add(new Category()
                    {
                        Name = categoryName
                    });
                    context.SaveChanges();
                }

                var categoryId = context.Categories.FirstOrDefault(c => c.Name == categoryName).Id;

                var hashIndex = file.Item1.IndexOf("#") + 1;
                var tagString = file.Item1.Substring(hashIndex, file.Item1.IndexOf("#", hashIndex) - hashIndex);

                var title = "Funny pic";

                var picObj = new Picture()
                {
                    CategoryId  = categoryId,
                    IsAnonymous = true,
                    Title       = title,
                    UrlPath     = file.Item2,
                    UserId      = userId
                };

                context.Pictures.Add(picObj);

                context.SaveChanges();

                var allTags = tagString.Split();

                foreach (var tag in allTags)
                {
                    if (context.Tags.FirstOrDefault(t => t.Name == tag.ToLower()) == null)
                    {
                        context.Tags.Add(new Tag()
                        {
                            Name = tag.ToLower()
                        });
                        context.SaveChanges();
                    }

                    context.Tags.FirstOrDefault(t => t.Name == tag.ToLower()).Pictures.Add(picObj);
                }

                context.SaveChanges();
            }
        }
예제 #3
0
        public void Generate(FunBookDbContext context)
        {
            if (!context.Users.Any())
            {
                throw new ArgumentException("Register at least one user first !");
            }

            if (!context.Jokes.Any() || !context.Categories.Any() || !context.Tags.Any())
            {
                throw new ArgumentException("Method needs already added jokes, categories and tags!");
            }

            var userId = context.Users.FirstOrDefault().Id;

            var titlesFromBase = context.Jokes.Select(j => j.Title);
            var titles         = new List <string>();

            foreach (var title in titlesFromBase)
            {
                var meaningfullTitle = title.IndexOf(" ") >= 0 ? title.Substring(0, title.IndexOf(" ")) : title;
                if (meaningfullTitle.Length > 3)
                {
                    titles.Add(meaningfullTitle);
                }
            }

            var categoryIds = context.Categories.Select(c => c.Id).ToList();

            WebClient client    = new WebClient();
            var       endpoints = new string[] { "http://imgfave.com/funny",
                                                 "http://imgfave.com/funny/page:2?after=1413928204",
                                                 "http://imgfave.com/funny/page:3?after=1413866704",
                                                 "http://imgfave.com/funny/page:4?after=1413842106",
                                                 "http://imgfave.com/funny/page:5?after=1413781205" };

            var linkString = @"<a class=""image_link"" href=""";

            foreach (var endpoint in endpoints)
            {
                var downloadString = client.DownloadString(endpoint);
                var URLs           = Regex.Split(downloadString, linkString);
                for (int i = 1; i < URLs.Length; i++)
                {
                    var title   = Regex.Replace(titles[(i - 1) % titles.Count], @"\W", "");
                    var link    = URLs[i].Substring(0, URLs[i].IndexOf("\"", linkString.Length + 1));
                    var linkObj = new Link()
                    {
                        CategoryId  = categoryIds[(i - 1) % categoryIds.Count],
                        UserId      = userId,
                        IsAnonymous = true,
                        Title       = title,
                        URL         = link
                    };

                    context.Links.Add(linkObj);

                    var tag = context.Tags.FirstOrDefault(t => t.Name == title.ToLower());
                    if (tag != null)
                    {
                        tag.Links.Add(linkObj);
                    }
                }

                context.SaveChanges();
            }
        }