Skip to content

Commit

Permalink
add Stations.Scrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Gramenz committed Feb 14, 2023
1 parent 46ffb3c commit 3608d53
Show file tree
Hide file tree
Showing 16 changed files with 1,371 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Webradio.Stations.Scrapper/Helper/Help.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Webradio.Stations.Helper;

internal class Help
{
public static string TimeStampToDate(double unixTimeStamp)
{
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dateTime.ToString("dd.MM.yyyy");
}

private static string Runtime(int seconds)
{
var ts = TimeSpan.FromSeconds(seconds);

var rt = ts.Minutes.ToString("D2") + ":" + ts.Seconds.ToString("D2");

if (ts.Hours > 0) rt = ts.Hours.ToString("D2") + ":" + rt;

return rt;
}
}
69 changes: 69 additions & 0 deletions Webradio.Stations.Scrapper/Helper/Http.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Net;

namespace Webradio.Stations.Helper;

internal class Http
{
public static HttpStatusCode StatusCode { get; set; }

public static async Task<string> Request(string url)
{
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
StatusCode = HttpStatusCode.Created;
string message;

try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "text/html, application/json");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
using var response = await client.GetAsync(url);

StatusCode = response.StatusCode;
message = await response.Content.ReadAsStringAsync();
}
catch (Exception)
{
return "";
}

return message;
}

public static async Task<HttpStatusCode> Check(string url)
{
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

try
{
var client = new HttpClient();
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
return response.StatusCode;
}
catch (Exception)
{
return HttpStatusCode.RequestTimeout;
}
}

public static async Task<string> GetSiteJson(string url)
{
var repeat = false;
var ret = await Request(url);
if (StatusCode != HttpStatusCode.OK && repeat == false)
{
repeat = true;
ret = await Request(url);
}

if (ret == "")
{
Console.WriteLine("Cant read " + url);
return "";
}

return ret.Substring("type=\"application/json\">", "<");
}
}
68 changes: 68 additions & 0 deletions Webradio.Stations.Scrapper/Helper/Json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Newtonsoft.Json;

namespace Webradio.Stations.Helper;

/// <summary>
/// Json Functions
/// </summary>
internal class Json
{
/// <summary>
/// Deserialize a json string as Object
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="json">Json String</param>
/// <returns></returns>
public static T Deserialize<T>(string json)
{
//try
//{
// using var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
// var settings = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
// var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T), settings);
// return (T)serializer.ReadObject(ms);
//}
//catch (System.Exception ex)
//{
// return (T)System.Activator.CreateInstance(typeof(T));
//}
try
{
var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
return JsonConvert.DeserializeObject<T>(json, settings);
}
catch (Exception ex)
{
return (T)Activator.CreateInstance(typeof(T));
}
}

public static string Serialize<T>(T jsonObject)
{
//try
//{
// using var ms = new System.IO.MemoryStream();
// var settings = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
// var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T), settings);
// serializer.WriteObject(ms, jsonObject);

// ms.Position = 0;
// StreamReader sr = new StreamReader(ms);
// return sr.ReadToEnd();
//}
//catch (System.Exception ex)
//{
// return "";
//}
try
{
//var settings = new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Formatting.Indented};
//return JsonConvert.SerializeObject(jsonObject, settings);
return JsonConvert.SerializeObject(jsonObject);
}
catch (Exception ex)
{
return "";
}
}
}
111 changes: 111 additions & 0 deletions Webradio.Stations.Scrapper/Helper/Standby.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Webradio.Stations.Helper;

