Skip to content

Instantly share code, notes, and snippets.

@kyle-seongwoo-jun
Created April 16, 2020 06:49
Show Gist options
  • Save kyle-seongwoo-jun/a9525b8b9f031d4ccad33fd49ca6bebd to your computer and use it in GitHub Desktop.
Save kyle-seongwoo-jun/a9525b8b9f031d4ccad33fd49ca6bebd to your computer and use it in GitHub Desktop.
TuneIn
uti id title platforms packages
com.xamarin.workbook
602af677-9374-44ef-b2fc-5122738b86e8
tunein
Console
id version
Newtonsoft.Json
12.0.3
#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)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment