OrderRelation.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. from airasia.OrderRelationEntity import OrderRelationEntity,PassengerEntity;
  2. import json
  3. import urllib3
  4. import requests
  5. import re
  6. from pyquery import PyQuery as pq
  7. from utils.HttpUtils import HttpUtils;
  8. urllib3.disable_warnings()
  9. headers = {
  10. 'Accept-Encoding': 'gzip',
  11. "Content-Encoding": "gzip, deflate, sdch",
  12. "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6",
  13. "Upgrade-Insecure-Requests": "1",
  14. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  15. "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
  16. }
  17. #解析出结果信息
  18. def getResultInfo(html):
  19. if "|0|hiddenField" in html:
  20. results = html.index("|0|hiddenField");
  21. str = html[results:html.__len__()];
  22. if "|" in str:
  23. strs = str.split("|");
  24. for ss in strs:
  25. if "alert" in ss:
  26. return ss;
  27. # 从页面解析参数
  28. def parserValueFroms(html ):
  29. jsoup =pq(html);
  30. __EVENTTARGET = jsoup.find("#eventTarget").attr("value");
  31. __EVENTARGUMENT = jsoup.find("#eventArgument").attr("value");
  32. viewState = jsoup.find("#viewState").attr("value");
  33. __VIEWSTATEGENERATOR = jsoup.find("#__VIEWSTATEGENERATOR").attr("value");
  34. ControlGroupBookingListView_BookingListSearchInputView_RadioForAgent = jsoup.find("#ControlGroupBookingListView_BookingListSearchInputView_RadioForAgent").attr("value");
  35. ControlGroupBookingListView_BookingListSearchInputView_TextBoxKeyword = jsoup.find("#ControlGroupBookingListView_BookingListSearchInputView_TextBoxKeyword").attr("value");
  36. froms = {
  37. "__EVENTARGUMENT":"",
  38. "__EVENTTARGET":"",
  39. "__VIEWSTATE":viewState,
  40. "__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR,
  41. "ControlGroupBookingListView$BookingListSearchInputView$DropDownListTypeOfSearch":"0",
  42. "ControlGroupBookingListView$BookingListSearchInputView$Search":ControlGroupBookingListView_BookingListSearchInputView_RadioForAgent,
  43. "ControlGroupBookingListView$BookingListSearchInputView$TextBoxKeyword":"KEYWORD",
  44. "pageToken":""
  45. };
  46. return froms;
  47. # 订单关联
  48. class OrderRelation():
  49. def __init__(self,params):
  50. self.josn_str = params;
  51. # 转换为实体类字段
  52. orderRelationEntityDict = json.loads(self.josn_str);
  53. # print(orderRelationEntityDict);
  54. result = "";
  55. self.orderRelationEntity = OrderRelationEntity();
  56. self.orderRelationEntity.__dict__ = orderRelationEntityDict;
  57. print(self.orderRelationEntity.singleAccountPassWord)
  58. self.passengerEntity= PassengerEntity();
  59. self.passengerEntity.__dict__ =self.orderRelationEntity.passengers;
  60. print(self.passengerEntity.bigNo)
  61. def worker(self):
  62. #代理ip
  63. proxies = {
  64. };
  65. # print("订单开始关联,关联信息为---"+self.josn_str);
  66. session = requests.session()
  67. session.headers.update(headers);
  68. HttpUtils.get(session,"https://www.airasia.com/my/en/login/travel-agent.page",proxies);
  69. print(session.cookies)
  70. logindata = {
  71. "__EVENTARGUMENT": "",
  72. "__EVENTTARGET": "ControlGroupLoginAgentView$AgentLoginView$LinkButtonLogIn",
  73. "ControlGroupLoginAgentView$AgentLoginView$TextBoxUserID": self.orderRelationEntity.singleAccount,
  74. "ControlGroupLoginAgentView$AgentLoginView$PasswordFieldPassword": self.orderRelationEntity.singleAccountPassWord,
  75. "pageToken": "",
  76. "TimeZoneDiff": "480"
  77. }
  78. res = HttpUtils.post(session,"https://booking2.airasia.com/LoginAgent.aspx", data=logindata);
  79. # print(res.content)
  80. AgentHome = res.url
  81. htym = res.content.decode()
  82. aacc = 'window.location.reload'
  83. print(AgentHome)
  84. if aacc in htym:
  85. print('哈哈 需要刷新一下')
  86. res = session.get(AgentHome)
  87. htym = res.content.decode()
  88. else:
  89. print(htym)
  90. currencycode = 'default'
  91. if 'ZENG' in htym:
  92. print("登录成功")
  93. print("开始跳转到订单查询页面");
  94. BookingListGetHtml = HttpUtils.get(session,"https://booking2.airasia.com/BookingList.aspx",proxies=proxies);
  95. print("进行订单的查询")
  96. BookingListFroms = parserValueFroms(BookingListGetHtml.content.decode());
  97. # print(BookingListFroms)
  98. BookingListPostHtml = HttpUtils.post(session,"https://booking2.airasia.com/BookingList.aspx",data=BookingListFroms,proxies=proxies)
  99. # print(BookingListPostHtml.content.decode())
  100. # 提交订单
  101. orderNo = self.orderRelationEntity.orderNo;
  102. # 解析出表单参数
  103. BookingListFroms["__EVENTARGUMENT"]="Edit:"+orderNo;
  104. BookingListFroms["__EVENTTARGET"]="ControlGroupBookingListView$BookingListSearchInputView";
  105. # print(BookingListFroms)
  106. BookingListPostHtml2 = HttpUtils.post(session,"https://booking2.airasia.com/BookingList.aspx",data=BookingListFroms,proxies=proxies);
  107. # print(BookingListPostHtml2.url)
  108. # #
  109. # ChangeItinerary = HttpUtils.get(session,"https://booking2.airasia.com/ChangeItinerary.aspx",proxies=proxies)
  110. #
  111. # BookingListFroms["MemberLoginChangeItineraryView2$TextBoxUserID"]="";
  112. # BookingListFroms["MemberLoginChangeItineraryView2$PasswordFieldPassword"]="";
  113. # BookingListFroms["memberLogin_chk_RememberMe"]="";
  114. # BookingListFroms["HiddenFieldPageBookingData"]=orderNo;
  115. # BookingListFroms["hdRememberMeEmail"]="";
  116. # BookingListFroms["__EVENTTARGET"]="ChangeControl$LinkButtonEarnBiggies";
  117. # BookingListFroms["__EVENTARGUMENT"]="";
  118. #
  119. # ChangeItineraryPostHtml = HttpUtils.post(session,"https://booking2.airasia.com/ChangeItinerary.aspx",data=BookingListFroms,proxies=proxies);
  120. EarnBiggiesGetHtml= HttpUtils.get(session,url="https://booking2.airasia.com/EarnBiggies.aspx",proxies=proxies);
  121. # print(EarnBiggiesGetHtml.content.decode())
  122. # 进入出订单关联的url中需要的 hashkey
  123. EarnBiggiesGetHtmlJsoup = pq(EarnBiggiesGetHtml.content.decode());
  124. src = EarnBiggiesGetHtmlJsoup.find("#glu").attr("src");
  125. if src is None:
  126. print("获取接口地址失败");
  127. EarnBiggiesGetHtml= HttpUtils.get(session,url="https://booking2.airasia.com/EarnBiggies.aspx",proxies=proxies);
  128. # 进入出订单关联的url中需要的 hashkey
  129. EarnBiggiesGetHtmlJsoup = pq(EarnBiggiesGetHtml.content.decode());
  130. src = EarnBiggiesGetHtmlJsoup.find("#glu").attr("src");
  131. print("重新获取一次")
  132. else:
  133. print(src);
  134. print("进入订单关联页面")
  135. src = src.replace("http","https");
  136. print("替换后的地址为-->"+src);
  137. PassengerBigIDLiveHtml = HttpUtils.get(session,src,proxies=proxies);
  138. # 解析表格
  139. PassengerBigIDLiveHtmlJsoup = pq(PassengerBigIDLiveHtml.content.decode());
  140. biggridview = PassengerBigIDLiveHtmlJsoup.find(".biggridview");
  141. # print(biggridview)
  142. centers = biggridview.html();
  143. trs = pq(centers).find("tr");
  144. passengerInfos = [];
  145. for tr in trs.items():
  146. passengerInfo = {};
  147. center=tr.find("td");
  148. spans = center.find("span");
  149. for span in spans:
  150. id = pq(span).attr("id");
  151. if id.find("firstname")>0:
  152. passengerInfo.setdefault("firstname",pq(span).text())
  153. elif id.find("lastname"):
  154. passengerInfo.setdefault("lastname",pq(span).text())
  155. key1 = center.find("input[type=hidden]").attr("name");
  156. key1Value = center.find("input[type=hidden]").attr("value");
  157. key2 = center.find("input[type=text]").attr("name");
  158. passengerInfo.setdefault("key1",key1);
  159. passengerInfo.setdefault("key1Value",key1Value);
  160. passengerInfo.setdefault("key2",key2);
  161. if passengerInfo is not None and passengerInfo.__len__() > 0 and passengerInfo.get("firstname") is not None:
  162. passengerInfos.append(passengerInfo);
  163. # print(json.dumps(passengerInfos))
  164. if passengerInfos is None or passengerInfos.__len__() <=0 :
  165. print("无可关联用户")
  166. return "无可关联用户";
  167. print("开始关联");
  168. PassengerBigIDLiveHtmlJsoup = pq(PassengerBigIDLiveHtml.content.decode());
  169. __VIEWSTATEGENERATOR = PassengerBigIDLiveHtmlJsoup.find("#__VIEWSTATEGENERATOR").attr("value");
  170. __VIEWSTATE = PassengerBigIDLiveHtmlJsoup.find("#__VIEWSTATE").attr("value");
  171. __EVENTVALIDATION =PassengerBigIDLiveHtmlJsoup.find("#__EVENTVALIDATION").attr("value");
  172. PassengerBigIDLiveHtmlFroms = {
  173. "ScriptManager1":"MainUpdatePanel|LinkButton1",
  174. "__EVENTTARGET":"LinkButton1",
  175. "__EVENTARGUMENT":"",
  176. "__VIEWSTATE":__VIEWSTATE,
  177. "__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR,
  178. "__EVENTVALIDATION":__EVENTVALIDATION,
  179. "hfpnr":orderNo,
  180. "hfurl":"https://member.airasia.com/big-look-up.aspx",
  181. "__ASYNCPOST":"true"
  182. }
  183. # print("passengerInfos--->"+json.dumps(passengerInfos));
  184. # print("PassengerBigIDLiveHtmlFroms-->"+json.dumps(PassengerBigIDLiveHtmlFroms))
  185. for centerList in passengerInfos:
  186. # print("centerList--->"+json.dumps(centerList))
  187. if centerList.get("firstname") == self.passengerEntity.firstName and centerList.get("lastname")==self.passengerEntity.lastName:
  188. PassengerBigIDLiveHtmlFroms.setdefault(centerList.get("key1"),centerList.get("key1Value"));
  189. PassengerBigIDLiveHtmlFroms.setdefault(centerList.get("key2"),self.passengerEntity.bigNo);
  190. # print(json.dumps(PassengerBigIDLiveHtmlFroms));
  191. PassengerBigIDLiveHtmlPostResp = HttpUtils.post(session,src,data=PassengerBigIDLiveHtmlFroms,proxies=proxies)
  192. if "0|error|500||" in PassengerBigIDLiveHtmlPostResp.content.decode():
  193. #重新请求一次
  194. PassengerBigIDLiveHtmlPostResp = HttpUtils.post(session,src,data=PassengerBigIDLiveHtmlFroms,proxies=proxies);
  195. result = getResultInfo(PassengerBigIDLiveHtmlPostResp.content.decode());
  196. print("结果为-->"+result);
  197. return result;