Пример #1
0
def getYesterdayKDJ(rlist):
	cyStockData = Object.extend('CYStockData')
	query = Query(cyStockData)  
	query.equal_to('kdjback', 1)
	query.greater_than('kdj',0)
	query.equal_to('volume',1)
	cyresults = query.find()
	for x in cyresults:		
		rlist.append(x.get("stcode"))
	szStockData = Object.extend('SZStockData')
	query = Query(szStockData)  
	query.equal_to('kdjback', 1)
	query.greater_than('kdj',0)
	query.equal_to('volume',1)
	szresults = query.find()
	for x in szresults:
		rlist.append(x.get("stcode"))
	shStockData = Object.extend('SHStockData')
	query = Query(shStockData)  
	query.equal_to('kdjback', 1)
	query.greater_than('kdj',0)
	query.equal_to('volume',1)
	shresults = query.find()
	for x in shresults:
		rlist.append(x.get("stcode"))
def get_tracker_data(table_name=None,tracker_list=None,field_name=None):

    DBTable = Object.extend(table_name)
    tracker_data_dict = {}
    for index,tracker in enumerate(tracker_list):
        #这样处理是因为可能一个user的记录超过了一次可以读取的数量(1K条)
        query = Query(DBTable)
        query.equal_to(field_name,tracker)
        query.less_than('createdAt',currentTime)
        total_count=query.count()
        # print 'TotalCount  %s' %str(total_count)

        query_times=(total_count+query_limit-1)/query_limit
        #如果想在这里按timestamp排序取出每个user的记录是不靠谱的,可能读取时还有插入,而且timestamp介于之间
        for index in range(query_times):
            # print 'querying index: %s' %str(index)
            query = Query(DBTable)
            query.equal_to(field_name,tracker)
            query.less_than('createdAt',currentTime)
            query.ascending('createdAt')
            query.limit(query_limit)
            query.skip(index*query_limit)
            if tracker in userDataDict.keys():
                tracker_data_dict.get(tracker).extend(query.find())
            else :
                tracker_data_dict[tracker]=query.find()
    return tracker_data_dict
Пример #3
0
def my_fans(username, howmany=100):
    user = user_by_username(username)
    
    fans_q = Query("_Follower")
    fans_q.equal_to("user", user)
    fans_q.limit(howmany)
    fans_q.include("follower")
    fans_res = fans_q.find()
    showrl(fans_res, show_func=showo_inStr)
    
    f_q = Query("_Follower")
    f_q.equal_to("user", user)
    f_q.limit(howmany)
    device_q = Query("Device")
    device_q.matches_key_in_query("caster", "follower", f_q)
    device_q.equal_to("status", "Active")
    device_q.include("caster")
    device_q.limit(howmany)
    device_res = device_q.find()
    showrl(device_res, show_func=showo_inStr)
    
    d = {}
    for f in fans_res:
        follower = f.get("follower")
        active_dev = None
        for dev in device_res:
            if dev.get("caster").id == follower.id:
                active_dev = dev
        d[follower.get('username')] = (follower, active_dev)
    return d
Пример #4
0
def updata_backend_info(parse_dict):
    print parse_dict
    user_id = parse_dict['user_id']
    # get user Object
    query = Query(Object.extend('_User'))
    query.equal_to('objectId', user_id)
    user = query.find()[0] if query.count() else None

    # get app Object
    query = Query(Object.extend('BindingInstallation'))
    query.equal_to('user', user)
    result_list = query.find()
    app_set = set()
    for result in result_list:
        app_set.add(result.attributes['application'].id)
    app_id_list = list(app_set)

    for app_id in app_id_list:
        query = Query(Object.extend('Application'))
        query.equal_to('objectId', app_id)
        app = query.find()[0]

        table_dash = Object.extend('DashboardSource')
        query = Query(table_dash)
        query.equal_to('app', app)
        query.equal_to('user', user)
        dst_table = query.find()
        if not dst_table:
            dst_table = table_dash()
        else:
            dst_table = dst_table[0]

        dst_table.set('app', app)
        for key, value in parse_dict.items():
            if key is 'user_id':
                dst_table.set('user', user)
            elif key is 'home_office_status':
                home_office_status = dst_table.get('home_office_status') or {}
                for k, v in parse_dict['home_office_status'].items():
                    home_office_status[k] = v
                dst_table.set('home_office_status', home_office_status)
            elif key is 'event':
                event = dst_table.get('event') or {}
                for k, v in parse_dict['event'].items():
                    event[k] = v
                dst_table.set('event', event)
            elif key is 'location':
                location = dst_table.get('location') or {}
                for k, v in parse_dict['location'].items():
                    location[k] = v
                dst_table.set('location', location)
            else:
                dst_table.set(key, value)

        dst_table.save()
        return True
Пример #5
0
    def get_age_and_gender_data_dict(self, table_name="AppStaticInfo", filed_name="app"):
        try:

            Application = Object.extend("Application")
            query = Query(Application)
            query.equal_to("app_id", self.app_id)
            result_list = query.find()
            length = len(result_list)
            if length == 0:
                print "error: application not exists in table Applicaiton"
                return 0
            elif length != 1:
                print "error: multi application exists in table Applicaiton"
                return 0
            else:
                app = result_list[0]
                DbTable = Object.extend(table_name)
                query = Query(DbTable)
                query.equal_to(filed_name, app)
                result_list = query.find()
                length = len(result_list)
                if length == 0:
                    print "error: application not exists in table %s" % (str(table_name))
                    return 0
                elif length != 1:
                    print "error: multi application  exists in table %s" % (str(table_name))
                    return 0
                else:
                    app_static_info = result_list[0]
                    age_and_gender_dict = app_static_info.get("age_and_gender")
                    return age_and_gender_dict

            # WeightedStaticInfo  = Object.extend('WeightedStaticInfo')
            # query = Query(WeightedStaticInfo)
            # query.exists('objectId')
            # query.select('age','gender')
            # staticInfoList = query.find()
            # gender_type_list =['man','woman']
            # age_type_list = ['16down','16to35','35to55','55up']
            # dataDict ={gender_type:{age_type:0 for age_type in age_type_list} for gender_type in gender_type_list}
            #
            # for staticInfo in staticInfoList:
            #     gender = 'man' if staticInfo.get('gender') >0 else 'woman'
            #     age_info_dict= staticInfo.get('age')
            #     dataDict[gender][age_info_dict.keys()[0]] += 1
            # # dataDict ={'man' if staticInfo.get('gender') >0 else 'woman':dataDict['man' if staticInfo.get('gender') >0 else 'woman'][staticInfo.get('age').keys()[0]] +=1 for staticInfo in staticInfoList}
            # new_data_dict = {key:[0 for i in range(4)] for key in dataDict.keys()}
            # for index ,age_type in enumerate(age_type_list):
            #     for gender_type in dataDict.keys():
            #         new_data_dict[gender_type][index] = dataDict[gender_type][age_type]

        except LeanCloudError, e:

            raise e
Пример #6
0
    def process_item(self, item, spider):

        Answers = Object.extend('Answers')
        Users = Object.extend('Users')


        for index ,ques in enumerate(item['floorNumList']):
            answer = Answers()
            user = Users()
            queryAnswer = Query(Answers)
            queryUser = Query(Users)
            queryAnswer.equal_to('questionLink',item['questionLink'])
            queryAnswer.equal_to('userId',item['userIdList'][index])
            queryAnswer.equal_to('answerTime',item['answerTimeList'][index])
            try:
                if queryAnswer.find():
                    pass
                else:
                    answer.set('questionLink',item['questionLink'])
                    answer.set('answerPageNum',item['answerPageNum'])

                    answer.set('floorNum',item['floorNumList'][index])
                    answer.set('answerPosition',item['answerPositionList'][index])

                    answer.set('userId',item['userIdList'][index])
                    answer.set('answerTime',item['answerTimeList'][index])
                    answer.set('answerIp',item['answerIpList'][index])
                    answer.set('answerContent',item['answerContentList'][index])
                    try:
                        answer.save()

                    except LeanCloudError,e:
                        print e
            except LeanCloudError,e:
                print e

            queryUser.equal_to('userId',item['userIdList'][index])
            try:
                if queryUser.find():
                    pass
                else:
                    user.set('userId',item['userIdList'][index])
                    user.set('userImgLink',item['userImgLinkList'][index])
                    user.set('userName',item['userNameList'][index])
                    user.set('userClass',item['userClass'][index])
                    user.set('userQuestionCount',item['userQuestionCountList'][index])
                    user.set('userScore',item['userScore'][index])
                    try:
                        user.save()
                    except LeanCloudError,e:
                        print e
            except LeanCloudError,e:
                print e
Пример #7
0
def answers():
    uid = request.cookies.get('uid')
    qid = request.cookies.get('qid')
    logger.debug('user id in cookie: ' + str(uid))
    wuser = None
    if uid:
        try:
            query = Query(WeUsers)
            wuser = query.get(uid)
        except leancloud.LeanCloudError:
            logger.warning("WeUser not found via uid %s", uid)
    logger.debug("request data: %s form: %s", request.data, request.form)
    if wuser and qid:
        query = Query(Answers)
        query.equal_to('qid', qid)
        query.equal_to('userid', uid)
        answer = None
        try:
            answer = query.first()
        except leancloud.LeanCloudError:
            logger.debug("Need to create table for answer first")
        if not answer:
            answer = Answers()
            answer.qid = qid
            answer.userid = uid
            answer.value = request.form.get('answer')
            answer.voter = wuser.nickname
            answer.headimg = wuser.headimgurl
            answer.save()
            logger.debug("answered as %s", answer)
        else:
            """
            check modify answer for question
            """
            logger.debug("already answered %s", answer)
        answers_query = Query(Answers)
        answers_query.equal_to('qid', qid)
        answers = answers_query.find()
        answer_count = len(answers)

        return render_template("answers.html", answers=answers, answers_count=answer_count)
    else:
        pass
        answers_query = Query(Answers)
        answers_query.equal_to('qid', "56def37b8ac2470056ece82d")
        answers = answers_query.find()
        answer_count = len(answers)
        return render_template("answers.html", answers=answers, answers_count=answer_count)
Пример #8
0
def validate_weuser():
    code = request.args.get('code')
    if code:
        url = str.format(
            'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code'
            % (wechat.conf.appid, wechat.conf.appsecret, str(code)))
    else:
        return None
    resp = requests.get(url)
    authorize_result = json.loads(resp.text)
    openid = authorize_result.get('openid')
    token = authorize_result.get('access_token')
    # userinfo = wechat.get_user_info(openid)
    if openid:
        logger.debug("wechat openId: %s, token: %s", openid, str(token))
    else:
        logger.debug("wechat openid invalidated!!!")
    try:
        query = Query(WeUsers)
        query.equal_to('openid', openid)
        wuser = query.find()
        if len(wuser) > 0:
            wuser = wuser[0]
        else:
            logger.debug('try to retrieve userinfo')
            wechathandler.get_user_info(openid, token)
            # wuser = wechathandler.retrieve_weuser(openid)
        return wuser
    except leancloud.LeanCloudError:
        logger.warning("WeUser not found")
Пример #9
0
def query_proxy_item():
    proxy_list = []
    query = Query(Proxy_Item)
    for i in query.find():
        if isinstance(i, Proxy_Item):
            proxy_list.append(i)
    return proxy_list
Пример #10
0
def sync_local():
    files = [ f for f in os.listdir(SYNC_DIR) if os.path.isfile(os.path.join(SYNC_DIR, f)) ]
    for f in files:
        fileName = f
        filePath = os.path.join(SYNC_DIR, f)
        if not (fileName.endswith('.jpeg') or fileName.endswith('.png') or fileName.endswith('.jpeg')):
            continue

        print('\n' + filePath)
        
        # 计算照片模糊 Hash 值
        im = Image.open(filePath)
        hash_meta = dhash(im)
        
        # 查询 LeanCloud,从本地移除重复照片
        query = Query(Photo)
        query.equal_to('dhash', hash_meta)
        results = query.find()
        if len(results) != 0:
            print "duplicate: (local=%s, leancloud=%s)" % (fileName, results[0].get('url'))
            os.rename(filePath, os.path.join(SYNC_DIR_DUPLICATED, fileName))
            continue

        # keep original file name for use in case
        originFileName = fileName

        millis = int(round(time.time() * 1000))
        hashFileName = hashids.encode(millis) + fileName[fileName.rfind('.')+1 :]
        os.rename(filePath, os.path.join(SYNC_DIR, hashFileName))

        # 解析 meta 信息
       	meta = parseImageMeta(hashFileName, im)
        # 保存链接与 meta 到 LeanCloud
        avosPhoto = savePhoto(meta)
def changeServerFileName(position,newName):
  query = Query(allPictures)
  query.equal_to('position', position)
  pictures = query.find()
  pictureSelect =  pictures[0]
  pictureSelect.set('image_src', newName)
  pictureSelect.save()
Пример #12
0
    def __init__(self):
        leancloud.init(settings.APP_ID, master_key=settings.MASTER_KEY)
        QuestionFollowee = Object.extend('Followee' + self.userDataId)
        questionFollowee = QuestionFollowee()  #
        query = Query(QuestionFollowee)

        query.equal_to('followerLinkId',self.userLinkId)
        curTime = datetime.now()
        query.less_than('createdAt', curTime)
        followeeCount = query.count()
        print "followeeNumbers: %s" % str(questionNum)
        queryLimit = 500
        queryTimes = int(followeeCount +queryLimit -1) / queryLimit
        self.urls = []
        for index in range(queryTimes):
            query = Query(QuestionFollowee)
            query.less_than('createdAt', curTime)
            query.equal_to('followerLinkId',self.userLinkId)
            query.descending('createdAt')
            query.limit(queryLimit)
            query.skip(index * queryLimit)
            query.select('followeeDataId')
            quesRet = query.find()
            for ques in quesRet:
                self.followeeDataIdList.extend(ques.get('followeeDataId'))
Пример #13
0
def is_exit(str):
    query = Query('DailySentence')
    query.equal_to('dateline', str)
    querys = query.find()
    size = len(querys)
    # print size
    return size
Пример #14
0
def isExit(title):
    global category
    query = Query('HJWordStudyCList')
    query.equal_to('category', category)
    query.equal_to('title', title)
    querys = query.find()
    return len(querys) > 0
def queryOneDataByPosition(position):
    query = Query(allPictures)
    query.equal_to('position', position)
    pictures = query.find()
    if len(pictures) > 0:
      return pictures[0].get('image_src')
    return ""
Пример #16
0
def getPositionLeanCloud():
    Position = Object.extend('Position')
    position = Query(Position)
    position.select('position')
    query_list = position.find()
    #    print (str(query_list[0]))
    return query_list
