|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.IO.Compression; |
| 8 | + |
| 9 | +namespace RedditSharp |
| 10 | +{ |
| 11 | + public static class ToolBoxUserNotes |
| 12 | + { |
| 13 | + private const string ToolBoxUserNotesWiki = "/r/{0}/wiki/usernotes"; |
| 14 | + public static IEnumerable<tbUserNote> GetUserNotes( IWebAgent webAgent, string subName) |
| 15 | + { |
| 16 | + var request = webAgent.CreateGet(String.Format(ToolBoxUserNotesWiki, subName)); |
| 17 | + var reqResponse = webAgent.ExecuteRequest(request); |
| 18 | + var response = JObject.Parse(reqResponse["data"]["content_md"].Value<string>()); |
| 19 | + |
| 20 | + int version = response["ver"].Value<int>(); |
| 21 | + string[] mods = response["constants"]["users"].Values<string>().ToArray(); |
| 22 | + |
| 23 | + string[] warnings = response["constants"]["warnings"].Values<string>().ToArray(); |
| 24 | + |
| 25 | + if (version < 6) |
| 26 | + { |
| 27 | + throw new ToolBoxUserNotesException("Unsupported ToolBox version"); |
| 28 | + } |
| 29 | + try { |
| 30 | + var data = Convert.FromBase64String(response["blob"].Value<string>()); |
| 31 | + |
| 32 | + string uncompressed; |
| 33 | + using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data)) |
| 34 | + { |
| 35 | + compressedStream.ReadByte(); |
| 36 | + compressedStream.ReadByte(); //skips first to bytes to fix zlib block size |
| 37 | + using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) |
| 38 | + { |
| 39 | + using (var decompressedReader = new System.IO.StreamReader(blobStream)) |
| 40 | + { |
| 41 | + uncompressed = decompressedReader.ReadToEnd(); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + JObject users = JObject.Parse(uncompressed); |
| 48 | + |
| 49 | + List<tbUserNote> toReturn = new List<tbUserNote>(); |
| 50 | + foreach (KeyValuePair<string, JToken> user in users) |
| 51 | + { |
| 52 | + var x = user.Value; |
| 53 | + foreach (JToken note in x["ns"].Children()) |
| 54 | + { |
| 55 | + |
| 56 | + tbUserNote uNote = new tbUserNote(); |
| 57 | + uNote.AppliesToUsername = user.Key; |
| 58 | + uNote.SubName = subName; |
| 59 | + uNote.SubmitterIndex = note["m"].Value<int>(); |
| 60 | + uNote.Submitter = mods[uNote.SubmitterIndex]; |
| 61 | + uNote.NoteTypeIndex = note["w"].Value<int>(); |
| 62 | + uNote.NoteType = warnings[uNote.NoteTypeIndex]; |
| 63 | + uNote.Message = note["n"].Value<string>(); |
| 64 | + uNote.Timestamp = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value<long>()); |
| 65 | + uNote.Url = UnsquashLink(subName, note["l"].ValueOrDefault<string>()); |
| 66 | + |
| 67 | + toReturn.Add(uNote); |
| 68 | + } |
| 69 | + } |
| 70 | + return toReturn; |
| 71 | + } |
| 72 | + catch(Exception e) |
| 73 | + { |
| 74 | + throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + public static string UnsquashLink(string subreddit,string permalink) |
| 79 | + { |
| 80 | + |
| 81 | + var link = "https://reddit.com/r/" + subreddit + "/"; |
| 82 | + if (string.IsNullOrEmpty(permalink)) { |
| 83 | + return link; |
| 84 | + } |
| 85 | + var linkParams = permalink.Split(','); |
| 86 | + |
| 87 | + if (linkParams[0] == "l") |
| 88 | + { |
| 89 | + link += "comments/" + linkParams[1] + "/"; |
| 90 | + if (linkParams.Length > 2) |
| 91 | + link += "-/" + linkParams[2] + "/"; |
| 92 | + } |
| 93 | + else if (linkParams[0] == "m") |
| 94 | + { |
| 95 | + link += "message/messages/" + linkParams[1]; |
| 96 | + } |
| 97 | + |
| 98 | + return link; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments