calculate remaining time
This commit is contained in:
parent
ed7ce6fb76
commit
09f666b39c
2 changed files with 188 additions and 0 deletions
6
AnimeCon.Vlc.Scraper/Form1.Designer.cs
generated
6
AnimeCon.Vlc.Scraper/Form1.Designer.cs
generated
|
@ -172,6 +172,7 @@
|
|||
this.btnClock.TabIndex = 12;
|
||||
this.btnClock.Text = "Send";
|
||||
this.btnClock.UseVisualStyleBackColor = true;
|
||||
this.btnClock.Click += new System.EventHandler(this.btnClock_Click);
|
||||
//
|
||||
// label6
|
||||
//
|
||||
|
@ -194,6 +195,11 @@
|
|||
// tmrVlc
|
||||
//
|
||||
this.tmrVlc.Interval = 1000;
|
||||
this.tmrVlc.Tick += new System.EventHandler(this.tmrVlc_Tick);
|
||||
//
|
||||
// tmrClock
|
||||
//
|
||||
this.tmrClock.Tick += new System.EventHandler(this.tmrClock_Tick);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
|
|
|
@ -4,14 +4,26 @@ using System.ComponentModel;
|
|||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
using System.Xml;
|
||||
using System.Dynamic;
|
||||
|
||||
namespace AnimeCon.Vlc.Scraper
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
private string VlcHostName { get; set; }
|
||||
private string VlcPassword { get; set; }
|
||||
private int remainingTime { get;set; }
|
||||
private string timeString { get; set; }
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
@ -19,7 +31,177 @@ namespace AnimeCon.Vlc.Scraper
|
|||
|
||||
private void btnConnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(lblStatus.Text != "Disconnected")
|
||||
{
|
||||
lblStatus.Text = "Disconnected";
|
||||
lblStatus.ForeColor = System.Drawing.Color.Red;
|
||||
btnConnect.Text = "Connect";
|
||||
lblTime.Text = "00:00:00";
|
||||
tmrVlc.Stop();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.VlcHostName = txtHostname.Text;
|
||||
this.VlcPassword = txtPassword.Text;
|
||||
lblTime.Text = "00:00:00";
|
||||
try
|
||||
{
|
||||
DoWebRequestAsync();
|
||||
tmrVlc.Start();
|
||||
btnConnect.Text = "Disconnect";
|
||||
}
|
||||
catch
|
||||
{
|
||||
tmrVlc.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DoWebRequestAsync()
|
||||
{
|
||||
using (HttpClient httpClient = new HttpClient())
|
||||
{
|
||||
// Convert the credentials to Base64
|
||||
string base64Credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($":{this.VlcPassword}"));
|
||||
|
||||
// Add Authorization header with Basic Authentication
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
|
||||
|
||||
try
|
||||
{
|
||||
// Make a GET request to the remote XML file
|
||||
HttpResponseMessage response = await httpClient.GetAsync($"http://{this.VlcHostName}/requests/status.xml");
|
||||
|
||||
// Ensure the request was successful
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
lblStatus.Text = "Connected";
|
||||
lblStatus.ForeColor = System.Drawing.Color.Green;
|
||||
|
||||
string xmlContent = await response.Content.ReadAsStringAsync();
|
||||
dynamic parsedObject = ParseXml(xmlContent);
|
||||
|
||||
int timeValue = GetPropertyValue(parsedObject, "time");
|
||||
int lengthValue = GetPropertyValue(parsedObject, "length");
|
||||
|
||||
remainingTime = lengthValue - timeValue;
|
||||
ConvertSecondsToTimeString(remainingTime);
|
||||
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Handle exception
|
||||
Console.WriteLine($"Error: {ex.Message}");
|
||||
lblStatus.Text = "ERROR!";
|
||||
lblStatus.ForeColor = System.Drawing.Color.Red;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnClock_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void tmrVlc_Tick(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
DoWebRequestAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
tmrVlc.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private void tmrClock_Tick(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static dynamic ParseXml(string xmlContent)
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
xmlDoc.LoadXml(xmlContent);
|
||||
|
||||
return ParseXmlNode(xmlDoc.DocumentElement);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Handle parsing exception
|
||||
Console.WriteLine($"Error parsing XML: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static dynamic ParseXmlNode(XmlNode node)
|
||||
{
|
||||
if (node.NodeType == XmlNodeType.Element)
|
||||
{
|
||||
dynamic dynamicObject = new ExpandoObject();
|
||||
var dictionary = (IDictionary<string, object>)dynamicObject;
|
||||
|
||||
foreach (XmlNode childNode in node.ChildNodes)
|
||||
{
|
||||
dictionary[childNode.Name] = ParseXmlNode(childNode);
|
||||
}
|
||||
|
||||
return dynamicObject;
|
||||
}
|
||||
else if (node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA)
|
||||
{
|
||||
return node.InnerText;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null; // Handle other node types as needed
|
||||
}
|
||||
}
|
||||
|
||||
static int GetPropertyValue(dynamic parsedObject, string propertyName)
|
||||
{
|
||||
if (parsedObject != null)
|
||||
{
|
||||
var dictionary = (IDictionary<string, object>)parsedObject;
|
||||
|
||||
if (dictionary.ContainsKey(propertyName))
|
||||
{
|
||||
dynamic propertyValue = dictionary[propertyName];
|
||||
|
||||
// Check if the property value is an array with the format ["#text", value]
|
||||
if (propertyValue is object[] && propertyValue.Length == 2 && propertyValue[0] == "#text")
|
||||
{
|
||||
return Convert.ToInt32(propertyValue[1]);
|
||||
}
|
||||
else if (propertyValue is ExpandoObject)
|
||||
{
|
||||
return GetPropertyValue(propertyValue, "#text");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Convert.ToInt32(propertyValue); // Fallback if the format is different
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0; // Default value if the property is not found
|
||||
}
|
||||
|
||||
private void ConvertSecondsToTimeString(int totalSeconds)
|
||||
{
|
||||
// Calculate hours, minutes, and seconds
|
||||
int hours = totalSeconds / 3600;
|
||||
int minutes = (totalSeconds % 3600) / 60;
|
||||
int seconds = totalSeconds % 60;
|
||||
|
||||
// Format the time string
|
||||
lblTime.Text = $"{hours:D2}:{minutes:D2}:{seconds:D2}";
|
||||
timeString = $"{hours:D2}{minutes:D2}{seconds:D2}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue