コード例 #1
0
    def post(self, request, *args, **kwargs):

        try:
            data = json.loads(request.body)
            user_id = data['user_id']
            device_id = data['device_id']
            bonus_id = data['bonus_id']

            r = RedisClient().connect()

            redis = RedisHelper()
            if redis.can_claim_bonus(bonus_id, device_id):
                print("Can claim bonus")
            else:
                print("Can not claim bonus")

            redis.set_bonus_by_device(bonus_id, device_id)
            redis.set_device_by_user(user_id, device_id)
            redis.set_user_by_device(user_id, device_id)
            print(redis.get_bonuses_by_device(device_id))
            print(redis.get_devices_by_user(user_id))
            print(redis.get_users_by_device(device_id))

            data = {}

            return HttpResponse(json.dumps(data),
                                content_type='application/json',
                                status=200)
        except Exception as e:
            logger.error("Error connect to AWS redis: {}".format(str(e)))
            return HttpResponse(status=400)
コード例 #2
0
    def post(self, request, *args, **kwargs):
        try:
            r = RedisClient().connect()
            redis = RedisHelper()

            start_time = redis.get_ky_bets_timestamp

            timestamp = get_timestamp()
            if start_time is None:
                start_time = timestamp - 300000  # five minutes before now

            # Query Bet Order
            if type(start_time) == bytes:
                start_time = start_time.decode("utf-8")

            end_time = timestamp - 60000  # one minute before now

            param = "s=6" + "&startTime=" + str(
                start_time) + "&endTime=" + str(end_time)

            param = aes_encode(KY_AES_KEY, param)
            param = base64.b64encode(param)
            param = str(param, "utf-8")

            key = KY_AGENT + str(timestamp) + KY_MD5_KEY
            key = hashlib.md5(key.encode())
            key = key.hexdigest()

            url = KY_RECORD_URL

            req_param = {}
            req_param["agent"] = KY_AGENT
            req_param["timestamp"] = str(timestamp)
            req_param["param"] = param
            req_param["key"] = key

            req = urllib.parse.urlencode(req_param)
            url = url + '?' + req
            res = requests.get(url)

            if res.status_code == 200:
                data = res.json()

                if data['d']['code'] == 0:
                    count = int(data['d']['count'])
                    record_list = data['d']['list']

                    provider = GameProvider.objects.get(
                        provider_name=KY_PROVIDER)
                    category = Category.objects.filter(name='Table Games')

                    game_id = record_list['GameID']
                    accounts = record_list['Accounts']
                    # server_id = record_list['ServerID']
                    # kind_id = record_list['KindID']
                    # table_id = record_list['TableID']
                    cell_score = record_list['CellScore']
                    profit = record_list['Profit']
                    revenue = record_list['Revenue']
                    start_time = record_list['GameStartTime']
                    end_time = record_list['GameEndTime']

                    gamebets_list = []

                    for i in range(0, count):
                        username = accounts[i][6:]
                        user = CustomUser.objects.get(username=username)

                        trans_id = user.username + "-" + timezone.datetime.today(
                        ).isoformat() + "-" + str(random.randint(0, 10000000))

                        bet_time = datetime.strptime(start_time[i],
                                                     '%Y-%m-%d %H:%M:%S')
                        bet_time = bet_time.replace(
                            tzinfo=pytz.timezone(provider.timezone))
                        bet_time = bet_time.astimezone(pytz.utc)

                        resolved_time = datetime.strptime(
                            end_time[i], '%Y-%m-%d %H:%M:%S')
                        resolved_time = resolved_time.replace(
                            tzinfo=pytz.timezone(provider.timezone))
                        resolved_time = resolved_time.astimezone(pytz.utc)

                        if int(cell_score[i] == 0):
                            outcome = 2  # Tie Game
                        if win_amount > 0:
                            outcome = 0  # Won
                        else:
                            outcome = 1  # Lose

                        gamebet = GameBet(
                            provider=provider,
                            category=category[0],
                            user=user,
                            user_name=user.username,
                            amount_wagered=decimal.Decimal(cell_score[i]),
                            amount_won=decimal.Decimal(win_amount),
                            outcome=outcome,
                            transaction_id=trans_id,
                            market=ibetCN,
                            ref_no=game_id[i],
                            bet_time=bet_time,
                            resolved_time=resolved_time,
                            other_data={})

                        gamebets_list.append(gamebet)

                    # bulk_create
                    GameBet.objects.bulk_create(gamebets_list)
                    # Set Redis
                    redis.set_ky_bets_timestamp(end_time)

                    return HttpResponse(
                        "You have add {} records".format(count), status=200)
                else:
                    redis.set_ky_bets_timestamp(end_time)
                    return HttpResponse("No record at this time", status=200)
            else:
                logger.warning("Kaiyuan GetRecord Failed: {}".format(
                    repr(res)))
                return HttpResponse("Kaiyuan GetRecord Failed: {}".format(
                    repr(res)))
        except Exception as e:
            logger.error("Kaiyuan Game Background Task Error: {}".format(
                repr(e)))
            return HttpResponse(
                "Kaiyuan Game Background Task Error: {}".format(repr(e)),
                status=200)
コード例 #3
0
 def __init__(self):
     self.r = RedisClient().connect()
コード例 #4
0
class RedisHelper():
    
    def __init__(self):
        self.r = RedisClient().connect()

    def can_claim_bonus(self, bonus_id, device_id):
        bonus_by_device_key = getBonusByDeviceRedisKey(device_id)
        return not self.r.sismember(bonus_by_device_key, bonus_id)

    def set_bonus_by_device(self, bonus_id, device_id):
        bonus_by_device_key = getBonusByDeviceRedisKey(device_id)
        return self.r.sadd(bonus_by_device_key, bonus_id)

    def set_device_by_user(self, user_id, device_id):
        devices_by_user_key = getDevicesByUserRedisKey(user_id)
        return self.r.sadd(devices_by_user_key, device_id)
    
    def set_user_by_device(self, user_id, device_id):
        users_by_device_key = getUsersByDeviceRedisKey(device_id)
        return self.r.sadd(users_by_device_key, user_id)

    def get_bonuses_by_device(self, device_id):
        bonus_by_device_key = getBonusByDeviceRedisKey(device_id)
        return self.r.smembers(bonus_by_device_key)

    def get_devices_by_user(self, user_id):
        devices_by_user_key = getDevicesByUserRedisKey(user_id)
        return self.r.smembers(devices_by_user_key)

    def get_users_by_device(self, device_id):
        users_by_device_key = getUsersByDeviceRedisKey(device_id)
        return self.r.smembers(users_by_device_key)

    def set_onebook_bet_details(self, onebook_run):
        onebook_bet_details = getOnebookBetDetailsRedisKey()
        return self.r.sadd(onebook_bet_details, onebook_run)

    def check_onebook_bet_details(self, onebook_run):
        onebook_bet_details = getOnebookBetDetailsRedisKey()
        return self.r.sismember(onebook_bet_details, onebook_run)

    def remove_onebook_bet_details(self, onebook_run):
        onebook_bet_details = getOnebookBetDetailsRedisKey()
        return self.r.srem(onebook_bet_details, onebook_run)

    def set_latest_timestamp(self, key, timestamp):
        return self.r.set(key, timestamp)

    def get_latest_timestamp(self, key):
        return self.r.get(key)

    def set_pt_starttime(self, timestamp):
        pt_key = getPTStarttimeKey()
        return self.r.set(pt_key, timestamp)

    def get_pt_starttime(self):
        pt_key = getPTStarttimeKey()
        return self.r.get(pt_key)


    def set_ag_added_file(self, ftp_file):
        ag_file = getAGFileHistoryRedisKey()
        return self.r.set(ag_file, ftp_file)
    
    def get_ag_added_file(self):
        ag_file = getAGFileHistoryRedisKey()
        return self.r.get(ag_file)

    def set_ea_last_file(self, file_name):
        ea_bet_history = getEABetHistroyRedisKey()
        return self.r.set(ea_bet_history, file_name)

    def get_ea_last_file(self):
        ea_bet_history = getEABetHistroyRedisKey()
        return self.r.get(ea_bet_history)

    def set_ky_bets_timestamp(self, timestamp):
        ky_bet_details = getKYBetDetailsRedisKey()
        return self.r.set(ky_bet_details, timestamp)

    def get_ky_bets_timestamp(self):
        ky_bet_details = getKYBetDetailsRedisKey()
        return self.r.get(ky_bet_details)

    def set_gpi_bets_starttime(self, starttime):
        gpi_bet_starttime = getGPIBetDetailsRedisKey()
        return self.r.set(gpi_bet_starttime, starttime)

    def get_gpi_bets_starttime(self):
        gpi_bet_starttime = getGPIBetDetailsRedisKey()
        return self.r.get(gpi_bet_starttime)
コード例 #5
0
    def post(self, request, *kw, **args):
        try:
            r = RedisClient().connect()
            redis = RedisHelper()

            start_time = redis.get_gpi_bets_starttime()

            if start_time is None:
                start_time = datetime.datetime.now() - datetime.timedelta(
                    minutes=10)  # 10 minutes before now
                start_time = start_time.strftime("%Y-%m-%d %H:%M:%S")

            # Query Bet Order
            if type(start_time) == bytes:
                start_time = start_time.decode("utf-8")

            end_time = datetime.datetime.now() - datetime.timedelta(
                minutes=1)  # 1 minutes before now
            end_time = end_time.strftime("%Y-%m-%d %H:%M:%S")

            req_param = {}
            req_param["merch_id"] = GPI_MERCH_ID
            req_param["merch_pwd"] = GPI_MERCH_PWD
            req_param["date_from"] = start_time
            req_param["date_to"] = end_time
            req_param["page_size"] = 999  # Max

            req = urllib.parse.urlencode(req_param)

            url = GPI_LIVE_CASINO_URL + 'newBetDetail.html' + '?' + req

            res = requests.get(url)

            res = xmltodict.parse(res.text)

            if res["resp"]["error_code"] == "0":
                res = res["resp"]["items"]
                if res["@total_row"] == '0':
                    return HttpResponse("No bet record at this time",
                                        status=200)
                else:
                    provider = GameProvider.objects.get(
                        provider_name=GPI_PROVIDER)
                    category = Category.objects.filter(name='Live Casino')

                    gamebets_list = []

                    for data in res["item"]:
                        user = data["@user_id"]
                        user = CustomUser.objects.get(pk=user)

                        resolved_time = data["@trans_date"]

                        outcome = Decimal(data["@winlose"])

                        if outcome > 0:
                            outcome = 0  # Win
                        elif outcome < 0:
                            outcome = 1  # Lose
                        else:
                            outcome = 2  # Tie

                        trans_id = user.username + "-" + timezone.datetime.today(
                        ).isoformat() + "-" + str(random.randint(0, 10000000))

                        # 2020-01-24 06:08:16
                        resolved_time = datetime.datetime.strptime(
                            data["@trans_date"], '%Y-%m-%d %H:%M:%S')
                        resolved_time = resolved_time.replace(
                            tzinfo=pytz.timezone(provider.timezone))
                        resolved_time = resolved_time.astimezone(pytz.utc)

                        gamebet = GameBet(
                            provider=provider,
                            category=category[0],
                            user=user,
                            user_name=user.username,
                            amount_wagered=Decimal(data["@bet"]),
                            amount_won=Decimal(data["@winlose"]),
                            outcome=outcome,
                            transaction_id=trans_id,
                            market=ibetCN,
                            ref_no=data["@bet_id"],
                            # bet_time=bet_time,
                            resolved_time=resolved_time,
                            other_data={})

                        gamebets_list.append(gamebet)

                    GameBet.objects.bulk_create(gamebets_list)
                    redis.set_gpi_bets_starttime(end_time)

                    return HttpResponse("You have add {} records".format(
                        len(gamebets_list)),
                                        status=200)
            else:
                logger.warning(json.dumps(res))
                return HttpResponse(json.dumps(res),
                                    type="json/application",
                                    status=200)

        except ObjectDoesNotExist:
            logger.error("Error: can not find user -- {}".format(
                str(username)))
        except Exception as e:
            logger.error("Error: GPI GetNewBetDetailAPI error -- {}".format(
                repr(e)))
            return HttpResponse(repr(e))
