roll_lanqiu.py 17 KB

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