3
3
import urllib2
4
4
import sys
5
5
import os
6
+ import optparse
6
7
7
8
GITHUB_REPOS_API_BASE_URL = 'https://api.github.com/repos/'
9
+ USERNAME = ""
10
+ PASSWORD = ""
8
11
9
- def write_file (item , dir_name ):
12
+
13
+ def read_url (url , private = False ):
14
+ if private :
15
+ request = urllib2 .Request (url )
16
+ base64string = base64 .encodestring (
17
+ '%s:%s' % (USERNAME , PASSWORD )).replace ('\n ' , '' )
18
+ request .add_header ("Authorization" , "Basic %s" % base64string )
19
+ return urllib2 .urlopen (request ).read ()
20
+ else :
21
+ return urllib2 .urlopen (url ).read ()
22
+
23
+
24
+ def write_file (item , dir_name , private = False ):
10
25
name = item ['name' ]
11
- res = urllib2 . urlopen (item ['url' ]). read ( )
26
+ res = read_url (item ['url' ], private )
12
27
coded_string = json .loads (res )['content' ]
13
28
contents = base64 .b64decode (coded_string )
14
29
print os .path .join (dir_name , name )
15
- f = open (os .path .join (dir_name , name ), 'w' )
16
- f .write (contents )
17
- f .close ()
30
+ with open (os .path .join (dir_name , name ), 'w' ) as f :
31
+ f .write (contents )
18
32
19
- def write_files (url , dir_name , recursive = True ):
20
33
34
+ def write_files (url , dir_name , recursive = True , private = False ):
21
35
print 'url' , url
22
36
os .makedirs (dir_name )
23
- github_dir = json .loads (urllib2 .urlopen (url ).read ())
37
+
38
+ github_dir = json .loads (read_url (url , private ))
24
39
for item in github_dir :
25
40
if item ['type' ] == 'file' :
26
- write_file (item , dir_name )
41
+ write_file (item , dir_name , private )
27
42
elif item ['type' ] == 'dir' :
28
- write_files (item ['url' ], dir_name = os .path .join (dir_name , item ['name' ]))
43
+ write_files (item ['url' ], dir_name = os .path .join (
44
+ dir_name , item ['name' ]))
29
45
30
46
31
47
if __name__ == '__main__' :
32
- args = dict (enumerate (sys .argv ))
33
- path = 'mfbx9da4/blog/server'
34
- path = args [1 ]
48
+ parser = optparse .OptionParser ()
49
+ parser .add_option ("--private" , action = "store_true" , default = False )
50
+ parser .add_option ("-r" , action = "store" )
51
+ parser .add_option ("-p" , action = "store" )
52
+ parser .add_option ("-b" , action = "store" )
53
+
54
+ options , args = parser .parse_args ()
55
+
56
+ path = args [0 ]
35
57
path = path .split ('/' )
36
-
58
+
37
59
new_dir_name = path [- 1 ]
38
60
if os .path .exists (new_dir_name ):
39
61
raise 'Directory' , new_dir_name , 'already exists'
40
-
62
+
41
63
# use contents api
42
- path .insert (2 , 'contents' )
64
+ path .append ("contents" )
65
+ if options .p :
66
+ path .append (options .p ) # filepath
67
+ if options .b :
68
+ path .append ("?ref=" + options .b ) # git branch
43
69
path = '/' .join (path )
44
-
45
- recursive = eval (args .get (2 )) if args .get (2 ) else True
46
- write_files (GITHUB_REPOS_API_BASE_URL + path , new_dir_name , recursive = recursive )
47
70
71
+ recursive = eval (options .r ) if options .r else True
48
72
73
+ private = True if options .private else False
49
74
75
+ if private :
76
+ USERNAME = raw_input ("username: " )
77
+ PASSWORD = raw_input ("password: " )
50
78
51
-
79
+ write_files (GITHUB_REPOS_API_BASE_URL + path ,
80
+ new_dir_name , recursive = recursive , private = private )
0 commit comments