Skip to content

Commit f156a04

Browse files
committed
Add support for Reddit live.
1 parent f0e363e commit f156a04

File tree

5 files changed

+928
-3
lines changed

5 files changed

+928
-3
lines changed

RedditSharp/Reddit.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class Reddit
3737
private const string GoldSubredditsUrl = "/subreddits/gold.json";
3838
private const string DefaultSubredditsUrl = "/subreddits/default.json";
3939
private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}";
40-
40+
private const string CreateLiveEventUrl = "/api/live/create";
41+
private const string GetLiveEventUrl = "https://www.reddit.com/live/{0}/about";
4142

4243
#endregion
4344

@@ -261,7 +262,7 @@ public Domain GetDomain(string domain)
261262
return new Domain(this, uri, WebAgent);
262263
}
263264

264-
public JToken GetToken(Uri uri)
265+
public JToken GetToken(Uri uri,bool isLive = false)
265266
{
266267
var url = uri.AbsoluteUri;
267268

@@ -273,14 +274,69 @@ public JToken GetToken(Uri uri)
273274
var data = WebAgent.GetResponseString(response.GetResponseStream());
274275
var json = JToken.Parse(data);
275276

276-
return json[0]["data"]["children"].First;
277+
if (isLive)
278+
return json;
279+
else
280+
return json[0]["data"]["children"].First;
277281
}
278282

279283
public Post GetPost(Uri uri)
280284
{
281285
return new Post().Init(this, GetToken(uri), WebAgent);
282286
}
283287

288+
/// <summary>
289+
/// Create a Reddit Live thread.
290+
/// </summary>
291+
/// <param name="title">Required.</param>
292+
/// <param name="description">Required</param>
293+
/// <param name="resources"></param>
294+
/// <param name="nsfw"></param>
295+
/// <returns></returns>
296+
public LiveUpdateEvent CreateLiveEvent(string title,string description,string resources = "", bool nsfw = false)
297+
{
298+
if (String.IsNullOrEmpty(title))
299+
throw new ArgumentException(nameof(title));
300+
301+
if (String.IsNullOrEmpty(description))
302+
throw new ArgumentException(nameof(description));
303+
304+
var request = WebAgent.CreatePost(CreateLiveEventUrl);
305+
WebAgent.WritePostBody(request.GetRequestStream(), new
306+
{
307+
api_type = "json",
308+
title = title,
309+
description = description,
310+
resources = resources,
311+
nsfw = nsfw
312+
});
313+
var response = request.GetResponse();
314+
var result = WebAgent.GetResponseString(response.GetResponseStream());
315+
var json = JObject.Parse(result);
316+
317+
if (json["json"]["errors"].Any())
318+
throw new Exception(json["json"]["errors"][0][0].ToString());
319+
320+
var id = json["json"]["data"]["id"].ToString();
321+
322+
return GetLiveEvent(new Uri(String.Format(GetLiveEventUrl, id)));
323+
}
324+
325+
/// <summary>
326+
/// Get a reddit live thread.
327+
/// </summary>
328+
/// <param name="uri">Uri of the live thread.</param>
329+
/// <returns></returns>
330+
public LiveUpdateEvent GetLiveEvent(Uri uri)
331+
{
332+
if (!uri.AbsoluteUri.EndsWith("about"))
333+
uri = new Uri(uri.AbsoluteUri + "/about");
334+
335+
var token = GetToken(uri,true);
336+
return new LiveUpdateEvent().Init(this, token, WebAgent);
337+
}
338+
339+
284340
/// <summary>
285341
///
286342
/// </summary>

RedditSharp/RedditSharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
<ItemGroup>
5151
<Compile Include="BotWebAgent.cs" />
5252
<Compile Include="Flairs\FlairTemplate.cs" />
53+
<Compile Include="Things\LiveUpdateEvent.cs" />
54+
<Compile Include="Things\LiveUpdate.cs" />
5355
<Compile Include="Moderator Actions\ModActionType.cs" />
5456
<Compile Include="Multi\MData.cs" />
5557
<Compile Include="Multi\Multi.cs" />

