#!/usr/bin/env python3
import requests

def get_token():
    appid = 'wx8e174d4fb7ef5fb6'
    appsecret = 'b14c516621f544416cac0a2113f1587b'
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appsecret}'.format(appid=appid, appsecret=appsecret)
    r = requests.get(url)
    return r.json()['access_token']

def mplink(query, path='pages/index/index', env_version='release'):
    token = get_token()
    url = 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=' + token
    r = requests.post(url, json={
        'path': path,
        'query': query,
        'expire_type': 1,
        'expire_interval': 30,
        'env_version': env_version,
    })
    return r.json()['url_link']
