1+ using System ;
2+ using System . Net ;
3+ using Mono . Options ;
4+ using Newtonsoft . Json . Linq ;
5+ using System . Collections . Generic ;
6+ using System . Text . RegularExpressions ;
7+
8+ namespace Gth {
9+ public struct UserItem {
10+ public int Count ;
11+ public string [ ] Arr ;
12+
13+ public UserItem ( int count ) {
14+ Count = count ;
15+ Arr = new string [ count ] ;
16+ }
17+ }
18+
19+ public class User {
20+ private const string API_USERS_URL = "https://api.github.com/users/" ;
21+ private const string USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36" ;
22+ private Regex fetch_patt = new Regex ( "(repos|gists|followers|following)" ) ;
23+ private string url ;
24+ private JObject json ;
25+
26+ public string Name ;
27+ public string Bio ;
28+ public string Link ;
29+ public UserItem Repos ;
30+ public UserItem Gists ;
31+ public UserItem Followers ;
32+ public UserItem Following ;
33+
34+ public User ( string name ) {
35+ this . url = API_USERS_URL + name ;
36+
37+ var client = new WebClient ( ) ;
38+ client . Headers . Add ( "user-agent" , USER_AGENT ) ;
39+
40+ var res = client . DownloadString ( this . url ) ;
41+ json = JObject . Parse ( res ) ;
42+
43+ this . Name = json . Value < string > ( "name" ) ;
44+ this . Bio = json . Value < string > ( "bio" ) ;
45+ this . Link = json . Value < string > ( "html_url" ) ;
46+
47+ this . Repos = new UserItem ( json . Value < int > ( "public_repos" ) ) ;
48+ this . Gists = new UserItem ( json . Value < int > ( "public_gists" ) ) ;
49+ this . Followers = new UserItem ( json . Value < int > ( "followers" ) ) ;
50+ this . Following = new UserItem ( json . Value < int > ( "following" ) ) ;
51+ }
52+
53+ public void Fetch ( string thing ) {
54+ if ( ! fetch_patt . Match ( thing ) . Success ) {
55+ Console . WriteLine ( $ "Unsupported endpoint: { thing } ") ;
56+ return ;
57+ }
58+
59+ var url = this . url + "/" + thing ;
60+ var client = new WebClient ( ) ;
61+ client . Headers . Add ( "user-agent" , USER_AGENT ) ;
62+
63+ var res = client . DownloadString ( url ) ;
64+ var arr = JArray . Parse ( res ) ;
65+
66+ if ( thing == "repos" ) {
67+ for ( int i = 0 ; i < arr . Count ; i ++ )
68+ this . Repos . Arr [ i ] = arr [ i ] . Value < string > ( "name" ) ;
69+ } else if ( thing == "gists" ) {
70+ for ( int i = 0 ; i < arr . Count ; i ++ )
71+ this . Gists . Arr [ i ] = arr [ i ] . Value < string > ( "description" ) ;
72+ } else if ( thing == "followers" ) {
73+ for ( int i = 0 ; i < arr . Count ; i ++ )
74+ this . Followers . Arr [ i ] = arr [ i ] . Value < string > ( "login" ) ;
75+ } else if ( thing == "following" ) {
76+ for ( int i = 0 ; i < arr . Count ; i ++ )
77+ this . Following . Arr [ i ] = arr [ i ] . Value < string > ( "login" ) ;
78+ }
79+ }
80+ }
81+ }
82+
83+ public class App {
84+ public static bool followers = false ;
85+ public static bool following = false ;
86+ public static bool repos = false ;
87+ public static bool gists = false ;
88+
89+ public static OptionSet opts = new OptionSet {
90+ { "f|followers" , "Shows user followers" , f => followers = f != null } ,
91+ { "F|following" , "Shows user following" , F => following = F != null } ,
92+ { "r|repos" , "Shows user repos" , r => repos = r != null } ,
93+ { "g|gists" , "Shows user gists" , g => gists = g != null }
94+ } ;
95+
96+ public static void Main ( string [ ] argv ) {
97+ List < string > args = opts . Parse ( argv ) ;
98+
99+ if ( ! ( args . Count > 0 ) ) {
100+ Console . WriteLine ( "No arguments" ) ;
101+ return ;
102+ } else {
103+ foreach ( string name in args ) {
104+ var user = new Gth . User ( name ) ;
105+
106+ Console . WriteLine ( $ "Name: { user . Name } ") ;
107+ Console . WriteLine ( $ "Bio: { user . Bio } ") ;
108+ Console . WriteLine ( $ "Link: { user . Link } ") ;
109+
110+ Console . WriteLine ( $ "Repos: { user . Repos . Count } ") ;
111+ if ( repos ) {
112+ user . Fetch ( "repos" ) ;
113+
114+ for ( var i = 0 ; i < user . Repos . Arr . Length ; i ++ )
115+ Console . WriteLine ( $ "| { i + 1 } . { user . Repos . Arr [ i ] } ") ;
116+ }
117+
118+ Console . WriteLine ( $ "Gists: { user . Gists . Count } ") ;
119+ if ( gists ) {
120+ user . Fetch ( "gists" ) ;
121+
122+ for ( var i = 0 ; i < user . Gists . Arr . Length ; i ++ )
123+ Console . WriteLine ( $ "| { i + 1 } . { user . Gists . Arr [ i ] } ") ;
124+ }
125+
126+ Console . WriteLine ( $ "Followers: { user . Followers . Count } ") ;
127+ if ( followers ) {
128+ user . Fetch ( "followers" ) ;
129+
130+ for ( var i = 0 ; i < user . Followers . Arr . Length ; i ++ )
131+ Console . WriteLine ( $ "| @{ user . Followers . Arr [ i ] } ") ;
132+ }
133+
134+ Console . WriteLine ( $ "Following: { user . Following . Count } ") ;
135+ if ( following ) {
136+ user . Fetch ( "following" ) ;
137+
138+ for ( var i = 0 ; i < user . Following . Arr . Length ; i ++ )
139+ Console . WriteLine ( $ "| @{ user . Following . Arr [ i ] } ") ;
140+ }
141+
142+ Console . Write ( "\n " ) ;
143+ }
144+ }
145+ }
146+ }
0 commit comments