示例#1
0
        private async Task <Listing> GetUserMultis(Listing listing)
        {
            var targetUri = string.Format("http://www.reddit.com/api/multi/mine.json");

            await ThrottleRequests();
            await EnsureRedditCookie();

            var subreddits = await _httpClient.GetStringAsync(targetUri);

            if (subreddits == "[]")
            {
                return(listing);
            }
            else
            {
                var userMultis = JsonConvert.DeserializeObject <Thing[]>(subreddits);
                foreach (var thing in userMultis)
                {
                    var labeledMulti = new TypedThing <LabeledMulti>(thing);
                    var multiPath    = labeledMulti.Data.Path;

                    multiPath = multiPath.Replace("/user/" + _userState.Username, "/me");

                    listing.Data.Children.Insert(0, (new Thing {
                        Kind = "t5", Data = new Subreddit {
                            DisplayName = labeledMulti.Data.Name, HeaderImage = "/Assets/multireddit.png", Title = labeledMulti.Data.Name, Url = multiPath, Headertitle = labeledMulti.Data.Name, Over18 = false
                        }
                    }));
                }
            }

            return(listing);
        }
示例#2
0
        private async Task <string> GetSubredditImpl(string name, string where, CancellationToken token, IProgress <float> progress)
        {
            name = name.ToLower();
            //no info for the front page
            if (name == "/")
            {
                return(JsonConvert.SerializeObject(new TypedThing <Subreddit>(new Thing {
                    Kind = "t5", Data = new Subreddit {
                        Headertitle = name
                    }
                })));
            }
            else if (name == "all" || name == "/r/all")
            {
                return(JsonConvert.SerializeObject(new TypedThing <Subreddit>(new Thing {
                    Kind = "t5", Data = new Subreddit {
                        Headertitle = "all", Url = "/r/all", Name = "all", DisplayName = "all", Title = "all", Id = "t5_fakeid"
                    }
                })));
            }

            if (!name.Contains("/m/"))
            {
                //this is passed in empty so we can get back the redirect url for things like /r/random
                var body      = new Dictionary <string, string>();
                var subreddit = await GetAuthedString(string.Format("/r/{0}/about{1}.json", MakePlainSubredditName(name), where != null ? "/" + where : ""), token, progress, body);

                if (body.ContainsKey("redirected-url"))
                {
                }
                //error page
                if (subreddit.ToLower().StartsWith("<!doctype html>"))
                {
                    throw new RedditNotFoundException(name);
                }
                else
                {
                    return(subreddit);
                }
            }
            else
            {
                if (name.StartsWith("/"))
                {
                    name = name.TrimStart('/');
                }

                if (name.StartsWith("me/"))
                {
                    name = name.Replace("me/", "user/" + _userState.Username + "/");
                }

                var description = await GetAuthedString(string.Format("/api/multi/{0}/description", name), token, progress);

                var labeledMulti = await GetAuthedString(string.Format("/api/multi/{0}/", name), token, progress);

                //error page
                if (description.ToLower().Contains("\"reason\": \"MULTI_NOT_FOUND\"") ||
                    labeledMulti.ToLower().Contains("\"reason\": \"MULTI_NOT_FOUND\""))
                {
                    throw new RedditNotFoundException(name);
                }
                else
                {
                    var typedMulti            = new TypedThing <LabeledMulti>(JsonConvert.DeserializeObject <Thing>(labeledMulti));
                    var typedMultiDescription = new TypedThing <LabeledMultiDescription>(JsonConvert.DeserializeObject <Thing>(labeledMulti));
                    var multiPath             = typedMulti.Data.Path;

                    if (!string.IsNullOrWhiteSpace(_userState.Username))
                    {
                        multiPath = multiPath.Replace("/user/" + _userState.Username, "/me");
                    }

                    return(JsonConvert.SerializeObject(new TypedThing <Subreddit>(new Thing {
                        Kind = "t5", Data = new Subreddit {
                            Description = typedMultiDescription.TypedData.BodyMD, DisplayName = typedMulti.Data.Name, Title = typedMulti.Data.Name, Url = multiPath, Headertitle = typedMulti.Data.Name, Over18 = false
                        }
                    })));
                }
            }
        }
示例#3
0
        public async Task <TypedThing <Subreddit> > GetSubreddit(string name)
        {
            //no info for the front page
            if (name == "/")
            {
                return(new TypedThing <Subreddit>(new Thing {
                    Kind = "t5", Data = new Subreddit {
                        Headertitle = name
                    }
                }));
            }
            else if (name == "all")
            {
                return(new TypedThing <Subreddit>(new Thing {
                    Kind = "t5", Data = new Subreddit {
                        Headertitle = "all", Url = "/r/all", Name = "all", DisplayName = "all", Title = "all", Id = "t5_fakeid"
                    }
                }));
            }

            string targetUri;

            if (!name.Contains("/m/"))
            {
                targetUri = string.Format("http://www.reddit.com/r/{0}/about.json", name);
                await ThrottleRequests();
                await EnsureRedditCookie();

                var subreddit = await _httpClient.GetStringAsync(targetUri);

                //error page
                if (subreddit.ToLower().StartsWith("<!doctype html>"))
                {
                    return(new TypedThing <Subreddit>(new Thing {
                        Kind = "t5", Data = new Subreddit {
                            Headertitle = name, Title = name, Url = string.Format("r/{0}", name), Created = DateTime.Now, CreatedUTC = DateTime.UtcNow, DisplayName = name, Description = "there doesnt seem to be anything here", Name = name, Over18 = false, PublicDescription = "there doesnt seem to be anything here", Subscribers = 0
                        }
                    }));
                }
                else
                {
                    return(new TypedThing <Subreddit>(JsonConvert.DeserializeObject <Thing>(subreddit)));
                }
            }
            else
            {
                if (name.StartsWith("/"))
                {
                    name = name.TrimStart('/');
                }

                if (name.StartsWith("me/"))
                {
                    name = name.Replace("me/", "user/" + _userState.Username + "/");
                }

                targetUri = string.Format("http://www.reddit.com/api/multi/{0}.json", name);
                await ThrottleRequests();
                await EnsureRedditCookie();

                var subreddit = await _httpClient.GetStringAsync(targetUri);

                //error page
                if (subreddit.ToLower().StartsWith("<!doctype html>"))
                {
                    throw new RedditNotFoundException(name);
                }
                else
                {
                    var labeledMulti = new TypedThing <LabeledMulti>(JsonConvert.DeserializeObject <Thing>(subreddit));
                    var multiPath    = labeledMulti.Data.Path;

                    if (!string.IsNullOrWhiteSpace(_userState.Username))
                    {
                        multiPath = multiPath.Replace("/user/" + _userState.Username, "/me");
                    }

                    return(new TypedThing <Subreddit>(new Thing {
                        Kind = "t5", Data = new Subreddit {
                            DisplayName = labeledMulti.Data.Name, Title = labeledMulti.Data.Name, Url = multiPath, Headertitle = labeledMulti.Data.Name, Over18 = false
                        }
                    }));
                }
            }
        }