-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathPeriscopeServer.cs
411 lines (385 loc) · 14.5 KB
/
PeriscopeServer.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using SitePluginCommon;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace PeriscopeSitePlugin
{
public class PeriscopeServer : ServerBase, IDataServer
{
public async Task<string> GetAsync(string url, CookieContainer cc)
{
var result = await GetInternalAsync(new HttpOptions
{
Url = url,
Cc = cc,
});
var str = await result.Content.ReadAsStringAsync();
return str;
}
public async Task<string> GetAsync(string url, string userAgent, CookieContainer cc)
{
var result = await GetInternalAsync(new HttpOptions
{
Url = url,
Cc = cc,
UserAgent = userAgent,
});
var str = await result.Content.ReadAsStringAsync();
return str;
}
public async Task<string> GetAsync(string url, Dictionary<string, string> headers, CookieContainer cc)
{
var result = await GetInternalAsync(new HttpOptions
{
Url = url,
Cc = cc,
Headers = headers,
});
var str = await result.Content.ReadAsStringAsync();
return str;
}
public async Task<string> GetAsync(string url)
{
using (var client = new HttpClient())
{
var result = await client.GetStringAsync(url);
return result;
}
}
public async Task<string> PostJsonAsync(string url, Dictionary<string, string> headers, string json, CookieContainer cc)
{
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var result = await PostInternalAsync(new HttpOptions
{
Url = url,
Cc = cc,
Headers = headers,
}, content);
var str = await result.Content.ReadAsStringAsync();
return str;
}
public async Task<string> PostAsync(string url, Dictionary<string, string> data, CookieContainer cc)
{
var content = new FormUrlEncodedContent(data);
var result = await PostInternalAsync(new HttpOptions
{
Url = url,
Cc = cc,
}, content);
var str = await result.Content.ReadAsStringAsync();
return str;
}
}
}
//namespace PeriscopeSitePlugin
//{ /// <summary>
// ///
// /// </summary>
// /// <remarks>接続毎にインスタンスを作る</remarks>
// class MessageProvider
// {
// public event EventHandler Opened;
// public event EventHandler<Result> Received;
// WebSocket _ws;
// TaskCompletionSource<object> _tcs;
// public Task ReceiveAsync()
// {
// _tcs = new TaskCompletionSource<object>();
// var cookies = new List<KeyValuePair<string, string>>();
// _ws = new WebSocket("wss://irc-ws.chat.twitch.tv/", "", cookies);
// _ws.MessageReceived += _ws_MessageReceived;
// _ws.Opened += _ws_Opened;
// _ws.Error += _ws_Error;
// _ws.Closed += _ws_Closed;
// _ws.Open();
// return _tcs.Task;
// }
// private void _ws_Closed(object sender, EventArgs e)
// {
// _tcs.SetResult(null);
// }
// private void _ws_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
// {
// _tcs.SetException(e.Exception);
// }
// private void _ws_Opened(object sender, EventArgs e)
// {
// Opened?.Invoke(this, e);
// }
// public async Task SendAsync(string s)
// {
// Debug.WriteLine("send: " + s);
// await Task.Yield();
// _ws.Send(s + "\r\n");
// }
// private void _ws_MessageReceived(object sender, MessageReceivedEventArgs e)
// {
// var arr = e.Message.Split(new[] { "\r\n" }, StringSplitOptions.None);
// foreach (var message in arr)
// {
// if (string.IsNullOrEmpty(message))
// continue;
// var result = Tools.Parse(message);
// Received?.Invoke(this, result);
// }
// }
// public void Disconnect()
// {
// _ws?.Close();
// _ws = null;
// }
// public MessageProvider()
// {
// }
// }
// class PeriscopeCommentProvider : ICommentProvider
// {
// private bool _canConnect;
// public bool CanConnect
// {
// get { return _canConnect; }
// set
// {
// if (_canConnect == value)
// return;
// _canConnect = value;
// CanConnectChanged?.Invoke(this, EventArgs.Empty);
// }
// }
// private bool _canDisconnect;
// public bool CanDisconnect
// {
// get { return _canDisconnect; }
// set
// {
// if (_canDisconnect == value)
// return;
// _canDisconnect = value;
// CanDisconnectChanged?.Invoke(this, EventArgs.Empty);
// }
// }
// public event EventHandler<List<ICommentViewModel>> InitialCommentsReceived;
// public event EventHandler<ICommentViewModel> CommentReceived;
// public event EventHandler<IMetadata> MetadataUpdated;
// public event EventHandler CanConnectChanged;
// public event EventHandler CanDisconnectChanged;
// private string GetChannelName(string input)
// {
// return Tools.GetChannelName(input);
// }
// private string _channelName;
// private MessageProvider _provider;
// //private CookieContainer _cc;
// public async Task ConnectAsync(string input, IBrowserProfile browserProfile)
// {
// CanConnect = false;
// CanDisconnect = true;
// try
// {
// _channelName = GetChannelName(input);
// var cc = new CookieContainer();
// try
// {
// var cookies = browserProfile.GetCookieCollection("twitch.tv");
// cc.Add(cookies);
// }
// catch (Exception ex)
// {
// _logger.LogException(ex);
// }
// _me = null;
// try
// {
// _me = await API.GetMeAsync(_server, cc);
// }
// catch (NotLoggedInException) { }
// catch (Exception ex)
// {
// _logger.LogException(ex);
// }
// if (_me != null)
// {
// _emotIcons = await API.GetEmotIcons(_server, _me.Id, cc);
// }
// _provider = new MessageProvider();
// _provider.Opened += Provider_Opened;
// _provider.Received += Provider_Received;
// await _provider.ReceiveAsync();
// }
// finally
// {
// CanConnect = true;
// CanDisconnect = false;
// }
// }
// IMe _me;
// LowObject.Emoticons _emotIcons;
// private async void Provider_Received(object sender, Result e)
// {
// var result = e;
// switch (result.Command)
// {
// case "PING":
// await _provider.SendAsync("PONG");
// break;
// case "GLOBALUSERSTATE":
// break;
// case "PRIVMSG":
// {
// var commentData = new CommentData(result);
// var user = _userStore.GetUser(commentData.UserId);
// if (!_userCommentDict.TryGetValue(user, out ObservableCollection<PeriscopeCommentViewModel> userComments))
// {
// userComments = new ObservableCollection<PeriscopeCommentViewModel>();
// _userCommentDict.Add(user, userComments);
// }
// var isFirstComment = userComments.Count == 0;
// var cvm = new PeriscopeCommentViewModel(_options, _siteOptions, commentData, _emotIcons, isFirstComment, this, user);
// await _dispatcher.BeginInvoke((Action)(() =>
// {
// userComments.Add(cvm);
// }));
// CommentReceived?.Invoke(this, cvm);
// }
// break;
// default:
// Debug.WriteLine($"Periscope unknown command={result.Command}");
// var info = new InfoCommentViewModel(_options, result.Raw, InfoType.Debug);
// CommentReceived?.Invoke(this, info);
// break;
// }
// }
// private async void Provider_Opened(object sender, EventArgs e)
// {
// try
// {
// if (IsLoggedIn())
// {
// await _provider.SendAsync("CAP REQ :twitch.tv/tags twitch.tv/commands");
// await _provider.SendAsync($"PASS oauth:{_me.ChatOauthToken}");
// await _provider.SendAsync($"NICK {_me.Name}");
// await _provider.SendAsync($"USER {_me.Name} 8 * :{_me.Name}");
// }
// else
// {
// var name = Tools.GetRandomGuestUsername();
// await _provider.SendAsync("CAP REQ :twitch.tv/tags twitch.tv/commands");
// await _provider.SendAsync($"PASS SCHMOOPIIE");
// await _provider.SendAsync($"NICK {name}");
// await _provider.SendAsync($"USER {name} 8 * :{name}");
// }
// await _provider.SendAsync($"JOIN " + _channelName);
// }
// catch (Exception ex)
// {
// _logger.LogException(ex);
// CommentReceived?.Invoke(this, new InfoCommentViewModel(_options, ""));
// }
// }
// private bool IsLoggedIn()
// {
// return _me != null;
// }
// public void Disconnect()
// {
// _provider.Disconnect();
// }
// public IEnumerable<ICommentViewModel> GetUserComments(IUser user)
// {
// var comments = _userCommentDict[user];
// return comments;
// }
// public async Task PostCommentAsync(string text)
// {
// var s = $"PRIVMSG {_channelName} :{text}";
// await Task.FromResult<object>(null);
// }
// private readonly Dictionary<IUser, ObservableCollection<PeriscopeCommentViewModel>> _userCommentDict = new Dictionary<IUser, ObservableCollection<PeriscopeCommentViewModel>>();
// private readonly IDataServer _server;
// private readonly ILogger _logger;
// private readonly ICommentOptions _options;
// private readonly PeriscopeSiteOptions _siteOptions;
// private readonly IUserStore _userStore;
// private readonly Dispatcher _dispatcher;
// public PeriscopeCommentProvider(IDataServer server, ILogger logger, ICommentOptions options, PeriscopeSiteOptions siteOptions, IUserStore userStore, Dispatcher dispacher)
// {
// _server = server;
// _logger = logger;
// _options = options;
// _siteOptions = siteOptions;
// _userStore = userStore;
// _dispatcher = dispacher;
// CanConnect = true;
// CanDisconnect = false;
// }
// }
// public class PeriscopeSiteContext : ISiteContext
// {
// public Guid Guid => new Guid("22F7824A-EA1B-411E-85CA-6C9E6BE94E39");
// public string DisplayName => "Periscope";
// public IOptionsTabPage TabPanel
// {
// get
// {
// var panel = new TabPagePanel();
// panel.SetViewModel(new PeriscopeSiteOptionsViewModel(_siteOptions));
// return new PeriscopeOptionsTabPage(DisplayName, panel);
// }
// }
// public ICommentProvider CreateCommentProvider()
// {
// return new PeriscopeCommentProvider(new PeriscopeServer(), _logger, _options, _siteOptions, _userStore, _dispatcher);
// }
// private PeriscopeSiteOptions _siteOptions;
// public void LoadOptions(string path, IIo io)
// {
// _siteOptions = new PeriscopeSiteOptions();
// try
// {
// var s = io.ReadFile(path);
// _siteOptions.Deserialize(s);
// }
// catch (Exception ex)
// {
// Debug.WriteLine(ex.Message);
// _logger.LogException(ex, "", $"path={path}");
// }
// }
// public void SaveOptions(string path, IIo io)
// {
// try
// {
// var s = _siteOptions.Serialize();
// io.WriteFile(path, s);
// }
// catch (Exception ex)
// {
// Debug.WriteLine(ex.Message);
// _logger.LogException(ex, "", $"path={path}");
// }
// }
// public bool IsValidInput(string input)
// {
// //チャンネル名だけ来られても他のサイトのものの可能性があるからfalse
// //"twitch.tv/"の後に文字列があったらtrueとする。
// var b = Regex.IsMatch(input, "twitch\\.tv/[a-zA-Z0-9_]+");
// return b;
// }
// public UserControl GetCommentPostPanel(ICommentProvider commentProvider)
// {
// return null;
// }
// private readonly ICommentOptions _options;
// private readonly ILogger _logger;
// private readonly IUserStore _userStore;
// private readonly Dispatcher _dispatcher;
// public PeriscopeSiteContext(ICommentOptions options, ILogger logger, IUserStore userStore, Dispatcher dispatcher)
// {
// _options = options;
// _logger = logger;
// _userStore = userStore;
// _dispatcher = dispatcher;
// }
// }
//}