コード例 #6
0
def getBetDetail(request):
    if request.method == 'POST':
        try:
            PROVIDER = GameProvider.objects.get(provider_name=ONEBOOK_PROVIDER)
        except ObjectDoesNotExist:
            logger.error("ONEBOOK::PROVIDER AND/OR CATEGORY RELATIONS DO NOT EXIST.")
            raise Exception("PROVIDER AND/OR CATEGORY RELATIONS DO NOT EXIST.")
            
        headers =  {'Content-Type': 'application/x-www-form-urlencoded'}
        delay = 2
        success = False
        version_key = PROVIDER.notes
        
        onebook_run = "run"
        try:
            r = RedisClient().connect()
            redis = RedisHelper()
        except:
            logger.error("ONEBOOK::There is something wrong with redis connection.")
            return Response({'status': 'There is something wrong with redis connection.'}, status=status.HTTP_400_BAD_REQUEST)

        #print(redis.check_onebook_bet_details(onebook_run))
        if redis.check_onebook_bet_details(onebook_run) is False: #if the key does not exist in redis
            redis.set_onebook_bet_details(onebook_run)  #insert the key to redis
            while(redis.check_onebook_bet_details(onebook_run)): #loop while the key is existed in redis
                try:
                    r = requests.post(ONEBOOK_API_URL + "GetBetDetail/", headers=headers, data={
                        "vendor_id": ONEBOOK_VENDORID,
                        "version_key": version_key,
                    })
                    rdata = r.json()
                except requests.RequestException:
                    logger.error("Connectivity error for onebook GetBetDetail API.")
                    redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis when break the while loop
                    return Response({'error': 'Connectivity error for onebook GetBetDetail API.'})
                except ValueError:
                    logger.error("JSON parsing error for onebook GetBetDetail API.")
                    redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis when break the while loop
                    return Response({'error': 'JSON parsing error for onebook GetBetDetail API.'})
                except (IndexError, KeyError):
                    logger.error("JSON format error for onebook GetBetDetail API.")
                    redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis when break the while loop
                    return Response({'error': 'JSON format error for onebook GetBetDetail API.'})
                if r.status_code == 200:
                    
                    version_key = rdata["Data"]["last_version_key"]        
                    
                    updates = GameProvider.objects.get(provider_name=ONEBOOK_PROVIDER)
                    
                    updates.notes = version_key
                    updates.save()
                        
                    if  "BetDetails" in rdata['Data']:
                        gamebets_list = []
                        # logger.info(rdata["Data"]["BetDetails"])
                        for i in range(len(rdata["Data"]["BetDetails"])):
                            
                            username = str(rdata["Data"]["BetDetails"][i]["vendor_member_id"]).split('_')[0]
                            # print(username)
                            
                            cate = Category.objects.get(name='Sports')
                            
                            trans_id = rdata["Data"]["BetDetails"][i]["trans_id"]

                            try:
                                user = CustomUser.objects.get(username=username)
                                
                            except ObjectDoesNotExist:     
                                redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis
                                logger.error("ONEBOOK::The user does not exist for get bet detail.")
                                return Response({'error': 'The user does not exist for get bet detail..'})

                            transid = user.username + "-" + timezone.datetime.today().isoformat() + "-" + str(random.randint(0, 10000000))
                            outcome = rdata["Data"]["BetDetails"][i]["ticket_status"]
                            
                            home_id = rdata["Data"]["BetDetails"][i]["home_id"]
                            away_id = rdata["Data"]["BetDetails"][i]["away_id"]

                            if rdata["Data"]["BetDetails"][i]["settlement_time"] == '':
                                    if not GameBet.objects.filter(ref_no=trans_id).exists():
                                    
                                        gamebet = GameBet(provider=PROVIDER,
                                                            category=cate,
                                                            user=user,
                                                            user_name=user.username,
                                                            game_name=str(home_id) + '/' + str(away_id),
                                                            transaction_id=transid,
                                                            odds=rdata["Data"]["BetDetails"][i]["odds"],
                                                            amount_wagered=rdata["Data"]["BetDetails"][i]["stake"],
                                                            currency=convertCurrency[rdata["Data"]["BetDetails"][i]["currency"]],
                                                            bet_type=rdata["Data"]["BetDetails"][i]["bet_type"],
                                                            ref_no=trans_id,
                                                            market=ibetCN,
                                                            other_data={
                                                                "version_key": version_key
                                                            }
                                                        )
                                        gamebets_list.append(gamebet)
                                
                            else:
                                if (outcome == "won" or outcome == "half won" or outcome == "lose" or outcome == "half lose" or outcome == "draw" or outcome == "reject" or  outcome == "refund" or outcome == "void"):
                                    
                                    resolve = datetime.datetime.strptime(rdata["Data"]["BetDetails"][i]["settlement_time"], '%Y-%m-%dT%H:%M:%S.%f')
                                       
                                    gamebet = GameBet(provider=PROVIDER,
                                                        category=cate,
                                                        transaction_id=transid,
                                                        user=user,
                                                        user_name=user.username,
                                                        game_name=str(home_id) + '/' + str(away_id),
                                                        odds=rdata["Data"]["BetDetails"][i]["odds"],
                                                        amount_wagered=rdata["Data"]["BetDetails"][i]["stake"],
                                                        currency=convertCurrency[rdata["Data"]["BetDetails"][i]["currency"]],
                                                        bet_type=rdata["Data"]["BetDetails"][i]["bet_type"],
                                                        amount_won=rdata["Data"]["BetDetails"][i]["winlost_amount"],
                                                        outcome=outcomeConversion[outcome],
                                                        resolved_time=utcToLocalDatetime(resolve),
                                                        ref_no=trans_id,
                                                        market=ibetCN,
                                                        other_data={
                                                                "version_key": version_key
                                                            }
                                                    )
                                    gamebets_list.append(gamebet)
                        GameBet.objects.bulk_create(gamebets_list)


                        # sleep(delay)  
                        # print("sleep")  
                    else:
                        redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis
                        break
                else:
                    redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis when break the while loop
                    logger.critical("There was something wrong with the onebook getBetDetail result")
                    return Response({'status': 'There was something wrong with the onebook getBetDetail result'}, status=status.HTTP_400_BAD_REQUEST)
            redis.remove_onebook_bet_details(onebook_run)  #remove the key from redis
            #print(redis.check_onebook_bet_details(onebook_run))        
            return Response({'status': 'success'}, status=status.HTTP_200_OK)
        else:
            logger.info("skip running this time for onebook getBetDetail.")
            return Response({'status': 'skip running this time onebook getBetDetail.'}, status=status.HTTP_200_OK)
