Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created October 7, 2012 00:24
Show Gist options
  • Select an option

  • Save ElemarJR/3846625 to your computer and use it in GitHub Desktop.

Select an option

Save ElemarJR/3846625 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks.Dataflow;
namespace TPLDataflow
{
class Program
{
static void Main(string[] args)
{
PrintLinksFromMSDNAsync();
Console.ReadLine();
}
public static void PrintLinksFromMSDNAsync()
{
var printUrl = new ActionBlock<string>(s => {
Console.WriteLine(s);
});
var extractUrlFromMatch = new TransformBlock<Match, string>(match =>
{
return match.Groups[1].Value;
});
extractUrlFromMatch.LinkTo(printUrl);
var getUrlMacthes = new TransformManyBlock<string, Match>(content => {
var regex = "href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
var matches = Regex.Matches(content, regex);
return matches.Cast<Match>();
});
getUrlMacthes.LinkTo(extractUrlFromMatch);
var getPageContent = new TransformBlock<string, string>(async url => {
return await new WebClient().DownloadStringTaskAsync(url);
});
getPageContent.LinkTo(getUrlMacthes);
getPageContent.Post("http://msdn.microsoft.com/pt-br/");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment