-
Notifications
You must be signed in to change notification settings - Fork 1
/
SearchResults.cs
384 lines (354 loc) · 15.1 KB
/
SearchResults.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO;
using System.Data.Common;
using System.Json;
namespace PlexWalk
{
public partial class SearchResults : Form, FormInterface
{
string title;
int count = 0;
bool AbortThreads = false;
List<Descriptor> searches;
List<TreeNode> results = new List<TreeNode>();
string query;
bool use_db;
SearchType searchType = SearchType.SearchServers;
public enum SearchType
{
Library,
Movie,
JsonSearchTestCase,
Shows,
SearchServers
}
public SearchResults(SearchType s, string query)
{
InitializeComponent();
searchType = s;
this.query = query;
use_db = false;
}
public SearchResults(List<Descriptor> searches, string query, bool use_db = false)
{
InitializeComponent();
this.searches = searches;
this.query = query;
title = this.Text + " - " + query;
this.use_db = use_db;
}
private void SearchResults_Load(object sender, EventArgs e)
{
if (searchType != SearchType.SearchServers)
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
string result = null;
using (WebClient wc = new WebClient())
{
result = wc.DownloadString("http://binaryoutlook.com:8080/?search=" + Uri.EscapeDataString(query)
+ (searchType == SearchType.Movie ? "&Movies=true" : "")
+ (searchType == SearchType.Library ? "&Sections=true" : "")
+ (searchType == SearchType.Shows ? "&Shows=true" : "")
);
}
parseJsonResponse(result);
});
}
else
{
this.Text = title + " (" + 0 + "/" + searches.Count + ")";
var tn = new TreeNode();
foreach (Descriptor desc in searches)
{
tn.Nodes.Add(new TreeNode(desc.serverName) { Tag = desc, Name = "/search?query=" + Uri.EscapeDataString(query) });
}
foreach (TreeNode n in tn.Nodes)
ThreadPool.QueueUserWorkItem(delegate (object state)
{
if (AbortThreads)
{
int max_count, max_total, count, total;
ThreadPool.GetMaxThreads(out max_count, out max_total);
ThreadPool.GetAvailableThreads(out count, out total);
if (count == max_count && total == max_total)
AbortThreads = false;
return;
}
try
{
PlexUtils.populateSubNodes(n, this, fakeNode);
}
catch { }
UpdateSearchStatus();
});
}
}
private void parseJsonResponse(string result)
{
var value = System.Json.JsonValue.Parse(result);
try
{
#region Directory
foreach (var dir in value["Directory"])
{
if (AbortThreads)
return;
string basePath, token;
tryReadServerInfo(dir.Value, out basePath, out token);
if (basePath == null || token == null)
continue;
string title = (string)dir.Value.ValueOrDefault("_title").ReadAs(typeof(String), null);
string type = (string)dir.Value.ValueOrDefault("_type").ReadAs(typeof(String), null);
string keyVal = (string)dir.Value.ValueOrDefault("_key").ReadAs(typeof(String), null);
string serverName = (string)dir.Value.ValueOrDefault("_sourceTitle").ReadAs(typeof(String), null);
string librarySectionTitle = (string)dir.Value.ValueOrDefault("_librarySectionTitle").ReadAs(typeof(String), null);
string librarySectionID = (string)dir.Value.ValueOrDefault("_librarySectionID").ReadAs(typeof(String), null);
TreeNode n = new TreeNode(title)
{
Tag = new Descriptor(basePath, token)
{
},
Name = keyVal
};
n.Nodes.Add("");
AddNode(fakeNode, n);
}
#endregion
} catch { }
try
{
#region Video
foreach (var video in value["Video"])
{
if (AbortThreads)
return;
string basePath, token;
tryReadServerInfo(video.Value, out basePath, out token);
if (basePath == null || token == null)
continue;
string title = (string)video.Value.ValueOrDefault("_title").ReadAs(typeof(String), null);
string type = (string)video.Value.ValueOrDefault("_type").ReadAs(typeof(String), null);
string duration = (string)video.Value.ValueOrDefault("_duration").ReadAs(typeof(String), null);
string keyVal = (string)video.Value.ValueOrDefault("_key").ReadAs(typeof(String), null);
string serverTitle = (string)video.Value.ValueOrDefault("_sourceTitle").ReadAs(typeof(String), null);
foreach (var media in video.Value["Media"])
{
string width = (string)media.Value.ValueOrDefault("_width").ReadAs(typeof(String), null);
string height = (string)media.Value.ValueOrDefault("_height").ReadAs(typeof(String), null);
string container = (string)media.Value.ValueOrDefault("_container").ReadAs(typeof(String), null);
duration = (string)media.Value.ValueOrDefault("_duration").ReadAs(typeof(String), null);
foreach (var part in media.Value["Part"])
{
string size = (string)part.Value.ValueOrDefault("_size").ReadAs(typeof(String), null);
duration = (string)part.Value.ValueOrDefault("_duration").ReadAs(typeof(String), null);
string pKey = (string)part.Value.ValueOrDefault("_key").ReadAs(typeof(String), null);
string file = (string)part.Value.ValueOrDefault("_file").ReadAs(typeof(String), null);
duration = duration == null ? "" : String.Format(" ({0})", TimeSpan.FromMilliseconds(Double.Parse(duration)).ToString(@"hh\:mm\:ss"));
TreeNode node = new TreeNode(title + (duration == null ? "" : duration))
{
Tag = new Descriptor(basePath, token)
{
canDownload = false
},
Name = keyVal
};
string EndPart = file.Substring(file.LastIndexOf(@"\") + 1).Substring(file.LastIndexOf(@"/") + 1);
TreeNode subNode = new TreeNode(String.Format("Download {0}", EndPart))
{
Tag = new Descriptor((Descriptor)node.Tag)
{
canDownload = true,
downloadUrl = string.Format("{0}{1}?{2}"
, basePath
, pKey.Replace("file"
, Uri.EscapeDataString(EndPart.Remove(EndPart.LastIndexOf('.')))), token)
},
Name = pKey
};
node.Nodes.Add(subNode);
AddNode(fakeNode, node);
}
}
}
#endregion
} catch { }
}
private void tryReadServerInfo(JsonValue value, out string basePath, out string token)
{
basePath = null;
token = null;
var server = value.ValueOrDefault("Server");
try
{
basePath = String.Format("{0}://{1}:{2}",
server["_scheme"].ReadAs(typeof(String)),
server["_address"].ReadAs(typeof(String)),
server["_port"].ReadAs(typeof(String))
);
token = String.Format("X-Plex-Token={0}", (string)server["_token"].ReadAs(typeof(String)));
}
catch { }
}
delegate void AnyDelegate();
private void UpdateSearchStatus()
{
if (!this.InvokeRequired)
{
this.Text = title + " (" + count++ + "/" + searches.Count + ")";
}
else
{
try
{
this.Invoke(new AnyDelegate(UpdateSearchStatus));
}
catch { }
}
}
private void waitForExit()
{
int total; int count; int max_total; int max_count;
while (!AbortThreads)
{
Thread.Sleep(1);
Application.DoEvents();
ThreadPool.GetMaxThreads(out max_count, out max_total);
ThreadPool.GetAvailableThreads(out count, out total);
if (count == max_count && total == max_total)
break;
}
}
delegate void ChangeNodeCallback(object sender, TreeNode src);
private void ExpandNode(object sender, TreeNode src)
{
if (((Control)sender).InvokeRequired)
{
ChangeNodeCallback d = new ChangeNodeCallback(ExpandNode);
this.Invoke(d, new object[] { sender, src });
}
else
PlexUtils.populateSubNodes(src, this);
}
delegate void AddNodeCallback(TreeNode Parent, TreeNode Child);
public void AddNode(TreeNode Parent, TreeNode Child)
{
if (searchTreeView.InvokeRequired)
{
AddNodeCallback d = new AddNodeCallback(AddNode);
this.Invoke(d, new object[] { Parent, Child });
}
else
{
if (Parent != fakeNode)
{
Parent.Nodes.Add(Child);
}
else
{
Thread.Sleep(1);
Application.DoEvents();
searchTreeView.Nodes.Add(Child);
}
}
}
private void searchTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
if (AbortThreads)
{
int total; int count;
int max_total ; int max_count;
ThreadPool.GetMaxThreads(out max_count, out max_total);
ThreadPool.GetAvailableThreads(out count,out total);
if (count == max_count && total == max_total)
AbortThreads = false;
return;
}
object[] array = state as object[];
TreeNode src = (TreeNode)array[0];
if (src.FirstNode.Text == null || src.FirstNode.Text == "")
{
ClearNodes(sender, src);
try
{
PlexUtils.populateSubNodes(src, this);
}
catch
{
}
}
}, new object[] { e.Node });
}
private void ClearNodes(object sender, TreeNode src)
{
if (((Control)sender).InvokeRequired)
{
ChangeNodeCallback d = new ChangeNodeCallback(ClearNodes);
try
{
this.Invoke(d, new object[] { sender, src });
} catch { }
}
else
src.Nodes.Clear();
}
TreeNode selected;
private TreeNode fakeNode = new TreeNode();
private void searchTreeView_MouseUp(object sender, MouseEventArgs e)
{
selected = ((TreeView)sender).GetNodeAt(e.Location);
if (selected == null || selected.Tag == null)
{
copyUrlToolStripMenuItem.Visible = false;
playInVlcToolStripMenuItem.Visible = false;
}
else
{
copyUrlToolStripMenuItem.Visible = ((Descriptor)selected.Tag).canDownload;
playInVlcToolStripMenuItem.Visible = ((Descriptor)selected.Tag).canDownload;
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
searchContextMenu.Show();
}
private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
{
DownloadInfo[] di = PlexUtils.getDownloads(selected,this).Select(x => new DownloadInfo(x.getDownloadURL(), PlexUtils.MakeValidFileName(x.downloadFilename), PlexUtils.MakeValidFileName(x.subdir))).ToArray();
if (PlexUtils.downloadDialog == null || PlexUtils.downloadDialog.IsDisposed)
PlexUtils.downloadDialog = new DownloadDialog(di);
else
PlexUtils.downloadDialog.enqueue(di);
PlexUtils.downloadDialog.Show();
}
private void copyUrlToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(PlexUtils.getDownloadURL(selected));
MessageBox.Show("URL Copied to clipboard");
}
private void copyUrlToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
}
private void playInVlcToolStripMenuItem_Click(object sender, EventArgs e)
{
if (((Descriptor)selected.Tag).isSubtitlesNode)
PlexUtils.PlayInVLC(selected.Parent.PrevNode, selected);
else
PlexUtils.PlayInVLC(selected);
}
private void SearchResults_FormClosing(object sender, FormClosingEventArgs e)
{
AbortThreads = true;
}
}
}