Skip to content
Snippets Groups Projects
Commit bf73bb84 authored by Maxime POULAIN's avatar Maxime POULAIN
Browse files

add TP9

parent f6a878d6
Branches main
No related tags found
No related merge requests found
TP9/DysonV8/DysonV8/bin/
TP9/DysonV8/DysonV8/obj/
{
"CurrentProjectSetting": null
}
\ No newline at end of file
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
\ No newline at end of file
File added
File added

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DysonV8", "DysonV8\DysonV8.csproj", "{90E6F200-2760-4660-98FD-C9BB49D500BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{90E6F200-2760-4660-98FD-C9BB49D500BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90E6F200-2760-4660-98FD-C9BB49D500BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90E6F200-2760-4660-98FD-C9BB49D500BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90E6F200-2760-4660-98FD-C9BB49D500BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08299DAC-6C36-4BCA-8994-FCAF5080FB0E}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Linq;
namespace DysonV8
{
public static class Client
{
private static HttpClient HttpClient { get; }
static Client()
{
HttpClient = new HttpClient();
}
public static string Get(string url)
{
return GetAsync(url).Result;
}
public static async Task<string> GetAsync(string url)
{
try
{
var result = await HttpClient.GetAsync(url);
return await result.Content.ReadAsStringAsync();
}
catch (Exception e)
{
return e.ToString();
}
}
public static List<string> FindLinks(string file, bool onlyHttpLinks = true)
{
List<string> list = new List<string>();
MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);
foreach (Match m in m1)
{
string value = m.Groups[1].Value;
string href;
Match m2 = Regex.Match(value, @"href=\""(.*?)\""", RegexOptions.Singleline);
if (m2.Success)
{
href = m2.Groups[1].Value;
list.Add(href);
}
}
list = list.Distinct().ToList();
list.RemoveAll(x => x == "/" || x.Contains('#'));
if (onlyHttpLinks)
list.RemoveAll(x => !x.Contains("http", StringComparison.OrdinalIgnoreCase));
return list;
}
public static void DownloadPage(string path, string url)
{
var page = GetAsync(url).Result;
string filename = $"{url.Replace('/', '_').Replace(':', '_').Replace('?', '_')}.html";
Console.WriteLine($"{(FileSaver.Save(path, filename, page) ? "Done" : "Failed")}");
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DysonV8
{
public static class FileSaver
{
public static bool Save(string path, string filemane, string content)
{
try
{
Directory.CreateDirectory(path);
File.WriteAllText(Path.Combine(path,filemane), content);
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
namespace DysonV8
{
public class Program
{
static void Main(string[] args)
{
var res = Client.GetAsync("https://stackoverflow.com/questions/14854878/creating-new-thread-with-method-with-parameter").Result;
var links = Client.FindLinks(res);
foreach (var link in links)
{
Console.WriteLine($"For : {link}");
new Thread(() => Client.DownloadPage("output", link)).Start();
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment