Skip to content

Instantly share code, notes, and snippets.

@ericsampson
Created November 11, 2020 04:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericsampson/8f34c68ce7677b12dd04838e7c243872 to your computer and use it in GitHub Desktop.
Save ericsampson/8f34c68ce7677b12dd04838e7c243872 to your computer and use it in GitHub Desktop.
C#9 version of deno syntax episode downloader
using System; using System.Net; using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Refit;
const string showList = "https://syntax.fm/api";
static async Task<List<Show>> getShowList() {
var syntaxApi = RestService.For<ISyntaxApi>(showList);
return await syntaxApi.GetShows();
}
static string generateShowName(Show show) {
string cleansedTitle = Path.GetInvalidFileNameChars().Aggregate(show.title, (f, c) => f.Replace(c, '_'));
return $"{ show.number } - { cleansedTitle } - { show.displayDate}.mp3";
}
static async Task downloadShow(WebClient client, Show show) {
await client.DownloadFileTaskAsync(new Uri(show.url), Path.Combine(".", "shows", generateShowName(show)));
}
static async Task go() {
var showList = await getShowList();
var client = new WebClient();
foreach (var show in showList) {
Console.WriteLine($"Downloading show {generateShowName(show)}");
await downloadShow(client, show);
}
Console.WriteLine("Done!");
}
await go();
record Show (
int number,
string title,
long date,
string url,
string slug,
string html,
string notesFile,
string displayDate,
string displayNumber
);
interface ISyntaxApi
{
[Get("/shows")]
Task<List<Show>> GetShows();
}
@ericsampson
Copy link
Author

This is a C#9 version of @wesbos's deno syntax episode downloader, just for a fun comparison :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment