#!/usr/bin/python
# -*- coding: UTF8 -*-

"""
    Tux Droid - Italian Tux Droid controller example
    Copyright (C) 2007 Francesco Frassinelli <frafra _at_ autistici _dot_ org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
"""

__author__  = 'Francesco Frassinelli <frafra _at_ autistici _dot_ org>'
__appname__ = 'italian tux droid controller'
__version__ = '0.0.1'
__date__    = '2007/12/24'
__license__ = 'GPL'

from sys import path
path.append('/opt/tuxdroid/api/python/')
from tux import *
from time import localtime

def say(text, mouthOpen=True, mouthClose=True):
    if mouthOpen: tux.cmd.mouth_open()
    tux.tts.speak(text.encode("utf-8"))
    if mouthClose: tux.cmd.mouth_close()

def clock():
    hour, minute = localtime()[3:5]
    text = hour == 1 and "E' l'una" or "Sono le %d" % hour
    text += " e " + (minute == 1 and "un minuto" or "%d minuti" % minute)
    say(text)

def calendar():
    year, month, day = localtime()[0:3]
    say("%d.%d.%d" % (day, month, year))

def rssReader(num=5):
    try:
        from feedparser import parse
    except:
        say("Hai bisogno di feedparser")
    url = "http://www.ossblog.it/rss2.xml"
    feed = parse(url)
    title = feed["channel"]["title"]
    news = []
    for item in feed["items"]:
        news.append(item["title"])
    say(title)
    tux.cmd.mouth_open()
    [say(item, False, False) for item in (len(news)>num and news[:num] or news)]
    tux.cmd.mouth_close()

if __name__ == "__main__":
    tux.event.on_remote_bt[K_RED] = clock
    tux.event.on_remote_bt[K_GREEN] = calendar
    tux.event.on_remote_bt[K_BLUE] = rssReader
    tux.event.wait_remote_bt(K_STANDBY, 9999)
    tux.cmd.mouth_close()
    tux.destroy()
