#!/usr/bin/python -tt

"""
Broadcast local weather to your Twitter account

Requires:
    pymetar     http://www.schwarzvogel.de/software-pymetar.shtml
    pyxmpp      http://pyxmpp.jajcus.net/
    
All you need to know is the NOAA Station ID for your location

    http://www.nws.noaa.gov/tg/siteloc.shtml
"""

version = '0.1.0'
author  = 'Mike Taylor'
license = """
Copyright (c) 2007 Mike Taylor
All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time
import pymetar
import xmpp

metarRF     = pymetar.ReportFetcher("KPHL")  # Philadelphia International Airport
metarRP     = pymetar.ReportParser()
metarData   = metarRF.FetchReport()
metarReport = metarRP.ParseReport(metarData)

weather     = "looks out the window: %s, %s F (%s C)" % (metarReport.getWeather(),
                                                         metarReport.getTemperatureFahrenheit(),
                                                         metarReport.getTemperatureCelsius())

#print 'weather: %s' % weather

    # JID of who is doing the sending
jid = xmpp.protocol.JID('your_jid@jabber.org')
jpw = 'your_jid_password'

    # JID of who to send the report to
    # I add my own JID for debugging
to_list = [xmpp.protocol.JID('twitter@twitter.com'),
           #xmpp.protocol.JID('bear@code-bear.com'),
          ]

c = xmpp.Client(jid.getDomain(), debug=[])

if c.connect():
    a = c.auth(jid.getNode(), jpw, resource=jid.getResource())

    if a:
        #print 'authenticated using', a
        for j in to_list:
            print j, '::', c.send(xmpp.protocol.Message(j, weather, 'chat'))

        # a short delay is needed to allow for the library to do it's thing
        time.sleep(1)

    c.disconnect()