def get_field_data_dict(table_name='WeightedStaticInfo',tracker_list=None):
    try:
        field_name = 'field'
        tracker_field_name = 'user'
        DBTable  = Object.extend(table_name)

        data_dict ={}
        # new_data_dict = {key:[0 for i in range(4)] for key in dataDict.keys()}
        total_count = len(tracker_list)
        for index, tracker in enumerate(tracker_list):
            query = Query(DBTable)
            query.equal_to(tracker_field_name,tracker)
            query.select(field_name)
            result_list = query.find()
            length = len(result_list)
            # 以后这个地方的判断还需要修改
            if length!=1:
                print 'error: the length of result_list is %s with index: %s with tracker_objectId: %s' %(str(length),str(index),tracker.id)
            if length >=1:
                result = result_list[0]
            else:
                continue
            print 'index: %s  field_name: %s: result: %s  ' %(str(index),str(field_name),str(result.get(field_name)))
            field_key= result.get(field_name).keys()[0]
            if field_key in data_dict.keys():
                data_dict[field_key] += 1
            else:
                data_dict[field_key] = 1
        known_count = sum(data_dict.values())
        data_dict['unknown'] = total_count - known_count
    except LeanCloudError, e:
         raise e
def get_all_tracker(table_name=INSTALLATION_TABLE):
    DbTable = table_name
    query = Query(DbTable)
    query.less_than('createdAt',current_time)
    query.exists('objectId')
    total_count=query.count()
    query_times=(total_count+query_limit-1)/query_limit
    all_installation_list = []
    for index in range(query_times):
        query = Query(DbTable)
        query.exists('objectId')
        query.less_than('createdAt',current_time)
        query.select(USER_FIELD)
        query.ascending('createdAt')
        query.limit(query_limit)
        query.skip(index*query_limit)
        all_installation_list.extend(query.find())

    user_id_set = set()
    user_list = []
    for installation in all_installation_list:
        user = installation.get(USER_FIELD)
        user_id = user.id
        if user_id not in user_id_set:
            user_id_set.add(user_id)
            user_list.append(user)


        else:
            pass
    print "Have gotten all the trackers,total number is: %s" %(str(len(user_list)))
    return user_list
def get_all_behavior_prediction(table_name='UserBehavior',field_name='prediction'):
    DBTable = Object.extend(table_name)
    query = Query(DBTable)
    query.exists(field_name)
    query.less_than('updatedAt',currentTime)
    total_count=query.count()
    print 'TotalCount  %s' %str(total_count)

    query_times=(total_count+query_limit-1)/query_limit

    behavior_prediction_dict={}
    for index in range(query_times):
        print 'querying index: %s' %str(index)
        query = Query(DBTable)
        query.exists(field_name)
        query.less_than('updatedAt',currentTime)
        query.limit(query_limit)
        query.skip(index*query_limit)
        query.descending('updatedAt')
        result_list=query.find()
        for result in result_list:
            record_wrap ={}
            record_wrap['startTime'] = result.get('startTime')
            record_wrap['endTime'] = result.get('endTime')
            record_wrap['prediction'] = result.get(field_name)
            # record_wrap['UserBehavior'] = result   # 这里是为了绑定userBehavior
            behavior_prediction_dict[result]=record_wrap  #这里的键user由于是对象,所以即使是同一个user,他们也是不同的
    return behavior_prediction_dict
Пример #20
0
def alias_filter(message):
    text = message.text
    query = Query(Alias)
    alises = query.find()
    if len(alises) == 0:
        return
    catch = False
    aliases_dict = {x.get('key'): x.get('value') for x in alises}
    keys = [x.get('key') for x in alises]
    # make the longer key be replaced first
    matches = sorted(re.findall('|'.join(keys), text),
                     key=lambda x: len(x),
                     reverse=True)
    if len(matches) > 0:
        catch = True
    if len(matches) == 1:
        if aliases_dict.get(matches[0]) == ('@' + message.from_user.username):
            return
    for m in matches:
        if '@' in aliases_dict.get(m):
            prefix = ' '
            if (prefix + m) in text:
                text = text.replace(m, aliases_dict.get(m) + ' ')
            else:
                text = text.replace(m, prefix + aliases_dict.get(m) + ' ')
        else:
            text = text.replace(m, aliases_dict.get(m))

    if catch == True:
        text = get_nickname(message.from_user) + ': ' + text
        bot.sendMessage(chat_id=message.chat.id, text=text)
Пример #21
0
def is_exit(str):
    global category
    global type_name
    query = Query('Reading')
    query.equal_to('source_url', str)
    querys = query.find()
    return len(querys) > 0
Пример #22
0
def test_pointer_query():  # type: () -> None
    foo = Object.create("Foo")
    bar = Object.create("Bar")
    bar.save()
    foo.set("bar", bar)
    foo.save()

    q = Query("Foo").equal_to("bar", bar)
    assert len(q.find()) == 1

    inner_query = leancloud.Query("Post")
    inner_query.exists("image")
    query = leancloud.Query("Comment")
    query.matches_query("post", inner_query)
    assert query.dump() == {
        "where": {
            "post": {
                "$inQuery": {
                    "className": "Post",
                    "where": {
                        "image": {
                            "$exists": True
                        }
                    }
                }
            }
        }
    }
Пример #23
0
def uploadUser():
  username = genPhoneNum()
  deviceId = genID()
  providerName = random.choice(("中国移动", "中国联通", "中国电信"))
  model = random.choice(('MX4 Pro', 'MI 4 LTE', 'SM-G9009W'))
  sdkVersion = random.choice(('19', '17', '21'))
  releaseVersion = random.choice(('4.3', '4.4.4', '4.1.2'))

  user = User()
  query = Query(User) 
  query.equal_to('username', username)

  if(query.find()):
    username = genPhoneNum()

  user.set("username", username)
  user.set("password", "123456")
  user.set("deviceId", deviceId)
  user.set("providerName", providerName)
  user.set("model", model)
  user.set("sdkVersion", sdkVersion)
  user.set("releaseVersion", releaseVersion)
  user.set("value", 0)
  user.sign_up()

  user.set("mobilePhoneNumber", username)
  user.save()
Пример #24
0
def is_exit(str):
    global category
    query = Query('Reading')
    query.equal_to('title', str)
    query.equal_to('category', category)
    querys = query.find()
    return len(querys) > 0
Пример #25
0
def pic(message):
    cmd, text = parse_cmd_text(message.text)
    url = None
    base_url = 'http://7xqh4i.com1.z0.glb.clouddn.com/pic'
    if text != None:
        url = base_url + str(text)
        pic_num = text
    else:
        query = Query(Pic)
        pics = query.find()
        bolcks = [pic.get('pid') for pic in pics]
        pic_num = None
        size_of_images = 330  # 0~size_of_images
        while pic_num == None or str(pic_num) in bolcks:
            pic_num = random.randint(0, size_of_images)
        url = base_url + str(pic_num)
    bot.sendChatAction(chat_id=message.chat.id,
                       action=telegram.ChatAction.UPLOAD_PHOTO)

    def send_photo_task():
        bot.sendPhoto(chat_id=message.chat.id,
                      photo=url + '.jpg',
                      caption=pic_num)

    t = threading.Thread(target=send_photo_task)
    t.start()
Пример #26
0
def query_proxy_item():
    proxy_list = []
    query = Query(Proxy_Item)
    for i in query.find():
        if isinstance(i, Proxy_Item):
            proxy_list.append(i)
    return proxy_list
Пример #27
0
def test_pointer_query():
    foo = Object.create('Foo')
    bar = Object.create('Bar')
    bar.save()
    foo.set('bar', bar)
    foo.save()

    q = Query('Foo').equal_to('bar', bar)
    assert len(q.find()) == 1

    inner_query = leancloud.Query('Post')
    inner_query.exists("image")
    query = leancloud.Query('Comment')
    query.matches_query("post", inner_query)
    assert query.dump() == {
        'where': {
            'post': {
                '$inQuery': {
                    'className': 'Post',
                    'where': {
                        'image': {
                            '$exists': True
                        }
                    }
                }
            }
        }
    }
Пример #28
0
def is_exit(url):
    global category
    query = Query('Reading')
    query.equal_to('category', category)
    query.equal_to('source_url', url)
    querys = query.find()
    return len(querys) > 0
Пример #29
0
    def create_new_app(self, app_name):
        try:
            if not app_name:
                return 0
            user = self.user.become(self.session_token)
            print "Got the user"
            Application = Object.extend(APPLICATION_CLASS)
            application = Application()
            query = Query(Application)
            query.equal_to("user", user)
            query.equal_to("app_name", app_name)
            if query.find():
                print "Application name exists!"
                return 0
            else:
                print "Application name not exists! "
                application.set("app_name", app_name)
                application.set("user", user)
                application.save()
                app_id = application.id
                app_key = (signer.sign(app_id).split(app_id + "."))[1]
                # app_key = app_id+"this is app_key"

                application.set("app_id", app_id)
                application.set("app_key", app_key)
                application.save()
                return 1
        except LeanCloudError, e:
            print e
            return 0
Пример #30
0
    def get_event_to_location_data(self, event_name=None, app_table=APPLICATION_CLASS, kind=None):

        # print app_table
        try:

            app = self.get_the_app(kind=kind)
            db_name = "FakeEventActivity"
            DbTable = Object.extend(db_name)
            query = Query(DbTable)
            # 这里只是测试知道是少于1K条的
            query.equal_to("application", app)
            query.exists("location_dict")
            if event_name:
                query.equal_to("event_name", event_name)
            # query.equal_to('application_id',application_id)
            query.descending("createdAt")
            query.limit(1)
            result_list = query.find()
            if result_list:
                # event_name = result_list[0].get('event_name')
                # activity_statistics_dict = {result_list[0].get('event_name'):result_list[0].get('activity_dict')}
                statistics_dict = result_list[0].get("location_dict")
            else:
                statistics_dict = []
        except LeanCloudError, e:
            raise e
