diff --git a/GithubNet/GithubNet.csproj b/GithubNet/GithubNet.csproj index e65f3b2..31f0fa0 100644 --- a/GithubNet/GithubNet.csproj +++ b/GithubNet/GithubNet.csproj @@ -6,7 +6,7 @@ enable True GithubNet - 0.3 + 0.3.2 liebki 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. liebki @@ -18,7 +18,7 @@ True icon.png GithubDataNet - Reworked everything, new methods, new types, new structure and new README.. + New GetReadmeAuto() method, fixed the username in the GetFullRepository() method and changed the standard GetReadMeUrl() method.. diff --git a/GithubNet/Managers/GithubNetManager.cs b/GithubNet/Managers/GithubNetManager.cs index 9354bee..643484b 100644 --- a/GithubNet/Managers/GithubNetManager.cs +++ b/GithubNet/Managers/GithubNetManager.cs @@ -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"; diff --git a/GithubNet/Managers/UtilManager.cs b/GithubNet/Managers/UtilManager.cs index 5e2ed16..bb956ec 100644 --- a/GithubNet/Managers/UtilManager.cs +++ b/GithubNet/Managers/UtilManager.cs @@ -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 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; diff --git a/GithubNet/Models/Repositories/FullRepository.cs b/GithubNet/Models/Repositories/FullRepository.cs index 040e9e5..1f97557 100644 --- a/GithubNet/Models/Repositories/FullRepository.cs +++ b/GithubNet/Models/Repositories/FullRepository.cs @@ -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}}}"; diff --git a/GithubNet/Models/Repositories/RepositoryBase.cs b/GithubNet/Models/Repositories/RepositoryBase.cs index fa0e09d..beb11d8 100644 --- a/GithubNet/Models/Repositories/RepositoryBase.cs +++ b/GithubNet/Models/Repositories/RepositoryBase.cs @@ -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() diff --git a/README.md b/README.md index 791215f..d2edd69 100644 --- a/README.md +++ b/README.md @@ -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.