Beispiel #1
0
def set_status(fromUser, choice):
    from model.redisdb import RedisDB
    con = RedisDB().con
    if con.exists('WECHAT_STATUS:'+fromUser) == False:
        con.set('WECHAT_STATUS:'+fromUser, 0)
    if choice == 0:
        con.set('WECHAT_STATUS:'+fromUser, 0)
    elif choice == 1:
        if con.get('WECHAT_STATUS:'+fromUser) == '1':
            num = con.get('WECHAT_IMG_NUM:'+fromUser)
            con.set('WECHAT_STATUS:'+fromUser, 0)
            return int(num)
        else:
            con.set('WECHAT_STATUS:'+fromUser, 1)
            con.set('WECHAT_IMG_NUM:'+fromUser, 0)
            con.expire('WECHAT_STATUS:'+fromUser, 1800)
            import os
            path = os.path.realpath(__file__)
            path = '/'.join(path.split('/')[:-2])
            os.system("mkdir %s/tmp/user/%s"%(path,fromUser))
            os.system("mkdir %s/tmp/user/%s/recent"%(path,fromUser))
            os.system("mkdir %s/tmp/user/%s/sanguosha"%(path,fromUser))
            return -1
    elif choice == 2:
        con.set('WECHAT_STATUS:'+fromUser, '201')
Beispiel #2
0
def get_accesstoken():
    try:
        from model.redisdb import RedisDB
        con = RedisDB().con
    except:
        from redis import Redis
        con = Redis('localhost')
    access_token = con.get('WECHAT_ACCESS_TOKEN')
    if access_token != None:
        return access_token

    from config import APP_KEY, APP_SECRET
    import requests
    import urllib, json
    url = "https://api.weixin.qq.com/cgi-bin/token?"
    data = {
        'grant_type':'client_credential',
        'appid':APP_KEY,
        'secret':APP_SECRET
    }
    res = requests.get(url + urllib.urlencode(data))
    res_data = json.loads(res.text)
    access_token = res_data['access_token']
    con.set('WECHAT_ACCESS_TOKEN', access_token)
    con.expire('WECHAT_ACCESS_TOKEN', 3600)
    return access_token
Beispiel #3
0
def random_choose_music():
    import requests, urllib, json
    from model.redisdb import RedisDB
    con = RedisDB().con
    if con.exists("WECHAT_HOST_SONG") == False:
        url = "http://tingapi.ting.baidu.com/v1/restserver/ting?"
        data = {
            'from':'qianqian',
            'version':'2.1.0',
            'method':'baidu.ting.billboard.billList',
            'format':'json',
            'type':'2',
            'offset':'0',
            'size':'50'
        }
        res = requests.get(url + urllib.urlencode(data))
        data = json.loads(res.text)
        song_list = data['song_list']

        song_id_list = []
        for item in song_list:
            song_id = item['song_id']
            song_id_list.append(song_id)

        song_url = 'http://ting.baidu.com/data/music/links?'
        song_id_str = ','.join(song_id_list)

        res = requests.get(song_url + urllib.urlencode({'songIds':song_id_str}))

        data = json.loads(res.text)
        data = data['data']
        songList = data['songList']

        result = []
        for item in songList:
            songName = item['songName']
            artistName = item['artistName']
            showLink = item['showLink']
            if 'yinyueshiting.baidu.com' in showLink:
                result.append(json.dumps({
                    'songName':songName,
                    'artistName':artistName,
                    'showLink':showLink
                    }))
        for item in result:
            con.rpush('WECHAT_HOST_SONG', item)
        con.expire('WECHAT_HOST_SONG', 7200)

    from random import randint
    hotsong = con.lrange("WECHAT_HOST_SONG", 0, -1)
    length = len(hotsong)
    num = randint(0, length-1)
    return json.loads(hotsong[num])
Beispiel #4
0
def get_username_by_session(session):
    '''
        根据session获得用户名
    '''
    from model.redisdb import RedisDB
    con = RedisDB().con
    key = 'session2username:'+session
    value = con.get(key)

    if value != None:
        con.expire(key, 3600)

    return value
Beispiel #5
0
def user_auth(session, username, password, remember):
    '''
        验证username和password是否匹配,若是则将sessoin与username关联
        若remember为1,则将session放入redis中的记住登录表中
    '''
    from model.mongodb import MongoDB
    db = MongoDB().db
    user = db.user

    one = user.find_one({'username':username,'password':password})
    if one == None:
        return False

    from model.redisdb import RedisDB
    con = RedisDB().con
    key = 'session2username:'+session
    con.set(key, username)
    con.expire(key, 3600)

    return True
Beispiel #6
0
def bind_success():
    args = request.args
    if not "code" in args:
        return "error"

    code = args["code"]

    cookies = request.cookies
    if not "wecode" in cookies:
        return "error"
    wecode = cookies["wecode"]

    from wechat.tools import get_accesstoken_by_code

    we_access_token, openid = get_accesstoken_by_code(wecode)

    import requests
    from config import UBER_ID, UBER_SECRET

    url = "https://login.uber.com.cn/oauth/v2/token"
    para = {
        "client_secret": UBER_SECRET,
        "client_id": UBER_ID,
        "grant_type": "authorization_code",
        "redirect_uri": "https://wull.me/bind",
        "code": code,
    }
    res = requests.post(url, data=para)
    import json

    data = json.loads(res.text)
    access_token = data["access_token"]
    print wecode, access_token
    from model.redisdb import RedisDB

    con = RedisDB().con
    con.set("weopenid2uber:%s" % openid, access_token)
    con.expire("weopenid2uber:%s" % openid, 3600)

    return render_template("bind_success.html")