Пример #31
0
    def get_tracker_of_app(self, app_id=""):
        try:
            self.tracker_list = []
            # user = self.user.become(self.session_token)
            Application = Object.extend("Application")
            query = Query(Application)
            # query.equal_to('user', user)

            # app_id = 'demo 55f7e36f60b2fe7115171b4b'
            print "@@@@@@@@@@@@@@@@@@" + app_id
            query.equal_to("app_id", app_id)
            app_list = query.find()

            if len(app_list) != 1:
                return []
            the_app = app_list[0]

            # Tracker = Object.extend('BindingInstallation')
            # query = Query(Tracker)
            # query.equal_to('application', the_app)
            # app_list = query.find()
            relation = the_app.relation("tracker")
            print relation,
            return 1
        except LeanCloudError, e:
            print e
            return 0
Пример #32
0
    def get_sport_data_dict(self, app_table=APPLICATION_CLASS, field_name="app", kind=None):
        try:
            field = "sport"
            app = self.get_the_app(kind=kind)
            static_info_table = STATIC_INFO_TABLE
            DbTable = Object.extend(static_info_table)
            query = Query(DbTable)
            query.equal_to(field_name, app)
            query.exists(field)
            result_list = query.find()
            length = len(result_list)
            if length == 0:
                print "error: application not exists in table %s" % (str(static_info_table))
                return {}
            elif length > 1:
                print "error: multi application  exists in table %s" % (str(static_info_table))
                # return 0

            app_static_info = result_list[0]
            data_dict = app_static_info.get(field)
            # return age_and_gender_dict

        except LeanCloudError, e:

            raise e
Пример #33
0
    def __init__(self):
        leancloud.init('mctfj249nwy7c1ymu3cps56lof26s17hevwq4jjqeqoloaey', master_key='ao6h5oezem93tumlalxggg039qehcbl3x3u8ofo7crw7atok')

        Users = Object.extend('Users')
        query = Query(Users)
        curTime = datetime.now()
        query.exists('userId')
        query.less_than('createdAt',curTime)
        userCount = query.count()


        print "userCounts: %s" %str(userCount)
        queryLimit = 500
        queryTimes = userCount/queryLimit + 1
        self.urls = []

        for index in range(queryTimes):
            query = Query(Users)
            query.exists('userId')
            query.less_than('createdAt',curTime)
            query.descending('createdAt')
            query.limit(queryLimit)
            query.skip(index*queryLimit)
            query.select('userId')
            userRet = query.find()
            for user in userRet:
                self.urls.append(self.baseUrl + user.get('userId') +".json")
        pass
def get_occupation_data_dict(table_name='WeightedStaticInfo',tracker_list=None):
    try:
        field_name = 'user'
        DBTable  = Object.extend(table_name)
        gender_type_list =['man','woman']
        age_type_list = ['16down','16to35','35to55','55up']
        dataDict ={gender_type:{age_type:0 for age_type in age_type_list} for gender_type in gender_type_list}
        # new_data_dict = {key:[0 for i in range(4)] for key in dataDict.keys()}
        total_count = len(tracker_list)
        for index, tracker in enumerate(tracker_list):
            query = Query(DBTable)
            query.equal_to(field_name,tracker)
            query.select('age','gender')
            result_list = query.find()
            length = len(result_list)
            if length!=1:
                print 'error: the length of result_list is %s with index: %s with tracker_objectId: %s' %(str(length),str(index),tracker.id)
            if length >=1:
                result = result_list[0]
            else:
                continue

            print 'index: %s  gender: %s  age: %s ' %(str(index),str(result.get('gender')),str(result.get('age')))
            gender = 'man' if result.get('gender') >0 else 'woman'
            age_info_dict= result.get('age')
            dataDict[gender][age_info_dict.keys()[0]] += 1
            # dataDict ={'man' if staticInfo.get('gender') >0 else 'woman':dataDict['man' if staticInfo.get('gender') >0 else 'woman'][staticInfo.get('age').keys()[0]] +=1 for staticInfo in staticInfoList}

            # for index ,age_type in enumerate(age_type_list):
            #     for gender_type in dataDict.keys():
            #         new_data_dict[gender_type][index] = dataDict[gender_type][age_type]
        known_count = sum(dataDict['man'].values())+sum(dataDict['woman'].values())
        dataDict['unknown'] = total_count -known_count
    except LeanCloudError, e:
         raise e
def connect_dev_and_real_app(table_name='Developer'):
    all_real_app = get_all_real_applications()
    DbTable = table_name
    query = Query(DbTable)
    query.less_than('createdAt',current_time)
    query.exists('objectId')
    query.equal_to('username','heamon7')
    dev = query.find()[0]

    # total_count=query.count()
    # query_times=(total_count+query_limit-1)/query_limit
    # user_list = []
    # for index in range(query_times):
    #     query = Query(DbTable)
    #     query.exists('objectId')
    #     query.less_than('createdAt',current_time)
    #     query.ascending('createdAt')
    #     query.limit(query_limit)
    #     query.skip(index*query_limit)
    #     user_list.extend(query.find())
    # return user_list

    # all_installation = get_all_installation()
    # print 'already get all installation ,length is %s' %(str(len(all_installation)))
    for app in all_real_app:
        app.set('developer',dev)
        app.save()
