Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- Fixed username in GetFullRepository()
- Included new GetReadmeAuto() to enhance the chance of finding the readme file and content
- Changed the standard GetReadMeUrl()
  • Loading branch information
liebki committed Dec 3, 2023
1 parent 4e455c2 commit 5f04268
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
4 changes: 2 additions & 2 deletions GithubNet/GithubNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>GithubNet</Title>
<Version>0.3</Version>
<Version>0.3.2</Version>
<Authors>liebki</Authors>
<Description>Github(Data)Net is a simple C# library, using HtmlAgilityPack to retrieve several things from GitHub, things like trending repositories, profiles of users, the repositories of users and related information.</Description>
<Copyright>liebki</Copyright>
Expand All @@ -18,7 +18,7 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageIcon>icon.png</PackageIcon>
<PackageId>GithubDataNet</PackageId>
<PackageReleaseNotes>Reworked everything, new methods, new types, new structure and new README..</PackageReleaseNotes>
<PackageReleaseNotes>New GetReadmeAuto() method, fixed the username in the GetFullRepository() method and changed the standard GetReadMeUrl() method..</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion GithubNet/Managers/GithubNetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ internal static FullRepository GetFullRepository(string RepoUrl)
(HtmlDocument fullRepositoryDocument, string finalUrl) = UtilManager.GetHtmlDoc(RepoUrl, true);
HtmlNode RepositoryNode = fullRepositoryDocument.DocumentNode;

string UsernameValue = UtilManager.GetUsernameFromGitHubUrl(RepoUrl);
string UsernameValue = RepoUrl.Split('/')[1];

HtmlNode RepoName = RepositoryNode.SelectSingleNode("/html/body/div[1]/div[4]/div/main/div/div[1]/div[1]/div/strong/a");
string RepoNameValue = "None";
Expand Down
48 changes: 48 additions & 0 deletions GithubNet/Managers/UtilManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,54 @@ internal static string GetUsernameFromGitHubUrl(string url)
}
}

public static async Task<(string ReadmeContent, string UsedUrl)> FindReadme(string username, string projectname)
{
string[] ReadmeUrls = BuildUrlList(username, projectname);
(string ReadmeContent, string UsedUrl) readme = ("None", "None");

using HttpClient client = new();
foreach (string url in ReadmeUrls)
{
try
{
if (await IsValidRequest(client, url))
{
string content = await client.GetStringAsync(url);
readme = (content, url);

break;
}
}
catch (Exception)
{
//
}
}

return readme;
}

private static string[] BuildUrlList(string username, string projectname)
{
string[] urls = {
$"https://raw.githubusercontent.com/{username}/{projectname}/main/readme.md",
$"https://raw.githubusercontent.com/{username}/{projectname}/master/readme.md",
$"https://raw.githubusercontent.com/{username}/{projectname}/main/README.md",
$"https://raw.githubusercontent.com/{username}/{projectname}/master/README.md",
$"https://raw.githubusercontent.com/{username}/{projectname}/main/ReadMe.md",
$"https://raw.githubusercontent.com/{username}/{projectname}/master/ReadMe.md"
};

return urls;
}


private static async Task<bool> IsValidRequest(HttpClient client, string url)
{
HttpResponseMessage response = await client.GetAsync(url);
return response.IsSuccessStatusCode;
}

private static bool IsGitHubRepositoryUrl(string url)
{
return url.StartsWith("https://github.com/") && url.Count(c => c == '/') >= 4;
Expand Down
10 changes: 0 additions & 10 deletions GithubNet/Models/Repositories/FullRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ public string GetLastCommitUrl(string branch = "")
return $"https://github.com/{this.Username}/{this.RepositoryName}/commit/{branch}";
}

public override string GetReadMeUrl(string branch = "")
{
if (string.IsNullOrEmpty(branch))
{
return $"https://raw.githubusercontent.com/{this.Username}/{this.RepositoryName}/{this.DefaultBranchName}/README.md";
}

return $"https://raw.githubusercontent.com/{this.Username}/{this.RepositoryName}/{branch}/README.md";
}

public override string ToString()
{
return $"{{{nameof(ProjectUrl)}={ProjectUrl}, {nameof(OpenIssueCount)}={OpenIssueCount.ToString()}, {nameof(OpenPullRequestsCount)}={OpenPullRequestsCount.ToString()}, {nameof(TotalCommitsCount)}={TotalCommitsCount.ToString()}, {nameof(LastCommitText)}={LastCommitText}, {nameof(WatcherCount)}={WatcherCount.ToString()}, {nameof(ContributorCount)}={ContributorCount.ToString()}, {nameof(Topics)}={Topics}, {nameof(ReleaseCount)}={ReleaseCount.ToString()}, {nameof(LastReleaseText)}={LastReleaseText}, {nameof(TagCount)}={TagCount.ToString()}, {nameof(BranchCount)}={BranchCount.ToString()}, {nameof(DefaultBranchName)}={DefaultBranchName}, {nameof(Url)}={Url}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
Expand Down
10 changes: 8 additions & 2 deletions GithubNet/Models/Repositories/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ public RepositoryBase(string url, string mainLanguage, int totalStars, int total

public string Description { get; set; }

public virtual string GetReadMeUrl(string branch = "")
public virtual string GetReadMeUrl(string branch, string readmeFile = "README.md")
{
return $"https://raw.githubusercontent.com/{this.Username}/{this.RepositoryName}/{branch}/README.md";
return $"https://raw.githubusercontent.com/{this.Username}/{this.RepositoryName}/{branch}/{readmeFile}";
}

public async Task<(string ReadmeContent, string Url)> GetReadmeAuto()
{
(string Content, string Url) readme = await UtilManager.FindReadme(this.Username, this.RepositoryName);
return readme;
}

public string GetStarsUrl()
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ string GetTopicUrlFromTopicName(string topicName);
For a demonstration of the library's functionality, refer to the included `GithubNetDemo` project.


## To-Do

- Crawl every repository from a user, currently only the first page is crawled so these are not all repositories of a user.


## License 📜

GithubNet is licensed under the GNU General Public License v3.0.
Expand Down

0 comments on commit 5f04268

Please sign in to comment.