Herunterladen von Warnmeldugen und Vorhersagen.
Zu beachten ist, daß für den FTP Zugang des DWD Servers ein User und Password notwendig ist. Diese Zugangsdaten können beim DWD kostenlos beantragt werden. Im Programmtext sind diese Anmeldeinformationen herausgenommen.
Code: Alles auswählen
#*********************************************************************
# Connect DWD FTP Server and download Forecast und Warnlagen
# Autor : Frank Ulbrich
# Date : 29.09.2016
#*********************************************************************
# Links
#=======
# http://www.pythonforbeginners.com/code-snippets-source-code/how-to-use-ftp-in-python/
#
# Server : ftp-outgoing2.dwd.de
# User :
# PW:
# Interpreter : python 2.7
#*********************************************************************
# Versionsnummer ausgeben
version = "0.7"
print("****************************************")
print("tlx_ftp.down FORECAST Version : " + version )
print("****************************************")
#*********************************************************************
# imported libraries
import fnmatch # match names
import ftplib # ftp connect
#*********************************************************************
# FTP Server Parameter
#*********************************************************************
filepath = "filelist/" # Linux
#filepath = ".\\filelist\\" # Windows
ftpserver = 'ftp-outgoing2.dwd.de'
ftpuser = 'GEHEIM'
ftppw = 'GEHEIM'
ftppath1 = '/gds/gds/specials/forecasts/text' # Infos fuer Deutschland - FPDL13*
ftppath2 = '/gds/gds/specials/forecasts/text' # Warnmeldungen - VHDL30_DWEH WARNLAGE NRW
ftppath3 = '/gds/gds/specials/forecasts/text' # Warnmeldungen - VHDL30_DWMG WARNLAGE BAYERN
ftppath4 = '/gds/gds/specials/forecasts/text' # Forecast - VHDL17_DWOG 10 Tage Wettervorhersage
ftppath5 = '/gds/gds/specials/forecasts/text' # Warnmeldungen - VHDL30_DWHG WARNLAGE NDS
ftppath6 = '/gds/gds/specials/forecasts/text' # Warnmeldungen - VHDL30_DWPH WARNLAGE MEG POM
filelist1 = filepath + 'dwd_forecast_de.txt'
filelist2 = filepath + 'dwd_warnlagen_nrw.txt'
filelist3 = filepath + 'dwd_warnlagen_bay.txt'
filelist4 = filepath + 'dwd_forecast_10.txt'
filelist5 = filepath + 'dwd_warnlagen_nds.txt'
filelist6 = filepath + 'dwd_warnlagen_pom.txt'
print ("* FTP - Server Connect ")
ftp = ftplib.FTP(ftpserver, ftpuser, ftppw)
#ftp = ftplib.FTP('ftp.sunet.se', 'anonymous', 'anonymous@sunet.se')
welcome = ftp.getwelcome()
print (welcome)
ftp.dir()
ftp.cwd(ftppath1) #changing to forecasts
ftp.dir()
#*********************************************************************
# Read filelist Forecast Deutschland
#*********************************************************************
print ("* Read forecast filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath1) # changing to directory
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
print (filelist)
#*********************************************************************
# Create textfile with filenames Forecast - Deutschland
#*********************************************************************
print ("* Create FORECAST textfile with filnames ")
# https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist1,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'FPDL1*'): # filter for files with specific characters
print item
myfile.write(item+"\n")
#myfile.writelines("%s\n" % item)
myfile.close()
#*********************************************************************
# Read filelist FVHDL30_DWEH WARNLAGE NRW
#*********************************************************************
print ("* Read NRW filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath2) # changing to
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True) # Absteigende Sortierung der Namen
print (filelist)
#*********************************************************************
# Create textfile with filenames VHDL30_DWEH WARNLAGE NRW
#*********************************************************************
print ("* Create NRW textfile with filnames ")
#https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist2,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'VHDL30_DWEH*'): # filter for files with specific characters
print item
myfile.write(item+"\n")
myfile.close()
#*********************************************************************
# Read filelist FVHDL30_DWHG WARNLAGE NDS
#*********************************************************************
print ("* Read NDS filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath5) # changing to
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True) # Absteigende Sortierung der Namen
print (filelist)
#*********************************************************************
# Create textfile with filenames VHDL30_DWHG WARNLAGE NDS
#*********************************************************************
print ("* Create NDS textfile with filnames ")
#https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist5,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'VHDL30_DWHG*'): # filter for files with specific characters
print item
myfile.write(item+"\n")
myfile.close()
#*********************************************************************
# Read filelist VHDL30_DWPH WARNLAGE POM
#*********************************************************************
print ("* Read POM filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath6) # changing to
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True) # Absteigende Sortierung der Namen
print (filelist)
#*********************************************************************
# Create textfile with filenames VHDL30_DWPH WARNLAGE POM
#*********************************************************************
print ("* Create POM textfile with filnames ")
#https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist6,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'VHDL30_DWPH*'): # filter for files with specific characters
print item
myfile.write(item+"\n")
myfile.close()
#*********************************************************************
# Read filelist FVHDL30_DWEH WARNLAGE BAYERN
#*********************************************************************
print ("* Read BAYERN filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath3) # changing to
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
filelist = sorted(filelist,reverse=True)
print (filelist)
#*********************************************************************
# Create textfile with filenames VHDL30_DWMG WARNLAGE BAYERN
#*********************************************************************
print ("* Create BAYERN textfile with filnames ")
#https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist3,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'VHDL30_DWMG*'): # filter for files with specific characters
print item
myfile.write(item+"\n")
myfile.close()
#*********************************************************************
# Read filelist 10 Tage Forecast Deutschland
#*********************************************************************
print ("* Read 10 Tage forecast filelist from ftp - server ")
filelist = [] # list for filenames
ftp.cwd(ftppath4) # changing to directory
line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
print (filelist)
#*********************************************************************
# Create textfile with filenames Forecast - 10 Tage Deutschland
#*********************************************************************
print ("* Create 10 Tage FORECAST textfile with filnames ")
# https://docs.python.org/2.7/library/fnmatch.html
myfile = open(filelist4,'w')
for item in filelist:
if fnmatch.fnmatch(item, 'VHDL17*'): # VHDL17_DWOG filter for files with specific characters
print item
myfile.write(item+"\n")
#myfile.writelines("%s\n" % item)
myfile.close()
##*********************************************************************
## Read filelist DEUTSCHLAND
##*********************************************************************
#print ("* Read NDS filelist from ftp - server ")
#filelist = [] # list for filenames
#ftp.cwd(ftppath3) # changing to Warnmeldungen SMS fuer NDS
#line = ftp.retrlines("NLST",filelist.append) # read filenames and write into list of files
#print (filelist)
##*********************************************************************
## Create textfile with filenames NDS
##*********************************************************************
#print ("* Create NDS textfile with filnames ")
#myfile = open(filelist3,'w')
#for item in filelist:
# if fnmatch.fnmatch(item, '*'): # filter for files with specific characters
# print item
# myfile.write(item+"\n")
# #myfile.writelines("%s\n" % item)
#myfile.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file Infos fuer Deutschland - FPDL13*
#*********************************************************************
print ('* Oeffne Listen Datei FDL13 : %s' %(filelist1))
myfile = open(filelist1,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entgernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath1)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\forecast\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/forecast/' + takefile + ".txt", 'wb').write)
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file Warnmeldungen - VHDL30_DWEH WARNLAGE NRW
#*********************************************************************
print ('* Oeffne Listen Datei WARNLAGE NRW : %s' %(filelist2))
myfile = open(filelist2,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entgernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath2)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\warnlagen\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/warnlagen/' + takefile + ".txt", 'wb').write)
#break # nur das erste Element herunterladen
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file Warnmeldungen - VHDL30_DWEH WARNLAGE NDS
#*********************************************************************
print ('* Oeffne Listen Datei WARNLAGE NDS : %s' %(filelist5))
myfile = open(filelist5,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entgernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath5)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\warnlagen\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/warnlagen/' + takefile + ".txt", 'wb').write)
#break # nur das erste Element herunterladen
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file Warnmeldungen - VHDL30_DWPH WARNLAGE POM
#*********************************************************************
print ('* Oeffne Listen Datei WARNLAGE POM : %s' %(filelist6))
myfile = open(filelist6,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entgernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath5)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\warnlagen\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/warnlagen/' + takefile + ".txt", 'wb').write)
#break # nur das erste Element herunterladen
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file Warnmeldungen - VHDL30_DWEH WARNLAGE BAYERN
#*********************************************************************
print ('* Oeffne Listen Datei WARNLAGE BAYERN : %s' %(filelist3))
myfile = open(filelist3,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entgernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath3)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\warnlagen\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/warnlagen/' + takefile + ".txt", 'wb').write)
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames from saved filelist.file
# 10 Tage fuer Deutschland VHDL17_DWOG
#*********************************************************************
print ('* Oeffne Listen Datei VHDL17_DWOG : %s' %(filelist4))
myfile = open(filelist4,'r')
inlist = myfile.readlines() # Using .readlines()
filelistfile = []
for i in inlist:
filelistfile.append(i.rstrip('\n')) # Das Zeichen \n entfernen
myfile.close()
print("* Inlist : ")
print(inlist)
print("* Filelistfile : ")
print(filelistfile)
print ('* Download FTP Dateien')
ftp.cwd(ftppath4)
for takefile in filelistfile:
try:
if fnmatch.fnmatch(takefile, '*'):
print ('* Download : %s' %(takefile))
#ftp.retrlines('RETR '+takefile, open('.\\filedownload\\forecast\\' + takefile + ".txt", 'wb').write)
ftp.retrlines('RETR '+takefile, open('filedownload/forecast/' + takefile + ".txt", 'wb').write)
except :
print ('Failed to download FTP file: %s' %(takefile))
ftp.close()
#*********************************************************************
# Download textfile with filenames retrlines
#*********************************************************************
#for takefile in filelist:
# try:
# #ftp.storlines('STOR ' + file, open(file))
# if fnmatch.fnmatch(takefile, 'H*'):
# ftp.retrlines('RETR '+takefile, open('.\\filedownload\\' + takefile, 'wb').write)
# except :
# print ('Failed to download FTP file: %s' %(takefile))
# ftp.close()
#for item in filelist:
# f.write("%s\n" % item)
#f.close()
#ftp.cwd("/conspiracy") #changing to /pub/unix
#filelist = ftp.dir()
#print filelist
#ftp.retrbinary('RETR README', open('README', 'wb').write)
#ftp.retrbinary('RETR index.html', open('index.html', 'wb').write)
#filelist = ftp.nlst()
# for file in filelist:
# ftp.retrbinary('RETR '+file, open(file, 'wb').write)
#def getFile(ftp, filename):
# try:
# ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
# except:
# print "Error"
#
#ASCII LINES lesen
#fd = open('README', 'wt')
#ftp.retrlines('RETR README', writeline)
#fd.close()
#ftp.quit()
#http://stackoverflow.com/questions/23624748/python-get-files-from-ftp-wildcards-and-date-changes-using
# http://ftputil.sschwarzer.net/trac/wiki/Documentation
#https://docs.python.org/3/library/fnmatch.html
#filename = 'index.html'
#localfile = open(filename, 'wb')
#ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
#print 'Closing file ' + filename
#localfile.close()
print ('* Closing FTP connection')
ftp.quit()
quit()
#Ende des Program