bangqiu.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # encoding: utf-8
  2. import datetime
  3. import pymongo
  4. import time
  5. import logging
  6. from twisted.internet import defer, reactor
  7. from ..utils.helper import Helper
  8. from .ball_func import new_time
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, POST_URL
  10. class Bangqiupipeline(object):
  11. def open_spider(self, spider):
  12. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  13. authSource=M_DB)
  14. self.db = self.mongo[M_DB]
  15. @defer.inlineCallbacks
  16. def process_item(self, item, spider):
  17. out = defer.Deferred()
  18. reactor.callInThread(self._do_calculation, item, out)
  19. yield out
  20. # defer.returnValue(item)
  21. def _do_calculation(self, item, out):
  22. # def process_item(self, item, spider):
  23. logger = logging.getLogger(__name__)
  24. # 联赛id
  25. league_id = item['league_id']
  26. # 联赛名
  27. league_name = item['league_name']
  28. # result = item['result']
  29. # 比赛id
  30. match_id = item['game_id']
  31. # 球队1
  32. team_home = item['team_home']
  33. # 球队2
  34. team_guest = item['team_guest']
  35. # 数量(97>)
  36. tag_number = item['number']
  37. # 比赛状态
  38. zhuangtai = item['zhuangtai']
  39. # 日期
  40. # data_game = item['data_game']
  41. try:
  42. data_game = item['data_game'].split("/")
  43. month = str(data_game[1].strip())
  44. day = str(data_game[0]).strip()
  45. except Exception as e:
  46. data_game = item['data_game'].split(" ")
  47. months = str(data_game[1].strip())
  48. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  49. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  50. month = month_dict[months]
  51. day = str(data_game[0]).strip()
  52. # 比赛时间
  53. time_game = str(item['time_game'])
  54. # 比赛时间,时间戳
  55. ctime = str(datetime.datetime.now().year) + "-" + month + "-" + day + " " + time_game + ":00"
  56. # 现在时间,时间戳
  57. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  58. pt = str(item['pt'])
  59. # 让球
  60. concedes_dict = item['concedes_dict']
  61. concedes_dict_rule = item['concedes_dict_rule']
  62. # 独赢
  63. capots_dict = item['capots_dict']
  64. # 总得分大小
  65. total_size_dict = item['total_size_dict']
  66. total_size_dict_rule = item['total_size_dict_rule']
  67. # 总得分单双
  68. odd_evens_dict = item['odd_evens_dict']
  69. odd_evens_dict_rule = item['odd_evens_dict_rule']
  70. match_date, match_time, time3 = new_time(ctime)
  71. us_time = ctime
  72. uuid = Helper.genearte_uuid(league_name)
  73. """联赛表"""
  74. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  75. if self.db.bq_league35.find({'lg_id': league_id}).count() < 1:
  76. league_dict = {"game_code": "bq", "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('{}/setLeague'.format(POST_URL), league_dict)
  84. if res:
  85. if res.get('status') == 1:
  86. self.db.bq_league35.insert(league_data)
  87. logger.info('棒球联赛提交成功, {}'.format(res))
  88. else:
  89. logger.warning('棒球联赛提交失败, {}, {}'.format(res, league_dict))
  90. else:
  91. logger.warning('棒球滚球联赛,接口异常,提交失败, {}, {}'.format(res, league_dict))
  92. else:
  93. logger.info('棒球滚球联赛, 已存在不提交')
  94. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  95. pt_status = pt_dict[pt]
  96. if pt == '3':
  97. is_rollball = 0
  98. is_today = 0
  99. is_morningplate = 0
  100. is_stringscene = 1
  101. elif pt == '2':
  102. is_rollball = 0
  103. is_today = 0
  104. is_morningplate = 1
  105. is_stringscene = 0
  106. else:
  107. is_today = 1
  108. is_rollball = 0
  109. is_morningplate = 0
  110. is_stringscene = 0
  111. """插入赛事表"""
  112. match_identity = Helper.genearte_uuid(team_home + team_guest + match_date)
  113. if self.db.bq_competition35.find({"match_id": match_id, pt_status: 1}).count() < 1:
  114. match_list = []
  115. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  116. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  117. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  118. "half_match_id", "is_today", "is_horn", 'match_identity']
  119. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  120. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, 0, match_identity]
  121. match_data = dict(zip(match_key, match_value))
  122. match_list.append(match_data)
  123. match_dict['data'] = match_list
  124. res = Helper.async_post('{}/setMatch'.format(POST_URL), match_dict)
  125. if res:
  126. if res.get('status') == 1:
  127. self.db.bq_competition35.insert(match_data)
  128. logger.info('棒球赛事提交成功, {}, {}'.format(res, match_data))
  129. else:
  130. logger.warning('棒球赛事提交失败, {}, {}'.format(res, match_data))
  131. else:
  132. logger.warning('棒球赛事接口异常, 提交失败, {}, {}'.format(res, match_data))
  133. else:
  134. logger.info("棒球赛事已存在,不做修改")
  135. """赔率"""
  136. data_list = []
  137. odds_onlys = []
  138. # 让球
  139. if concedes_dict:
  140. for key, value in concedes_dict.items():
  141. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  142. sole = Helper.genearte_MD5(sole_str, pt)
  143. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  144. value)
  145. odds_only = Helper.genearte_MD5(odds_str, pt)
  146. condition = str(concedes_dict_rule[key])
  147. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  148. "odds_only", "sole", "source", "type", "team"]
  149. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  150. odds_only, sole, 'hg3535', '0', '']
  151. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  152. data_list.append(concedes_dict_data)
  153. # 总得分大/小
  154. if total_size_dict:
  155. for key, value in total_size_dict.items():
  156. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  157. sole = Helper.genearte_MD5(sole_str, pt)
  158. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  159. total_size_dict_rule[key]) + str(value)
  160. odds_only = Helper.genearte_MD5(odds_str, pt)
  161. condition = str(total_size_dict_rule[key])
  162. total_size_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  163. "condition", "odds_only", "sole", "source", "type", "team"]
  164. total_size_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  165. odds_only, sole, 'hg3535', '0', '']
  166. total_size_dict_data = dict(zip(total_size_dict_key, total_size_dict_value))
  167. data_list.append(total_size_dict_data)
  168. # 独赢
  169. if capots_dict:
  170. for key, value in capots_dict.items():
  171. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  172. sole = Helper.genearte_MD5(sole_str, pt)
  173. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  174. odds_only = Helper.genearte_MD5(odds_str, pt)
  175. capots_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  176. "condition", "odds_only", "sole", "source", "type", "team"]
  177. capots_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  178. odds_only, sole, 'hg3535', '0', '']
  179. capots_dict_data = dict(zip(capots_dict_key, capots_dict_value))
  180. data_list.append(capots_dict_data)
  181. # 总得分单双
  182. if odd_evens_dict:
  183. for key, value in odd_evens_dict.items():
  184. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  185. sole = Helper.genearte_MD5(sole_str, pt)
  186. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  187. odd_evens_dict_rule[key]) + str(value)
  188. odds_only = Helper.genearte_MD5(odds_str, pt)
  189. condition = str(odd_evens_dict_rule[key])
  190. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  191. "condition", "odds_only", "sole", "source", "type", "team"]
  192. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  193. odds_only, sole, 'hg3535', '0', '']
  194. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  195. data_list.append(odd_evens_dict_data)
  196. # 让球 前五局
  197. # 总得分大/小 前五局
  198. if pt == '3':
  199. ris_stringscene = 1
  200. else:
  201. ris_stringscene = 0
  202. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  203. "is_stringscene", "utime", "pt", 'match_identity']
  204. odds_value = ["bq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  205. ris_stringscene, utime, pt, match_identity]
  206. odds_dict = dict(zip(odds_key, odds_value))
  207. if data_list:
  208. res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict)
  209. if res:
  210. if res.get('status') == 1:
  211. logger.info('棒球赔率提交成功, {}, {}'.format(res, odds_dict))
  212. else:
  213. logger.warning('棒球赔率提交失败, {}, {}'.format(res, odds_dict))
  214. else:
  215. logger.warning('棒球详细接口赔率,接口异常, 提交失败, {}'.format(odds_dict))
  216. else:
  217. logger.info('棒球详细赔率列表为空, 不提交')
  218. reactor.callFromThread(out.callback, item)