Skip to content

Instantly share code, notes, and snippets.

@moogoo78
Created February 26, 2015 09:10
Show Gist options
  • Select an option

  • Save moogoo78/f936420e807a42c02fa7 to your computer and use it in GitHub Desktop.

Select an option

Save moogoo78/f936420e807a42c02fa7 to your computer and use it in GitHub Desktop.

Revisions

  1. moogoo78 created this gist Feb 26, 2015.
    53 changes: 53 additions & 0 deletions landsat5
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/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>')