roll_lanqiu.py 17 KB

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