#!/usr/bin/python
# encoding: utf-8

import sys
from workflow import Workflow, web
import datetime

ICON_DEFAULT = 'icon.png'

def get_yunbi_tickers(query):
    url = 'https://yunbi.com//api/v2/tickers.json'
    r = web.get(url)
    r.raise_for_status()
    tickers = r.json()

    for name in tickers:

        if name[:-3] not in query:
            continue

        last  = tickers[name]['ticker']['last']
        sell  = tickers[name]['ticker']['sell']
        high  = tickers[name]['ticker']['high']
        buy   = tickers[name]['ticker']['buy']
        low   = tickers[name]['ticker']['low']
        vol   = tickers[name]['ticker']['vol']

        yunb_title    = (name[:-3]).upper() + u":\t云币\t最新价 " + str(last)
        yunb_arg      = "https://yunbi.com/markets/" + name
        yunb_subtitle = u"量:" + vol + u" 买:" + str(buy) + u" 卖:" + \
                        str(sell) + u" 高:" + high + u" 低:" + low

        wf.add_item(title    = yunb_title, 
                    subtitle = yunb_subtitle,
                    arg      = yunb_arg,
                    valid    = True,
                    icon     = ICON_DEFAULT)

def get_jubi_tickers(query):
    url = 'https://www.jubi.com/api/v1/allticker'
    r = web.get(url)
    r.raise_for_status()
    tickers = r.json()

    for name in tickers:

        if name not in query:
            continue

        last  = tickers[name]['last']
        buy   = tickers[name]['buy']
        sell  = tickers[name]['sell']
        high  = tickers[name]['high']
        low   = tickers[name]['low']
        vol   = tickers[name]['vol']

        jub_title    = name.upper() + u":\t聚币\t最新价 " + str(last) 
        jub_arg      = "https://www.jubi.com/coin/" + name
        jub_subtitle = u"量:" + str(vol) + u" 买:" + str(buy) + u" 卖:" + \
                       str(sell) + u" 高:" + str(high) + u" 低:" + str(low)

        wf.add_item(title    = jub_title,
                    subtitle = jub_subtitle,
                    arg      = jub_arg,
                    valid    = True,
                    icon     = ICON_DEFAULT)

def get_yuanbao_tickers(query):
    
    names = ['ans', 'btc', 'eth', 'etc', 'ltc', 'zec', 'qtum']

    for id in query:
        
        if id not in names:
            continue

        url = 'https://www.yuanbao.com/api_market/getInfo_cny/coin/' + id
        r = web.get(url)
        r.raise_for_status()
        ticker = r.json()

        name = ticker['name']
        last = ticker['price']
        buy  = ticker['buy']
        sell = ticker['sale']
        high = ticker['max']
        low  = ticker['min']
        vol  = ticker['volume_24h']
        url  = ticker['Markets']
        
        yuanb_title    = name + u":\t元宝\t最新价 " + last
        yuanb_subtitle = u"量:" + vol + u" 买:" + str(buy) + u" 卖:" + \
                         str(sell) + u" 高:" + high + u" 低:" + low

        wf.add_item(title    = yuanb_title, 
                    subtitle = yuanb_subtitle,
                    arg      = url,
                    valid    = True,
                    icon     = ICON_DEFAULT)

def main(wf):

    query = wf.args[0].strip().replace("\\", "")
    if not isinstance(query, unicode):
        query = query.decode('utf8')

    names = ['ans', 'btc', 'eth', 'etc', 'ltc', 'zec', 'qtum', 'bts', 'eos', 'sc']
    if query == 'yun':
        get_yunbi_tickers(names)
    elif query == 'ju':
        get_jubi_tickers(names)                     
    elif query == 'bao':                            
        get_yuanbao_tickers(names)
    else:
        get_yuanbao_tickers([query])
        get_yunbi_tickers([query])
        get_jubi_tickers([query])

    wf.send_feedback()
    

if __name__ == '__main__':
    wf = Workflow()
    logger = wf.logger
    sys.exit(wf.run(main))

  

