-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmains.py
More file actions
223 lines (183 loc) · 6.55 KB
/
Copy pathmains.py
File metadata and controls
223 lines (183 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# encoding=utf8
"""set default encoding format"""
from __future__ import print_function
from __future__ import absolute_import
from future.standard_library import install_aliases
install_aliases()
from .utils import url_parse, get_req, geturl_req
import os
import sys
import argparse
import json
import urllib.request
import base64
import dateutil.parser
from prettytable import PrettyTable
GITHUB_API = 'https://api.github.com/'
API_TOKEN = os.environ.get('GITHUB_TOKEN')
# MAIN
def main():
"""main function"""
parser = argparse.ArgumentParser(
description='Github within the Command Line')
group = parser.add_mutually_exclusive_group()
group.add_argument('-n', '--url', type=str,
help="Get repos from the user profile's URL")
group.add_argument('-r', '--recursive', type=str,
help="Get the file structure from the repo link")
group.add_argument('-R', '--readme', type=str,
help="Get the raw version of the repo readme from repo link")
group.add_argument('-re', '--releases', type=str,
help="Get the list of releases from repo link")
group.add_argument('-dt', '--tarball', type=str,
help="Download the tarball of the given repo")
group.add_argument('-dz', '--zipball', type=str,
help="Download the zipball of the given repo")
group.add_argument('-op', '--openfile', type=str,
help="Show the contents of the given file in a repo")
group.add_argument('-f', '--followers', type=str,
help="Get followers of the user")
group.add_argument('-fo', '--following', type=str,
help="Get people following the user")
group.add_argument('-c', '--contributors', type=str,
help="Get contributors of a repo")
if len(sys.argv) == 1:
parser.print_help()
return
args = parser.parse_args()
# URL
if args.url:
name = url_parse(args.url)
url = GITHUB_API + 'users/' + name + '/repos'
# TREE
if args.recursive:
name = url_parse(args.recursive)
url = GITHUB_API + 'repos/' + name + '/branches/master'
response = get_req(url)
jsondata = json.loads(response)
sha = jsondata['commit']['commit']['tree']['sha']
url = GITHUB_API + 'repos/' + name + '/git/trees/' + sha + '?recursive=1'
# README
if args.readme:
name = url_parse(args.readme)
url = GITHUB_API + 'repos/' + name + '/readme'
# RELEASES
if args.releases:
name = url_parse(args.releases)
url = GITHUB_API + 'repos/' + name + '/releases'
# TARBALL/ZIPBALL
if args.tarball or args.zipball:
if args.tarball:
key = '/tarball/'
name = url_parse(args.tarball)
if args.zipball:
key = '/zipball/'
name = url_parse(args.zipball)
url = GITHUB_API + 'repos/' + name + key + 'master'
# OPEN ONE FILE
if args.openfile:
name = url_parse(args.openfile)
position = name.find('/')
user = name[:position+1]
rest = name[position+1:]
position = rest.find('/')
repo = rest[:position+1]
rest = rest[position+1:]
url = GITHUB_API + 'repos/' + user + repo + 'contents/' + rest
# GET RESPONSES
# TARBALL/ZIPBALL
if args.tarball or args.zipball:
response_url = geturl_req(url)
position = name.find('/')
name = name[position+1:]
if args.tarball:
name = name+'.tar.gz'
if args.zipball:
name = name+'.zip'
print("\nDownloading " + name + '...\n')
urllib.request.urlretrieve(response_url, name)
print(name + ' has been saved\n')
return
# FOLLOWERS
if args.followers:
name = url_parse(args.followers)
url = GITHUB_API + 'users/' + name + '/followers'
#FOLLOWING
if args.following:
name = url_parse(args.following)
url = GITHUB_API + 'users/' + name + '/following'
#CONTRIBUTORS
if args.contributors:
name = url_parse(args.contributors)
url = GITHUB_API + 'repos/' + name + '/contributors'
# OTHER OPTIONS
response = get_req(url)
jsondata = json.loads(response)
# USERNAME and URL
if args.url:
table = PrettyTable([" Repository ", "★ Star"])
table.align[" Repository "] = "l"
for i in jsondata:
table.add_row([i['name'], i['stargazers_count']])
print(table)
# RECURSIVE TREE
if args.recursive:
table = PrettyTable([" File/Folder ", " Size (Bytes) "])
table.align[" File/Folder "] = "l"
for i in jsondata['tree']:
size = '-'
path = i['path']+'/'
if i['type'] == 'blob':
size = i['size']
path = path[:-1]
table.add_row([path, size])
print(table)
# README
if args.readme:
print(base64.b64decode(jsondata['content']).decode('utf-8'))
# RELEASES
if args.releases:
table = PrettyTable([" Release name ", " Release Date ", " Release Time "])
for i in jsondata:
time = str(dateutil.parser.parse(i['published_at']))
date = time[:10]
time = time[11:]
time = time[:5]
time = time + ' UTC'
table.add_row([i['tag_name'], date, time])
print(table)
# OPEN ONE FILE
if args.openfile:
try:
print(base64.b64decode(jsondata['content']).decode('utf-8'))
return
except:
print("\nDirectory URL was given, hence its contents will be displayed\n")
table = PrettyTable(["Folder Contents"])
for i in jsondata:
table.add_row([i['name']])
print(table)
# GET FOLLOWERS
if args.followers:
table = PrettyTable([" FOLLOWERS "])
table.align[" FOLLOWERS "] = "l"
for i in jsondata:
table.add_row([i['login']])
print("Number of followers:"+str(len(jsondata)))
print(table)
# GET FOLLOWING
if args.following:
table = PrettyTable([" FOLLOWING "])
table.align[" FOLLOWING "] = "l"
for i in jsondata:
table.add_row([i['login']])
print("Number of following:"+str(len(jsondata)))
print(table)
# GET CONTRIBUTORS
if args.contributors:
table = PrettyTable([" CONTRIBUTORS "])
table.align[" CONTRIBUTORS "] = "l"
for i in jsondata:
table.add_row([i['login']])
print("Number of contributors:"+str(len(jsondata)))
print(table)