-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path001-download.py
executable file
·106 lines (86 loc) · 2.7 KB
/
001-download.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
#!/usr/bin/python
import os
import sys
import subprocess
import time
import re
dateMonth = time.strftime('%Y%m', time.gmtime())
FTPBASE = 'ftp://mtsat-1r.cr.chiba-u.ac.jp/grid-MTSAT-2.0/MTSAT2/' + dateMonth
BASEPATH = os.path.realpath(os.path.dirname(sys.argv[0]))
OUTPUTPATH = os.path.join(BASEPATH, 'site', 'mirror')
MAXTIME = 1200 # MAX run time, approx.
try:
os.makedirs(OUTPUTPATH)
except:
pass
def howOld(timeobj):
nowtime = time.mktime(time.gmtime())
compare = time.mktime(timeobj)
return nowtime - compare
def filterNewFile(dataList, days):
outList = []
for filename in dataList:
filetime = False
try:
filetime = time.strptime(filename[0:12], '%Y%m%d%H%M')
except:
pass
if filetime:
if howOld(filetime) < 86400 * days:
outList.append(filename)
return outList
try:
print '> Begin connect to ftp server and retrieve the list.'
ftplist = subprocess.check_output(['python', 'ftplist.py', dateMonth])
except:
print "> Download error. Exit now."
sys.exit(1)
ftplist = ftplist.split('\n')
print "> Got total %d files." % len(ftplist)
##############################################################################
pattern = re.compile("^[0-9]{12}\.(vis|ir)\.tar\.bz2$")
gotlist = []
for each in ftplist:
parts = each.split(' ')
if len(parts) > 0:
filename = parts[-1].strip()
if None != pattern.match(filename):
gotlist.append(filename)
mirrorList = filterNewFile(gotlist, 1)
targetList = os.listdir(OUTPUTPATH)
downloadList = []
print "> Check mirror directory and decide download list..."
for filename in mirrorList:
if filename in targetList:
print ">> Skip file: %s" % filename
continue
downloadList.append(filename)
print "> Begin downloading..."
starttime = time.time()
for filename in downloadList:
outputFilename = os.path.join(OUTPUTPATH, filename)
outputFilenameTemp = outputFilename + '.tmp'
cmd = [\
'wget',
'-c', # continue download
'-U',
'Mozilla/5.0 (compatible; NeoAtlantis MTSAT-2 Satellite Image Service; +http://mtsat-2.neoatlantis.org/)',
'-o',
'/dev/null', # no log recorded
'-O',
outputFilenameTemp,
FTPBASE + '/' + filename,
]
nowtime = time.time()
if nowtime - starttime > MAXTIME:
print "> Download have costed enough time. Exit now."
break
try:
print "> Download file: %s" % (FTPBASE + '/' + filename)
ret = subprocess.call(cmd)
except Exception,e:
continue
if 0 == ret:
os.rename(outputFilenameTemp, outputFilename)
time.sleep(5)
print "> Finish."