Skip to content

Instantly share code, notes, and snippets.

@FennyFatal
Created January 6, 2015 18:48
Show Gist options
  • Save FennyFatal/f5f99af652cf2644a937 to your computer and use it in GitHub Desktop.
Save FennyFatal/f5f99af652cf2644a937 to your computer and use it in GitHub Desktop.
WalkPlex
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using System.Collections.Specialized;
namespace PlexWalk1
{
public partial class BrowseForm : Form
{
String basepath = "/library/sections";
public class Descriptor
{
public string host;
public string token;
public Descriptor(string host, string token)
{
this.host = host;
this.token = token;
}
}
public string parseME;
public BrowseForm()
{
InitializeComponent();
}
private TreeNodeCollection parseServers(string servers_xml)
{
TreeNode tn = new TreeNode();
#region ServerData
using (XmlReader reader = XmlReader.Create(new StringReader(servers_xml)))
{
String address;
String name;
int port;
string accessToken = "";
string scheme;
while (reader.ReadToFollowing("Server"))
{
if (reader.MoveToAttribute("name"))
{
name = reader.ReadContentAsString();
if (reader.MoveToAttribute("address"))
{
address = reader.ReadContentAsString();
{
if (reader.MoveToAttribute("port"))
{
port = reader.ReadContentAsInt();
if (reader.MoveToAttribute("scheme"))
{
scheme = reader.ReadContentAsString();
if (reader.MoveToAttribute("accessToken"))
{
accessToken = reader.ReadContentAsString();
accessToken = String.Format("?X-Plex-Token={0}", accessToken);
}
TreeNode node = new TreeNode(name);
node.Tag = new Descriptor(String.Format("{0}://{1}:{2}", scheme, address, port), accessToken);
tn.Nodes.Add(node);
}
}
}
}
}
}
}
#endregion
return tn.Nodes;
}
private void Form1_Load(object sender, EventArgs e)
{
using (WebClient wc = new System.Net.WebClient())
{
Boolean fail = false;
do
{
try
{
Login loginform = new Login();
loginform.ShowDialog();
wc.Credentials = loginform.creds;
parseME = wc.DownloadString("https://plex.tv/pms/servers.xml");
fail = false;
}
catch
{
fail = true;
}
} while (fail);
}
foreach (TreeNode tn in parseServers(parseME))
{
plexTreeView.Nodes.Add(tn);
tn.Name = basepath;
try
{
populateSubNodes(tn);
}
catch { }
}
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
TreeNode src = e.Node;
if (src.FirstNode.Text == null || src.FirstNode.Text == "")
{
src.Nodes.Clear();
populateSubNodes(src);
}
}
private void populateSubNodes(TreeNode tnode)
{
using (WebClient wc = new System.Net.WebClient())
{
String xmlString = null;
xmlString = wc.DownloadString(((Descriptor)tnode.Tag).host + tnode.Name + ((Descriptor)tnode.Tag).token);
#region FolderData
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
String key;
String title;
while (reader.ReadToFollowing("Directory"))
{
if (reader.MoveToAttribute("key"))
{
key = reader.ReadContentAsString();
if (reader.MoveToAttribute("title"))
{
title = reader.ReadContentAsString();
{
TreeNode node = new TreeNode(title);
node.Name = key.StartsWith("/") ? key : (string)tnode.Name + '/' + key;
node.Tag = tnode.Tag;
node.Nodes.Add(new TreeNode());
tnode.Nodes.Add(node);
}
}
}
}
}
#endregion
#region VideoData
if (xmlString.Contains("<Video "))
{
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
String key;
String title;
while (reader.ReadToFollowing("Video"))
{
if (reader.MoveToAttribute("key"))
{
key = reader.ReadContentAsString();
if (reader.MoveToAttribute("title"))
{
title = reader.ReadContentAsString();
{
TreeNode node = new TreeNode(title);
node.Name = key.StartsWith("/") ? key : (string)tnode.Name + '/' + key;
node.Tag = tnode.Tag;
if (reader.ReadToFollowing("Media"))
{
if (reader.MoveToAttribute("width"))
{
int width = reader.ReadContentAsInt();
if (reader.MoveToAttribute("height"))
{
int height = reader.ReadContentAsInt();
if (reader.ReadToFollowing("Part"))
{
do
{
if (reader.MoveToAttribute("key"))
{
key = reader.ReadContentAsString();
if (reader.MoveToAttribute("file"))
{
string filename = reader.ReadContentAsString();
filename = filename.Substring(filename.LastIndexOf("/") + 1);
title = String.Format("Download {0} ({1}x{2})", filename, width, height);
TreeNode subnode = new TreeNode(title);
subnode.Name = key.StartsWith("/") ? key : (string)tnode.Name + '/' + key;
subnode.Tag = node.Tag;
node.Nodes.Add(subnode);
}
}
}
while (reader.ReadToNextSibling("Part"));
}
}
}
}
tnode.Nodes.Add(node);
}
}
}
}
}
}
#endregion
#region TrackData
if (xmlString.Contains("<Track "))
{
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
String key;
String title;
while (reader.ReadToFollowing("Track"))
{
if (reader.MoveToAttribute("key"))
{
key = reader.ReadContentAsString();
if (reader.MoveToAttribute("title"))
{
title = reader.ReadContentAsString();
{
TreeNode node = new TreeNode(title);
node.Name = key.StartsWith("/") ? key : (string)tnode.Name + '/' + key;
node.Tag = tnode.Tag;
if (reader.ReadToFollowing("Media"))
{
if (reader.MoveToAttribute("duration"))
{
int duration = reader.ReadContentAsInt();
if (reader.ReadToFollowing("Part"))
{
do
{
if (reader.MoveToAttribute("key"))
{
key = reader.ReadContentAsString();
if (reader.MoveToAttribute("file"))
{
string filename = reader.ReadContentAsString();
filename = filename.Substring(filename.LastIndexOf("/") + 1);
title = String.Format("Download {0} ({1})", filename, duration);
TreeNode subnode = new TreeNode(title);
subnode.Name = key.StartsWith("/") ? key : (string)tnode.Name + '/' + key;
subnode.Tag = node.Tag;
node.Nodes.Add(subnode);
}
}
}
while (reader.ReadToNextSibling("Part"));
}
}
}
tnode.Nodes.Add(node);
}
}
}
}
}
}
#endregion
}
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
if (plexTreeView.SelectedNode.Text.StartsWith("Download"))
{
System.Diagnostics.Process.Start(((Descriptor)plexTreeView.SelectedNode.Tag).host + plexTreeView.SelectedNode.Name + (((Descriptor)plexTreeView.SelectedNode.Tag).token != "" ? ((Descriptor)plexTreeView.SelectedNode.Tag).token : ""));
}
}
}
}
namespace PlexWalk
{
partial class BrowseForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.plexTreeView = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// plexTreeView
//
this.plexTreeView.Location = new System.Drawing.Point(13, 13);
this.plexTreeView.Name = "plexTreeView";
this.plexTreeView.Size = new System.Drawing.Size(259, 237);
this.plexTreeView.TabIndex = 0;
this.plexTreeView.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.plexTreeView.DoubleClick += new System.EventHandler(this.treeView1_DoubleClick);
//
// BrowseForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.plexTreeView);
this.Name = "BrowseForm";
this.Text = "PlexWalk";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView plexTreeView;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace PlexWalk
{
public partial class Login : Form
{
public ICredentials creds;
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
creds = new NetworkCredential(username.Text, password.Text);
this.Close();
}
}
}
namespace PlexWalk
{
partial class Login
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.username = new System.Windows.Forms.TextBox();
this.password = new System.Windows.Forms.TextBox();
this.btnLogin = new System.Windows.Forms.Button();
this.lblUN = new System.Windows.Forms.Label();
this.lblPW = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// username
//
this.username.Location = new System.Drawing.Point(78, 16);
this.username.Name = "username";
this.username.Size = new System.Drawing.Size(149, 20);
this.username.TabIndex = 0;
//
// password
//
this.password.Location = new System.Drawing.Point(78, 40);
this.password.Name = "password";
this.password.PasswordChar = '*';
this.password.Size = new System.Drawing.Size(149, 20);
this.password.TabIndex = 1;
//
// button1
//
this.btnLogin.Location = new System.Drawing.Point(150, 67);
this.btnLogin.Name = "button1";
this.btnLogin.Size = new System.Drawing.Size(75, 23);
this.btnLogin.TabIndex = 2;
this.btnLogin.Text = "Login";
this.btnLogin.UseVisualStyleBackColor = true;
this.btnLogin.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.lblUN.AutoSize = true;
this.lblUN.Location = new System.Drawing.Point(12, 19);
this.lblUN.Name = "label1";
this.lblUN.Size = new System.Drawing.Size(60, 13);
this.lblUN.TabIndex = 3;
this.lblUN.Text = "User Name";
//
// label2
//
this.lblPW.AutoSize = true;
this.lblPW.Location = new System.Drawing.Point(19, 43);
this.lblPW.Name = "label2";
this.lblPW.Size = new System.Drawing.Size(53, 13);
this.lblPW.TabIndex = 4;
this.lblPW.Text = "Password";
//
// Login
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(237, 102);
this.Controls.Add(this.lblPW);
this.Controls.Add(this.lblUN);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.password);
this.Controls.Add(this.username);
this.Name = "Login";
this.Text = "Login to Plex";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox username;
private System.Windows.Forms.TextBox password;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Label lblUN;
private System.Windows.Forms.Label lblPW;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment