Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created March 21, 2018 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnboker/ae7ae5d25140cc74b77163b8d55a5afb to your computer and use it in GitHub Desktop.
Save johnboker/ae7ae5d25140cc74b77163b8d55a5afb to your computer and use it in GitHub Desktop.
Spider a repo showing files and directories.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GitSpider
{
class Program
{
private static string ApiBase => "https://api.github.com/";
private static string User => "johnboker";
private static string Repo => "AoC2016";
static async Task Main(string[] args)
{
await SpiderRepo();
}
private static async Task SpiderRepo(int depth = 0, string path = "")
{
var contents = await GetContents(path);
foreach (var c in contents)
{
Console.WriteLine($"{new string('-', depth)}{c.type} - {c.path}");
if (c.type == "dir")
{
await SpiderRepo(depth+1, c.path);
}
}
}
private static async Task<Content[]> GetContents(string path = "")
{
var url = $"{ApiBase}repos/{User}/{Repo}/contents/{path}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", User);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
var contents = JsonConvert.DeserializeObject<Content[]>(json);
return contents;
}
}
}
public class Links
{
public string self { get; set; }
public string git { get; set; }
public string html { get; set; }
}
public class Content
{
public string type { get; set; }
public int size { get; set; }
public string name { get; set; }
public string path { get; set; }
public string sha { get; set; }
public string url { get; set; }
public string git_url { get; set; }
public string html_url { get; set; }
public string download_url { get; set; }
public Links links { get; set; }
}
}
file - .DS_Store
file - .gitignore
file - AoC2016.sln
file - AoC2016.userprefs
dir - AoC2016
-file - AoC2016/.DS_Store
-file - AoC2016/AdventOfCodeDay.cs
-file - AoC2016/AoC2016.csproj
-file - AoC2016/AoC2016.sln
-file - AoC2016/AoC2016.userprefs
-file - AoC2016/Program.cs
-dir - AoC2016/Properties
--file - AoC2016/Properties/AssemblyInfo.cs
-dir - AoC2016/Solutions
--file - AoC2016/Solutions/Day01.cs
--file - AoC2016/Solutions/Day02.cs
--file - AoC2016/Solutions/Day03.cs
--file - AoC2016/Solutions/Day04.cs
--file - AoC2016/Solutions/Day05.cs
--file - AoC2016/Solutions/Day06.cs
--file - AoC2016/Solutions/Day07.cs
--file - AoC2016/Solutions/Day08.cs
--file - AoC2016/Solutions/Day09.cs
--file - AoC2016/Solutions/Day10.cs
--file - AoC2016/Solutions/Day11.cs
--file - AoC2016/Solutions/Day12.cs
--file - AoC2016/Solutions/Day13.cs
--file - AoC2016/Solutions/Day14.cs
--file - AoC2016/Solutions/Day15.cs
--file - AoC2016/Solutions/Day16.cs
--file - AoC2016/Solutions/Day17.cs
--file - AoC2016/Solutions/Day18.cs
--file - AoC2016/Solutions/Day19.cs
--file - AoC2016/Solutions/Day20.cs
--file - AoC2016/Solutions/Day21.cs
--file - AoC2016/Solutions/Day22.cs
--file - AoC2016/Solutions/Day23.cs
--file - AoC2016/Solutions/Day24.cs
--file - AoC2016/Solutions/Day25.cs
-file - AoC2016/Utilities.cs
-dir - AoC2016/input
--file - AoC2016/input/day1-example.txt
--file - AoC2016/input/day1.txt
--file - AoC2016/input/day10.txt
--file - AoC2016/input/day11.txt
--file - AoC2016/input/day12.txt
--file - AoC2016/input/day15-andrew.txt
--file - AoC2016/input/day15-example.txt
--file - AoC2016/input/day15-part1.txt
--file - AoC2016/input/day15-part2.txt
--file - AoC2016/input/day2-example.txt
--file - AoC2016/input/day2.txt
--file - AoC2016/input/day20-example.txt
--file - AoC2016/input/day20.txt
--file - AoC2016/input/day21-example.txt
--file - AoC2016/input/day21.txt
--file - AoC2016/input/day22-example.txt
--file - AoC2016/input/day22.txt
--file - AoC2016/input/day23-example.txt
--file - AoC2016/input/day23-optimized.txt
--file - AoC2016/input/day23.txt
--file - AoC2016/input/day24-example.txt
--file - AoC2016/input/day24.txt
--file - AoC2016/input/day25.txt
--file - AoC2016/input/day3.txt
--file - AoC2016/input/day4-example.txt
--file - AoC2016/input/day4.txt
--file - AoC2016/input/day6.txt
--file - AoC2016/input/day7.txt
--file - AoC2016/input/day8-example.txt
--file - AoC2016/input/day8.txt
--file - AoC2016/input/day9-example.txt
--file - AoC2016/input/day9.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment