-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·114 lines (104 loc) · 3.26 KB
/
client.py
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
from tqdm import tqdm
import requests
import argparse
import os
def download_links(IP):
print('Downloading links!')
f = open("links.txt",'wb')
f.write(requests.get('http://{}/links'.format(IP)).content)
f.close()
print('Ready')
def remove_lines(line):
found=False
with open('links.txt', "r") as f:
Lines = f.readlines()
with open("links.txt", "w") as f:
for l in Lines:
if found:
f.write(l)
else:
if l.strip("\n") == line:
found=True
def load_links(resume):
file1 = open('links.txt', 'r')
Lines = file1.readlines()
if not resume:
try:
Lines.remove('LINKS.TXT\n')
except:
try:
Lines.remove('links.txt\n')
except:
raise Exception("\nSD card not mount!!")
for l in Lines:
if l[-5:] !=".jpg\n" and l[-5:] !=".JPG\n":
Lines.remove(l)
if not os.path.exists('DATA'):
os.mkdir("DATA")
else:
for i in os.listdir("DATA"):
os.remove("DATA/"+i)
file1.close()
return Lines
def downloading(Lines,IP):
try:
count = 0
print("downloading...")
for line in tqdm(Lines):
line=line.rstrip("\n")
count += 1
f = open('DATA/'+line,'wb')
f.write(requests.get('http://{}/{}'.format(IP,line)).content)
f.close()
except KeyboardInterrupt:
print("downloading cancelled at {} run resume by `python client.py c".format(line))
remove_lines(line)
except Exception as e:
print("ESP network missed at downloading {} run resume by `python client.py c".format(line))
remove_lines(line)
return count
def print_size(count):
# get size
size=0
for path, dirs, files in os.walk("DATA"):
for f in files:
fp = os.path.join(path, f)
size += os.path.getsize(fp)
print("downloaded {} instance {} MB".format(count,size/1048576))
def download_manager(ip):
download_links(ip)
try:
Lines=load_links(False)
except:
return False
count=downloading(Lines,ip)
if count==len(Lines):
print("Download completed")
else :
while True:
print("Download crupted retrying")
Lines=load_links(True)
count=downloading(Lines,ip)
if count==len(Lines):
print("Download completed")
break
print_size(count)
return True
def main():
parser=argparse.ArgumentParser(description='script to download from ESP',epilog='Attention : if it is going to resume download add c command')
parser.add_argument("operation",metavar='operation',help="c for resume d for download" )
parser.add_argument('-ip',"--serverip",metavar='serverip',help="Enter ip of server")
args=parser.parse_args()
if args.operation=="c":
resume=True
IP=args.serverip
else:
resume=False
IP=args.serverip
download_links(IP)
Lines=load_links(resume)
count=downloading(Lines,IP)
print_size(count)
print('DONE!')
if __name__=="__main__":
main()