-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathGet Post From Permalink.cs
More file actions
34 lines (29 loc) · 914 Bytes
/
Copy pathGet Post From Permalink.cs
File metadata and controls
34 lines (29 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Reddit;
using Reddit.Controllers;
using System;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
public class GetPost
{
private RedditClient Reddit { get; set; }
public GetPost(string appId, string refreshToken)
{
Reddit = new RedditClient(appId, refreshToken);
}
public Post FromPermalink(string permalink)
{
// Get the ID from the permalink, then preface it with "t3_" to convert it to a Reddit fullname. --Kris
Match match = Regex.Match(permalink, @"\/comments\/([a-z0-9]+)\/");
string postFullname = "t3_" + (match != null && match.Groups != null && match.Groups.Count >= 2
? match.Groups[1].Value
: "");
if (postFullname.Equals("t3_"))
{
throw new Exception("Unable to extract ID from permalink.");
}
// Retrieve the post and return the result. --Kris
return Reddit.Post(Reddit.Models, postFullname).About();
}
}
}