-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlurkHelper.cs
201 lines (167 loc) · 6.74 KB
/
PlurkHelper.cs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Specialized;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace RenRen.Plurk
{
/// <summary>
/// Provides functionalities of interacting with Plurk.
/// </summary>
public sealed class PlurkHelper
{
#region "Constructor"
public PlurkHelper()
{
instance = new OAuthInstance();
// You can assign your token here if you aren't making an interactive client
// e.g. instance.Token = new OAuthToken(content, secret, OAuthTokenType.Permanent);
}
#endregion
#region "Private Fields"
private OAuthInstance instance;
#endregion
#region "Properties"
/// <summary>
/// Gets the OAuth client implementation used by this instance.
/// </summary>
public IOAuthClient Client
{
get { return instance; }
}
#endregion
#region "Timeline/"
public Entities.GetPlurkResponse GetPlurk(long plurk_id)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("plurk_id", plurk_id.ToString());
string req = instance.SendRequest("Timeline/getPlurk", nvc);
return CreateEntity<Entities.GetPlurkResponse>(req);
}
public Entities.GetPlurksResponse GetUnreadPlurks()
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("limit", "200");
string req = instance.SendRequest("Timeline/getUnreadPlurks", nvc);
return CreateEntity<Entities.GetPlurksResponse>(req);
}
public Entities.GetPlurksResponse GetPublicPlurks(int userId, DateTime offset,
PlurkType type = PlurkType.All, int limit = 20)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("user_id", userId.ToString());
nvc.Add("limit", limit.ToString());
if (offset <= DateTime.Now)
nvc.Add("offset", offset.ToString("yyyy-MM-ddTHH:mm:ss"));
switch (type) {
case PlurkType.MyPlurks:
nvc.Add("filter", "only_user"); break;
case PlurkType.RespondedPlurks:
nvc.Add("filter", "only_responded"); break;
case PlurkType.PrivatePlurks:
nvc.Add("filter", "only_private"); break;
case PlurkType.FavoritePlurks:
nvc.Add("filter", "only_favorite"); break;
}
string req = instance.SendRequest("Timeline/getPublicPlurks", nvc);
return CreateEntity<Entities.GetPlurksResponse>(req);
}
public void AddPlurk(string qualifier, string message)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("qualifier", qualifier);
nvc.Add("content", message);
string req = instance.SendRequest("Timeline/plurkAdd", nvc);
}
public void MutePlurks(long[] plurk_ids)
{
NameValueCollection nvc = new NameValueCollection();
StringBuilder sb = new StringBuilder();
string prefix = "[";
foreach (long id in plurk_ids)
{ sb.Append(prefix).Append(id); prefix = ","; }
sb.Append("]");
nvc.Add("ids", sb.ToString());
string req = instance.SendRequest("Timeline/mutePlurks", nvc);
}
#endregion
#region "Responses/"
public void AddResponse(long plurk_id, string qualifier, string message)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("qualifier", qualifier);
nvc.Add("content", message);
nvc.Add("plurk_id", plurk_id.ToString());
string req = instance.SendRequest("Responses/responseAdd", nvc);
}
public Entities.GetResponseResponse GetResponses(long plurk_id)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("plurk_id", plurk_id.ToString());
string req = instance.SendRequest("Responses/get", nvc);
return CreateEntity<Entities.GetResponseResponse>(req);
}
#endregion
#region "FriendsFans/"
public IEnumerator<Entities.User> EnumerateFriends(int userId)
{
int offset = 0;
do
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("user_id", userId.ToString());
nvc.Add("limit", "25");
if (offset > 0)
nvc.Add("offset", offset.ToString());
string req = instance.SendRequest("FriendsFans/getFriendsByOffset", nvc);
Entities.User[] users = new Entities.User[] {};
users = CreateEntity<Entities.User[]>(req);
offset += users.Length;
if (users.Length <= 0) break;
foreach (Entities.User u in users)
yield return u;
} while (offset > 0);
}
#endregion
#region "Private Functions"
private string SendAPIRequest(string uri, NameValueCollection param)
{
try
{
return instance.SendRequest(uri, param);
}
catch (OAuthRequestException ex)
{
try
{
Entities.ErrorResponse err =
JsonConvert.DeserializeObject<Entities.ErrorResponse>(ex.ResponseData);
if (err.error_text == "Plurk not found")
throw new PlurkNotFoundException();
else if (err.error_text == "No permissions")
throw new PlurkPermissionException();
else if (err.error_text.StartsWith("anti-flood-"))
throw new PlurkFloodException();
else
throw new PlurkException(
String.Format("Plurk rejected the request due to {0}.", err.error_text));
}
catch (JsonSerializationException) {} throw;
}
}
private T CreateEntity<T>(string jsonString)
{
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (JsonSerializationException ex)
{
var err = new InvalidCastException("An error occured parsing JSON", ex);
err.Data.Add("RequestEntity", Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString)));
throw err;
}
}
#endregion
}
}