public void AddRedBaoItem(RedBaoItem item)
 {
     if (!_grid.IsDisposed && !_grid.Disposing)
     {
         _grid.BeginInvoke(new MethodInvoker(() =>
         {
             Items.Insert(0, item);
             _grid.FirstDisplayedScrollingRowIndex = 0;
         }));
     }
 }
 public void ProcessRedBaoItem(RedBaoItem item, int goldAmount, string status, string senderName)
 {
     if (!_grid.IsDisposed && !_grid.Disposing)
     {
         _grid.BeginInvoke(new MethodInvoker(() =>
         {
             if (_grid.IsDisposed || _grid.Disposing)
             {
                 return;
             }
             item.SenderName       = senderName;
             item.IsProcessed      = true;
             item.Status           = status;
             item.FetchedGoldCount = goldAmount;
             _grid.FirstDisplayedScrollingRowIndex = 0;
         }));
     }
 }
Пример #3
0
        public void RunRedBao()
        {
            while (true)
            {
                //myCtrl.UpdateRoomCount(_userIds.Count.ToString());
                RountCount++;
                if (!Started)
                {
                    Thread.Sleep(2000);
                    continue;
                }
                if (_userIds.Count != 0 && _tokens.Count > 0)
                {
                    if (!Logined)
                    {
                        Logined = true;
                        myCtrl.ChangeLoginStatus(Logined);
                    }

                    int totalCount   = _userIds.Count;
                    int currentCount = 0;

                    _userIds.ToList().ForEach((userid =>
                    {
                        currentCount++;
                        //                        _tokens.ToList().ForEach(_token =>
                        //                        {

                        if (_waittingForProcessingItems.ToList().Any(item => item.UserId == userid))
                        {
                            return;
                        }


                        string tempLiveId = string.Empty;
                        if (!_UserIdToLiveId.ContainsKey(userid))
                        {
                            return;
                        }
                        string getResult =
                            GetRequest(
                                $@"https://redenvelope.lizhi.fm/redenvelope/getCandidateLiveRedEnvelop?receiverLiveUserId={
                                        userid
                                    }&token={_tokens[0]}&liveId={_UserIdToLiveId[userid]}&deviceId={_TokenToDeviceId[_tokens[0]]}&accessToken={_TokenToAccessToken[_tokens[0]]}&r={new Random().NextDouble()}");
                        //}&token={_tokens[0]}&liveId={_UserIdToLiveId[userid]}&deviceId={_TokenToDeviceId[_tokens[0]]}&r={new Random().NextDouble()}");

                        var redbaoItem = new RedBaoItem();

                        myCtrl.SetProgressBar(currentCount * 100 / totalCount);

                        if (!string.IsNullOrEmpty(getResult) && !getResult.Contains("无红包可抢") &&
                            getResult.Contains(@"""id"":"))
                        {
                            string roomInfoResponse =
                                GetRequest(
                                    $@"http://www.lizhi.fm/user/{userid}");
                            Regex roomUserPhotoAndRoomNameRegex =
                                new Regex(
                                    @"<div class=""user-info-img"">[^<]+<img src=""([^""]+)"" alt=""([^""]+)"" />");
                            Regex roomPicRegex =
                                new Regex(
                                    @"data-cover=""([^""]+)""");
                            //@" < div class=""radioCover left"">[^<]+<img alt=""[^""]+"" src=""([^""]+)"" />");
                            Regex roomFMAndRoomTitleRegex =
                                new Regex("<h1 class=\"user-info-name\">([^ ]+)([^<]+)<i class=");
                            //new Regex(@"div class=""left""><a href=""/"">发现</a><a href=""/([^""]+)/"">([^<]+)</a>");
                            string roomFmId = "未获取";
                            string roomFmName = "未获取";
                            string roomOwner = "未获取";
                            if (!string.IsNullOrEmpty(roomInfoResponse))
                            {
                                var roomUserPhotoAndRoomName = roomUserPhotoAndRoomNameRegex.Match(roomInfoResponse);
                                if (roomUserPhotoAndRoomName.Success)
                                {
                                    //房主图片
                                    redbaoItem.RoomOwnerPic = (roomUserPhotoAndRoomName.Groups[1].Value);
                                    myCtrl.SetRoomOwner(roomUserPhotoAndRoomName.Groups[1].Value);
                                    //房主名字
                                    roomOwner = (roomUserPhotoAndRoomName.Groups[2].Value);
                                }
                                var roomPic = roomPicRegex.Match(roomInfoResponse);
                                if (roomPic.Success)
                                {
                                    //房间图片
                                    redbaoItem.RoomPic = (roomPic.Groups[1].Value);
                                    myCtrl.SetRoomConver(roomPic.Groups[1].Value);
                                }
                                var roomFMAndRoomTitle = roomFMAndRoomTitleRegex.Match(roomInfoResponse);
                                if (roomFMAndRoomTitle.Success)
                                {
                                    //FM ID
                                    roomFmId = roomFMAndRoomTitle.Groups[1].Value;
                                    //房间名字
                                    roomFmName = roomFMAndRoomTitle.Groups[2].Value;
                                }
                            }

                            Regex regex = new Regex(@"\{""id"":""([0-9]+)""");
                            string resultID = regex.Match(getResult).Groups[1].Value;
                            redbaoItem.FieldResultID = resultID;

                            if (!string.IsNullOrEmpty(resultID))
                            {
                                if (!_userIds.Contains(resultID))
                                {
                                    _userIds.Add(resultID);
                                    myCtrl.AddMessage($@"Added new userID: {resultID}");
                                }
                            }

                            var senderIdMatch = new Regex(@"""senderId"":""([^""]+)""").Match(getResult);
                            if (senderIdMatch.Success)
                            {
                                redbaoItem.RoomFmNumber = roomFmId;
                                redbaoItem.RoomName = roomFmName;
                                redbaoItem.RoomOwnerName = roomOwner;
                                redbaoItem.UserId = userid;
                                redbaoItem.RoomId = userid;
                                redbaoItem.SenderId = senderIdMatch.Groups[1].Value;
                                redbaoItem.Status = "等待抢...";
                                redbaoItem.IsProcessed = false;
                                redbaoItem.Id = AutoIncreaseId++;
                                //myCtrl.AddMessage($"^^^红包,房间FM:{roomFmId} 房间名:{roomFmName} 房主:{roomOwner}");
                                //myCtrl.AddMessage($"^^^红包,发送者ID:{senderIdMatch.Groups[1].Value}, 房间ID:{userid}.^^^");
                            }


                            var senderPhotoMatch = new Regex(@"""senderCover"":""([^""]+)""").Match(getResult);
                            if (senderPhotoMatch.Success)
                            {
                                //发送者图片
                                redbaoItem.SenderPic = (senderPhotoMatch.Groups[1].Value);
                                myCtrl.SetImage(senderPhotoMatch.Groups[1].Value);
                            }

                            redbaoItem.DelayDateTime = DateTime.Now.AddMilliseconds(myCtrl.GetDelayFetchGoldMillSecs());

                            lock (_waittingForProcessingItems)
                                _waittingForProcessingItems.Add(redbaoItem);
                            myCtrl.AddRedBaoItem(redbaoItem);
                        }

                        //                        });
                    }));
                }
                else
                {
                    if (Logined)
                    {
                        Logined = false;
                        myCtrl.ChangeLoginStatus(Logined);
                    }
                }
                Thread.Sleep(DelayTime);
            }
        }
Пример #4
0
        public void RunRedBaoEmptyRoom()
        {
            while (true)
            {
                myCtrl.UpdateRoomCount((_userIds.Count + _userEmptyRoomIds.Count).ToString());
                RountCount++;
                if (!Started)
                {
                    Thread.Sleep(2000);
                    continue;
                }

                //获得热门用户

                List <UserId> hotList = new List <UserId>();
                if (myCtrl.IsLoadHotLiveRooms)
                {
                    GetHotUsersList(hotList);
                }


                if (_userEmptyRoomIds.Count != 0 && _tokens.Count > 0)
                {
                    if (!Logined)
                    {
                        Logined = true;
                        myCtrl.ChangeLoginStatus(Logined);
                    }

                    int totalCount   = _userEmptyRoomIds.Count;
                    int currentCount = 0;

                    var pendingAddUsers = new List <string>();

                    _userEmptyRoomIds.ToList().ForEach((userid =>
                    {
                        currentCount++;
                        //                        _tokens.ToList().ForEach(_token =>
                        //                        {

                        if (_waittingForProcessingItems.ToList().Any(item => item.UserId == userid))
                        {
                            return;
                        }
                        if (!_UserIdToLiveId.ContainsKey(userid) || !_TokenToDeviceId.ContainsKey(_tokens[0]))
                        {
                            return;
                        }
                        string getResult =
                            GetRequest(
                                $@"https://redenvelope.lizhi.fm/redenvelope/getCandidateLiveRedEnvelop?receiverLiveUserId={
                                        userid
                                    }&token={_tokens[0]}&liveId={_UserIdToLiveId[userid]}&deviceId={_TokenToDeviceId[_tokens[0]]}&r={new Random().NextDouble()}");
                        //      }&token={_tokens[0]}&liveId={_UserIdToLiveId[userid]}&deviceId={ _TokenToDeviceId[_tokens[0]]}&accessToken={_TokenToAccessToken[_tokens[0]]}&r={new Random().NextDouble()}");

                        var redbaoItem = new RedBaoItem();

                        myCtrl.SetProgressBarSecond(currentCount * 100 / totalCount);

                        if (!string.IsNullOrEmpty(getResult) && !getResult.Contains("无红包可抢") &&
                            getResult.Contains(@"""id"":"))
                        {
                            //add to quick fetch process
                            pendingAddUsers.Add(userid);

                            string roomInfoResponse =
                                GetRequest(
                                    $@"http://www.lizhi.fm/user/{userid}");
                            Regex roomUserPhotoAndRoomNameRegex =
                                new Regex(
                                    @"<div class=""user-info-img"">[^<]+<img src=""([^""]+)"" alt=""([^""]+)"" />");
                            Regex roomPicRegex =
                                new Regex(
                                    @"data-cover=""([^""]+)""");
                            //@" < div class=""radioCover left"">[^<]+<img alt=""[^""]+"" src=""([^""]+)"" />");
                            Regex roomFMAndRoomTitleRegex =
                                new Regex("<h1 class=\"user-info-name\">([^ ]+)([^<]+)<i class=");
                            //new Regex(@"div class=""left""><a href=""/"">发现</a><a href=""/([^""]+)/"">([^<]+)</a>");
                            string roomFmId = "未获取";
                            string roomFmName = "未获取";
                            string roomOwner = "未获取";
                            if (!string.IsNullOrEmpty(roomInfoResponse))
                            {
                                var roomUserPhotoAndRoomName = roomUserPhotoAndRoomNameRegex.Match(roomInfoResponse);
                                if (roomUserPhotoAndRoomName.Success)
                                {
                                    //房主图片
                                    redbaoItem.RoomOwnerPic = (roomUserPhotoAndRoomName.Groups[1].Value);
                                    myCtrl.SetRoomOwner(roomUserPhotoAndRoomName.Groups[1].Value);
                                    //房主名字
                                    roomOwner = (roomUserPhotoAndRoomName.Groups[2].Value);
                                }
                                var roomPic = roomPicRegex.Match(roomInfoResponse);
                                if (roomPic.Success)
                                {
                                    //房间图片
                                    redbaoItem.RoomPic = (roomPic.Groups[1].Value);
                                    myCtrl.SetRoomConver(roomPic.Groups[1].Value);
                                }
                                var roomFMAndRoomTitle = roomFMAndRoomTitleRegex.Match(roomInfoResponse);
                                if (roomFMAndRoomTitle.Success)
                                {
                                    //FM ID
                                    roomFmId = roomFMAndRoomTitle.Groups[1].Value;
                                    //房间名字
                                    roomFmName = roomFMAndRoomTitle.Groups[2].Value;
                                }
                            }

                            Regex regex = new Regex(@"\{""id"":""([0-9]+)""");
                            string resultID = regex.Match(getResult).Groups[1].Value;
                            redbaoItem.FieldResultID = resultID;

//                            if (!string.IsNullOrEmpty(resultID))
//                            {
//                                if (!_userIds.Contains(resultID) && !_userEmptyRoomIds.Contains(resultID))
//                                {
//                                    _userEmptyRoomIds.Add(resultID);
//                                    myCtrl.AddMessage($@"Added new userID: {resultID}");
//                                }
//                            }

                            var senderIdMatch = new Regex(@"""senderId"":""([^""]+)""").Match(getResult);
                            if (senderIdMatch.Success)
                            {
                                redbaoItem.RoomFmNumber = roomFmId;
                                redbaoItem.RoomName = roomFmName;
                                redbaoItem.RoomOwnerName = roomOwner;
                                redbaoItem.UserId = userid;
                                redbaoItem.RoomId = userid;
                                redbaoItem.SenderId = senderIdMatch.Groups[1].Value;
                                redbaoItem.Status = "等待抢...";
                                redbaoItem.IsProcessed = false;
                                redbaoItem.Id = AutoIncreaseId++;
                                //myCtrl.AddMessage($"^^^红包,房间FM:{roomFmId} 房间名:{roomFmName} 房主:{roomOwner}");
                                //myCtrl.AddMessage($"^^^红包,发送者ID:{senderIdMatch.Groups[1].Value}, 房间ID:{userid}.^^^");
                            }


                            var senderPhotoMatch = new Regex(@"""senderCover"":""([^""]+)""").Match(getResult);
                            if (senderPhotoMatch.Success)
                            {
                                //发送者图片
                                redbaoItem.SenderPic = (senderPhotoMatch.Groups[1].Value);
                                myCtrl.SetImage(senderPhotoMatch.Groups[1].Value);
                            }

                            redbaoItem.DelayDateTime = DateTime.Now.AddMilliseconds(myCtrl.GetDelayFetchGoldMillSecs());



//
//
//
//                            lock (_waittingForProcessingItems)
//                                _waittingForProcessingItems.Add(redbaoItem);



                            myCtrl.AddRedBaoItem(redbaoItem);


                            /**************************************/
                            List <string> randomTokens = shuffle(_tokens.ToList());
                            int amount = 0;
                            string senderName = string.Empty;


                            randomTokens.ForEach(token =>
                            {
                                int perAmount = 0;
                                if (!_TokenToDeviceId.ContainsKey(token))
                                {
                                    myCtrl.AddMessage("One of the users doesn't fill the deviceId successful!!!");
                                    myCtrl.AddMessage("Missing token of deviceId: " + token);
                                    return;
                                }
                                var postResult = PostRequest(
                                    $"https://redenvelope.lizhi.fm/redenvelope/grab?token={token}",
                                    $"id={redbaoItem.FieldResultID}&token={token}&liveId={_UserIdToLiveId[redbaoItem.UserId]}&deviceId={_TokenToDeviceId[token]}&"
                                    // $"id={itemDoing.FieldResultID}&token={token}&liveId={_UserIdToLiveId[itemDoing.UserId]}&deviceId={_TokenToDeviceId[token]}&accessToken={_TokenToAccessToken[token]}"
                                    );
                                if (GetMoneyAmount(postResult, ref senderName, out perAmount) == 0)
                                {
                                    //清空BAG,发生概率低忽略效率
                                    var tempList = _userIds.ToList();
                                    while (!_userIds.IsEmpty)
                                    {
                                        string someItem;
                                        _userIds.TryTake(out someItem);
                                    }
                                    tempList.Remove(redbaoItem.UserId);
                                    tempList.ForEach(item => _userIds.Add(item));
                                }
                                amount += perAmount;
                            });
                            if (amount > 0)
                            {
                                myCtrl.ProcessRedBaoItem(redbaoItem, amount, "完成", senderName);
                            }
                            else
                            {
                                myCtrl.ProcessRedBaoItem(redbaoItem, amount, "失败", senderName);
                            }

//                            lock (_waittingForProcessingItems)
//                                _waittingForProcessingItems.Remove(itemDoing);
                        }
                    }));



//
//                    // Thread.Sleep(10000);
//                    //Doing quick process transfer
//                    if (pendingAddUsers.Count > 0)
//                    {
//                        pendingAddUsers.ForEach(userid =>
//                        {
//                            if (!_userIds.Contains(userid))
//                                _userIds.Add(userid);
//
//                        });
//                        var tempEmptyUserIds = _userEmptyRoomIds.ToList();
//
//                        //ignore prefermance...just code like this.
//                        while (!_userEmptyRoomIds.IsEmpty)
//                        {
//                            string someItem;
//                            _userEmptyRoomIds.TryTake(out someItem);
//                        }
//
//                        tempEmptyUserIds.ForEach(item =>
//                        {
//                            if (!pendingAddUsers.Contains(item))
//                            {
//                                _userEmptyRoomIds.Add(item);
//                            }
//                        });
//
//                        pendingAddUsers.Clear();
//                    }
//



                    Thread.Sleep(DelayTime);
                }
                else
                {
                    if (Logined)
                    {
                        Logined = false;
                        myCtrl.ChangeLoginStatus(Logined);
                    }
                    Thread.Sleep(1000);
                }
            }
        }