Created
August 4, 2017 09:42
-
-
Save lanfon72/1aa957f992b4ec337cdd920cb8151d72 to your computer and use it in GitHub Desktop.
saving files from garner ftp with requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from ftplib import FTP | |
import requests | |
def get_connect(url): | |
root, foldr = url.split('//', 1)[-1].split('/', 1) | |
ftp = FTP(root) | |
ftp.login() | |
ftp.cwd(foldr) | |
return ftp | |
def save_file(path, url): | |
try: | |
data = requests.get(url).content | |
with open(path, 'wb') as f: | |
f.write(data) | |
except Exception as e: | |
print("get error on", path, "due to", repr(e)) | |
return path | |
else: | |
print(path, "saved.") | |
return None | |
def main(files, url): | |
fname = files.pop() | |
while files: | |
fname = save_file(fname, "%s/%s" % (url, fname)) | |
fname = fname if fname else files.pop() | |
if __name__ == '__main__': | |
url = "garner.ucsd.edu/pub/rinex/2003/303" | |
conn = get_connect(url) | |
files = set(conn.nlst()).difference(os.listdir()) | |
conn.quit() | |
if files: | |
url = r'http://anonymous:jason%40ucsd.edu@' + url | |
main(files, url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment