public ActionResult Index()
        {
            var vm = new HomeViewModel();

            vm.MinimumPosts = 3;
            using (var context = new MassTaggerContext())
            {
                var subs = new List <SubredditViewModel>();
                foreach (var subreddit in context.Subreddits.OrderBy(sub => sub.SubName))
                {
                    var count = context.SubredditUsers.Count(su => su.SubredditId == subreddit.id);
                    if (count > 0)
                    {
                        var svm = new SubredditViewModel
                        {
                            Id       = subreddit.id,
                            Name     = subreddit.SubName,
                            Ignored  = false,
                            TagColor = Util.Enum.TagColor.red
                        };
                        subs.Add(svm);
                    }
                }
                vm.Subreddits = subs;
            }
            return(View(vm));
        }
Example #2
0
        public static Dictionary <string, Tag> GetTags(IEnumerable <SubredditViewModel> subreddits, int minimumPosts)
        {
            var tags = new Dictionary <string, Tag>();

            foreach (var subreddit in subreddits)
            {
                using (var context = new MassTaggerContext())
                {
                    var users = context.SubredditUsers
                                .Include("Posts")
                                .Include("User")
                                .Where(su => su.SubredditId == subreddit.Id && su.Posts.Count >= minimumPosts);
                    foreach (var user in users)
                    {
                        if (!tags.ContainsKey("tag." + user.User.Username))
                        {
                            var tag = new Tag
                            {
                                link    = "/User/" + user.User.Username,
                                color   = subreddit.TagColor.ToString(),
                                ignored = subreddit.Ignored,
                                text    = "/r/" + subreddit.Name + " user"
                            };
                            tags.Add("tag." + user.User.Username, tag);
                        }
                    }
                }
            }
            return(tags);
        }
 private static void AddRecords(ConcurrentQueue <PostRecord> concurrentQueue)
 {
     while (true)
     {
         try
         {
             if (concurrentQueue.TryDequeue(out PostRecord pr))
             {
                 var user          = GetUser(pr.Username);
                 var subreddit     = GetSubreddit(pr.Subreddit);
                 var subredditUser = GetSubredditUser(subreddit, user);
                 var post          = new Post {
                     SubredditUserID = subredditUser.id, Url = pr.Url
                 };
                 using (var context = new MassTaggerContext())
                 {
                     context.Posts.Add(post);
                     context.SaveChanges();
                 }
             }
         }
         catch (Exception e)
         {
         }
     }
 }
Example #4
0
        // GET: User
        public ActionResult Index(string userId)
        {
            RedditUser user;

            using (MassTaggerContext context = new MassTaggerContext())
            {
                user = context.RedditUsers.Include("SubredditUsers.Subreddit").Include("SubredditUsers.Posts").FirstOrDefault(u => u.Username == userId);
            }
            if (user == null)
            {
                return(HttpNotFound());
            }
            return(View(user));
        }
 private static SubredditUser GetSubredditUser(Subreddit subreddit, RedditUser user)
 {
     using (var context = new MassTaggerContext())
     {
         var subUser = context.SubredditUsers.FirstOrDefault(su => su.UserId == user.id && su.SubredditId == subreddit.id);
         if (subUser == null)
         {
             subUser = new SubredditUser {
                 UserId = user.id, SubredditId = subreddit.id
             };
             context.SubredditUsers.Add(subUser);
             context.SaveChanges();
         }
         return(subUser);
     }
 }
 private static Subreddit GetSubreddit(string subName)
 {
     using (var context = new MassTaggerContext())
     {
         var subreddit = context.Subreddits.FirstOrDefault(s => s.SubName == subName);
         if (subreddit == null)
         {
             subreddit = new Subreddit {
                 SubName = subName
             };
             context.Subreddits.Add(subreddit);
             context.SaveChanges();
         }
         return(subreddit);
     }
 }
 private static RedditUser GetUser(string username)
 {
     using (var context = new MassTaggerContext())
     {
         var user = context.RedditUsers.FirstOrDefault(u => u.Username == username);
         if (user == null)
         {
             user = new RedditUser {
                 Username = username
             };
             context.RedditUsers.Add(user);
             context.SaveChanges();
         }
         return(user);
     }
 }
        private static void RegisterThreads()
        {
            var concurrentQueue = new ConcurrentQueue <PostRecord>();
            var reddit          = new Reddit();
            //var subredditNames = new List<string>();
            var sb = new StringBuilder();

            using (MassTaggerContext context = new MassTaggerContext())
            {
                foreach (var subreddit in context.Subreddits)
                {
                    sb.Append(subreddit.SubName + "+");
                }
            }
            try
            {
                var sub       = sb.ToString().Substring(0, sb.ToString().Length - 1);
                var subreddit = reddit.GetCombinedSubreddit(sub);
                if (subreddit != null)
                {
                    void commentStart() => MakeCommentListener(subreddit, concurrentQueue);
                    void postStart() => MakePostListener(subreddit, concurrentQueue);

                    var commentThread = new Thread(commentStart);
                    var postThread    = new Thread(postStart);
                    commentThread.Start();
                    postThread.Start();
                }
            }
            catch (Exception e) { }

            void addRecords() => AddRecords(concurrentQueue);

            var addThread = new Thread(addRecords);

            addThread.Start();
        }