# -*- coding: utf-8 -*-

import tweepy
from cmd import Cmd
import sys, time

import banner
import conf
import unicodedata
from datetime import timedelta

from synthDrivers import _nvdajp_jtalk
_jtalk_voice_m001 = {"id": "V1", "name": "m001", "samp_rate": 16000, "fperiod":  80, "alpha": 0.42, "dir": r"synthDrivers\jtalk\voice"}
_jtalk_voice_mei  = {"id": "V2", "name": "mei", "samp_rate": 48000, "fperiod": 240, "alpha": 0.55, "dir": r"synthDrivers\jtalk\mei_normal"}

in_encode = sys.stdin.encoding
out_encode = sys.stdout.encoding

try:
    if conf.access_token and conf.access_secret:
        default_auth = True
    else:
        default_auth = False
except:
    default_auth = False

tl_default_num = 30
tweet_tmpl = "\n[%s] : %s >> %s"

class TwCmd(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        self.intro = banner.twcmd_banner
        self.prompt = "Twitter >>> "
        self.auth = None
        self.api = None
        self.auth = None
        self.key = None
        self.secret = None


    def emptyline(self):
        pass


    def do_tw(self, tweet):
        try:
            if self.api:
                s = tweet.decode(in_encode).encode(out_encode)
                self.api.update_status(s)
            else:
                print "Please login using 'login' command first"

        except Exception, e:
            print "Tweet failed : ", e
            

    def do_mentions(self, dummy):
        try:
            mentions = self.api.mentions()
            mentions.reverse()
            for m in mentions:
                print (tweet_tmpl 
                       % (m.created_at, m.user.screen_name, m.text))
        except:
            pass
                

    def do_tl(self, line):
        s = line.split()
        
        try:
            num = tl_default_num
            if len(s) > 0:
                try:
                    num = int(s[0])
                except Exception, e:
                    print e
            
            timeline = self.api.home_timeline(count=num)
            timeline.reverse()
            texts = []
            for tw in timeline:
                print (tweet_tmpl 
                       % (tw.created_at + timedelta(hours=9), tw.user.screen_name, tw.text))
                texts.append(tw.text)
            
            for text in texts:
                _nvdajp_jtalk.initialize()
                _nvdajp_jtalk.speak(text)
                time.sleep(len(text)*0.25)
                _nvdajp_jtalk.stop()
                _nvdajp_jtalk.terminate()
        except Exception, e:
            print e

    def help_tl(self):
        print "usage : tl [# of tweets]"


    def do_list(self, line):
        # by nishimotz
        voices = [_jtalk_voice_m001, _jtalk_voice_mei]
        exclude_users = ('nhk_asianvoices',)
        list_user, list_name = 'tsuda', 'eq-genchi' # 'nhk_pr', 'nhk-news'
        while True:
            try:
                timeline = self.api.list_timeline(list_user, list_name)
                timeline.reverse()
                texts = []
                for tw in timeline:
                    if tw.user.screen_name not in exclude_users:
                        tw_text = tw.text.replace(u'\u200b', ' ').replace(u'\u2022', ' ')
                        try:
                            print (tweet_tmpl 
                                   % (tw.created_at + timedelta(hours=9), tw.user.screen_name, tw_text))
                            texts.append(("%s" % (tw.created_at + timedelta(hours=9)), "%s" % tw.user.screen_name, tw.text))
                        except Exception, e:
                            print e
                
                for text1, text2, text3 in texts:
                    _nvdajp_jtalk.initialize(voices[0])
                    _nvdajp_jtalk.set_rate(0)
                    _nvdajp_jtalk.speak(text1)
                    time.sleep(5)
                    _nvdajp_jtalk.stop()
                    _nvdajp_jtalk.terminate()
                    #
                    _nvdajp_jtalk.initialize(voices[1])
                    _nvdajp_jtalk.set_rate(-30)
                    _nvdajp_jtalk.speak(text2 + u' ' + text3)
                    time.sleep(30)
                    _nvdajp_jtalk.stop()
                    _nvdajp_jtalk.terminate()
                    time.sleep(1)
            except Exception, e:
                print e

    def help_list(self):
        print "usage : list [# of tweets]"


    def do_user(self, line):
        usernames = line.split()
        users = [tweepy.api.get_user(u) for u in usernames]
        for u in users:
            print ("\n[%s] %s (%s) : following %s  follower %s\n\t%s"
                   % (u.id, u.screen_name, u.name, u.friends_count, 
                      u.followers_count, u.description, ))


    def do_login(self, line):
        s = line.split()
        try:
            auth = tweepy.OAuthHandler(conf.consumer_key,
                                       conf.consumer_secret)

            if not default_auth:
                redirect_url = auth.get_authorization_url()
                print ("\nGet PIN code from following URL and input it\n%s\n"
                       % redirect_url)
                verifier = raw_input("input PIN code: ").strip()

                auth.get_access_token(verifier)
                self.key = auth.access_token.key
                self.secret = auth.access_token.secret
                print self.key
                print self.secret
            else:
                self.key = conf.access_token
                self.secret = conf.access_secret

            auth.set_access_token(self.key, self.secret)
            self.api = tweepy.API(auth)
            print "%s logged in" % self.api.me().screen_name
            self.auth = auth

        except Exception, e:
            print e
            self.help_login()

    def help_login(self):
        print banner.help_login


if __name__ == '__main__':

    tw = TwCmd()
    tw.do_login(''); tw.do_list('')
    # tw.cmdloop()
