diff --git a/.gitignore b/.gitignore index b6e4761..873759e 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +.vscode/ +*.txt diff --git a/bdshare.py b/bdshare.py index dc686a7..5d4a8a2 100644 --- a/bdshare.py +++ b/bdshare.py @@ -14,22 +14,49 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from os.path import exists, getsize, split +from os.path import exists, getsize, split, isdir, isfile, relpath +from os import listdir from hashlib import md5 import sys from time import time + +def listdirc(path: str, path2: str = None): + if path2 is None and path[-1] != '/' and path[-1] != '\\': + path2 = path + '/' + elif path2 is None: + path2 = path + l = listdir(path) + r = [] + for i in l: + fn = f'{path}/{i}' + if exists(fn): + if isfile(fn): + r.append({'fn': fn, 'path': path2}) + elif isdir(fn): + r = r + listdirc(fn, path2) + return r + + limit_size = 256 * 1024 if __name__ == "__main__": if len(sys.argv) > 1: - file_list = sys.argv[1:] + filel = sys.argv[1:] else: - print('未输入文件') - exit(-1) + filel = ['.'] + file_list = [] + for i in filel: + if exists(i): + if isfile(i): + file_list.append({'fn': i, 'path': ''}) + elif isdir(i): + file_list = file_list + listdirc(i) e = 1 l = len(file_list) - for i in file_list: + f2 = open('bdshare.txt', 'w', encoding='utf8') + for j in file_list: + i = j['fn'] if exists(i): fs = getsize(i) with open(i, 'rb', 1024) as f: @@ -50,5 +77,8 @@ if __name__ == "__main__": md52 = md.hexdigest() if fs < limit_size: md51 = md52 - print(f"\r[{e}/{l}]:{md52}#{md51}#{fs}#{split(i)[1]}") + share = f'{md52}#{md51}#{fs}#{relpath(i,j["path"])}' + f2.write(share+'\n') + print(f"\r[{e}/{l}]:{share}") e = e + 1 + f2.close()