Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- Reverted namespace changes, only 'GithubNet' now
- Added project url, readme url etc.
- Added custom toString to all types
  • Loading branch information
liebki committed Dec 2, 2023
1 parent 2ad24ee commit 4e455c2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 70 deletions.
6 changes: 1 addition & 5 deletions GithubNet/Managers/GithubNetClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using GithubNet.Models.Extra;
using GithubNet.Models.Repositories;
using GithubNet.Models.Userprofiles;

namespace GithubNet.Managers
namespace GithubNet
{
public class GithubNetClient
{
Expand Down
43 changes: 27 additions & 16 deletions GithubNet/Managers/GithubNetManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using GithubNet.Models.Extra;
using GithubNet.Models.Repositories;
using GithubNet.Models.Userprofiles;
using HtmlAgilityPack;
using HtmlAgilityPack;
using System;
using System.Net;

namespace GithubNet.Managers
namespace GithubNet
{
public static class GithubNetManager
{
Expand All @@ -15,7 +12,7 @@ public static class GithubNetManager
internal static List<string> GetAllTrendRepositoryUrls(string CustomTrendingQuery)
{
List<string> TrendRepositoryUrlList = new();
HtmlDocument GithubPageDocument = UtilManager.GetHtmlDoc(CustomTrendingQuery, true);
(HtmlDocument GithubPageDocument, string _) = UtilManager.GetHtmlDoc(CustomTrendingQuery, true);

IEnumerable<HtmlNode> DocumentNodes = GithubPageDocument.QuerySelectorAll("article.Box-row");
foreach (HtmlNode docNode in DocumentNodes)
Expand Down Expand Up @@ -46,7 +43,7 @@ internal static List<FullRepository> GetAllFullRepositoriesFromTrends(string Cus
internal static List<TrendRepository> GetAllTrendRepositories(string CustomTrendingQuery = "https://github.com/trending")
{
List<TrendRepository> TrendingRepositoriesList = new();
HtmlDocument GithubPageDocument = UtilManager.GetHtmlDoc(CustomTrendingQuery, true);
(HtmlDocument GithubPageDocument, string _) = UtilManager.GetHtmlDoc(CustomTrendingQuery, true);

IEnumerable<HtmlNode> DocumentNodes = GithubPageDocument.QuerySelectorAll("article.Box-row");
foreach (HtmlNode docNode in DocumentNodes)
Expand Down Expand Up @@ -99,7 +96,7 @@ internal static List<TrendRepository> GetAllTrendRepositories(string CustomTrend
}
}

TrendRepository repository = new(TotalStarsFiltered, ProgrammingLanguage, TotalForksFiltered, StarsTodayFiltered, UsernameFiltered, RepositoryName, DescriptionFiltered);
TrendRepository repository = new(TotalStarsFiltered, RepositoryLinkFiltered, ProgrammingLanguage, TotalForksFiltered, StarsTodayFiltered, UsernameFiltered, RepositoryName, DescriptionFiltered);
TrendingRepositoriesList.Add(repository);
}
catch (Exception e)
Expand Down Expand Up @@ -156,8 +153,8 @@ internal static (LightUserprofile Userprofile, IEnumerable<UserRepository> UserR
(LightUserprofile profileObj, _) = GetUserprofileData(Username);
LightUserprofile lightUserprofile = profileObj;

HtmlDocument UserRepositoriesDocument = UtilManager.GetHtmlDoc($"https://github.com/{Username}?tab=repositories", true);
HtmlNode UserRepositoriesNode = UserRepositoriesDocument.DocumentNode;
(HtmlDocument LightUserAndRepositoryDocument, string _) = UtilManager.GetHtmlDoc($"https://github.com/{Username}?tab=repositories", true);
HtmlNode UserRepositoriesNode = LightUserAndRepositoryDocument.DocumentNode;

IEnumerable<HtmlNode> RawUserRepositoriesList = UserRepositoriesNode.QuerySelectorAll("li.col-12");
List<UserRepository> UserRepositoriesList = new();
Expand All @@ -167,6 +164,12 @@ internal static (LightUserprofile Userprofile, IEnumerable<UserRepository> UserR
HtmlNode nameNode = repo.SelectSingleNode(".//h3/a");
string name = (nameNode != null && !string.IsNullOrEmpty(nameNode.InnerText.Trim())) ? nameNode.InnerText.Trim() : "None";

string repoUrl = "None";
if (nameNode != null)
{
repoUrl = $"https://github.com{UtilManager.ClearStrings(nameNode.Attributes["href"].Value)}";
}

HtmlNode descriptionNode = repo.SelectSingleNode(".//p[@itemprop='description']");
string description = (descriptionNode != null && !string.IsNullOrEmpty(descriptionNode.InnerText.Trim())) ? descriptionNode.InnerText.Trim() : "None";

Expand Down Expand Up @@ -196,7 +199,7 @@ internal static (LightUserprofile Userprofile, IEnumerable<UserRepository> UserR
HtmlNode lastUpdateNode = repo.SelectSingleNode(".//relative-time/@datetime");
string lastUpdate = (lastUpdateNode != null && !string.IsNullOrEmpty(lastUpdateNode.GetAttributeValue("datetime", string.Empty).Trim())) ? lastUpdateNode.GetAttributeValue("datetime", string.Empty).Trim() : "None";

UserRepository userRepository = new(isFork, licenseValue, lastUpdate, language, starCountValue, forkCountValue, Username, name, description);
UserRepository userRepository = new(isFork, licenseValue, lastUpdate, repoUrl, language, starCountValue, forkCountValue, Username, name, description);
UserRepositoriesList.Add(userRepository);
}

Expand All @@ -210,8 +213,8 @@ internal static (LightUserprofile Userprofile, IEnumerable<UserRepository> UserR

internal static FullRepository GetFullRepository(string RepoUrl)
{
HtmlDocument RepositoryDocument = UtilManager.GetHtmlDoc(RepoUrl, true);
HtmlNode RepositoryNode = RepositoryDocument.DocumentNode;
(HtmlDocument fullRepositoryDocument, string finalUrl) = UtilManager.GetHtmlDoc(RepoUrl, true);
HtmlNode RepositoryNode = fullRepositoryDocument.DocumentNode;

string UsernameValue = UtilManager.GetUsernameFromGitHubUrl(RepoUrl);

Expand All @@ -231,6 +234,14 @@ internal static FullRepository GetFullRepository(string RepoUrl)
DescriptionValue = UtilManager.ClearStrings(Description.InnerHtml);
}

HtmlNode ProjectUrl = RepositoryNode.SelectSingleNode("/html/body/div[1]/div[4]/div/main/turbo-frame/div/div/div/div[2]/div[2]/div/div[1]/div/div/div[1]/span/a");
string ProjectUrlValue = "None";

if (ProjectUrl != null)
{
ProjectUrlValue = UtilManager.ClearStrings(ProjectUrl.Attributes["href"].Value);
}

HtmlNode OpenIssueCount = RepositoryNode.SelectSingleNode("//*[@id=\"issues-repo-tab-count\"]");
int OpenIssueCountValue = 0;

Expand Down Expand Up @@ -345,7 +356,7 @@ internal static FullRepository GetFullRepository(string RepoUrl)
ForksValue = UtilManager.ParseNumberValue(UtilManager.ClearStrings(Forks.InnerHtml));
}

FullRepository fullRepository = new($"https://github.com{RepoUrl}", OpenIssueCountValue, OpenPullRequestsCountValue, TotalCommitsCountValue, LastCommitTextValue, WatcherCountValue, ContributorCountValue, TopicValues.ToArray(), ReleaseCountValue, LatestReleaseTextValue, TagCountValue, BranchCountValue, DefaultBranchNameValue, MainLanguageValue, StarsValue, ForksValue, UsernameValue, RepoNameValue, DescriptionValue);
FullRepository fullRepository = new(ProjectUrlValue, OpenIssueCountValue, OpenPullRequestsCountValue, TotalCommitsCountValue, LastCommitTextValue, WatcherCountValue, ContributorCountValue, TopicValues.ToArray(), ReleaseCountValue, LatestReleaseTextValue, TagCountValue, BranchCountValue, DefaultBranchNameValue, finalUrl, MainLanguageValue, StarsValue, ForksValue, UsernameValue, RepoNameValue, DescriptionValue);
return fullRepository;
}

Expand Down Expand Up @@ -390,7 +401,7 @@ private static int GetWatcherCount(HtmlNode DocumentNode)

private static IEnumerable<string> GetAllRepositorsUrls(string Username)
{
HtmlDocument UserRepositoriesDocument = UtilManager.GetHtmlDoc($"https://github.com/{Username}?tab=repositories", true);
(HtmlDocument UserRepositoriesDocument, string _) = UtilManager.GetHtmlDoc($"https://github.com/{Username}?tab=repositories", true);
HtmlNode UserRepositoriesNode = UserRepositoriesDocument.DocumentNode;

IEnumerable<HtmlNode> UserRepositorieList = UserRepositoriesNode.QuerySelectorAll("li.col-12");
Expand All @@ -414,7 +425,7 @@ private static IEnumerable<string> GetAllRepositorsUrls(string Username)

private static (LightUserprofile profil, HtmlDocument profilepage) GetUserprofileData(string Username)
{
HtmlDocument GithubProfilepage = UtilManager.GetHtmlDoc($"https://github.com/{Username}", true);
(HtmlDocument GithubProfilepage, string _) = UtilManager.GetHtmlDoc($"https://github.com/{Username}", true);

HtmlNode ProfileName = GithubProfilepage.QuerySelector(".p-name");
string ProfileNameValue = ProfileName.InnerHtml.Trim();
Expand Down
10 changes: 4 additions & 6 deletions GithubNet/Managers/UtilManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using GithubNet.Models.Extra;
using GithubNet.Models.Repositories;
using HtmlAgilityPack;
using HtmlAgilityPack;
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace GithubNet.Managers
namespace GithubNet
{
internal static class UtilManager
{
Expand Down Expand Up @@ -62,7 +60,7 @@ private static bool IsGitHubRepositoryUrl(string url)
return url.StartsWith("https://github.com/") && url.Count(c => c == '/') >= 4;
}

internal static HtmlDocument GetHtmlDoc(string Url, bool IsGithubPage = false)
internal static (HtmlDocument htmlDocument, string finalUrl) GetHtmlDoc(string Url, bool IsGithubPage = false)
{
string RequestUrl = Url;
if (!Url.StartsWith("https://github.com") && IsGithubPage)
Expand All @@ -76,7 +74,7 @@ internal static HtmlDocument GetHtmlDoc(string Url, bool IsGithubPage = false)
};

HtmlDocument PageDocument = Page.Load(RequestUrl);
return PageDocument;
return (PageDocument, RequestUrl);
}

internal static int ParseNumberValue(string value)
Expand Down
2 changes: 1 addition & 1 deletion GithubNet/Models/Extra/TopValues.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace GithubNet.Models.Extra
namespace GithubNet
{
public class TopValues
{
Expand Down
26 changes: 23 additions & 3 deletions GithubNet/Models/Repositories/FullRepository.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Linq;

namespace GithubNet.Models.Repositories
namespace GithubNet
{
public class FullRepository : RepositoryBase
{
public FullRepository(string projectUrl, int openIssueCount, int openPullRequestsCount, int totalCommitsCount, string lastCommitText, int watcherCount, int contributorCount, string[] topics, int releaseCount, string lastReleaseText, int tagCount, int branchCount, string defaultBranchName, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(mainLanguage, totalStars, totalForks, username, repositoryName, description)
public FullRepository(string projectUrl, int openIssueCount, int openPullRequestsCount, int totalCommitsCount, string lastCommitText, int watcherCount, int contributorCount, string[] topics, int releaseCount, string lastReleaseText, int tagCount, int branchCount, string defaultBranchName, string url, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(url, mainLanguage, totalStars, totalForks, username, repositoryName, description)
{
ProjectUrl = projectUrl;
OpenIssueCount = openIssueCount;
Expand Down Expand Up @@ -48,9 +48,29 @@ public FullRepository(string projectUrl, int openIssueCount, int openPullRequest

public string DefaultBranchName { get; set; }

public string GetLastCommitUrl(string branch = "")
{
if (string.IsNullOrEmpty(branch))
{
return $"https://github.com/{this.Username}/{this.RepositoryName}/commit/{this.DefaultBranchName}";
}

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)}={string.Join(",", Topics)}, {nameof(ReleaseCount)}={ReleaseCount.ToString()}, {nameof(LastReleaseText)}={LastReleaseText}, {nameof(TagCount)}={TagCount.ToString()}, {nameof(BranchCount)}={BranchCount.ToString()}, {nameof(DefaultBranchName)}={DefaultBranchName}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
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}}}";
}
}
}
30 changes: 28 additions & 2 deletions GithubNet/Models/Repositories/RepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Linq;

namespace GithubNet.Models.Repositories
namespace GithubNet
{
public class RepositoryBase
{
public RepositoryBase(string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description)
public RepositoryBase(string url, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description)
{
Url = url;
MainLanguage = mainLanguage;
TotalStars = totalStars;
TotalForks = totalForks;
Expand All @@ -15,6 +16,8 @@ public RepositoryBase(string mainLanguage, int totalStars, int totalForks, strin
Description = description;
}

public string Url { get; set; }

public string MainLanguage { get; set; }

public int TotalStars { get; set; }
Expand All @@ -27,6 +30,29 @@ public RepositoryBase(string mainLanguage, int totalStars, int totalForks, strin

public string Description { get; set; }

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

public string GetStarsUrl()
{
return $"https://github.com/{this.Username}/{this.RepositoryName}/stargazers";
}

public string ParseCreatedByText()
{
return $"{this.RepositoryName} created by {this.Username}";
}

public string GetForksUrl()
{
return $"https://github.com/{this.Username}/{this.RepositoryName}/forks";
}

public override string ToString()
{
return $"{{{nameof(Url)}={Url}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
}
}
}
6 changes: 3 additions & 3 deletions GithubNet/Models/Repositories/TrendRepository.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Linq;

namespace GithubNet.Models.Repositories
namespace GithubNet
{
public class TrendRepository : RepositoryBase
{
public TrendRepository(int starsToday, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(mainLanguage, totalStars, totalForks, username, repositoryName, description)
public TrendRepository(int starsToday, string url, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(url, mainLanguage, totalStars, totalForks, username, repositoryName, description)
{
StarsToday = starsToday;
}
Expand All @@ -14,7 +14,7 @@ public TrendRepository(int starsToday, string mainLanguage, int totalStars, int

public override string ToString()
{
return $"{{{nameof(StarsToday)}={StarsToday.ToString()}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
return $"{{{nameof(StarsToday)}={StarsToday.ToString()}, {nameof(Url)}={Url}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
}
}
}
7 changes: 3 additions & 4 deletions GithubNet/Models/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Linq;

namespace GithubNet.Models.Repositories
namespace GithubNet
{
public class UserRepository : RepositoryBase
{

public UserRepository(bool isForked, string licenseText, string lastUpdateText, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(mainLanguage, totalStars, totalForks, username, repositoryName, description)
public UserRepository(bool isForked, string licenseText, string lastUpdateText, string url, string mainLanguage, int totalStars, int totalForks, string username, string repositoryName, string description) : base(url, mainLanguage, totalStars, totalForks, username, repositoryName, description)
{
IsForked = isForked;
LicenseText = licenseText;
Expand All @@ -21,7 +20,7 @@ public UserRepository(bool isForked, string licenseText, string lastUpdateText,

public override string ToString()
{
return $"{{{nameof(IsForked)}={IsForked.ToString()}, {nameof(LicenseText)}={LicenseText}, {nameof(LastUpdateText)}={LastUpdateText}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
return $"{{{nameof(IsForked)}={IsForked.ToString()}, {nameof(LicenseText)}={LicenseText}, {nameof(LastUpdateText)}={LastUpdateText}, {nameof(Url)}={Url}, {nameof(MainLanguage)}={MainLanguage}, {nameof(TotalStars)}={TotalStars.ToString()}, {nameof(TotalForks)}={TotalForks.ToString()}, {nameof(Username)}={Username}, {nameof(RepositoryName)}={RepositoryName}, {nameof(Description)}={Description}}}";
}
}
}
2 changes: 1 addition & 1 deletion GithubNet/Models/Userprofiles/FullUserprofile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace GithubNet.Models.Userprofiles
namespace GithubNet
{
public class FullUserprofile : LightUserprofile
{
Expand Down
2 changes: 1 addition & 1 deletion GithubNet/Models/Userprofiles/LightUserprofile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace GithubNet.Models.Userprofiles
namespace GithubNet
{
public class LightUserprofile
{
Expand Down
Loading

0 comments on commit 4e455c2

Please sign in to comment.