RedditSharp/Things/LiveUpdate.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Net;
7+
using System.Net.WebSockets;
8+
using System.Security.Authentication;
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Linq;
11+
12+
namespace RedditSharp.Things
13+
{
14+
public class LiveUpdate : CreatedThing
15+
{
16+
private const string StrikeUpdateUrl = "/api/live/{0}/strike_update";
17+
private const string DeleteUpdateUrl = "/api/live/{0}/delete_update";
18+
19+
[JsonProperty("body")]
20+
public string Body { get; set; }
21+
22+
[JsonProperty("body_html")]
23+
public string BodyHtml { get; set; }
24+
25+
[JsonProperty("name")]
26+
public string Name { get; set; }
27+
28+
[JsonProperty("mobile_embeds")]
29+
public ICollection<MobileEmbed> MobileEmbeds { get; set; }
30+
31+
[JsonProperty("author")]
32+
public string Author { get; set; }
33+
34+
[JsonProperty("embeds")]
35+
public ICollection<Embed> Embeds { get; set; }
36+
37+
[JsonProperty("stricken")]
38+
public bool IsStricken { get; set; }
39+
40+
[JsonIgnore]
41+
private Reddit Reddit { get; set; }
42+
43+
[JsonIgnore]
44+
private IWebAgent WebAgent { get; set; }
45+
46+
public void Strike()
47+
{
48+
SimpleAction(StrikeUpdateUrl);
49+
}
50+
51+
public void Delete()
52+
{
53+
SimpleAction(DeleteUpdateUrl);
54+
}
55+
56+
public async Task<LiveUpdate> InitAsync(Reddit reddit, JToken post, IWebAgent webAgent)
57+
{
58+
CommonInit(reddit, post, webAgent);
59+
await JsonConvert.PopulateObjectAsync(post["data"].ToString(), this, reddit.JsonSerializerSettings);
60+
return this;
61+
}
62+
63+
public LiveUpdate Init(Reddit reddit, JToken post, IWebAgent webAgent)
64+
{
65+
CommonInit(reddit, post, webAgent);
66+
JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings);
67+
return this;
68+
}
69+
70+
private void CommonInit(Reddit reddit, JToken json, IWebAgent webAgent)
71+
{
72+
base.Init(json);
73+
Reddit = reddit;
74+
WebAgent = webAgent;
75+
}
76+
77+
public void SimpleAction(string url)
78+
{
79+
if (Reddit.User == null)
80+
throw new AuthenticationException("No user logged in.");
81+
var request = WebAgent.CreatePost(String.Format(url, Name));
82+
var stream = request.GetRequestStream();
83+
WebAgent.WritePostBody(stream, new
84+
{
85+
api_type = "json",
86+
id = Name,
87+
uh = Reddit.User.Modhash
88+
});
89+
stream.Close();
90+
var response = request.GetResponse();
91+
var data = WebAgent.GetResponseString(response.GetResponseStream());
92+
}
93+
94+
public class MobileEmbed
95+
{
96+
[JsonProperty("provider_url")]
97+
public string ProviderUrl { get; set; }
98+
99+
[JsonProperty("description")]
100+
public string Description { get; set; }
101+
102+
[JsonProperty("original_url")]
103+
public string Original_Url { get; set; }
104+
105+
[JsonProperty("url")]
106+
public string Url { get; set; }
107+
108+
[JsonProperty("title")]
109+
public string Title { get; set; }
110+
111+
[JsonProperty("thumbnail_width")]
112+
public int ThumbnailWidth { get; set; }
113+
114+
[JsonProperty("thumbnail_height")]
115+
public int ThumbnailHeight { get; set; }
116+
117+
[JsonProperty("thumbnail_url")]
118+
public string ThumbnailUrl { get; set; }
119+
120+
[JsonProperty("author_name")]
121+
public string AuthorName { get; set; }
122+
123+
[JsonProperty("version")]
124+
public string Version { get; set; }
125+
126+
[JsonProperty("provider_name")]
127+
public string ProviderName { get; set; }
128+
129+
[JsonProperty("type")]
130+
public string Type { get; set; }
131+
132+
[JsonProperty("author_url")]
133+
public string AuthorUrl { get; set; }
134+
}
135+
136+
public class Embed
137+
{
138+
[JsonProperty("url")]
139+
public string AuthorUrl { get; set; }
140+
141+
[JsonProperty("width")]
142+
public int Width { get; set; }
143+
144+
[JsonProperty("height")]
145+
public int Height { get; set; }
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)