roll_lanqiu.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. # from .ball_func import
  7. from ..utils.helper import Helper
  8. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT, MATCH_STATUS
  9. class Roll_Lanqiupipeline(object):
  10. def open_spider(self, spider):
  11. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST)
  12. self.db = self.mongo[M_DB]
  13. @defer.inlineCallbacks
  14. def process_item(self, item, spider):
  15. out = defer.Deferred()
  16. reactor.callInThread(self._do_calculation, item, out)
  17. yield out
  18. defer.returnValue(item)
  19. # def process_item(self, item, spider):
  20. def _do_calculation(self, item, out):
  21. url = 'http://admin.5gogo.com'
  22. roll_lq = []
  23. # 使用twisted将mysql插入变成异步执行
  24. logger = logging.getLogger(__name__)
  25. # 联赛id
  26. league_id = item['league_id']
  27. # 联赛名
  28. league_name = item['league_name']
  29. # result = item['result']
  30. # 比赛id
  31. match_id = item['game_id']
  32. # 球队1 #home_team
  33. team_home = item['team_home']
  34. # 球队2 # guest_team
  35. team_guest = item['team_guest']
  36. # 数量(97>)
  37. number = item['number']
  38. # 比赛状态
  39. zhuangtai = item['zhuangtai']
  40. # 日期
  41. # data_game = item['data_game']
  42. result_mark = item['score_dict']
  43. try:
  44. data_game = item['data_game'].split("/")
  45. month = str(data_game[1].strip())
  46. day = str(data_game[0])
  47. except Exception as e:
  48. logger.warning(e)
  49. data_game = item['data_game'].split(" ")
  50. months = str(data_game[1].strip())
  51. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  52. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  53. month = month_dict[months]
  54. day = str(data_game[0])
  55. # 比赛时间
  56. time_game = str(item['time_game'])
  57. # 比赛时间,时间戳
  58. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  59. r_ctime = "2019" + "-" + month + "-" + day
  60. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60)) # 过期时间
  61. # 现在时间,时间戳
  62. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  63. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  64. # 队1分数
  65. score_home = item['score_home']
  66. # 队2分数
  67. score_guest = item['score_guest']
  68. # 第几节
  69. jijie = item['jijie']
  70. # 球队得分
  71. qiudui = item['qiudui']
  72. pt = 4
  73. match_score = "{}:{}".format(score_home, score_guest)
  74. concedes_dict = item['concede']
  75. concedes_dict_rule = item['concede_rule']
  76. odd_evens_dict = item['odd_even']
  77. odd_evens_dict_rule = item['odd_even_rule']
  78. total_sizes_dict = item['total_size']
  79. total_sizes_dict_rule = item['total_size_rule']
  80. last_numbers_dict = item['last_number']
  81. capots_dict = item['capot']
  82. team_scores_dict = item['team_score']
  83. team_scores_dict_rule = item['team_score_rule']
  84. last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year)
  85. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  86. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  87. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  88. league_list = []
  89. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  90. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  91. league_data = dict(zip(league_key, league_value))
  92. league_list.append(league_data)
  93. league_dict['data'] = league_list
  94. res = Helper.async_post(LEAGUE_URL, league_dict)
  95. if res:
  96. if '成功' in res:
  97. logger.info('篮球滚球联赛提交成功, {}'.format(res))
  98. self.db.hg3535_lq_league.insert(league_data)
  99. logger.info(league_dict)
  100. else:
  101. logger.warning('篮球滚球联赛提交失败, {}'.format(res))
  102. else:
  103. logger.warning('篮球滚球联赛接口, 提交失败, {}'.format(res))
  104. logger.warning(league_dict)
  105. else:
  106. logger.info('篮球滚球联赛已存在')
  107. if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  108. match_list = []
  109. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  110. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  111. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  112. "half_match_id", "is_today", "is_horn"]
  113. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, "00:00", number,
  114. "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0]
  115. match_data = dict(zip(match_kay, match_value))
  116. match_list.append(match_data)
  117. match_dict['data'] = match_list
  118. res = Helper.async_post(MATCH_URL, match_dict)
  119. if res:
  120. if '成功' in res:
  121. logger.info('篮球滚球赛事提交成功, {}'.format(res))
  122. self.db.hg3535_lq_competition.insert(match_data)
  123. logger.info(match_dict)
  124. else:
  125. logger.warning('篮球滚球赛事提交失败, {}'.format(res))
  126. logger.warning(match_dict)
  127. else:
  128. logger.warning('篮球滚球赛事接口异常,提交失败, {}'.format(res))
  129. logger.warning(match_dict)
  130. else:
  131. logger.info('篮球滚球赛事已存在')
  132. # 赔率
  133. data_list = []
  134. odds_onlys = []
  135. if concedes_dict:
  136. for key, value in concedes_dict.items():
  137. if value:
  138. for index, concede_value in enumerate(value):
  139. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  140. concede_value) + "hg3535" + str(match_id)
  141. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  142. odds_only = Helper.genearte_MD5(hash_str, pt)
  143. sole = Helper.genearte_MD5(sole_str, pt)
  144. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  145. "condition", "odds_only", "sole", "source", "type", "team"]
  146. condition = concedes_dict_rule[key][index]
  147. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  148. odds_only, sole, "hg3535", "0", ""]
  149. concede_data = dict(zip(concede_key, concede_value))
  150. data_list.append(concede_data)
  151. if total_sizes_dict:
  152. for key, value in total_sizes_dict.items():
  153. if value:
  154. for index, total_sizes_value in enumerate(value):
  155. condition = total_sizes_dict_rule[key][index]
  156. hash_str = "TN" + str(key) + str(index) + str(condition) + str(
  157. total_sizes_value) + "hg3535" + str(match_id)
  158. sole_str = "TN" + str(key) + str(index) + str(match_id) + "hg3535"
  159. odds_only = Helper.genearte_MD5(hash_str, pt)
  160. sole = Helper.genearte_MD5(sole_str, pt)
  161. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  162. "condition", "odds_only", "sole", "source", "type", "team"]
  163. total_sizes_value = [match_id, league_id, key, "0", index, 'TN', total_sizes_value,
  164. condition, odds_only, sole, "hg3535", "0", ""]
  165. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  166. data_list.append(total_sizes_data)
  167. if odd_evens_dict:
  168. for key, value in odd_evens_dict.items():
  169. if value:
  170. condition = odd_evens_dict_rule[key]
  171. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  172. value) + "hg3535" + str(match_id)
  173. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  174. odds_only = Helper.genearte_MD5(hash_str, pt)
  175. sole = Helper.genearte_MD5(sole_str, pt)
  176. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  177. "condition", "odds_only", "sole", "source", "type", "team"]
  178. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  179. condition, odds_only, sole, "hg3535", "0", ""]
  180. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  181. data_list.append(odd_evens_data)
  182. lastnumber_dict = {'0或5': '_zero_five', '1或6': '_one_six', '2或7': '_two_seven', '3或8': '_three_eight',
  183. '4或9': '_four_nine'}
  184. last_home = last_numbers_dict['last_home']
  185. if last_home:
  186. for key, value in last_home.items():
  187. odds_code = 'last_home' + lastnumber_dict[key]
  188. condition = lastnumber_dict[key]
  189. hash_str = "LN" + odds_code + '0' + str(condition) + str(
  190. value) + "hg3535" + str(match_id)
  191. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  192. odds_only = Helper.genearte_MD5(hash_str, pt)
  193. sole = Helper.genearte_MD5(sole_str, pt)
  194. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  195. "condition", "odds_only", "sole", "source", "type", "team"]
  196. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  197. condition, odds_only, sole, "hg3535", "0", ""]
  198. last_home_data = dict(zip(last_home_key, last_home_value))
  199. data_list.append(last_home_data)
  200. last_guest = last_numbers_dict['last_guest']
  201. if last_guest:
  202. for key, value in last_guest.items():
  203. odds_code = 'last_guest' + lastnumber_dict[key]
  204. condition = lastnumber_dict[key]
  205. hash_str = "LN" + odds_code + '0' + str(condition) + str(value) + "hg3535" + str(match_id)
  206. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  207. odds_only = Helper.genearte_MD5(hash_str, pt)
  208. sole = Helper.genearte_MD5(sole_str, pt)
  209. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  210. "condition", "odds_only", "sole", "source", "type", "team"]
  211. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  212. condition, odds_only, sole, "hg3535", "0", ""]
  213. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  214. data_list.append(last_guest_data)
  215. if capots_dict:
  216. for key, value in capots_dict.items():
  217. if value:
  218. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  219. sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535"
  220. odds_only = Helper.genearte_MD5(hash_str, pt)
  221. sole = Helper.genearte_MD5(sole_str, pt)
  222. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  223. "condition", "odds_only", "sole", "source", "type", "team"]
  224. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  225. "", odds_only, sole, "hg3535", "0", ""]
  226. capots_data = dict(zip(capots_key, capots_value))
  227. data_list.append(capots_data)
  228. if team_scores_dict:
  229. for key, team_scores_value in team_scores_dict.items():
  230. if team_scores_value:
  231. for index, value in enumerate(team_scores_value):
  232. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  233. value) + "hg3535" + str(match_id)
  234. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  235. odds_only = Helper.genearte_MD5(hash_str, pt)
  236. sole = Helper.genearte_MD5(sole_str, pt)
  237. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  238. "condition", "odds_only", "sole", "source", "type", "team"]
  239. condition = team_scores_dict_rule[key][index]
  240. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  241. odds_only, sole, "hg3535", "0", ""]
  242. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  243. data_list.append(team_scores_data)
  244. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  245. "is_stringscene", "utime", "pt"]
  246. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid,
  247. 0, utime, pt]
  248. odds_dict = dict(zip(odds_key, odds_value))
  249. if data_list:
  250. res = Helper.async_post(ODDS_URL, odds_dict)
  251. if res:
  252. if "成功" in res:
  253. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  254. else:
  255. logger.warning('篮球滚球详细赔率提交失败, {}'.format(res))
  256. logger.warning(odds_dict)
  257. else:
  258. logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}'.format(res))
  259. logger.warning(odds_dict)
  260. else:
  261. logger.info('篮球滚球详细赔率列表, 不提交, {}'.format(res))
  262. data_list = []
  263. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  264. "lg_id": league_id, "home_rate": 0,
  265. "guest_rate": 0, "home_score": score_home,
  266. "guest_score": score_guest, "all_goal": "", "status": 1,
  267. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  268. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  269. "match_process": jijie, "tag": number, "result_mark": result_mark,
  270. "match_id": match_id, "p_code": ""}
  271. data_list.append(lq_rball)
  272. r_data_dict = {
  273. "game_code": "lq",
  274. "title": "match_result_r",
  275. "source": "hg3535",
  276. "data": data_list
  277. }
  278. if data_list:
  279. try:
  280. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  281. if "成功" in res:
  282. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  283. else:
  284. logger.warning('篮球滚球赛事结果记录提交失败, {}'.format(res))
  285. logger.warning(r_data_dict)
  286. except Exception as e:
  287. logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
  288. else:
  289. logger.info('篮球滚球赛事结果数据,为空不提交')
  290. status_dict = {"game_code": 'lq', "title": "match_status", "source": "hg3535"}
  291. data_list = []
  292. data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  293. "is_stringscene": 0, "is_horn": 0}
  294. data_list.append(data)
  295. status_dict['data'] = data_list
  296. res = Helper.async_post(MATCH_STATUS, status_dict)
  297. if res:
  298. if "成功" in res:
  299. self.db.match_status35.insert(status_dict)
  300. logger.info('{},赛事结果状态交成功, {}'.format('lq', res))
  301. logger.info(status_dict)
  302. else:
  303. logger.warning('{},赛事结果状态交失败, {}'.format('lq', res))
  304. logger.warning(status_dict)
  305. else:
  306. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('lq', res))
  307. logger.warning(status_dict)
  308. reactor.callFromThread(out.callback, item)