def is_exit(str):
    global category
    query = Query('Reading')
    query.equal_to('title', str)
    query.equal_to('category', category)
    querys = query.find()
    return len(querys) > 0
Пример #37
0
def is_exit(name, url):
    query = Query('Caricature')
    query.equal_to('name', name)
    query.equal_to('url', url)
    querys = query.find()

    return len(querys) > 0
Пример #38
0
def uploadG3s():
    query = Query(User) 
    query.exists("username")
    results = query.find()

    for i in range (0, 10):
        uploadG3(results)  
Пример #39
0
def alias_filter(message):
    text = message.text
    query = Query(Alias)
    alises = query.find()
    if len(alises) == 0:
        return
    catch = False
    aliases_dict = {x.get('key'): x.get('value') for x in alises}
    keys = [x.get('key') for x in alises]
    # make the longer key be replaced first
    matches = sorted(re.findall('|'.join(keys), text), key=lambda x: len(x), reverse=True)
    if len(matches) > 0:
        catch = True
    if len(matches) == 1:
        if aliases_dict.get(matches[0]) == ('@' + message.from_user.username):
            return
    for m in matches:
        if '@' in aliases_dict.get(m):
            prefix = ' '
            if (prefix + m) in text:
                text = text.replace(m, aliases_dict.get(m) + ' ')
            else:
                text = text.replace(m, prefix + aliases_dict.get(m) + ' ')
        else:
            text = text.replace(m, aliases_dict.get(m))

    if catch == True:
        text = get_nickname(message.from_user) + ': ' + text
        bot.sendMessage(chat_id=message.chat.id,
                        text=text)
def create_demo_application(table_name='DemoApplication'):
    all_real_app = get_all_real_applications()
    DbTable = Object.extend(table_name)

    for index, app in enumerate(all_real_app):
        db_table = DbTable()
        relation = app.relation('tracker')
        # query = Query(DbTable)
        query = relation.query()
        user_list = query.find()
        relation = db_table.relation('tracker')
        for user in user_list:
            ADbTable = Object.extend('Tracker')
            query = Query(ADbTable)
            query.equal_to('objectId',user.id)
            result_list = query.find()
            if result_list:
                tracker = result_list[0]
                relation.add(tracker)
            else:
                print 'tracker not exists and objectId is: %s' %(str(user.id))

        db_table.set('app_name',app.get('app_name'))
        db_table.set('origin_name',app.get('name'))
        db_table.save()
Пример #41
0
def get_readys():
    '''
    返回当前准备好的人
    '''
    user_query = Query(User)
    user_query.equal_to('ready', True)
    return user_query.find()
def get_tracker_of_app(app_id):
    query = Query(Object.extend('Application'))
    query.equal_to('app_id', app_id)
    app_list = query.find()
    if not app_list:
        return []
    the_app = app_list[0]

    query = Query(Object.extend('BindingInstallation'))
    query.equal_to('application', the_app)
    query.select('user')
    installation_list = query.find()
    user_set = set()
    for installation in installation_list:
        user_set.add(installation.attributes['user'].id)
    return list(user_set)
Пример #43
0
def changeServerFileName(position, newName):
    query = Query(allPictures)
    query.equal_to('position', position)
    pictures = query.find()
    pictureSelect = pictures[0]
    pictureSelect.set('image_src', newName)
    pictureSelect.save()
Пример #44
0
def queryOneDataByPosition(position):
    query = Query(allPictures)
    query.equal_to('position', position)
    pictures = query.find()
    if len(pictures) > 0:
        return pictures[0].get('image_src')
    return ""
Пример #45
0
def get_all_feed(name):
    try:
        query = Query(Feed)
        query.equal_to('name', name).descending("time")
        return query.find()
    except LeanCloudError, e:
        if e.code == 101: return []
        else: raise (e)
Пример #46
0
def clear_feed(name, day):
    oldest = datetime.now() - timedelta(days=day)
    query = Query(Feed)
    query.equal_to('name', name).less_than("time", oldest)
    for e in query.find():
        print('delete old feed: %s (%s)' %
              (e.get('title').encode('utf-8'), e.get('time')))
        e.destroy()
Пример #47
0
def device_status():
    try:
        query = Query(DeviceKey)
        query.ascending('index')
        devices = query.find()
    except:
        jsonify(status='find error')
    return render_template('status.html', devices=devices)
Пример #48
0
def query_proxy():
    proxy_list = []
    query = Query(Proxy)
    query.select('src')
    reslut = query.find()
    for i in reslut:
        proxy_list.append(i.get('src'))
    return proxy_list
Пример #49
0
def averageStars(movie):
    sum = 0
    query = Query('Review')
    try:
        reviews = query.find()
    except leancloud.LeanCloudError, e:
        print e
        raise e
