-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54f3799
commit 5437787
Showing
14 changed files
with
416 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
using PipManager.PackageSearch.Wrappers.Query; | ||
|
||
namespace PipManager.PackageSearch; | ||
|
||
public interface IPackageSearchService | ||
{ | ||
public Task<QueryWrapper> Query(string name, int page=1); | ||
} |
This file contains 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,65 @@ | ||
using HtmlAgilityPack; | ||
using PipManager.PackageSearch.Wrappers.Query; | ||
|
||
namespace PipManager.PackageSearch; | ||
|
||
public class PackageSearchService(HttpClient httpClient) : IPackageSearchService | ||
{ | ||
public Dictionary<(string, int), QueryWrapper> QueryCaches { get; set; } = []; | ||
public async Task<QueryWrapper> Query(string name, int page=1) | ||
{ | ||
if(QueryCaches.ContainsKey((name, page))) | ||
{ | ||
return QueryCaches[(name, page)]; | ||
} | ||
var htmlContent = ""; | ||
try | ||
{ | ||
htmlContent = await httpClient.GetStringAsync($"https://pypi.org/search/?q={name}&page={page}"); | ||
} | ||
catch (Exception exception) when (exception is TaskCanceledException || exception is HttpRequestException) | ||
{ | ||
return new QueryWrapper | ||
{ | ||
Status = QueryStatus.Timeout | ||
}; | ||
} | ||
var htmlDocument = new HtmlDocument(); | ||
htmlDocument.LoadHtml(htmlContent); | ||
|
||
var queryWrapper = new QueryWrapper | ||
{ | ||
ResultCount = htmlDocument.DocumentNode.SelectSingleNode("/html/body/main/div/div/div[2]/form/div[1]/div[1]/p/strong").InnerText | ||
}; | ||
queryWrapper.Status = queryWrapper.ResultCount != "0" ? QueryStatus.Success : QueryStatus.NoResults; | ||
if (queryWrapper.Status == QueryStatus.NoResults) | ||
{ | ||
return queryWrapper; | ||
} | ||
var pageNode = htmlDocument.DocumentNode.SelectSingleNode("/html/body/main/div/div/div[2]/form/div[3]/div"); | ||
queryWrapper.MaxPageNumber = pageNode == null ? 0 : int.Parse(pageNode.ChildNodes[^4].InnerText); | ||
|
||
try | ||
{ | ||
var resultList = htmlDocument.DocumentNode.SelectSingleNode("/html/body/main/div/div/div[2]/form/div[3]/ul").ChildNodes.Where(result => result.InnerLength != 15).Select(result => result.ChildNodes[1]); | ||
queryWrapper.Results = []; | ||
foreach (var resultItem in resultList) | ||
{ | ||
queryWrapper.Results.Add(new QueryListItemModel | ||
{ | ||
Name = resultItem.ChildNodes[1].ChildNodes[1].InnerText, | ||
Version = resultItem.ChildNodes[1].ChildNodes[3].InnerText, | ||
Description = resultItem.ChildNodes[3].InnerText, | ||
UpdateTime = DateTime.ParseExact(resultItem.ChildNodes[1].ChildNodes[5].ChildNodes[0].Attributes["datetime"].Value, "yyyy-MM-ddTHH:mm:sszzz", null, System.Globalization.DateTimeStyles.RoundtripKind) | ||
}); | ||
} | ||
} | ||
catch (ArgumentOutOfRangeException) | ||
{ | ||
QueryCaches.Add((name, page), queryWrapper); | ||
return queryWrapper; | ||
} | ||
QueryCaches.Add((name, page), queryWrapper); | ||
return queryWrapper; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/PipManager.PackageSearch/PipManager.PackageSearch.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="HtmlAgilityPack" Version="1.11.58" /> | ||
<PackageReference Include="Serilog" Version="3.1.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
10 changes: 10 additions & 0 deletions
10
src/PipManager.PackageSearch/Wrappers/Query/QueryListItemModel.cs
This file contains 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,10 @@ | ||
namespace PipManager.PackageSearch.Wrappers.Query; | ||
|
||
public class QueryListItemModel | ||
{ | ||
public required string Name { get; set; } | ||
public required string Version { get; set; } | ||
public required string Description { get; set; } | ||
public DateTime UpdateTime { get; set; } | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
src/PipManager.PackageSearch/Wrappers/Query/QueryStatus.cs
This file contains 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,11 @@ | ||
| ||
|
||
namespace PipManager.PackageSearch.Wrappers.Query; | ||
|
||
public enum QueryStatus | ||
{ | ||
Success, | ||
NoResults, | ||
Timeout | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
src/PipManager.PackageSearch/Wrappers/Query/QueryWrapper.cs
This file contains 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,10 @@ | ||
namespace PipManager.PackageSearch.Wrappers.Query; | ||
|
||
public class QueryWrapper | ||
{ | ||
public QueryStatus Status { get; set; } | ||
public string? ResultCount { get; set; } | ||
public List<QueryListItemModel>? Results { get; set;} | ||
public int MaxPageNumber { get; set; } | ||
} | ||
|
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.