forked from patrickjennings/XPlanet-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxplanet-download_clouds.py
85 lines (69 loc) · 2.08 KB
/
xplanet-download_clouds.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
#!/usr/bin/python
#
# download_clouds.py ver. 1.1
#
# Download coulds map for xplanet from a random mirror, optionally
# save the one to archive directory.
#
# Usage:
# python download_clouds.py - to save the output as clouds_2048.jpg
# python download_coulds.py output.jpg - to save the output as output.jpg
#
# (C) 2004 Michal Pasternak <[email protected]>
#
# This file is in public domain.
# user-tunable parameters:
# how often to download the image?
maxDownloadFrequencyHours = 3
# how many times to retry if download fails (each time tries using a
# different mirror)
maxRetries = 3
# default filename of the output file (in current directory). You can
# specify it on the command line
defaultOutputFile = "clouds_4096.jpg"
# archive dir - where to save old files?
archiveDir = None
# The list of mirrors. Add new ones here.
mirrors = ["ftp://ftp.iastate.edu/pub/xplanet/clouds_4096.jpg",
"http://home.megapass.co.kr/~holywatr/cloud_data/clouds_4096.jpg"]
# end of user-tunable parameters
import random, urllib, sys, stat, time, os
# set output file name
try:
outputFile = sys.argv[1]
except:
outputFile = defaultOutputFile
pass
# check if the file exists and is old enough to overwrite
try:
s = os.stat(outputFile)
mtime = s[stat.ST_MTIME]
fs = s[stat.ST_SIZE]
found = True
except:
mtime = 0
fs = 0
found = False
pass
if time.time() - mtime < maxDownloadFrequencyHours * 3600 and fs > 400000:
# sys.stderr.write("File is already up to date!\n")
sys.exit(0)
else:
if found and archiveDir is not None:
# archivize old file
import shutil
archName = os.path.join(archiveDir, time.strftime("%Y-%m-%d_%H-%I") + "_" + os.path.basename(outputFile))
# sys.stderr.write("Archivizing old file to %s...\n" % archName)
shutil.move(outputFile, archName)
pass
pass
# ok, download:
for a in range(maxRetries):
try:
url = mirrors [ random.randint(0, len(mirrors)-1) ]
# sys.stderr.write("Using %s\nDownloading...\n" % url)
urllib.urlretrieve(url, outputFile)
break
except:
pass
pass