uti | id | title | platforms | packages | ||||||
---|---|---|---|---|---|---|---|---|---|---|
com.xamarin.workbook |
602af677-9374-44ef-b2fc-5122738b86e8 |
tunein |
|
|
#r "Newtonsoft.Json"
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Net.Http.Headers;
async Task<string> GetStreamingUrlAsync(string id)
{
using (var client = new HttpClient())
{
string url = $"https://opml.radiotime.com/Tune.ashx?id={id}&render=json&formats=mp3,aac,ogg,hls";
var res = await client.GetAsync(url);
string jsonString = await res.Content.ReadAsStringAsync();
var json = JObject.Parse(jsonString);
var array = (JArray)json["body"];
return array.FirstOrDefault()?["url"].ToObject<string>();
}
}
async Task<string> GetTitleAsync(string url)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("ko_KR"));
var res = await client.GetAsync(url);
string htmlString = await res.Content.ReadAsStringAsync();
return Regex.Match(htmlString, @"<title.*?>(.*?) \| Free Internet Radio \| TuneIn<\/title>").Groups[1].Value;
}
}
async Task<(string, string)> GetAsync(string url)
{
var match = Regex.Match(url, @"https:\/\/tunein.com\/radio\/.*-(s\d+)");
string id = match.Groups[1].Value;
return (await GetTitleAsync(url), await GetStreamingUrlAsync(id));
}
string[] tuneinUrls =
{
"https://tunein.com/radio/Radio-Swiss-Pop-s25243/",
"https://tunein.com/radio/Radio-Swiss-Jazz-s6814/",
"https://tunein.com/radio/Radio-Swiss-Classic-s25582/",
"https://tunein.com/radio/Jazz24-885-s34682/"
};
await Task.WhenAll(tuneinUrls.Select(url => GetAsync(url)));