-
Notifications
You must be signed in to change notification settings - Fork 0
Web downloader #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vologin-dmitry
wants to merge
2
commits into
master
Choose a base branch
from
WebDownloader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Web downloader #75
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| image: Visual Studio 2017 | ||
| image: Visual Studio 2019 | ||
|
|
||
| init: | ||
| - git config --global core.autocrlf true | ||
|
|
||
| environment: | ||
| matrix: | ||
| - solution: Semester4/WebPageDownloader/WebPageDownloader.sln | ||
|
|
||
| before_build: | ||
| - nuget restore semester2/6.1/HW6T2.sln | ||
| - nuget restore %solution% | ||
|
|
||
| build: | ||
| project: semester2/2.3/2.3.sln | ||
| project: $(solution) | ||
|
|
||
| test_script: | ||
| - dotnet test %solution% |
21 changes: 21 additions & 0 deletions
21
semester4/WebPageDownloader/WebPageDownloader.Tests/UnitTest1.fs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module WebPageDownloader.Tests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open Downloader | ||
|
|
||
| [<Test>] | ||
| let ``Cheeze test`` () = | ||
| let data = (getAllMentionedPages "https://akulovka.com/blog/syr/").Value | ||
| data |> snd |> List.length |> should equal 27 | ||
| data |> snd |> List.item 0 |> fst |> should equal "https://akulovka.com/blog/rss/" | ||
|
|
||
| [<Test>] | ||
| let ``Github test`` () = | ||
| let data = (getAllMentionedPages "https://github.com/").Value | ||
| data |> snd |> List.length |> should equal 53 | ||
| data |> snd |> List.item 0 |> fst |> should equal "https://github.githubassets.com" | ||
|
|
||
| [<Test>] | ||
| let ``Invalid url test`` () = | ||
| getAllMentionedPages "https://theresnotgingihope.com" |> should equal None |
25 changes: 25 additions & 0 deletions
25
semester4/WebPageDownloader/WebPageDownloader.Tests/WebPageDownloader.Tests.fsproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsUnit" Version="4.0.1" /> | ||
| <PackageReference Include="nunit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="UnitTest1.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\WebPageDownloader\WebPageDownloader.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "WebPageDownloader", "WebPageDownloader\WebPageDownloader.fsproj", "{1BC317B7-AD15-4A86-B19E-D9F1ACBF11B5}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "WebPageDownloader.Tests", "WebPageDownloader.Tests\WebPageDownloader.Tests.fsproj", "{E0403355-E29F-4F86-914A-E08F63A77FCB}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {1BC317B7-AD15-4A86-B19E-D9F1ACBF11B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {1BC317B7-AD15-4A86-B19E-D9F1ACBF11B5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {1BC317B7-AD15-4A86-B19E-D9F1ACBF11B5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {1BC317B7-AD15-4A86-B19E-D9F1ACBF11B5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {E0403355-E29F-4F86-914A-E08F63A77FCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E0403355-E29F-4F86-914A-E08F63A77FCB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E0403355-E29F-4F86-914A-E08F63A77FCB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E0403355-E29F-4F86-914A-E08F63A77FCB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
50 changes: 50 additions & 0 deletions
50
semester4/WebPageDownloader/WebPageDownloader/Downloader.fs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| module Downloader | ||
|
|
||
| open System.Net | ||
| open System.IO | ||
| open System.Text.RegularExpressions | ||
| open System | ||
|
|
||
| let expr = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>\S+))" | ||
| let regex = Regex (expr, RegexOptions.IgnoreCase) | ||
|
|
||
| /// Downloads page and returns page and its url | ||
| let fetchAsync url = | ||
| async { | ||
| try | ||
| let request = WebRequest.Create(Uri(url)) | ||
| use! response = request.AsyncGetResponse() | ||
| use stream = response.GetResponseStream() | ||
| use reader = new StreamReader(stream) | ||
| let page = reader.ReadToEnd() | ||
| return Some (url, page) | ||
| with | ||
| | _ -> | ||
| return None | ||
| } | ||
|
|
||
| /// Finds all links on given page | ||
| let getAllLinks page = | ||
| regex.Matches(page) |> Seq.map (fun (x : Match) -> x.Groups.[1].Value) |> Seq.toList | ||
|
|
||
| /// Tales list of urls and download all page by every url | ||
| let getAllPages links = | ||
| List.map fetchAsync links |> Async.Parallel |> Async.RunSynchronously |> Array.toList | ||
|
|
||
| /// Returns page located by given url and all pages mentioned there | ||
| let getAllMentionedPages url = | ||
| let firstPage = fetchAsync url |> Async.RunSynchronously | ||
| match firstPage with | ||
| | None -> None | ||
| | Some (_, page) -> | ||
| let pages = page |> getAllLinks |> getAllPages |> List.filter (fun x -> x.IsSome) |> List.map Option.get | ||
| Some (firstPage.Value, pages) | ||
|
|
||
| /// Prints all urls mentioned in page located by given url and number of symbols in these pages | ||
| let printAllData url = | ||
| match getAllMentionedPages url with | ||
| | None -> printfn "Nothing had been found, seems like url is wrong" | ||
| | Some (page, mentioned) -> | ||
| printfn "Original url %s --- %i symbols" (fst page) ((snd page).Length) | ||
| mentioned |> List.length |> printfn "Found %i valid links" | ||
| mentioned |> List.iter (fun item -> printfn "Url: %s --- %i symbols" (fst item) (snd item).Length) | ||
12 changes: 12 additions & 0 deletions
12
semester4/WebPageDownloader/WebPageDownloader/WebPageDownloader.fsproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Downloader.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень уж разрешающий регэксп. Если просто где-то на странице найдётся, например,
href='sdfds", оно подумает, что это ссылка. Правильнее было бы искать начало тэга<a