@@ -105,6 +105,10 @@ class panbaiducom_HOME(object):
105105 def __init__ (self ):
106106 self ._download_do = self ._play_do if args .play else self ._download_do
107107 self .ondup = 'overwrite'
108+ self .highlights = {}
109+ if args .tail : self .highlights ['tail' ] = {'text' : args .tail .decode ('utf8' , 'ignore' ), 'is_regex' : 0 }
110+ if args .head : self .highlights ['head' ] = {'text' : args .head .decode ('utf8' , 'ignore' ), 'is_regex' : 0 }
111+ if args .include : self .highlights ['include' ] = {'text' : args .include .decode ('utf8' , 'ignore' ), 'is_regex' : 1 }
108112
109113 def init (self ):
110114 if os .path .exists (cookie_file ):
@@ -248,7 +252,7 @@ def sort(reverse, arg, fileslist=fileslist):
248252 reverse = False
249253 fileslist = sort (reverse , 'size' )
250254
251- # for size
255+ # for time
252256 if arguments .get ('time' ):
253257 reverse = None
254258 if arguments ['time' ] == 'reverse' :
@@ -291,14 +295,26 @@ def sort(reverse, arg, fileslist=fileslist):
291295 fileslist = [fileslist [i ] for i in indexs ]
292296
293297 dirs = [i for i in fileslist if i ['isdir' ]]
298+ t , tt = [], []
299+ args .type_ = args .type_ if args .type_ else ''
300+ if 'e' in args .type_ :
301+ for i in dirs :
302+ d = i ['path' ].encode ('utf8' )
303+ j = self ._get_file_list ('name' , None , d , 1 , all = False )
304+ if not j ['list' ]:
305+ t .append (i )
306+ else :
307+ tt .append (i )
308+ if 'e' in args .type_ .split (',' ): dirs = t
309+ if 'ne' in args .type_ .split (',' ): dirs = tt
294310 files = [i for i in fileslist if not i ['isdir' ]]
295311 if arguments .get ('desc' ) == 1 :
296312 dirs .reverse ()
297313 files .reverse ()
298314
299- if args . type_ == 'f' :
315+ if 'f' in args . type_ :
300316 fileslist = files
301- elif args . type_ == 'd' :
317+ elif 'd' in args . type_ :
302318 fileslist = dirs
303319 else :
304320 fileslist = dirs + files
@@ -315,7 +331,7 @@ def _get_path(self, url):
315331 t = urllib .unquote_plus (t )
316332 return t
317333
318- def _get_file_list (self , order , desc , dir_ ):
334+ def _get_file_list (self , order , desc , dir_ , num , all = True ):
319335 t = {'Referer' :'http://pan.baidu.com/disk/home' }
320336 theaders = headers
321337 theaders .update (t )
@@ -324,14 +340,15 @@ def _get_file_list(self, order, desc, dir_):
324340 "channel" : "chunlei" ,
325341 "clienttype" : 0 ,
326342 "web" : 1 ,
327- "num" : 10000 , ## max amount of listed file at one page
343+ "showempty" : 1 ,
344+ "num" : num , ## max amount is 10000
328345 "t" : int (time .time ()* 1000 ),
329346 "dir" : dir_ ,
330347 "page" : 1 ,
331348 "desc" : 1 , ## reversely
332349 "order" : order , ## sort by name, or size, time
333- "_" : int (time .time ()* 1000 )
334- #"bdstoken": token
350+ "_" : int (time .time ()* 1000 ),
351+ #"bdstoken": self._get_bdstoken(),
335352 }
336353 if not desc : del p ['desc' ]
337354 url = 'http://pan.baidu.com/api/list'
@@ -346,7 +363,9 @@ def _get_file_list(self, order, desc, dir_):
346363 else :
347364 infos += j ['list' ]
348365
349- if len (infos ) == 10000 :
366+ if not all : return j
367+
368+ if len (infos ) == num :
350369 p ['page' ] += 1
351370 else :
352371 j ['list' ] = infos
@@ -471,7 +490,7 @@ def download(self, paths):
471490 if meta ['info' ][0 ]['isdir' ]:
472491 dir_loop = [path ]
473492 for d in dir_loop :
474- j = self ._get_file_list ('name' , None , d )
493+ j = self ._get_file_list ('name' , None , d , 10000 )
475494 if j ['list' ]:
476495 if args .recursive :
477496 for i in j ['list' ]:
@@ -869,7 +888,7 @@ def _upload_file(self, lpath, rpath):
869888 self .upload_datas [lpath ]['is_over' ] = True
870889 self .upload_datas [lpath ]['remotepaths' ].update ([rpath ])
871890 #self.save_upload_datas()
872- print s % (1 , 92 , ' |-- success.\n ' )
891+ print s % (1 , 92 , '\n |-- success.\n ' )
873892 break
874893 else :
875894 print s % (1 , 91 , ' !! Error: _upload_one_file is fall, retry.' )
@@ -1216,6 +1235,33 @@ def _search(self, keyword, directory):
12161235 print s % (1 , 91 , ' !! Error at _search' ), j
12171236 sys .exit (1 )
12181237
1238+ def _highlight_filename_zones (self , info ):
1239+ filename = info ['server_filename' ]
1240+ target = ['.' for i in xrange (len (filename ))]
1241+ for hl in self .highlights .values ():
1242+ re_tp = hl ['text' ] if hl ['is_regex' ] else re .escape (hl ['text' ])
1243+ for m in re .finditer (re_tp , filename , re .I ):
1244+ for i in xrange (m .start (), m .end ()):
1245+ target [i ] = ' '
1246+
1247+ highlight_zones = []
1248+ for m in re .finditer ('\s+' , '' .join (target )):
1249+ highlight_zones .append (m .start ())
1250+ highlight_zones .append (m .end ())
1251+
1252+ filename_zones = []
1253+ for i in xrange (len (highlight_zones )):
1254+ if i == 0 :
1255+ filename_zones .append (filename [:highlight_zones [i ]])
1256+ filename_zones .append (filename [highlight_zones [i ]:highlight_zones [i + 1 ]])
1257+ else :
1258+ try :
1259+ filename_zones .append (filename [highlight_zones [i ]:highlight_zones [i + 1 ]])
1260+ except :
1261+ filename_zones .append (filename [highlight_zones [i ]:])
1262+
1263+ return filename_zones if filename_zones else [filename ]
1264+
12191265 def _find_display (self , info ):
12201266 if args .type_ == 'f' :
12211267 if info ['isdir' ]:
@@ -1229,26 +1275,24 @@ def _find_display(self, info):
12291275 if args .ls_color == 'on' :
12301276 isdir = s % (1 , 93 , 'd' ) if info ['isdir' ] else s % (1 , 97 , '-' )
12311277 size = s % (1 , 91 , sizeof_fmt (info ['size' ]).rjust (8 ))
1232- base_dir , filename = os .path .split (info ['path' ])
1278+ base_dir = os .path .split (info ['path' ])[ 0 ]
12331279 base_dir = s % (2 , 95 , base_dir .encode ('utf8' )) if base_dir != '/' else '/'
12341280
1235- highligh_text = info .get ('highligh_text' )
1236- ii = filename .lower ().find (highligh_text .lower ()) if highligh_text else - 1
1237- if ii != - 1 : highligh_text = filename [ii :ii + len (highligh_text )]
1238- t1 = filename [:ii ] if ii != - 1 else filename
1239- t2 = filename [ii + len (highligh_text ):] if ii != - 1 else u''
1240- if info ['isdir' ]:
1241- t1 = s % (2 , 94 , t1 .encode ('utf8' ))
1242- t2 = s % (2 , 94 , t2 .encode ('utf8' )) if t2 else ''
1243- highligh_text = s % (2 , 93 , highligh_text .encode ('utf8' )) \
1244- if ii != - 1 else ''
1245- filename = t1 + highligh_text + t2
1246- else :
1247- t1 = t1 .encode ('utf8' )
1248- t2 = t2 .encode ('utf8' ) if t2 else ''
1249- highligh_text = s % (2 , 93 , highligh_text .encode ('utf8' )) \
1250- if ii != - 1 else ''
1251- filename = t1 + highligh_text + t2
1281+ filename_zones = self ._highlight_filename_zones (info )
1282+ t = ''
1283+ for i in xrange (0 , len (filename_zones ), 2 ):
1284+ if info ['isdir' ]:
1285+ try :
1286+ t += s % (2 , 94 , filename_zones [i ].encode ('utf8' )) + s % (2 , 93 , filename_zones [i + 1 ].encode ('utf8' ))
1287+ except :
1288+ t += s % (2 , 94 , filename_zones [i ].encode ('utf8' ))
1289+ else :
1290+ try :
1291+ t += filename_zones [i ].encode ('utf8' ) + s % (2 , 93 , filename_zones [i + 1 ].encode ('utf8' ))
1292+ except :
1293+ t += filename_zones [i ].encode ('utf8' )
1294+ filename = t
1295+
12521296 path = os .path .join (base_dir , filename )
12531297
12541298 elif args .ls_color == 'off' :
@@ -1267,9 +1311,9 @@ def _find_display(self, info):
12671311 def find (self , keywords , ** arguments ):
12681312 infos = []
12691313 for keyword in keywords :
1270- t = self ._search (keyword , arguments .get ('directory' ))
1271- for i in xrange ( len ( t )): t [ i ][ 'highligh_text' ] = keyword .decode ('utf8' , 'ignore' )
1272- infos += t
1314+ infos + = self ._search (keyword , arguments .get ('directory' ))
1315+ kw = keyword .decode ('utf8' , 'ignore' )
1316+ self . highlights [ 'find' ] = { 'text' : kw , 'is_regex' : 0 }
12731317 infos = {i ['fs_id' ]: i for i in infos }.values ()
12741318 infos = self ._sift (infos , name = arguments .get ('name' ), \
12751319 size = arguments .get ('size' ), time = arguments .get ('time' ), \
@@ -1342,9 +1386,9 @@ def _ls_directory(self, order, desc, path):
13421386 y = 1
13431387 sum_size = 0
13441388 for dir_ in directorys :
1345- infos = self ._get_file_list (order , desc , dir_ .encode ('utf8' ))['list' ]
1389+ infos = self ._get_file_list (order , desc , dir_ .encode ('utf8' ), 10000 )['list' ]
13461390 tinfos = infos
1347- if args .head or args .tail or args .include or args .exclude :
1391+ if args .head or args .tail or args .include or args .exclude or args . type_ :
13481392 tinfos = self ._sift (infos )
13491393 if args .type_ != 'du' :
13501394 self ._ls_display (tinfos , dir_ )
@@ -1531,7 +1575,7 @@ def rnre(self, foo, bar, dirs):
15311575 directorys = [path .decode ('utf8' , 'ignore' )]
15321576 y = 1
15331577 for dir_ in directorys :
1534- infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ))['list' ]
1578+ infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ), 10000 )['list' ]
15351579 t = infos
15361580 if args .type_ == 'f' or args .type_ == 'd' :
15371581 t = self ._sift (infos )
@@ -1551,7 +1595,7 @@ def rnre(self, foo, bar, dirs):
15511595 self ._rnre_do (foo , bar , tinfos )
15521596
15531597 def _rmre_do (self , infos ):
1554- if args .recursive and args . type_ == 'f' :
1598+ if args .recursive and 'f' in args . type_ :
15551599 paths = [i ['path' ].encode ('utf8' ) for i in infos if not i ['isdir' ]]
15561600 else :
15571601 paths = [i ['path' ].encode ('utf8' ) for i in infos ]
@@ -1576,7 +1620,7 @@ def rmre(self, dirs):
15761620 directorys = [path .decode ('utf8' , 'ignore' )]
15771621 y = 1
15781622 for dir_ in directorys :
1579- infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ))['list' ]
1623+ infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ), 10000 )['list' ]
15801624 tinfos += infos
15811625 if args .recursive :
15821626 subdirs = [i ['path' ] for i in infos if i ['isdir' ]]
@@ -1622,7 +1666,7 @@ def cmre(self, type, dirs, todir):
16221666 directorys = [path .decode ('utf8' , 'ignore' )]
16231667 y = 1
16241668 for dir_ in directorys :
1625- infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ))['list' ]
1669+ infos = self ._get_file_list ('name' , None , dir_ .encode ('utf8' ), 10000 )['list' ]
16261670 tinfos += infos
16271671 if args .recursive :
16281672 subdirs = [i ['path' ] for i in infos if i ['isdir' ]]
@@ -2205,6 +2249,10 @@ def main(argv):
22052249 sl -H head -T tail -E "^re(gul.*) ex(p|g)ress$" path1 path2 ..
22062250 # 显示文件size, md5
22072251 sl path1 path2 .. -v
2252+ # 空文件夹
2253+ l path1 path2 -t e,d
2254+ # 非空文件夹
2255+ l path1 path2 -t ne,d
22082256
22092257 # 查看文件占用空间
22102258 du path1 path2 .. 文件夹下所有*文件(不包含下层文件夹)*总大小
@@ -2250,12 +2298,15 @@ def main(argv):
22502298 -s SECRET, --secret SECRET 提取密码
22512299 -f number, --from_ number 从第几个开始下载,eg: -f 42
22522300 -t ext, --type_ ext 类型参数.
2253- eg: l -t f (文件); l -t d (文件夹)
2301+ eg:
2302+ l -t f # 文件
2303+ l -t d # 文件夹
2304+ l -t du # 查看文件占用空间
2305+ l -t e,d # 空文件夹
22542306 a -t m,d,p,a
22552307 u -t r # 只进行 rapidupload
22562308 u -t e # 如果云端已经存在则不上传(不比对md5)
22572309 u -t r,e
2258- l -t du # 查看文件占用空间
22592310 -l amount, --limit amount 下载速度限制,eg: -l 100k
22602311 -m {o,c}, --uploadmode {o,c} 上传模式: o --> 重新上传. c --> 连续上传.
22612312 -R, --recursive 递归, 用于download, play, ls, find, rmre, rnre, rmre, cpre
0 commit comments