Skip to content

Instantly share code, notes, and snippets.

@moogoo78
Created February 26, 2015 09:10
Show Gist options
  • Save moogoo78/f936420e807a42c02fa7 to your computer and use it in GitHub Desktop.
Save moogoo78/f936420e807a42c02fa7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -.- coding: utf-8 -.-
"""
Usage:
landsat5.py <username> <password>
Requirements:
beautifulsoup4==4.0.5
requests==2.0.1
"""
import sys
import requests
from bs4 import BeautifulSoup
LOGIN_URL = 'https://earthexplorer.usgs.gov/login/'
DOWNLOAD_URL = 'http://earthexplorer.usgs.gov/download/options/3119/LT51170432011037BKT00'
def main(uname, passwd):
sess = requests.session()
payload = {
'username': uname,
'password': passwd,
'rememberMe': 1
}
print ('[1/3] login...')
login = sess.post(LOGIN_URL, data=payload)
if login.status_code == 200:
print ('[2/3] get download page and parse html to find file')
r = sess.get(DOWNLOAD_URL)
soup = BeautifulSoup(r.text)
tags = soup.select('#optionsPage .list .button input')
url = tags[3]['onclick'].split('=')[1][1:-1]
print ('[4/3] download file')
r = sess.get(url, stream=True)
fname = 'out.tgz'
content = r.headers['content-disposition']
if 'filename=' in content:
fname = content.split('=')[1].replace('"', '')
chunk_size = 1024
with open(fname, 'wb') as f:
for chunk in r.iter_content(chunk_size):
f.write(chunk)
f.close()
if __name__ == '__main__':
if len(sys.argv) > 2:
main(sys.argv[1], sys.argv[2])
else:
print ('usage: landsat5.py <username> <password>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment