roll_lanqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. from .ball_func import fuhao
  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
  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. # self._do_calculation(item)
  20. # return
  21. def _do_calculation(self, item, out):
  22. url = 'http://admin.5gogo.com'
  23. roll_lq = []
  24. # 使用twisted将mysql插入变成异步执行
  25. logger = logging.getLogger(__name__)
  26. # 联赛id
  27. league_id = item['league_id']
  28. # 联赛名
  29. league_name = item['league_name']
  30. # result = item['result']
  31. # 比赛id
  32. match_id = item['game_id']
  33. # 球队1 #home_team
  34. team_home = item['team_home']
  35. # 球队2 # guest_team
  36. team_guest = item['team_guest']
  37. # 数量(97>)
  38. number = item['number']
  39. # 比赛状态
  40. zhuangtai = item['zhuangtai']
  41. # 日期
  42. # data_game = item['data_game']
  43. result_mark = item['score_dict']
  44. try:
  45. data_game = item['data_game'].split("/")
  46. month = str(data_game[1].strip())
  47. day = str(data_game[0])
  48. except Exception as e:
  49. logger.warning(e)
  50. data_game = item['data_game'].split(" ")
  51. months = str(data_game[1].strip())
  52. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  53. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  54. month = month_dict[months]
  55. day = str(data_game[0])
  56. # 比赛时间
  57. time_game = str(item['time_game'])
  58. # 比赛时间,时间戳
  59. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  60. r_ctime = "2019" + "-" + month + "-" + day
  61. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60)) # 过期时间
  62. # 现在时间,时间戳
  63. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  64. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  65. # 队1分数
  66. score_home = item['score_home']
  67. # 队2分数
  68. score_guest = item['score_guest']
  69. # 第几节
  70. jijie = item['jijie']
  71. # 球队得分
  72. qiudui = item['qiudui']
  73. pt = 4
  74. match_score = "{}:{}".format(score_home, score_guest)
  75. concedes_dict = item['concede']
  76. concedes_dict_rule = item['concede_rule']
  77. odd_evens_dict = item['odd_even']
  78. odd_evens_dict_rule = item['odd_even_rule']
  79. total_sizes_dict = item['total_size']
  80. total_sizes_dict_rule = item['total_size_rule']
  81. last_numbers_dict = item['last_number']
  82. capots_dict = item['capot']
  83. team_scores_dict = item['team_score']
  84. team_scores_dict_rule = item['team_score_rule']
  85. last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year)
  86. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  87. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  88. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  89. league_list = []
  90. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  91. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  92. league_data = dict(zip(league_key, league_value))
  93. league_list.append(league_data)
  94. league_dict['data'] = league_list
  95. res = Helper.async_post(LEAGUE_URL, league_dict)
  96. if '成功' in res:
  97. logger.info('篮球滚球联赛提交成功, {}'.format(res))
  98. self.db.hg3535_lq_league.insert(league_list)
  99. else:
  100. logger.warning('篮球滚球联赛提交失败, {}'.format(res))
  101. else:
  102. logger.info('篮球滚球联赛已存在')
  103. if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  104. match_list = []
  105. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  106. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  107. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  108. "half_match_id", "is_today"]
  109. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, "00:00", number,
  110. "hg3535", 1, 0, 0, utime, uuid, 0, 0]
  111. match_data = dict(zip(match_kay, match_value))
  112. match_list.append(match_data)
  113. match_dict['data'] = match_list
  114. res = Helper.async_post(MATCH_URL, match_dict)
  115. if '成功' in res:
  116. logger.info('篮球滚球赛事提交成功, {}'.format(res))
  117. self.db.hg3535_lq_competition.insert(match_list)
  118. else:
  119. logger.warning('篮球滚球赛事提交失败, {}'.format(res))
  120. logger.warning(match_dict)
  121. else:
  122. logger.info('篮球滚球赛事已存在')
  123. # 赔率
  124. data_list = []
  125. odds_onlys = []
  126. if concedes_dict:
  127. for key, value in concedes_dict.items():
  128. if value:
  129. for index, concede_value in enumerate(value):
  130. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  131. concede_value) + "hg3535" + str(match_id)
  132. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  133. odds_only = Helper.genearte_MD5(hash_str, pt)
  134. sole = Helper.genearte_MD5(sole_str, pt)
  135. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  136. "condition", "odds_only", "sole", "source", "type", "team"]
  137. condition = fuhao(concedes_dict_rule[key][index])
  138. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  139. odds_only, sole, "hg3535", "0", ""]
  140. concede_data = dict(zip(concede_key, concede_value))
  141. data_list.append(concede_data)
  142. if total_sizes_dict:
  143. for key, value in total_sizes_dict.items():
  144. if value:
  145. for index, total_sizes_value in enumerate(value):
  146. condition = fuhao(total_sizes_dict_rule[key][index])
  147. hash_str = "total_size" + str(key) + str(index) + str(condition) + str(
  148. total_sizes_value) + "hg3535" + str(match_id)
  149. sole_str = "total_size" + str(key) + str(index) + str(match_id) + "hg3535"
  150. odds_only = Helper.genearte_MD5(hash_str, pt)
  151. sole = Helper.genearte_MD5(sole_str, pt)
  152. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  153. "condition", "odds_only", "sole", "source", "type", "team"]
  154. total_sizes_value = [match_id, league_id, key, "0", index, 'total_size', total_sizes_value,
  155. condition, odds_only, sole, "hg3535", "0", ""]
  156. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  157. data_list.append(total_sizes_data)
  158. if odd_evens_dict:
  159. for key, value in odd_evens_dict.items():
  160. if value:
  161. condition = odd_evens_dict_rule[key]
  162. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  163. value) + "hg3535" + str(match_id)
  164. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  165. odds_only = Helper.genearte_MD5(hash_str, pt)
  166. sole = Helper.genearte_MD5(sole_str, pt)
  167. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  168. "condition", "odds_only", "sole", "source", "type", "team"]
  169. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  170. condition, odds_only, sole, "hg3535", "0", ""]
  171. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  172. data_list.append(odd_evens_data)
  173. lastnumber_dict = {'0或5': '_zero_five', '1或6': '_one_six', '2或7': '_two_seven', '3或8': '_three_eight',
  174. '4或9': '_four_nine'}
  175. last_home = last_numbers_dict['last_home']
  176. if last_home:
  177. for key, value in last_home.items():
  178. odds_code = 'last_home' + lastnumber_dict[key]
  179. condition = lastnumber_dict[key]
  180. hash_str = "LN" + odds_code + '0' + str(condition) + 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. condition, 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 = 'last_guest' + lastnumber_dict[key]
  195. condition = lastnumber_dict[key]
  196. hash_str = "LN" + odds_code + '0' + str(condition) + 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. condition, 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 "成功" in res:
  243. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  244. else:
  245. logger.warning('篮球滚球详细赔率提交失败, {}'.format(res))
  246. logger.warning(odds_dict)
  247. data_list = []
  248. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  249. "lg_id": league_id, "home_rate": 0,
  250. "guest_rate": 0, "home_score": score_home,
  251. "guest_score": score_guest, "all_goal": "", "status": 1,
  252. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  253. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  254. "match_process": jijie, "tag": number, "result_mark": result_mark,
  255. "match_id": match_id, "p_code": ""}
  256. data_list.append(lq_rball)
  257. r_data_dict = {
  258. "game_code": "lq",
  259. "title": "match_result_r",
  260. "source": "hg3535",
  261. "data": data_list
  262. }
  263. if data_list:
  264. try:
  265. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  266. if "成功" in res:
  267. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  268. else:
  269. logger.warning('篮球滚球赛事结果记录提交失败, {}'.format(res))
  270. logger.warning(r_data_dict)
  271. except Exception as e:
  272. logger.warning('滚球数据接口异常,提交失败, {}'.format(e))
  273. reactor.callFromThread(out.callback, item)