Skip to content

Commit 760e24d

Browse files
authored
Merge pull request #46 from pimanac/live
Add support for Reddit live.
2 parents 83a81ee + 5d29d02 commit 760e24d

File tree

5 files changed

+930
-2
lines changed

5 files changed

+930
-2
lines changed

RedditSharp/Reddit.cs

Lines changed: 61 additions & 2 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

@@ -292,6 +293,7 @@ public Domain GetDomain(string domain)
292293
return new Domain(this, uri, WebAgent);
293294
}
294295

296+
295297
/// <summary>
296298
/// Get a <see cref="JToken"/> from a url.
297299
/// </summary>
@@ -309,7 +311,10 @@ public JToken GetToken(Uri uri)
309311
var data = WebAgent.GetResponseString(response.GetResponseStream());
310312
var json = JToken.Parse(data);
311313

312-
return json[0]["data"]["children"].First;
314+
if (isLive)
315+
return json;
316+
else
317+
return json[0]["data"]["children"].First;
313318
}
314319

315320
/// <summary>
@@ -323,6 +328,60 @@ public Post GetPost(Uri uri)
323328
}
324329

325330
/// <summary>
331+
/// Create a Reddit Live thread.
332+
/// </summary>
333+
/// <param name="title">Required.</param>
334+
/// <param name="description">Required</param>
335+
/// <param name="resources"></param>
336+
/// <param name="nsfw"></param>
337+
/// <returns></returns>
338+
public LiveUpdateEvent CreateLiveEvent(string title,string description,string resources = "", bool nsfw = false)
339+
{
340+
if (String.IsNullOrEmpty(title))
341+
throw new ArgumentException(nameof(title));
342+
343+
if (String.IsNullOrEmpty(description))
344+
throw new ArgumentException(nameof(description));
345+
346+
var request = WebAgent.CreatePost(CreateLiveEventUrl);
347+
WebAgent.WritePostBody(request.GetRequestStream(), new
348+
{
349+
api_type = "json",
350+
title = title,
351+
description = description,
352+
resources = resources,
353+
nsfw = nsfw
354+
});
355+
var response = request.GetResponse();
356+
var result = WebAgent.GetResponseString(response.GetResponseStream());
357+
var json = JObject.Parse(result);
358+
359+
if (json["json"]["errors"].Any())
360+
throw new Exception(json["json"]["errors"][0][0].ToString());
361+
362+
var id = json["json"]["data"]["id"].ToString();
363+
364+
return GetLiveEvent(new Uri(String.Format(GetLiveEventUrl, id)));
365+
}
366+
367+
/// <summary>
368+
/// Get a reddit live thread.
369+
/// </summary>
370+
/// <param name="uri">Uri of the live thread.</param>
371+
/// <returns></returns>
372+
public LiveUpdateEvent GetLiveEvent(Uri uri)
373+
{
374+
if (!uri.AbsoluteUri.EndsWith("about"))
375+
uri = new Uri(uri.AbsoluteUri + "/about");
376+
377+
var token = GetToken(uri,true);
378+
return new LiveUpdateEvent().Init(this, token, WebAgent);
379+
}
380+
381+
382+
/// <summary>
383+
///
384+
326385
/// Compose a private message.
327386
/// </summary>
328387
/// <param name="subject">message subject</param>

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)