Exemple #1
0
        public RedditUser GetUser(string name)
        {
            var request  = _webAgent.CreateGet(string.Format(UserInfoUrl, name));
            var response = request.GetResponse();
            var result   = _webAgent.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(result);

            return(new RedditUser().Init(this, json, _webAgent));
        }
        public AuthenticatedUser GetUser(string accessToken)
        {
            var request = _webAgent.CreateGet(OauthGetMeUrl);

            request.Headers["Authorization"] = String.Format("bearer {0}", accessToken);
            var response  = (HttpWebResponse)request.GetResponse();
            var result    = _webAgent.GetResponseString(response.GetResponseStream());
            var thingjson = "{\"kind\": \"t2\", \"data\": " + result + "}";
            var json      = JObject.Parse(thingjson);

            return(new AuthenticatedUser().Init(new Reddit(), json, _webAgent));
        }
        public static IEnumerable<TBUserNote> GetUserNotes(IWebAgent webAgent, string subName)
        {
            var request = webAgent.CreateGet(String.Format(ToolBoxUserNotesWiki, subName));
            var reqResponse = webAgent.ExecuteRequest(request);
            var response = JObject.Parse(reqResponse["data"]["content_md"].Value<string>());

            int version = response["ver"].Value<int>();
            string[] mods = response["constants"]["users"].Values<string>().ToArray();

            string[] warnings = response["constants"]["warnings"].Values<string>().ToArray();

            if (version < 6) throw new ToolBoxUserNotesException("Unsupported ToolBox version");

            try
            {
                var data = Convert.FromBase64String(response["blob"].Value<string>());

                string uncompressed;
                using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data))
                {
                    compressedStream.ReadByte();
                    compressedStream.ReadByte(); //skips first to bytes to fix zlib block size
                    using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        using (var decompressedReader = new System.IO.StreamReader(blobStream))
                        {
                            uncompressed = decompressedReader.ReadToEnd();
                        }

                    }
                }

                JObject users = JObject.Parse(uncompressed);

                List<TBUserNote> toReturn = new List<TBUserNote>();
                foreach (KeyValuePair<string, JToken> user in users)
                {
                    var x = user.Value;
                    foreach (JToken note in x["ns"].Children())
                    {

                        TBUserNote uNote = new TBUserNote();
                        uNote.AppliesToUsername = user.Key;
                        uNote.SubName = subName;
                        uNote.SubmitterIndex = note["m"].Value<int>();
                        uNote.Submitter = mods[uNote.SubmitterIndex];
                        uNote.NoteTypeIndex = note["w"].Value<int>();
                        uNote.NoteType = warnings[uNote.NoteTypeIndex];
                        uNote.Message = note["n"].Value<string>();
                        uNote.Timestamp = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value<long>());
                        uNote.Url = UnsquashLink(subName, note["l"].ValueOrDefault<string>());

                        toReturn.Add(uNote);
                    }
                }
                return toReturn;
            }
            catch (Exception e)
            {
                throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e);
            }
        }
        public static IEnumerable <TBUserNote> GetUserNotes(IWebAgent webAgent, string subName)
        {
            var request     = webAgent.CreateGet(string.Format(ToolBoxUserNotesWiki, subName));
            var reqResponse = webAgent.ExecuteRequest(request);
            var response    = JObject.Parse(reqResponse["data"]["content_md"].Value <string>());

            int version = response["ver"].Value <int>();

            string[] mods = response["constants"]["users"].Values <string>().ToArray();

            string[] warnings = response["constants"]["warnings"].Values <string>().ToArray();

            if (version < 6)
            {
                throw new ToolBoxUserNotesException("Unsupported ToolBox version");
            }

            try
            {
                var data = Convert.FromBase64String(response["blob"].Value <string>());

                string uncompressed;
                using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data))
                {
                    compressedStream.ReadByte();
                    compressedStream.ReadByte(); //skips first to bytes to fix zlib block size
                    using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        using (var decompressedReader = new System.IO.StreamReader(blobStream))
                        {
                            uncompressed = decompressedReader.ReadToEnd();
                        }
                    }
                }

                JObject users = JObject.Parse(uncompressed);

                List <TBUserNote> toReturn = new List <TBUserNote>();
                foreach (KeyValuePair <string, JToken> user in users)
                {
                    var x = user.Value;
                    foreach (JToken note in x["ns"].Children())
                    {
                        TBUserNote uNote = new TBUserNote();
                        uNote.AppliesToUsername = user.Key;
                        uNote.SubName           = subName;
                        uNote.SubmitterIndex    = note["m"].Value <int>();
                        uNote.Submitter         = mods[uNote.SubmitterIndex];
                        uNote.NoteTypeIndex     = note["w"].Value <int>();
                        uNote.NoteType          = warnings[uNote.NoteTypeIndex];
                        uNote.Message           = note["n"].Value <string>();
                        uNote.Timestamp         = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value <long>());
                        uNote.Url = UnsquashLink(subName, note["l"].ValueOrDefault <string>());

                        toReturn.Add(uNote);
                    }
                }
                return(toReturn);
            }
            catch (Exception e)
            {
                throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e);
            }
        }