-
Notifications
You must be signed in to change notification settings - Fork 0
Code analysis
The code behind GhRI may seem complicated, so this is a guide to the code!
using Octokit;
namespace GhRI {
class GhRI {
static async System.Threading.Tasks.Task<int> Main(string[] args) {Here, you may notice that there is only one using statement. I do not using system; because you can still call functions in System anyway, you just need to prefix it with System. Example:
System.Console.WriteLine("HA!");I use Octokit to interface with the GitHub API. I was originally going to use HttpClient, but I kept getting 403 (Forbidden), so Octokit it is.
And finally, you will notice that my main function is asynchronous and returns a Task<int>. This is because most functions in the Octokit library are async, and even if I await, VS still gives me an error. I don't know if this is a GooD PRaCtiCe and don't care. As long as it works, that's what matters.
string Repository = args[1];
string User = args[0];Here we declare two variables: Repository and User.
GitHubClient Github = new GitHubClient(new ProductHeaderValue("GhRI-Finxx1"));
Repository Repo = await Github.Repository.Get(User, Repository);Here we get an instance of a GitHubClient, and use it to get the repository defined in the variables previously created.
if (Repo.Description.ToLower().Contains("library")) {
System.Console.WriteLine("This is a library you dummy!");
return -1;
}Here we figure out if the following project is a library. I tried looking for the library tag, but it never showed up, even on repositories that had it.
Release CurrentRelease;
try {
CurrentRelease = await Github.Repository.Release.GetLatest(User, Repository);
} catch (NotFoundException) {
System.Console.WriteLine("This repository has no releases!");
return -1;
}Here we get the current release of the specified repository or notify the user that a repository has no releases.
if (CurrentRelease.Assets.Count == 0) {
System.Console.WriteLine("This repository has no releases with binaries!");
return -1;
}Here we notify the user if the release has no assets.
System.Net.WebClient Downloader = new System.Net.WebClient();
int Chosen = 0;
if (CurrentRelease.Assets.Count > 1) {
System.Console.WriteLine("Choose one of the following:");
for (int i = 0; i < CurrentRelease.Assets.Count; i++) {
System.Console.WriteLine(i.ToString() + ": " + CurrentRelease.Assets[i].Name);
}
Chosen = int.Parse(System.Console.ReadLine());
}Here, the first line creates a WebClient we will use later to download the file. Next, we get the user to choose a file to download if there is more than one asset in the current release and store that in an int called Chosen.
System.Console.Write("\nAre you sure you want to download? y/n : ");
if (System.Console.ReadKey().KeyChar.Equals('y')) {
if (new System.Random().Next(5) == 1) { // I was bored okay
System.Console.Write("\nWhat am I, a slave or something???? y/n : ");
if (System.Console.ReadKey().KeyChar.Equals('n')) {
System.Console.WriteLine("\n:D");
return 0;
} else {
System.Console.WriteLine("\nD:");
}
}
Downloader.DownloadFile(CurrentRelease.Assets[Chosen].BrowserDownloadUrl, CurrentRelease.Assets[Chosen].Name);
}
return 0;Here, we make sure the user wants to download this file. Next, we pick a random number from 0 to 5 and if that number is 1, we add another prompt to slow the user down so they don't activate the rate limit that GitHub API's have. Finally, we use the WebClient we defined earlier to download the file at Assets[Chosen]. Finally, we return 0. And there it is, a complete analysis of the GhRI code.