暑期没地方去,闲着无聊,看到Delphi XE7已出,又加上最近香港Iphone6抢购的火热,而我之前也有关注过Iphone6的抢购,故趁此闲暇时光,做个抢购工具给需要的朋友用一下。
程序的流程,主要是借用了Swish大侠的qworker,HTTPS主要是用了Idhttp去实现,数据的保存使用了JSON
http://download.csdn.net/detail/cntlis/8002561
具体的预约代码如下,里边代码不算精炼,主要考虑循环次数有限
procedure DoStartReserveProc(AJob: PQJob); var FTaskInfo: PReserveTasklInfo; FHttp: TPNHttp; FFindProduct, FFindTimeSlot, FReserveSucess: Boolean; FHttpResult, FPartNumbers, FselectedPartNumber, FselectedTimeSlotId: string; FHttpParams: TStrings; FTimeSlotResultJo, FTimeSlotJa, FPhoneResultJo, FPhoneJa, FReserveResultJo, FReserveErrJa: ISuperObject; i, j, k, l, FReserveProductIndex: Integer; begin try OnBeginProcessData(AJob, FTaskInfo, FHttp); if FTaskInfo.RunStatus<> rsRun then begin Workers.Signal(Signal_Stop, AJob.Data); Exit; end; FReserveSucess:= False; Inc(FTaskInfo^.CurrReserveStoreIndex); if (FTaskInfo^.CurrReserveStoreIndex<0) or (FTaskInfo^.CurrReserveStoreIndex>= Length(FTaskInfo^.UserInfo^.ReserveInfo.StoreList)) then FTaskInfo^.CurrReserveStoreIndex:= 0; WriteLog(‘开始获取开放预订信息[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName+ ‘]..‘, FTaskInfo.UserIndex, 1); FHttp:= CreateHttp(FTaskInfo.CookieMgr); try FHttpParams:= TStringList.Create; FHttpParams.Add(‘_eventId=timeslots‘); FHttpParams.Add(‘ajaxSource=true‘); FHttpParams.Add(‘p_ie=‘+ FTaskInfo.Param_p_ie); FHttpParams.Add(‘storeNumber=‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreNumber); FHttpResult:= FHttp.HttpPost(FUrlToReserveMain+ ‘reserve/iPhone?execution=e1s3‘, FHttpParams, True, True, TEncoding.UTF8); finally FreeAndNil(FHttpParams); end; if Pos(FUrlSignInMain, FHttp.Url)> 0 then begin WriteLog(‘登录超时,请重新登录..‘, FTaskInfo.UserIndex, 1); Workers.Signal(Signal_Login, AJob.Data); Exit; end; FTimeSlotResultJo:= SO(FHttpResult); FTimeSlotJa:= FTimeSlotResultJo.N[‘timeslots‘]; if not FTimeSlotJa.IsType(stArray) then begin WriteLog(‘获取店铺开放预订信息失败,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; //重新开始下一步了哇 end; if FTimeSlotJa.AsArray.Length<= 0 then begin WriteLog(‘获取店铺开放预订信息时间戳个数为0,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; //重新开始下一步了哇 end else begin WriteLog(‘获取店铺开放预订信息成功,时间戳个数:‘+ IntToStr(FTimeSlotJa.AsArray.Length), FTaskInfo.UserIndex, 1); end; {查询商品库存} //拼凑要查询的商品信息 FFindProduct:= False; FPartNumbers:= ‘‘; for i := 0 to Length(FTaskInfo.UserInfo^.ReserveInfo.ProductList)- 1 do begin if FPartNumbers= ‘‘ then FPartNumbers:= FPartNumbers+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[i].part_number else FPartNumbers:= FPartNumbers+ ‘,‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[i].part_number end; try FHttpParams:= TStringList.Create; FHttpParams.Add(‘_eventId=availability‘); FHttpParams.Add(‘ajaxSource=true‘); FHttpParams.Add(‘partNumbers=‘+ FPartNumbers); FHttpParams.Add(‘p_ie=‘+ FTaskInfo.Param_p_ie); FHttpParams.Add(‘selectedContractType=UNLOCKED‘); FHttpParams.Add(‘storeNumber=‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreNumber); FHttpResult:= FHttp.HttpPost(FUrlToReserveMain+ ‘reserve/iPhone?execution=e1s3‘, FHttpParams, True, True, TEncoding.UTF8); finally FreeAndNil(FHttpParams); end; if Pos(FUrlSignInMain, FHttp.Url)> 0 then begin WriteLog(‘登录超时,请重新登录..‘, FTaskInfo.UserIndex, 1); Workers.Signal(Signal_Login, AJob.Data); Exit; end; FPhoneResultJo:= SO(FHttpResult); FPhoneJa:= FPhoneResultJo.N[‘inventories‘]; if not FPhoneJa.IsType(stArray) then begin WriteLog(‘获取店铺开放预订型号失败,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; //重新开始下一步了哇 end; FFindTimeSlot:= False; for i := 0 to FPhoneJa.AsArray.Length- 1 do begin if FPhoneJa.AsArray[i].B[‘available‘] then begin //找有效时间了啊 for j := 0 to Length(FTaskInfo.UserInfo^.ReserveInfo.TimeSlotList)- 1 do begin if FFindTimeSlot then Break; for k := 0 to FTimeSlotJa.AsArray.Length- 1 do begin if Pos(FTaskInfo.UserInfo^.ReserveInfo.TimeSlotList[j].TimeFormat, FTimeSlotJa.AsArray[k].S[‘timeSlotId‘])> 0 then begin FFindTimeSlot:= True; FselectedTimeSlotId:= FTimeSlotJa.AsArray[k].S[‘timeSlotId‘]; Break; end; end; end; if Not FFindTimeSlot then begin WriteLog(‘获取店铺有效开放时间失败,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; end; //商品有库存了啊 FFindProduct:= True; //先找到具体是哪个商品 FselectedPartNumber:= FPhoneJa.AsArray[i].S[‘partNumber‘]; FReserveProductIndex:= -1; for j := 0 to Length(FTaskInfo.UserInfo^.ReserveInfo.ProductList)- 1 do if FTaskInfo.UserInfo^.ReserveInfo.ProductList[j].part_number= FselectedPartNumber then begin FReserveProductIndex:= j; Break; end; if FReserveProductIndex< 0 then Continue; //没有找到该商品的代码, 那就不要抢啊 WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName + ‘]开始抢购商品[‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].part_number+ ‘]...‘, FTaskInfo.UserIndex, 1); try FHttpParams:= TStringList.Create; FHttpParams.Add(‘_eventId=next‘); FHttpParams.Add(‘_flowExecutionKey=e1s3‘); FHttpParams.Add(‘color=‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].color); FHttpParams.Add(‘email=‘+ FTaskInfo.UserInfo^.GovtIdInfo.Email); FHttpParams.Add(‘firstName=‘+ FTaskInfo.UserInfo^.GovtIdInfo.FirstName); FHttpParams.Add(‘govtId=‘+ FTaskInfo.UserInfo^.GovtIdInfo.GovId); FHttpParams.Add(‘lastName=‘+ FTaskInfo.UserInfo^.GovtIdInfo.LastName); FHttpParams.Add(‘product=‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].localizedName); FHttpParams.Add(‘p_ie=‘+ FTaskInfo.Param_p_ie); FHttpParams.Add(‘selectedContractType=UNLOCKED‘); FHttpParams.Add(‘selectedGovtIdType=‘+ FTaskInfo.UserInfo^.GovtIdInfo.GovIdType); FHttpParams.Add(‘selectedPartNumber=‘+ FselectedPartNumber); FHttpParams.Add(‘selectedQuantity=‘+ IntToStr(FTaskInfo.UserInfo^.ReserveInfo.Quantity)); FHttpParams.Add(‘selectedStoreNumber=‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreNumber); FHttpParams.Add(‘selectedTimeSlotId=‘+ FselectedTimeSlotId); FHttpResult:= FHttp.HttpPost(FUrlToReserveMain+ ‘reserve/iPhone?execution=e1s3‘, FHttpParams, True, True, TEncoding.UTF8); finally FreeAndNil(FHttpParams); end; if Pos(FUrlSignInMain, FHttp.Url)> 0 then begin WriteLog(‘登录超时,请重新登录..‘, FTaskInfo.UserIndex, 1); Workers.Signal(Signal_Login, AJob.Data); Exit; end; //获取抢购结果 FHttpResult:= FHttp.HttpGet(FUrlToReserveMain+ ‘reserve/iPhone?execution=e1s4&ajaxSource=true&_eventId=context‘, True, True, TEncoding.UTF8); if Pos(FUrlSignInMain, FHttp.Url)> 0 then begin WriteLog(‘登录超时,请重新登录..‘, FTaskInfo.UserIndex, 1); Workers.Signal(Signal_Login, AJob.Data); Exit; end; if Pos(‘{‘, FHttpResult)<> 1 then begin WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName + ‘]抢购商品[‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].part_number+ ‘]结果未知...‘, FTaskInfo.UserIndex, 1); Continue; end; FReserveResultJo:= SO(FHttpResult); FReserveErrJa:= FTimeSlotResultJo.N[‘errors‘]; if FTimeSlotJa.IsType(stArray) then begin WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName + ‘]抢购商品[‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].part_number+ ‘]结果失败[‘+ FTimeSlotJa.AsString+ ‘]...‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Continue;; //重新开始下一步了哇 end else begin //这里应该是抢购成功了哦 WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName + ‘]抢购商品[‘+ FTaskInfo.UserInfo^.ReserveInfo.ProductList[FReserveProductIndex].part_number+ ‘]结果成功[‘+ FReserveResultJo.AsString+ ‘]...‘, FTaskInfo.UserIndex, 1); FTaskInfo^.HttpError:= FReserveResultJo.AsString; Workers.Signal(Signal_ReserveSucess, AJob.Data); FReserveSucess:= True; end; end; end; //没有找到商品库存 if not FFindProduct then begin WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName+ ‘]已无可预订的型号,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; end; if not FReserveSucess then begin WriteLog(‘店铺[‘+ FTaskInfo^.UserInfo^.ReserveInfo.StoreList[FTaskInfo^.CurrReserveStoreIndex].StoreName+ ‘]未预订到相关型号,‘+ IntToStr(FTaskInfo.UserInfo^.ReservalInterval)+ ‘毫秒后尝试下一店铺‘, FTaskInfo.UserIndex, 1); Workers.Delay(DoStartReserveProc, FTaskInfo.UserInfo^.ReservalInterval* 10, AJob.Data); Exit; end; finally OnAfterProcessData(AJob, FTaskInfo, FHttp); end; end;
时间: 2024-11-07 03:56:42