Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Threading.Tasks.Dataflow;
- namespace Get_Links
- {
- class Files
- {
- IEnumerable<Uri> items;
- int parallel;
- HttpClient http;
- public Files(IEnumerable<Uri> items, int parallel)
- {
- this.items = items;
- this.parallel = parallel;
- http = new HttpClient();
- }
- public Task CopyTo(string path, CancellationToken cancel = default)
- {
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- var action = new ActionBlock<Uri>(
- async x => File.WriteAllBytes($"{Path.Combine(path, x.Host)}.txt", await http.GetByteArrayAsync(x)),
- new ExecutionDataflowBlockOptions { CancellationToken = cancel, MaxDegreeOfParallelism = parallel });
- foreach (var item in items)
- action.Post(item);
- action.Complete();
- return action.Completion;
- }
- }
- class Links : IEnumerable<Uri>
- {
- const string URL_REGEX = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
- readonly IEnumerable<Uri> e;
- public Links(string input)
- {
- e = Regex
- .Matches(input, URL_REGEX)
- .Cast<Match>()
- .Select(x => new Uri(x.Value));
- }
- public IEnumerator<Uri> GetEnumerator() => e.GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
- }
- class Program
- {
- static async Task Main(string[] args)
- {
- var files = new Files(new Links("https://2ch.hk https://www.yandex.ru"), 4);
- await files.CopyTo("files");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement