#!/usr/bin/env python
# coding:utf-8
import sys
import urllib2
import time
import json
import requests
 
reload(sys)
sys.setdefaultencoding('utf-8')
print sys.argv
 
title = sys.argv[2]
content = sys.argv[3]
user = sys.argv[1]
 
 
class Token(object):
    # 获取token
    def __init__(self, corpid, corpsecret):
            corpid, corpsecret)
        self.expire_time = sys.maxint
 
    def get_token(self):
        if self.expire_time > time.time():
            request = urllib2.Request(self.baseurl)
            response = urllib2.urlopen(request)
            ret = response.read().strip()
            ret = json.loads(ret)
            if 'errcode' in ret.keys():
                print >> ret['errmsg'], sys.stderr
                sys.exit(1)
            self.expire_time = time.time() + ret['expires_in']
            self.access_token = ret['access_token']
        return self.access_token
 
 
def send_msg(content):
    # 发送消息
    # 填写自己应用的
    corpid = "wx1bf897b86ad3abe0"
    # 填写自己应用的
    corpsecret = 'aquTZMoaLJ8M0hovSCDZeTPfqQuxLT12PnPQ8KwRcifikz7qxd1pwtZxPRqUXPJC'
    qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token()
        qs_token)
    payload = {
        "touser": user,
        #"touser": "@all",
        "msgtype""text",
        "agentid""1",
        "text": {
                   #"content": "标题:{0}\n 内容:{1}".format(title, content)
                    "content""{0}".format(content)
 
        },
        "safe""0"
    }
    ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False))
    print ret.json()
 
if __name__ == '__main__':
    # print title, content
    send_msg(content)