コード例 #7
0
def agftp(request):

    try:
        ftp = ftpClient.AgFTP()

        try:
            r = RedisClient().connect()
            redis = RedisHelper()
        except:
            logger.warning(
                "There is something wrong with AG redis connection.")
            return HttpResponse({
                'status':
                'There is something wrong with AG redis connection.'
            })

        try:
            folders = ftp.ftp_session.nlst()

        except ftplib.error_perm as resp:
            if str(resp) == "550 No files found":
                logger.warning("No files in this directory of AG folders")
                return HttpResponse(ERROR_CODE_NOT_FOUND)
            else:
                raise
        for folder in folders:
            ftp.ftp_session.cwd(folder)

            small_folders = ftp.ftp_session.nlst()
            last_added_file = redis.get_ag_added_file(
            )  #get last added file in redis

            if last_added_file is not None:
                last_added_file = last_added_file.decode("utf-8")
                last_added_file = last_added_file.replace(".xml", "")
                last_folder = last_added_file[:8]  #get last added folder
            else:
                last_added_file = 0
                last_folder = 0
            for sf in small_folders:
                if int(sf.replace("/", "")) >= int(
                        last_folder
                ):  #only edit the folders more recently then the one we get
                    ftp.ftp_session.cwd(sf)
                    try:
                        files = ftp.ftp_session.nlst()
                    except ftplib.error_perm as resp:
                        if str(resp) == "550 No files found":
                            logger.warning(
                                "No files in this directory of AG small folders"
                            )
                            return HttpResponse(ERROR_CODE_NOT_FOUND)
                        else:
                            raise

                    # latest_time = None
                    # latest_name = None
                    for file in files:

                        last_file = file.replace(".xml", "")

                        if int(last_file) > int(last_added_file):

                            # if redis.check_ag_added_file(file) is False:   #if the file does not exist in redis
                            #     redis.set_ag_added_file(file)              #then add the file into redis
                            # else:
                            #     continue                                   #if it is already existed then go to next index

                            # time = ftp.ftp_session.voidcmd("MDTM " + file)
                            # if (latest_time is None) or (time > latest_time):
                            #     latest_name = file
                            #     latest_time = time

                            r = BytesIO()
                            read = ftp.ftp_session.retrbinary(
                                'RETR ' + file, r.write)
                            rdata = r.getvalue().decode("utf-8")
                            xml = '<root>' + rdata + '</root>'

                            writeToS3(rdata, AWS_BET_S3_BUCKET,
                                      'AG-game-history/{}'.format(file))

                            logger.info(
                                'finished writting AG last file {} to s3'.
                                format(file))
                            root = ET.fromstring(xml)
                            gamebets_list = []
                            for child in root:
                                dataType = child.attrib['dataType']

                                if dataType == 'HSR':  #捕鱼王場景的下注记录
                                    ID = child.attrib['ID']
                                    playerName = child.attrib['playerName']
                                    tradeNo = child.attrib['tradeNo']
                                    transferAmount = child.attrib[
                                        'transferAmount']
                                    flag = child.attrib['flag']
                                    SceneStartTime = child.attrib[
                                        'SceneStartTime']
                                    SceneEndTime = child.attrib['SceneEndTime']
                                    Jackpotcomm = child.attrib['Jackpotcomm']
                                    gameType = child.attrib['gameType']
                                    sceneId = child.attrib['sceneId']
                                    agtype = child.attrib['type']
                                    Roomid = child.attrib['Roomid']
                                    Roombet = child.attrib['Roombet']
                                    Cost = child.attrib['Cost']
                                    Earn = child.attrib['Earn']
                                    exchangeRate = child.attrib['exchangeRate']
                                    creationTime = child.attrib['creationTime']
                                    deviceType = child.attrib['deviceType']
                                    gameCode = child.attrib['gameCode']

                                    try:
                                        user = CustomUser.objects.get(
                                            username=playerName)

                                    except ObjectDoesNotExist:
                                        logger.error(
                                            "This user does not exist in AG ftp."
                                        )
                                        return HttpResponse(
                                            ERROR_CODE_INVALID_INFO)

                                    trans_id = user.username + "-" + timezone.datetime.today(
                                    ).isoformat() + "-" + str(
                                        random.randint(0, 10000000))

                                    SceneStartTime = datetime.datetime.strptime(
                                        SceneStartTime, '%Y-%m-%d %H:%M:%S')
                                    SceneStartTime = SceneStartTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    SceneEndTime = datetime.datetime.strptime(
                                        SceneEndTime, '%Y-%m-%d %H:%M:%S')
                                    SceneEndTime = SceneEndTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    if float(transferAmount) > 0:
                                        outcome = 0
                                    elif float(transferAmount) == 0:
                                        outcome = 2
                                    else:
                                        outcome = 1

                                    gamebet = GameBet(
                                        provider=GameProvider.objects.get(
                                            provider_name=AG_PROVIDER),
                                        category=Category.objects.get(
                                            name='Live Casino'),
                                        user=user,
                                        user_name=user.username,
                                        transaction_id=trans_id,
                                        amount_wagered=abs(
                                            decimal.Decimal(transferAmount)),
                                        currency=user.currency,
                                        ref_no=tradeNo,
                                        amount_won=decimal.Decimal(
                                            transferAmount),
                                        market=ibetCN,
                                        bet_time=SceneStartTime,
                                        resolved_time=SceneEndTime,
                                        outcome=outcome,
                                        other_data={
                                            "ID": ID,
                                            "sceneId": sceneId,
                                            "type": agtype,
                                            "Roomid": Roomid,
                                            "Roombet": Roombet,
                                            "Cost": Cost,
                                            "Earn": Earn,
                                            "exchangeRate": exchangeRate,
                                            "creationTime": creationTime,
                                            "deviceType": deviceType,
                                            "gameCode": gameCode,
                                            "flag": flag
                                        })
                                    gamebets_list.append(gamebet)

                                elif dataType == 'BR':  #下注记录详情
                                    playerName = child.attrib['playerName']
                                    billNo = child.attrib['billNo']
                                    betAmount = child.attrib['betAmount']
                                    flag = child.attrib['flag']
                                    betTime = child.attrib['betTime']
                                    gameCode = child.attrib['gameCode']
                                    netAmount = child.attrib['netAmount']
                                    gameType = child.attrib['gameType']
                                    result = child.attrib['result']
                                    agentCode = child.attrib['agentCode']
                                    playType = child.attrib['playType']
                                    tableCode = child.attrib['tableCode']
                                    recalcuTime = child.attrib['recalcuTime']
                                    platformType = child.attrib['platformType']
                                    aground = child.attrib['round']
                                    beforeCredit = child.attrib['beforeCredit']
                                    deviceType = child.attrib['deviceType']

                                    try:
                                        user = CustomUser.objects.get(
                                            username=playerName)

                                    except ObjectDoesNotExist:
                                        logger.error(
                                            "This user does not exist in AG ftp."
                                        )
                                        return HttpResponse(
                                            ERROR_CODE_INVALID_INFO)

                                    if float(netAmount) > float(betAmount):
                                        outcome = 0
                                    elif float(netAmount) == float(betAmount):
                                        outcome = 2
                                    else:
                                        outcome = 1

                                    betTime = datetime.datetime.strptime(
                                        betTime, '%Y-%m-%d %H:%M:%S')
                                    betTime = betTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    recalcuTime = datetime.datetime.strptime(
                                        recalcuTime, '%Y-%m-%d %H:%M:%S')
                                    recalcuTime = recalcuTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    trans_id = user.username + "-" + timezone.datetime.today(
                                    ).isoformat() + "-" + str(
                                        random.randint(0, 10000000))

                                    gamebet = GameBet(
                                        provider=GameProvider.objects.get(
                                            provider_name=AG_PROVIDER),
                                        category=Category.objects.get(
                                            name='Live Casino'),
                                        user=user,
                                        user_name=user.username,
                                        transaction_id=trans_id,
                                        amount_wagered=decimal.Decimal(
                                            betAmount),
                                        currency=user.currency,
                                        amount_won=decimal.Decimal(netAmount),
                                        ref_no=billNo,
                                        market=ibetCN,
                                        bet_time=betTime,
                                        resolved_time=recalcuTime,
                                        outcome=outcome,
                                        other_data={
                                            "agentCode":
                                            agentCode,
                                            "gameCode":
                                            gameCode,
                                            "betTime":
                                            child.attrib['betTime'],
                                            "recalcuTime":
                                            child.attrib['recalcuTime'],
                                            "gameType":
                                            gameType,
                                            "flag":
                                            flag,
                                            "playType":
                                            playType,
                                            "tableCode":
                                            tableCode,
                                            "platformType":
                                            platformType,
                                            "round":
                                            aground,
                                            "beforeCredit":
                                            beforeCredit,
                                            "deviceType":
                                            deviceType
                                        })
                                    gamebets_list.append(gamebet)

                                elif dataType == 'EBR':  #电子游戏的下注记录
                                    playerName = child.attrib['playerName']
                                    billNo = child.attrib['billNo']
                                    betAmount = child.attrib['betAmount']
                                    flag = child.attrib['flag']
                                    betTime = child.attrib['betTime']
                                    gameCode = child.attrib['gameCode']
                                    netAmount = child.attrib['netAmount']
                                    gameType = child.attrib['gameType']
                                    result = child.attrib['result']
                                    slottype = child.attrib['slottype']
                                    mainbillno = child.attrib['mainbillno']
                                    subbillno = child.attrib['subbillno']
                                    gameCategory = child.attrib['gameCategory']
                                    netAmountBonus = child.attrib[
                                        'netAmountBonus']
                                    netAmountBase = child.attrib[
                                        'netAmountBase']
                                    betAmountBonus = child.attrib[
                                        'betAmountBonus']
                                    betAmountBase = child.attrib[
                                        'betAmountBase']
                                    recalcuTime = child.attrib['recalcuTime']

                                    try:
                                        user = CustomUser.objects.get(
                                            username=playerName)

                                    except ObjectDoesNotExist:
                                        logger.error(
                                            "This user does not exist in AG ftp."
                                        )
                                        return HttpResponse(
                                            ERROR_CODE_INVALID_INFO)

                                    trans_id = user.username + "-" + timezone.datetime.today(
                                    ).isoformat() + "-" + str(
                                        random.randint(0, 10000000))

                                    if float(netAmount) > float(betAmount):
                                        outcome = 0
                                    elif float(netAmount) == float(betAmount):
                                        outcome = 2
                                    else:
                                        outcome = 1

                                    betTime = datetime.datetime.strptime(
                                        betTime, '%Y-%m-%d %H:%M:%S')
                                    betTime = betTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    recalcuTime = datetime.datetime.strptime(
                                        recalcuTime, '%Y-%m-%d %H:%M:%S')
                                    recalcuTime = recalcuTime.astimezone(
                                        pytz.timezone(
                                            GameProvider.objects.get(
                                                provider_name=AG_PROVIDER).
                                            timezone))

                                    gamebet = GameBet(
                                        provider=GameProvider.objects.get(
                                            provider_name=AG_PROVIDER),
                                        category=Category.objects.get(
                                            name='Live Casino'),
                                        user=user,
                                        user_name=user.username,
                                        transaction_id=trans_id,
                                        amount_wagered=decimal.Decimal(
                                            betAmount),
                                        currency=user.currency,
                                        amount_won=decimal.Decimal(netAmount),
                                        ref_no=billNo,
                                        market=ibetCN,
                                        outcome=outcome,
                                        bet_time=betTime,
                                        resolved_time=recalcuTime,
                                        other_data={
                                            "gameType": gameType,
                                            "result": result,
                                            "slottype": slottype,
                                            "mainbillno": mainbillno,
                                            "subbillno": subbillno,
                                            "gameCategory": gameCategory,
                                            "netAmountBonus": netAmountBonus,
                                            "netAmountBase": netAmountBase,
                                            "betAmountBonus": betAmountBonus,
                                            "betAmountBase": betAmountBase
                                        })
                                    gamebets_list.append(gamebet)
                                elif dataType == 'TR':  #TIP
                                    agentCode = child.attrib['agentCode']
                                    ID = child.attrib['ID']
                                    transferId = child.attrib['transferId']
                                    tradeNo = child.attrib['tradeNo']
                                    platformType = child.attrib['platformType']
                                    playerName = child.attrib['playerName']
                                    transferType = child.attrib['transferType']
                                    transferAmount = child.attrib[
                                        'transferAmount']
                                    previousAmount = child.attrib[
                                        'previousAmount']
                                    currentAmount = child.attrib[
                                        'currentAmount']
                                    currency = child.attrib['currency']
                                    exchangeRate = child.attrib['exchangeRate']
                                    IP = child.attrib['IP']
                                    flag = child.attrib['flag']
                                    creationTime = child.attrib['creationTime']
                                    gameCode = child.attrib['gameCode']
                                    if transferType == 'DONATEFEE':  #tip

                                        try:
                                            user = CustomUser.objects.get(
                                                username=playerName)

                                        except ObjectDoesNotExist:
                                            logger.error(
                                                "This user does not exist in AG ftp."
                                            )
                                            return HttpResponse(
                                                ERROR_CODE_INVALID_INFO)

                                        trans_id = user.username + "-" + timezone.datetime.today(
                                        ).isoformat() + "-" + str(
                                            random.randint(0, 10000000))

                                        creationTime = datetime.datetime.strptime(
                                            creationTime, '%Y-%m-%d %H:%M:%S')
                                        creationTime = creationTime.astimezone(
                                            pytz.timezone(
                                                GameProvider.objects.get(
                                                    provider_name=AG_PROVIDER).
                                                timezone))

                                        gamebet = GameBet(
                                            provider=GameProvider.objects.get(
                                                provider_name=AG_PROVIDER),
                                            category=Category.objects.get(
                                                name='Live Casino'),
                                            user=user,
                                            user_name=user.username,
                                            transaction_id=trans_id,
                                            amount_wagered=abs(
                                                decimal.Decimal(
                                                    transferAmount)),
                                            currency=user.currency,
                                            ref_no=tradeNo,
                                            market=ibetCN,
                                            bet_type=TIP,
                                            bet_time=creationTime,
                                            other_data={
                                                "agentCode": agentCode,
                                                "gameCode": gameCode,
                                                "flag": flag,
                                                "IP": IP,
                                                "ID": ID,
                                                "transferId": transferId,
                                                "exchangeRate": exchangeRate,
                                                "currentAmount": currentAmount,
                                                "previousAmount":
                                                previousAmount,
                                                "transferAmount":
                                                transferAmount,
                                                "platformType": platformType
                                            })
                                        gamebets_list.append(gamebet)
                            GameBet.objects.bulk_create(gamebets_list)
                            redis.set_ag_added_file(file)
                        else:
                            logger.info("AG:: No new file from ftp.")
                            continue

                    ftp.ftp_session.cwd('..')

                else:
                    logger.info("AG:: No new folder from ftp.")
                    continue

            ftp.ftp_session.cwd('..')
        return HttpResponse(CODE_SUCCESS, status=status.HTTP_200_OK)
    except ftplib.error_temp:
        logger.warning("Cannot connect with AG ftp.")
        return HttpResponse(ERROR_CODE_FAIL)

    except Exception as e:
        logger.critical(
            "(FATAL_ERROR) Getting AG bet histroy error. {}".format(str(e)))
        return HttpResponse(ERROR_CODE_FAIL)
