roll_lanqiu.py 16 KB

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