Exemplo n.º 1
0
        /// <summary>
        /// Search Reddit and return the results as mixed listings.
        /// Use this method if you're specifying multiple values for the "type" parameter.
        /// </summary>
        /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
        /// <param name="subreddit">The subreddit being searched</param>
        /// <returns>A listing of things that match the search criteria.</returns>
        public MultiSearchResults MultiSearch(SearchGetSearchInput searchGetSearchInput, string subreddit = null)
        {
            MixedListingContainer mix = GetSearch <MixedListingContainer>(searchGetSearchInput, subreddit);

            MultiSearchResults res = new MultiSearchResults();

            foreach (MixedListingChild mixedListingChild in mix.Data.Children)
            {
                switch (mixedListingChild.Kind)
                {
                case "t2":
                    res.Users.Add(JsonConvert.DeserializeObject <User>(JsonConvert.SerializeObject(mixedListingChild.Data)));
                    break;

                case "t3":
                    res.Posts.Add(JsonConvert.DeserializeObject <Post>(JsonConvert.SerializeObject(mixedListingChild.Data)));
                    break;

                case "t5":
                    res.Subreddits.Add(JsonConvert.DeserializeObject <Subreddit>(JsonConvert.SerializeObject(mixedListingChild.Data)));
                    break;
                }
            }

            res.First = mix?.Data?.Children?.First()?.Data?["name"]?.ToString();
            res.Last  = mix?.Data?.Children?.Last()?.Data?["name"]?.ToString();

            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Search all subreddits for posts.
        /// To search a specific subreddit for posts, use the Subreddit controller.
        /// </summary>
        /// <param name="q">A valid search query</param>
        /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance (optional)</param>
        /// <returns>A list of posts that match the search criteria.</returns>
        public List <Post> Search(string q, SearchGetSearchInput searchGetSearchInput = null)
        {
            if (searchGetSearchInput == null)
            {
                searchGetSearchInput = new SearchGetSearchInput();
            }

            searchGetSearchInput.q           = q;
            searchGetSearchInput.restrict_sr = false;

            return(Search(searchGetSearchInput));
        }
Exemplo n.º 3
0
        private List <Post> GetPosts(
            RedditClient reddit,
            string after,
            int limit,
            string query,
            int count)
        {
            var searchInput = new SearchGetSearchInput(
                q: query,
                after: after,
                limit: limit,
                count: count);

            return(reddit.Search(searchInput));
        }
Exemplo n.º 4
0
        private static List <Post> GetPosts(
            string query,
            string after  = null,
            string before = null,
            int limit     = 25,
            int count     = 0)
        {
            var reddit = new RedditClient(
                appId: "Mx2Rp1J2roDMdg",
                appSecret: "eDT3-0no1WHyTuBWTLoNDQNUqWA",
                refreshToken: "291925913345-DFbyOHX5f6zz-__Dqbr41jCOoPs");

            var searchInput = new SearchGetSearchInput(
                q: query,
                after: after,
                before: before,
                limit: limit,
                count: count);

            return(reddit.Search(searchInput));
        }
Exemplo n.º 5
0
        private void SearchAndPopulate(List <string> subreddits, string ticker, bool sorting = false, string sort = "relevance")
        {
            // If we are sorting, we have to empty our dictionarys as we getting all new data.
            if (sorting)
            {
                SearchModel.SavedData.PostDict               = new Dictionary <string, List <Post> >();
                SearchModel.SavedData.SubredditImages        = new Dictionary <string, string>();
                SearchModel.SavedData.SubredditColors        = new Dictionary <string, string>();
                SearchModel.SavedData.PostsTimePassedStrings = new Dictionary <Post, Tuple <string, string> >();
            }
            foreach (string sub in subreddits)
            {
                // Get current subreddit from api
                Subreddit currentSubreddit = SearchModel.SavedData.Reddit.Subreddit(name: sub);

                SearchGetSearchInput searchQuery = new SearchGetSearchInput(ticker, sort: sort);
                List <Post>          subPosts    = currentSubreddit.Search(searchQuery);
                SearchModel.SavedData.PostDict.Add(sub, subPosts);

                GetPostsTimePassed(subPosts);


                Subreddit currentSubredditInfo = currentSubreddit.About();

                string dirtyBackgroundImgUrl = currentSubredditInfo.CommunityIcon;
                string backgroundImgUrl;
                if (dirtyBackgroundImgUrl.Contains(".png"))
                {
                    backgroundImgUrl = dirtyBackgroundImgUrl.Substring(0, dirtyBackgroundImgUrl.LastIndexOf(".png") + 4);
                }
                else
                {
                    backgroundImgUrl = dirtyBackgroundImgUrl.Substring(0, dirtyBackgroundImgUrl.LastIndexOf(".jpg") + 4);
                }

                // Add to the subreddit images
                SearchModel.SavedData.SubredditImages.Add(sub, backgroundImgUrl);
                SearchModel.SavedData.SubredditColors.Add(sub, currentSubredditInfo.PrimaryColor);
            }
        }
Exemplo n.º 6
0
        private static async Task TestRedditAsync()
        {
            var reddit = new RedditClient(
                appId: "Mx2Rp1J2roDMdg",
                appSecret: "eDT3-0no1WHyTuBWTLoNDQNUqWA",
                refreshToken: "291925913345-DFbyOHX5f6zz-__Dqbr41jCOoPs");
            var indices = Enumerable.Range(0, 100);
            // ei33zr
            // ei37wh
            string after = null;
            var    limit = 50;
            //var before = "ei2mja";
            DateTime?before = null;
            var      query  = "university OR study OR studying OR college NOT football";
            // Since we only need the posts, there's no need to call .About() on this one.  --Kris
            int total = 0;

            //string MaxStr(string a, string b)
            //{
            //    var cmp = StringComparer.Create(CultureInfo.InvariantCulture, false);

            //    if (cmp.Compare(a, b) > 0)
            //    {
            //        return a;
            //    }
            //    return b;
            //}

            DateTime Max(DateTime a, DateTime?b)
            {
                if (!b.HasValue)
                {
                    return(a);
                }

                if (a < b.Value)
                {
                    return(b.Value);
                }
                return(a);
            }

            List <Post> get(
                RedditClient reddit,
                string after,
                int limit,
                string query,
                int count)
            {
                var searchInput = new SearchGetSearchInput(
                    q: query,
                    after: after,
                    //  before:before,
                    limit: limit,
                    count: count);

                return(reddit.Search(searchInput));
            }

            var subs = new ConcurrentDictionary <string, int>();

            while (true)
            {
                var maxBefore   = before;
                var count       = 0;
                var postListing = get(reddit, after, limit, query, count);
                var outDated    = false;
                while (postListing.Count > 0)
                {
                    var children = postListing;
                    foreach (var item in children)
                    {
                        if (item.Created <= before)
                        {
                            outDated = true;
                            Console.WriteLine("Outdated encountered");
                            break;
                        }

                        count++;
                        subs.AddOrUpdate(item.Subreddit, 1, (k, v) => v + 1);
                        maxBefore = Max(item.Created, maxBefore);
                        var title     = item.Title;
                        var text      = item.Listing.SelfText;
                        var subLength = 20000;
                        if (text.Length > subLength)
                        {
                            text = text.Substring(0, subLength);
                        }
                        Console.WriteLine($"{item.Fullname} {item.Listing.CreatedUTC}");
                        Console.WriteLine($"\t{title}");
                        Console.WriteLine($"\t{text}");
                        var comments = item.Comments.GetTop(100);
                        foreach (var(i, c) in indices.Zip(comments))
                        {
                            Console.WriteLine($"C-{$"{i:00}"}:\t{c.Body}");
                        }
                        Console.WriteLine(string.Concat(Enumerable.Range(0, 80).Select(r => "*")));
                    }
                    PrintDict(subs, total + count);
                    if (outDated)
                    {
                        Console.WriteLine("outdated");
                        break;
                    }
                    after = postListing.Count > 0 ? postListing.Last().Fullname : after;
                    Console.WriteLine($"after:{after}");
                    postListing = get(reddit, after, limit, query, count);
                }
                before = maxBefore;
                Console.WriteLine($"waiting: before; {before} after: {after}, c:{count} ");
                total += count;

                PrintDict(subs, total);
                after = null;
                count = 0;
                await Task.Delay(TimeSpan.FromSeconds(10));
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Search Reddit and return the results as user listings.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <param name="subreddit">The subreddit being searched</param>
 /// <returns>A listing of users that match the search criteria.</returns>
 public UserContainer SearchUsers(SearchGetSearchInput searchGetSearchInput, string subreddit = null)
 {
     searchGetSearchInput.type = "user";
     return(GetSearch <UserContainer>(searchGetSearchInput, subreddit));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Search Reddit and return the results as post listings.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <param name="subreddit">The subreddit being searched</param>
 /// <returns>A listing of posts that match the search criteria.</returns>
 public PostContainer SearchPosts(SearchGetSearchInput searchGetSearchInput, string subreddit = null)
 {
     searchGetSearchInput.type = "link";
     return(GetSearch <PostContainer>(searchGetSearchInput, subreddit));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Search Reddit and return the results as subreddit listings.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <param name="subreddit">The subreddit being searched</param>
 /// <returns>A listing of subreddits that match the search criteria.</returns>
 public SubredditContainer SearchSubreddits(SearchGetSearchInput searchGetSearchInput, string subreddit = null)
 {
     searchGetSearchInput.type = "sr";
     return(GetSearch <SubredditContainer>(searchGetSearchInput, subreddit));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Search links page.
 /// This endpoint is a listing.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <param name="subreddit">The subreddit being searched</param>
 /// <returns>A listing of things that match the search criteria.</returns>
 public T GetSearch <T>(SearchGetSearchInput searchGetSearchInput, string subreddit = null)
 {
     return(SendRequest <T>(Sr(subreddit) + "search", searchGetSearchInput));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Search all subreddits for posts.
 /// To search a specific subreddit for posts, use the Subreddit controller.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <returns>A list of posts that match the search criteria.</returns>
 public List <Post> Search(SearchGetSearchInput searchGetSearchInput)
 {
     searchGetSearchInput.restrict_sr = false;
     return(Account.Lists.GetPosts(Account.Validate(Models.Search.SearchPosts(searchGetSearchInput)), Models));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Search Reddit for matching things.
 /// This method can return links, subreddits, and/or users.  To include all of them, set type to "link,sr,user".
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <returns>A list of things that match the search criteria.</returns>
 public Things.MixedListingContainer MixedSearch(SearchGetSearchInput searchGetSearchInput)
 {
     return(Account.Validate(Models.Search.MultiSearch(searchGetSearchInput)));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Search Reddit for matching users.
 /// </summary>
 /// <param name="searchGetSearchInput">A valid SearchGetSearchInput instance</param>
 /// <returns>A list of users that match the search criteria.</returns>
 public List <User> SearchUsers(SearchGetSearchInput searchGetSearchInput)
 {
     searchGetSearchInput.restrict_sr = false;
     return(Account.Lists.GetUsers(Account.Validate(Models.Search.SearchUsers(searchGetSearchInput)), Models));
 }