internal class Standby
{
private static IntPtr _currentPowerRequest;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct PowerRequestContext
{
public uint Version;
public uint Flags;
[MarshalAs(UnmanagedType.LPWStr)] public string SimpleReasonString;
}

private enum PowerRequestType
{
PowerRequestDisplayRequired = 0, // Not to be used by drivers
PowerRequestSystemRequired,
PowerRequestAwayModeRequired, // Not to be used by drivers
PowerRequestExecutionRequired // Not to be used by drivers
}

#region const

private const int PowerRequestContextVersion = 0;
private const int PowerRequestContextSimpleString = 0x1;

#endregion

#region DllImport

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr PowerCreateRequest(ref PowerRequestContext context);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool PowerSetRequest(IntPtr powerRequestHandle, PowerRequestType requestType);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool PowerClearRequest(IntPtr powerRequestHandle, PowerRequestType requestType);

#endregion

#region public functions

public static void Suppress()
{
// Clear current power request if there is any.
if (_currentPowerRequest != IntPtr.Zero)
{
PowerClearRequest(_currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);
_currentPowerRequest = IntPtr.Zero;
}

// Create new power request.
PowerRequestContext pContext;
pContext.Flags = PowerRequestContextSimpleString;
pContext.Version = PowerRequestContextVersion;
pContext.SimpleReasonString = "Standby suppressed by PowerAvailabilityRequests.exe";

_currentPowerRequest = PowerCreateRequest(ref pContext);

if (_currentPowerRequest == IntPtr.Zero)
{
// Failed to create power request.
var error = Marshal.GetLastWin32Error();

if (error != 0)
throw new Win32Exception(error);
}

var success = PowerSetRequest(_currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

if (!success)
{
// Failed to set power request.
_currentPowerRequest = IntPtr.Zero;
var error = Marshal.GetLastWin32Error();

if (error != 0)
throw new Win32Exception(error);
}
}

public static void Enable()
{
// Only try to clear power request if any power request is set.
if (_currentPowerRequest != IntPtr.Zero)
{
var success = PowerClearRequest(_currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

if (!success)
{
// Failed to clear power request.
_currentPowerRequest = IntPtr.Zero;
var error = Marshal.GetLastWin32Error();

if (error != 0)
throw new Win32Exception(error);
}
else
{
_currentPowerRequest = IntPtr.Zero;
}
}
}

#endregion
}
34 changes: 34 additions & 0 deletions Webradio.Stations.Scrapper/Helper/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Webradio.Stations.Helper;

public static class StringExtensions
{
public static string Substring(this string value, string start, string ende)
{
try
{
var a = value.IndexOf(start) + start.Length;
if (a - start.Length <= 0) return "";
var b = value.IndexOf(ende, a, StringComparison.Ordinal);
return value.Substring(a, b - a);
}
catch (Exception)
{
return "";
}
}

public static string Substring(this string value, string start, string start2, string ende)
{
try
{
var a = value.IndexOf(start) + start.Length;
var a2 = value.IndexOf(start2, a, StringComparison.Ordinal) + start2.Length;
var b = value.IndexOf(ende, a2, StringComparison.Ordinal);
return value.Substring(a2, b - a2);
}
catch (Exception)
{
return "";
}
}
}
77 changes: 77 additions & 0 deletions Webradio.Stations.Scrapper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Diagnostics;
using Webradio.Stations.Helper;
using Webradio.Stations.RadioNet;

namespace Webradio.Stations;

internal class Program
{
public static RadioStations RadioStations = new();

private static bool scrapp = true;

private static async Task Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
Console.CancelKeyPress += ConsoleOnCancelKeyPress;

if (File.Exists(RadioStations.FileName))
{
Console.WriteLine("Found existing " + RadioStations.FileName + ":");
Console.WriteLine("Test this File? y/n");
var res = Console.ReadKey();
Console.WriteLine("");
if (res.Key == ConsoleKey.Y)
{
scrapp = false;
RadioStations.Read();
}
}

Standby.Suppress();
var sw = new Stopwatch();
sw.Start();

if (scrapp)
{
Console.WriteLine("Start Scapper");
var rns = new Scrapper();
var re1 = await Task.Run(() => rns.Start());
RadioStations.Write();
}

Console.WriteLine("Start Tester");
var stt = new StationsTester();
var re2 = await Task.Run(() => stt.Start());

Console.WriteLine("Start Cleaner");
var stc = new StationsCleaner();
var re3 = await Task.Run(() => stc.Start());

RadioStations.Write();

sw.Stop();
var ts = TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds);
var time = ts.Hours.ToString("D2") + ":" + ts.Minutes.ToString("D2") + ":" + ts.Seconds.ToString("D2");
Console.WriteLine("Finished with " + RadioStations.Stations.Count + " Stations in " + time);

Standby.Enable();

Console.ReadKey();
}

private static void ConsoleOnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
MyExit();
}

private static void CurrentDomainOnProcessExit(object? sender, EventArgs e)
{
MyExit();
}

private static void MyExit()
{
Standby.Enable();
}
}
Loading

0 comments on commit 3608d53

Please sign in to comment.