コード例 #8
0
    def post(self, request, *args, **kwargs):
        headers = {
            'Pragma': '',
            'Keep-Alive': 'timeout=5, max=100',
            'X_ENTITY_KEY': ENTITY_KEY
        }
        try:
            # with tempfile.NamedTemporaryFile(delete=False) as temp:
            pt_key = tempfile.NamedTemporaryFile(delete=False)
            pt_key.write(PTKEY)
            pt_key.flush()  # ensure all data written
            # to get the path/file
            pt_pem = tempfile.NamedTemporaryFile(delete=False)
            pt_pem.write(PTPEM)
            pt_pem.flush()

            # get time
            r = RedisClient().connect()
            redis = RedisHelper()

            ts_from_redis = redis.get_pt_starttime()
            if ts_from_redis is None:

                startdate = getGMTtime()
                end_time = datetime.strptime(
                    startdate, '%Y-%m-%d%%20%H:%M:%S') + timedelta(minutes=5)
                enddate = end_time.strftime('%Y-%m-%d%%20%H:%M:%S')

            else:
                start_time = datetime.strptime(ts_from_redis.decode(),
                                               '%Y-%m-%d%%20%H:%M:%S')
                startdate = start_time.strftime('%Y-%m-%d%%20%H:%M:%S')
                end_time = start_time + timedelta(minutes=5)
                enddate = end_time.strftime('%Y-%m-%d%%20%H:%M:%S')

            rr = requests.post(PT_BASE_URL + "/game/flow/startdate/" +
                               startdate + "/enddate/" + enddate,
                               headers=headers,
                               cert=(pt_pem.name, pt_key.name))

            if rr.status_code == 200:
                rrdata = rr.json()

                try:
                    records = rrdata['result']
                    provider = GameProvider.objects.get(
                        provider_name=PT_PROVIDER)
                    category = Category.objects.get(name='Games')
                    for record in records:
                        # get user here
                        try:
                            playername = record['PLAYERNAME']
                            user = CustomUser.objects.get(
                                username__iexact=playername.split('_')[1])

                            trans_id = user.username + "-" + timezone.datetime.today(
                            ).isoformat() + "-" + str(
                                random.randint(0, 10000000))
                            if (float(record['BET']) - float(record['WIN']) >
                                    0):
                                outcome = 1  # lose
                            elif (float(record['BET']) -
                                  float(record['WIN']) == 0):
                                outcome = 2  # Tie/Push
                            else:
                                outcome = 0  # win

                            resolve = datetime.strptime(
                                record['GAMEDATE'], '%Y-%m-%d %H:%M:%S')
                            resolve = resolve.replace(
                                tzinfo=pytz.timezone(provider.timezone))
                            resolve = resolve.astimezone(pytz.utc)

                            GameBet.objects.create(
                                provider=provider,
                                category=category,
                                user=user,
                                user_name=user.username,
                                amount_wagered=decimal.Decimal(record['BET']),
                                amount_won=decimal.Decimal(record['WIN']),
                                outcome=outcome,
                                transaction_id=trans_id,
                                market=ibetCN,
                                ref_no=record['GAMEID'],
                                resolved_time=resolve,
                                other_data={})

                        except Exception as e:
                            logger.error(
                                "PT cannot get customuser in get record.")
                            return HttpResponse(
                                "PT cannot get customuser in bet records.",
                                status=200)

                    redis.set_pt_starttime(enddate)
                    return HttpResponse(
                        "PT get {} bet records successfully".format(
                            len(records)),
                        status=200)
                except Exception as e:
                    logger.error("PT cannot get bet records")
                    return HttpResponse("PT cannot get bet records.",
                                        status=200)

            else:
                logger.critical("FATAL__ERROR: PT get bet record status code.")
                return HttpResponse("PT get bad bet record status code.",
                                    status=200)
        except Exception as e:
            logger.error("PT APi key error or Redis connect error.{}".format(
                str(e)))
            return HttpResponse("PT APi key error or Redis connect error.",
                                status=200)
        finally:
            # delete the file.
            os.unlink(pt_key.name)
            os.unlink(pt_pem.name)
コード例 #9
0
    def post(self, request, *args, **kwargs):
        logger.info("connecting ea ftp")
        try:
            ftp_connection = ftpClient.EaFTP()
        except Exception as e:
            logger.warning(
                "There is something wrong with ftp connection. {}".format(
                    str(e)))
            return HttpResponse(json.dumps({
                'status':
                'There is something wrong with ftp connection.' + str(e)
            }),
                                content_type='application/json')

        try:
            r = RedisClient().connect()
            redis = RedisHelper()
            logger.info("connecting redis")
        except Exception as e:
            logger.critical(
                "There is something wrong with redis connection. {}".format(
                    str(e)))
            return HttpResponse(json.dumps({
                'status':
                'There is something wrong with redis connection.' + str(e)
            }),
                                content_type='application/json')

        gamebets_list = []
        file_list = []

        try:
            ftp_connection.ftp_session.retrlines('RETR gameinfolist.txt',
                                                 file_list.append)
        except:
            logger.warning("Getting gameinfolist.txt fail. {}".format(str(e)))
            return HttpResponse(json.dumps(
                {'status': 'Getting gameinfolist.txt fail: ' + str(e)}),
                                content_type='application/json')

        if len(file_list) <= 0:
            logger.warning("There is no new data in gameinfolist.txt")
            return HttpResponse(json.dumps(
                {'status': 'There is no new data in gameinfolist.txt'}),
                                content_type='application/json')

        try:
            last_file = redis.get_ea_last_file()
            processed = True
            if last_file is None:
                processed = False
            else:
                last_file = last_file.decode("utf-8")

            for f in file_list:

                local_file_name = f.split('/')[-1]
                if last_file == local_file_name:
                    processed = False
                if last_file == local_file_name or processed:
                    continue

                logger.info('writing file to local: ' + local_file_name)
                localFile = open(local_file_name, 'wb')
                ftp_connection.ftp_session.retrbinary('RETR ' + f,
                                                      localFile.write)
                localFile.close()

                s3client = boto3.client("s3")
                try:
                    s3client.upload_file(
                        local_file_name, AWS_BET_S3_BUCKET,
                        'EA-game-history/{}'.format(local_file_name))
                except Exception as e:
                    logger.warning("Uploading to S3 error", e)

                logger.info('Uploading to S3 to bucket ' + AWS_BET_S3_BUCKET +
                            ' with file name ' + local_file_name)

                list_history = []
                with open(local_file_name, 'r') as f:
                    data = xmltodict.parse(f.read())
                    # print(data)
                    if 'gameinfo' in data and data[
                            'gameinfo'] and 'game' in data['gameinfo']:
                        all_game_types = data['gameinfo']
                        #multiple type of games playing in this time range
                        if isinstance(all_game_types['game'], list):
                            for each_game_type in all_game_types['game']:
                                # print(each_game_type)
                                game_code = each_game_type['@code']
                                bet_detail = each_game_type['deal']

                                # print(bet_detail)
                                list_history = gameHistoryToDatabase(
                                    bet_detail, game_code)
                        # only one type of game playing in this time range
                        else:
                            game_code = all_game_types['game']['@code']
                            bet_detail = all_game_types['game']['deal']
                            # if isinstance(bet_detail, list):
                            #     bet_detail = each_game_type['deal']
                            # else:
                            # print(bet_detail)
                            list_history = gameHistoryToDatabase(
                                bet_detail, game_code)
                        gamebets_list = gamebets_list + list_history
                        logger.info('store EA bet history to database')
                    else:
                        logger.info(
                            'There is no bet history between this time range')

                os.remove(local_file_name)

            GameBet.objects.bulk_create(gamebets_list)

            last_file = file_list[-1]
            last_file_name = last_file.split('/')[-1]
            redis.set_ea_last_file(last_file_name)
            logger.info(
                'finished writting last file {} to redis'.format(last_file))
            logger.info(
                'finished processing the ea bet history and end file is {}'.
                format(last_file))
            response = {
                "success":
                True,
                "message":
                'finished processing the ea bet history and end file is {}'.
                format(last_file)
            }
            return HttpResponse(json.dumps(response),
                                content_type="application/json")
        except Exception as e:
            logger.critical(
                "There is something wrong with get ea bet detail {}".format(
                    str(e)))
            return HttpResponse(json.dumps({
                'status':
                'There is something wrong with get ea bet detail.' + str(e)
            }),
                                content_type='application/json')