roll_lanqiu.py 17 KB

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