#!/usr/bin/python
import urllib, urllib2, json, os, time, sys

# You need to edit from here
tmpl_id = 'jX6DIlRzbr3dV1v6195qgLRFn6TVr2SEd6TGoFo5s1U'
appid = 'wx54602a12c477c961'
secret = 'dde1ceb8fb98cb3ec0a70e9f0849221b'
user_id = 'oUeGNtz41L35y49a_xqXGjWeBazU'

tmpl_data = {
    'touser':user_id,
    'template_id':tmpl_id,
    'url': 'http://www.domyself.me',
    'topcolor': '#ff0000',
    'data': {
        'first': {
            'value': '',
            'color': '#173177'
        },
        'send': {
            'value': '',
            'color': '#173177'
        },
        'text': {
            'value': '',
            'color': '#173177'
        },
        'time': {
            'value': '',
            'color': '#173177'
        },
        'remark': {
            'value': '',
            'color': '#173177'
        }
    }
}
# You need to edit end this

access_url = 'https://api.weixin.qq.com/cgi-bin/token'
access_url += '?grant_type=client_credential&appid='
access_url += appid+'&secret='+secret

tmpl_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send'
tmpl_url += '?access_token='

def get_access_token():
    realpath = os.path.split(os.path.realpath(__file__))[0]
    cachepath = realpath + r'/cache.log'

    if os.path.exists(cachepath):
        f = open(cachepath, 'r')
        data = f.readline().split('|')
        f.close()
        if len(data) == 2:
            if (int(data[1]) + 7100) > int( time.time() ):
                access_token = data[0]
            else:
                access_token = ''
        else:
            access_token = ''
    else:
        access_token = ''

    if access_token == '':
        req = urllib2.Request(access_url)
        res_data = urllib2.urlopen(req)
        res = res_data.read()
        decodejson = json.loads(res)
        access_token = decodejson['access_token']
        f = file(cachepath,"w+")
        cache_str = [access_token + r'|' + str( int( time.time() ) )]
        f.writelines(cache_str)
        f.close()
    return access_token

def get_tmpl_url():
    return tmpl_url + get_access_token();

def send(params):
    param_arr = params.split('|')
    for v in param_arr:
        v = v.split(':')
        tmpl_data['data'][v[0]]['value'] = v[1]
    request = urllib2.Request(get_tmpl_url() , json.dumps(tmpl_data))
    response = urllib2.urlopen(request)

if len(sys.argv) == 3:
    user_id = sys.argv[2]
if len(sys.argv) == 2:
    send(sys.argv[1])
@@ -1111,36 +1111,8 @@
 ='%0A%0A
-def get_access_token():%0A    
 real
@@ -1163,20 +1163,16 @@
 __))%5B0%5D%0A
-    
 cachepat
@@ -1201,16 +1201,40 @@
 e.log'%0A%0A
+def get_access_token():%0A
     if o
@@ -2172,18 +2172,53 @@
 split(':
+:
 ')
+   # using :: to avoid conflicting
 %0A       
@@ -2328,16 +2328,16 @@
 _data))%0A
-
     resp
@@ -2367,16 +2367,204 @@
 request)
+%0A    res = response.read()%0A    print res%0A    if json.loads(res)%5B'errcode'%5D == 40001:%0A        f = file(cachepath,%22w+%22)%0A        f.writelines(%5B''%5D)%0A        f.close()%0A        send(sys.argv%5B1%5D)
 %0A%0Aif len

fix bug