1#!/usr/local/bin/python3.8
2#####################################################################
3#
4#			pyWeather
5#			Ver 0.1b
6#			Scripted by Groundhog[@linuxfreak.com]
7#			-pyWeather grabs weather info for the given METAR and then
8#			  fowards it through SMTP to given email  -- Ideal for Cron
9#
10#####################################################################
11
12from ftplib import *
13import rfc822, string, smtplib, os, getpass
14from sys import *
15
16# BETA !!! - Future will bring command line argments for this - Right now just mod.
17
18# Pre-execution variable inits.
19noaa_url = "weather.noaa.gov"
20metar_dir = "/data/observations/metar/decoded/"
21mail_from = "pyWeather@localhost"
22
23user = getpass.getuser()
24if user == 'root':
25	tmp = "/root/.pyweatherdat"
26else:
27	tmp = "/home/" + user + "/.pyweatherdat"
28
29
30
31#Some name crap -- gotta make sure they know who wrote this
32print "\n\t\t\tpyWeather\n\t\t\tVer. 0.1b\n\t\t\tby GroundHog[@linuxfreak.com]\n\t\t\t6.30.99\n\nVisit www.nws.noaa.gov/oso/oso1/oso12/metar.htm for METAR ID's for your area.\n"
33
34
35#Variable initiation
36#-Catch command line fubar's
37try:
38	metar = argv[1]
39	foward_mail = argv[2]		# (@localhost !!!)
40	smtp_serv = argv[3]
41	mail_sub = "pyWeather Updates for " + metar + "\n"
42	metar = metar + ".TXT"
43	weather = mail_sub + '\n'
44except IndexError:
45	print "Usage: pyWeather [METAR] [FOWARD EMAIL ADDRESS] [SMTP SERVER]"
46	sys.exit()
47
48#Random data init's
49if os.path.exists(tmp) == 1:
50	os.remove(tmp)
51
52data = open(tmp,'w')
53
54if smtp_serv == 'localhost':
55	smpt_serv = '127.0.0.1'
56
57
58# FTP Sh-ite
59ftp = FTP(noaa_url)
60ftp.login()
61ftp.cwd(metar_dir)
62ftp.retrbinary('RETR ' + metar, data.write)
63ftp.quit
64
65data.close()
66
67report = open(tmp, 'r')
68weather = weather + report.read()
69weather = weather + "\npyWeather - Ver .01b\nQuestion's/Comments/Sugestions Groundhog@linuxfreak.com\n"
70
71
72# Mail Crappola
73toaddrs  = string.splitfields(foward_mail, ',')		# RFC822 conformacy
74server = smtplib.SMTP(smtp_serv)
75server.sendmail(mail_from, toaddrs, weather)
76server.quit()