wangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 .ball_func import new_time, out_time
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  10. class Wangqiupipeline(object):
  11. def open_spider(self, spider):
  12. # self.connection = AsyncIOMotorClient("mongodb://{}:{}@{}:{}/database?authSource={}".format('kaiyou', 'kaiyou', '192.168.2.200', 27017, 'kaiyou'))
  13. # session = aiohttp.ClientSession()
  14. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource='kaiyou')
  15. self.db = self.mongo[M_DB]
  16. @defer.inlineCallbacks
  17. def process_item(self, item, spider):
  18. out = defer.Deferred()
  19. reactor.callInThread(self._do_calculation, item, out)
  20. yield out
  21. defer.returnValue(item)
  22. def _do_calculation(self, item, out):
  23. logger = logging.getLogger(__name__)
  24. # logger.warning(query.addErrback(self.handle_error, item, spider))
  25. # 联赛id
  26. league_id = item['league_id']
  27. # 联赛名
  28. league_name = item['league_name']
  29. # result = item['result']
  30. # 比赛id
  31. match_id = item['game_id']
  32. # print(game_id)
  33. # 球队1
  34. team_home = item['team_home']
  35. # 球队2
  36. team_guest = item['team_guest']
  37. # 数量(97>)
  38. tag_number = item['number']
  39. # 比赛状态
  40. zhuangtai = item['zhuangtai']
  41. # 日期
  42. # data_game = item['data_game']
  43. try:
  44. data_game = item['data_game'].split("/")
  45. month = str(data_game[1].strip())
  46. day = str(data_game[0])
  47. except Exception as e:
  48. logger.warning(e)
  49. data_game = item['data_game'].split(" ")
  50. months = str(data_game[1].strip())
  51. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  52. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  53. month = month_dict[months]
  54. day = str(data_game[0])
  55. # 比赛时间
  56. time_game = str(item['time_game'])
  57. # 比赛时间,时间戳
  58. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  59. r_ctime = "2019" + "-" + month + "-" + day
  60. # 现在时间,时间戳
  61. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  62. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  63. # 队1分数
  64. score_home = item['score_home']
  65. # 队2分数
  66. score_guest = item['score_guest']
  67. # 第几节
  68. jijie = item['jijie']
  69. # 球队得分
  70. qiudui = item['qiudui']
  71. pt = str(item['pt'])
  72. # 让盘
  73. concedes_dict = item['concedes_dict']
  74. concedes_dict_rule = item['concedes_dict_rule']
  75. # 冠军
  76. kemps_dict = item['kemps_dict']
  77. # 让局
  78. bureaus_dict = item['bureaus_dict']
  79. bureaus_dict_rule = item['bureaus_dict_rule']
  80. # 总局数大小
  81. total_number_dict = item['total_number_dict']
  82. total_number_dict_rule = item['total_number_dict_rule']
  83. # 总决赛单双
  84. odd_evens_dict = item['odd_evens_dict']
  85. odd_evens_dict_rule = item['odd_evens_dict_rule']
  86. us_time = ctime
  87. match_date, match_time, time3 = new_time(ctime)
  88. n_time = out_time(time3, 3)
  89. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  90. """联赛"""
  91. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  92. if self.db.wq_league35.find({'lg_id': league_id}).count() < 1:
  93. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  94. league_list = []
  95. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  96. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  97. league_data = dict(zip(league_key, league_value))
  98. league_list.append(league_data)
  99. league_dict['data'] = league_list
  100. res = Helper.async_post(LEAGUE_URL, league_dict)
  101. if res:
  102. if '成功' in res:
  103. self.db.wq_league35.insert(league_data)
  104. logger.info('网球联赛提交成功, {}'.format(res))
  105. else:
  106. logger.warning('网球联赛提交失败, {}'.format(res))
  107. logger.warning(league_dict)
  108. else:
  109. logger.warning('网球联赛接口异常,提交失败, {}'.format(res))
  110. logger.warning(league_dict)
  111. else:
  112. logger.info('网球联赛已存在, 不提交')
  113. """赛事"""
  114. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  115. pt_status = pt_dict[pt]
  116. if pt == '3':
  117. is_rollball = 0
  118. is_today = 0
  119. is_morningplate = 0
  120. is_stringscene = 1
  121. elif pt == '2':
  122. is_rollball = 0
  123. is_today = 0
  124. is_morningplate = 1
  125. is_stringscene = 0
  126. else:
  127. is_today = 1
  128. is_rollball = 0
  129. is_morningplate = 0
  130. is_stringscene = 0
  131. if self.db.wq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  132. match_list = []
  133. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  134. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  135. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  136. "half_match_id", "is_today", 'rule']
  137. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  138. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '']
  139. match_data = dict(zip(match_key, match_value))
  140. match_list.append(match_data)
  141. match_dict['data'] = match_list
  142. res = Helper.async_post(MATCH_URL, match_dict)
  143. if res:
  144. if '成功' in res:
  145. self.db.wq_competition35.insert(match_data)
  146. logger.info('网球赛事提交成功, {}'.format(res))
  147. else:
  148. logger.warning('网球赛事提交失败, {}'.format(res))
  149. logger.warning(match_data)
  150. else:
  151. logger.warning('网球赛事接口异常,提交失败, {}'.format(res))
  152. logger.warning(match_dict)
  153. else:
  154. logging.info('网球赛事已存在, 不提交')
  155. """赔率"""
  156. try:
  157. result = self.db.hg3535_wq_odds.find_one({'match_id': match_id, 'pt': pt})
  158. odds_only_list = result['odds_only']
  159. except:
  160. result = ''
  161. odds_only_list = []
  162. wangqiu = item['wangqiu']
  163. data_list = []
  164. odds_onlys = []
  165. # 让盘
  166. if concedes_dict:
  167. for key, value in concedes_dict.items():
  168. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  169. sole = Helper.genearte_MD5(sole_str, pt)
  170. odds_str = 'LD' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  171. value)
  172. odds_only = Helper.genearte_MD5(odds_str, pt)
  173. condition = concedes_dict_rule[key]
  174. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  175. "odds_only", "sole", "source", "type", "team"]
  176. concedes_dict_value = [match_id, league_id, key, '0', 0, 'LD', value, condition,
  177. odds_only, sole, 'hg3535', '0', '']
  178. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  179. data_list.append(concedes_dict_data)
  180. # 让局
  181. if bureaus_dict:
  182. for key, value in bureaus_dict.items():
  183. sole_str = 'LB' + str(key) + str(match_id) + 'hg3535'
  184. sole = Helper.genearte_MD5(sole_str, pt)
  185. odds_str = 'LB' + str(key) + str(match_id) + 'hg3535' + str(bureaus_dict_rule[key]) + str(
  186. value)
  187. odds_only = Helper.genearte_MD5(odds_str, pt)
  188. condition = bureaus_dict_rule[key]
  189. bureaus_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  190. "odds_only", "sole", "source", "type", "team"]
  191. bureaus_dict_value = [match_id, league_id, key, '0', 0, 'LB', value, condition,
  192. odds_only, sole, 'hg3535', '0', '']
  193. bureaus_dict_data = dict(zip(bureaus_dict_key, bureaus_dict_value))
  194. data_list.append(bureaus_dict_data)
  195. # 总局数大小
  196. if total_number_dict:
  197. for key, value in total_number_dict.items():
  198. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  199. sole = Helper.genearte_MD5(sole_str, pt)
  200. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  201. total_number_dict_rule[key]) + str(value)
  202. odds_only = Helper.genearte_MD5(odds_str, pt)
  203. condition = total_number_dict_rule[key]
  204. total_number_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  205. "condition",
  206. "odds_only", "sole", "source", "type", "team"]
  207. total_number_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  208. odds_only, sole, 'hg3535', '0', '']
  209. total_number_dict_data = dict(zip(total_number_dict_key, total_number_dict_value))
  210. data_list.append(total_number_dict_data)
  211. # 总局数单双
  212. if odd_evens_dict:
  213. for key, value in odd_evens_dict.items():
  214. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  215. sole = Helper.genearte_MD5(sole_str, pt)
  216. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(odd_evens_dict_rule[key]) + str(
  217. value)
  218. odds_only = Helper.genearte_MD5(odds_str, pt)
  219. condition = odd_evens_dict_rule[key]
  220. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  221. "odds_only", "sole", "source", "type", "team"]
  222. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  223. odds_only, sole, 'hg3535', '0', '']
  224. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  225. data_list.append(odd_evens_dict_data)
  226. # 冠军
  227. if kemps_dict:
  228. for key, value in kemps_dict.items():
  229. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  230. sole = Helper.genearte_MD5(sole_str, pt)
  231. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + '2' + str(value)
  232. odds_only = Helper.genearte_MD5(odds_str, pt)
  233. kemps_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  234. "odds_only", "sole", "source", "type", "team"]
  235. kemps_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  236. odds_only, sole, 'hg3535', '0', '']
  237. kemps_dict_data = dict(zip(kemps_dict_key, kemps_dict_value))
  238. data_list.append(kemps_dict_data)
  239. if pt == '3':
  240. ris_stringscene = 1
  241. else:
  242. ris_stringscene = 0
  243. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  244. "is_stringscene", "utime", "pt"]
  245. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  246. ris_stringscene, utime, pt]
  247. odds_dict = dict(zip(odds_key, odds_value))
  248. # print(odds_dict)
  249. # print(123)
  250. if data_list:
  251. res = Helper.async_post(ODDS_URL, odds_dict)
  252. if res:
  253. if "成功" in res:
  254. logger.info('网球赔率提交成功, {}'.format(res))
  255. logger.info(odds_dict)
  256. else:
  257. logger.warning('网球赔率提交失败, {}'.format(res))
  258. logger.warning(odds_dict)
  259. else:
  260. logger.warning('网球详细接口赔率返回异常, {}'.format(res))
  261. logger.warning(odds_dict)
  262. else:
  263. logger.info('网球详细赔率列表为空, 不提交')
  264. reactor.callFromThread(out.callback, item)