Created
October 7, 2012 00:24
-
-
Save ElemarJR/3846625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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