Пример #50
0
def getOnePictureByPosition(position):
    query = Query(allPictures)
    query.equal_to('position', position)
    pictures = query.find()
    if len(pictures) > 0:
        return pictures[0]
    else:
        return None
def get_lastest_item_id():
    query = Query('Reading')
    query.equal_to('category', 'composition')
    query.equal_to('source_name', '恒星英语')
    query.descending("item_id")
    query.limit(1)
    querys = query.find()
    itemId = querys[0].get("item_id")
    return itemId
Пример #52
0
def get_lastest_item_id():
    query = Query('Reading')
    query.descending("item_id")
    query.limit(1)
    querys = query.find()
    if len(querys) == 0:
        return 0
    else:
        return querys[0].get("item_id")
Пример #53
0
def test_pointer_query():
    foo = Object.create('Foo')
    bar = Object.create('Bar')
    bar.save()
    foo.set('bar', bar)
    foo.save()

    q = Query('Foo').equal_to('bar', bar)
    assert len(q.find()) == 1
Пример #54
0
def getType(type_name):
    query = Query('HJWordStudyCategory')
    query.equal_to('name', type_name)
    querys = query.find()
    if len(querys) > 0:
        data = querys[0]
        return data.get('type_code')
    else:
        print 'not exit'
Пример #55
0
def requestConfirmationCode(**params):
    print 'request confirmation code starts'
    if 'email' in params:
        try:
            fromaddr = "*****@*****.**"
            toaddr = params['email']
            print "toaddr: " + toaddr
            password = "******"

            code = ''.join(choice(digits) for i in xrange(6))
            # code = "123456"
            print "code: " + code
            ConfirmationCode = Object.extend('ConfirmationCode')
            query = Query(ConfirmationCode)
            query.equal_to('email', toaddr)
            query_list = query.find()
            if len(query_list) > 0:
                concode = query_list[0]
                concode.set('code', code)
                concode.save()
            else:
                concode = ConfirmationCode()
                concode.set('email', toaddr)
                concode.set('code', code)
                concode.save()

            message = MIMEMultipart()
            message['From'] = fromaddr
            message['To'] = toaddr
            message['Subject'] = "【USC日常】你的注册验证码是 " + code
            body = ""
            body += "同学你好!\n\n你正在注册使用USC日常,你的验证码是 "
            body += code
            body += ".\n此验证码将会在20分钟后失效,请尽快验证。\n\n\n"
            body += "\n如果你并没有注册USC日常,请忽略此邮件。\n"
            body += "\n\nbest,"
            body += "\nUSC日常APP"
            message.attach(MIMEText(body, 'plain'))

            server = smtplib.SMTP('smtp.gmail.com', '587')
            server.ehlo()
            server.starttls()
            server.login(fromaddr, password)
            text = message.as_string()
            server.sendmail(fromaddr, toaddr, text)
            server.quit()
            print 'request confirmation code ends'
            return True
        except Exception as e:
            print e
            print 'request confirmation code ends'
            raise LeanEngineError('发送验证码失败,请稍后重试')
    else:
        print "email cannot be empty"
        print 'request confirmation code ends'
        raise LeanEngineError('邮箱地址不能为空')
Пример #56
0
def test_basic_query():
    # find
    q = Query(GameScore)
    results = q.find()
    eq_(len(results), 10)

    # first
    q = Query(GameScore)
    game_score = q.first()
    assert game_score

    # get
    q = Query(GameScore)
    local_game_score = game_scores[0]
    q.get(local_game_score.id)

    # count
    q = Query(GameScore)
    eq_(q.count(), 10)

    # descending
    q = Query(GameScore).descending('score')
    eq_([x.get('score') for x in q.find()], range(9, -1, -1))

    # greater_than
    q = Query(GameScore).greater_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(6, 10))

    q = Query(GameScore).greater_than_or_equal_to('score',
                                                  5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(5, 10))

    q = Query(GameScore).less_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 5))

    q = Query(GameScore).less_than_or_equal_to('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 6))

    q = Query(GameScore).contained_in('score', [1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(1, 4))

    q = Query(GameScore).not_contained_in('score',
                                          [0, 1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(4, 10))

    q = Query(GameScore).select('score')
    assert not q.find()[0].has('playerName')
Пример #57
0
def min_pm(key):
    query = Query(test_esp)
    query.equal_to('key', key)
    query.descending('createdAt')
    pm = []
    query.limit(60)
    results = query.find()
    for result in results:
        pm.append(result.get('pm'))
    pm.reverse()
    return pm
Пример #58
0
def min_noise(key):
    query = Query(test_esp)
    query.equal_to('key', key)
    query.descending('createdAt')
    noise = []
    query.limit(60)
    results = query.find()
    for result in results:
        noise.append(result.get('noise'))
    noise.reverse()
    return noise
Пример #59
0
def get_lastest_item_id():
    query = Query('Reading')
    query.equal_to('category', 'composition')
    query.equal_to('source_name', '水滴英语作文网')
    query.descending("item_id")
    query.limit(1)
    querys = query.find()
    if len(querys) == 0:
        return 0
    else:
        return querys[0].get("item_id")