#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# name     : wbrelay, weibo relay
# author   : Xu Xiaodong <xxdlhy@gmail.com>
# created  : 2014 Sep 05
# modified : 2014 Sep 09
#

'''
wbrelay - weibo relay

Usage:
    wbrelay <status> [<picture>]

Example:
    wbrelay 'Hello wbrelay!'
    wbrelay 'Hello wbrelay!' /path/to/picture.png
'''

import os
import os.path
import sys
import time
import ConfigParser

from snspy import APIClient, SinaWeiboMixin


config = ConfigParser.RawConfigParser()
home = os.environ['HOME']
config.read(home + '/.wbrelayrc')

APP_KEY = config.get('wbrelay', 'app_key')
APP_SECRET = config.get('wbrelay', 'app_secret')
CALLBACK_URL = config.get('wbrelay', 'callback_url')
TOKEN_FILE = home + '/.wbrelayat'


def save_token():
    client = APIClient(SinaWeiboMixin, 
            app_key=APP_KEY, 
            app_secret=APP_SECRET, 
            redirect_uri=CALLBACK_URL)

    auth_url = client.get_authorize_url()
    print('Please open %s, then type code: ' % auth_url)

    code = input()

    req = client.request_access_token(code)
    access_token = req.access_token
    expires = req.expires

    token = open(TOKEN_FILE, 'w')
    token.write(access_token + ' ' + str(expires))
    token.close()


def post_status(status, pic=None):
    if not os.path.isfile(TOKEN_FILE):
        save_token()

    token = open(TOKEN_FILE)
    line = token.read().strip()
    access_token, expires = line.split()

    now = time.time()
    if float(expires) < now:
        print('Expired token :(')
        save_token()
        sys.exit(1)

    client = APIClient(SinaWeiboMixin, 
            app_key=APP_KEY, 
            app_secret=APP_SECRET,
            redirect_uri=CALLBACK_URL, 
            access_token=access_token, 
            expires=expires)

    if pic:
        pic_file = open(pic, 'rb')
        rc = client.statuses.upload.post(status=status, pic=pic_file)
    else:
        rc = client.statuses.update.post(status=status)

    return rc


if __name__ == '__main__':
    arg = len(sys.argv) - 1
    if arg == 1:
        status = sys.argv[1]
        post_status(status)
    elif arg == 2:
        status = sys.argv[1]
        pic = sys.argv[2]
        post_status(status, pic)
    else:
        print(__doc__)
