forked from StackExchange/stack-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcasts.csx
32 lines (28 loc) · 1.6 KB
/
podcasts.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Xml.Linq;
using System.Text.RegularExpressions;
var soundCloudIds = (
from item in XDocument.Load("http://feeds.soundcloud.com/users/soundcloud:users:4273388/sounds.rss").Descendants("item")
let guid = item.Element("guid").Value
select Tuple.Create(guid.Substring(guid.LastIndexOf('/') + 1), item))
.ToLookup(x => x.Item1, x => x.Item2.Element("enclosure").Attribute("url").Value);
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
const string fm = "---";
foreach(var hit in
from path in Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "_posts")).AsParallel()
where Regex.Match(path, @"podcast", RegexOptions.IgnoreCase).Success
let text = File.ReadAllText(path)
let player = Regex.Match(text, @"<(iframe|object).*(/>|</iframe>)", RegexOptions.IgnoreCase | RegexOptions.Multiline)
where player.Success
let trackId = Regex.Match(player.Value, @"tracks(/|%2f)(\d+)", RegexOptions.IgnoreCase)
where trackId.Success
let url = soundCloudIds[trackId.Groups[2].Value].FirstOrDefault()
where url != null
select new { path, url, text })
{
var frontMatterStart = hit.text.IndexOf(fm);
var frontMatterEnd = hit.text.IndexOf(fm, frontMatterStart + fm.Length);
var frontMatter = hit.text.Substring(fm.Length, frontMatterEnd - fm.Length);
if (Regex.Match(frontMatter, @"podcast: http", RegexOptions.IgnoreCase).Success) continue;
frontMatter += "podcast: " + hit.url + (frontMatter.StartsWith("\r\n") ? "\r\n" : "\n");
File.WriteAllText(hit.path, fm + frontMatter + hit.text.Substring(frontMatterEnd), utf8WithoutBom);
}