wangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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
  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]).strip()
  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]).strip()
  55. year = datetime.datetime.now().year
  56. # 比赛时间
  57. time_game = str(item['time_game'])
  58. # 比赛时间,时间戳
  59. ctime = str(year) + "-" + month + "-" + day + " " + time_game + ":00"
  60. r_ctime = str(year) + "-" + month + "-" + day
  61. # 现在时间,时间戳
  62. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  63. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  64. # 队1分数
  65. # score_home = item['score_home']
  66. # 队2分数
  67. # score_guest = item['score_guest']
  68. # 第几节
  69. # jijie = item['jijie']
  70. # 球队得分
  71. # qiudui = item['qiudui']
  72. pt = str(item['pt'])
  73. # 让盘
  74. concedes_dict = item['concedes_dict']
  75. concedes_dict_rule = item['concedes_dict_rule']
  76. # 冠军
  77. kemps_dict = item['kemps_dict']
  78. # 让局
  79. bureaus_dict = item['bureaus_dict']
  80. bureaus_dict_rule = item['bureaus_dict_rule']
  81. # 总局数大小
  82. total_number_dict = item['total_number_dict']
  83. total_number_dict_rule = item['total_number_dict_rule']
  84. # 总决赛单双
  85. odd_evens_dict = item['odd_evens_dict']
  86. odd_evens_dict_rule = item['odd_evens_dict_rule']
  87. us_time = ctime
  88. match_date, match_time, time3 = new_time(ctime)
  89. # n_time = out_time(time3, 3)
  90. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  91. """联赛"""
  92. last_time = '{}-12-31 23:59:59'.format(year)
  93. if self.db.wq_league35.find({'uuid': uuid}).count() < 1:
  94. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  95. league_list = []
  96. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  97. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  98. league_data = dict(zip(league_key, league_value))
  99. league_list.append(league_data)
  100. league_dict['data'] = league_list
  101. res = Helper.async_post(LEAGUE_URL, league_dict)
  102. if res:
  103. if res.get('status') == 1:
  104. self.db.wq_league35.insert(league_data)
  105. # self.db.wq_league35.update({'lg_id': league_id}, {'$set': league_data},
  106. # upsert=True)
  107. logger.info('网球联赛提交成功, {}'.format(res))
  108. else:
  109. logger.warning('网球联赛提交失败, {}'.format(res))
  110. # logger.warning(league_dict)
  111. else:
  112. logger.warning('网球联赛接口异常,提交失败, {}'.format(res))
  113. # logger.warning(league_dict)
  114. else:
  115. logger.info('网球联赛已存在, 不提交')
  116. """赛事"""
  117. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  118. pt_status = pt_dict[pt]
  119. if pt == '3':
  120. is_rollball = 0
  121. is_today = 0
  122. is_morningplate = 0
  123. is_stringscene = 1
  124. elif pt == '2':
  125. is_rollball = 0
  126. is_today = 0
  127. is_morningplate = 1
  128. is_stringscene = 0
  129. else:
  130. is_today = 1
  131. is_rollball = 0
  132. is_morningplate = 0
  133. is_stringscene = 0
  134. match_identity = Helper.genearte_uuid(team_home + team_guest + r_ctime)
  135. if self.db.wq_competition35.find({'match_identity': match_identity, pt_status: 1}).count() < 1:
  136. match_list = []
  137. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  138. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  139. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  140. "half_match_id", "is_today", 'rule', "is_horn", "match_identity"]
  141. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  142. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '', 0, match_identity]
  143. match_data = dict(zip(match_key, match_value))
  144. match_list.append(match_data)
  145. match_dict['data'] = match_list
  146. res = Helper.async_post(MATCH_URL, match_dict)
  147. if res:
  148. if res.get('status') == 1:
  149. self.db.wq_competition35.insert(match_data)
  150. # self.db.wq_competition35.update({'match_id': match_id, pt_status: 1}, {'$set': match_data},
  151. # upsert=True)
  152. logger.info('网球赛事提交成功, {}'.format(res))
  153. else:
  154. logger.warning('网球赛事提交失败, {}'.format(res))
  155. # logger.warning(match_data)
  156. else:
  157. logger.warning('网球赛事接口异常,提交失败, {}'.format(res))
  158. # logger.warning(match_dict)
  159. else:
  160. logging.info('网球赛事已存在, 不提交')
  161. """赔率"""
  162. data_list = []
  163. odds_onlys = []
  164. # 让盘
  165. if concedes_dict:
  166. for key, value in concedes_dict.items():
  167. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  168. sole = Helper.genearte_MD5(sole_str, pt)
  169. odds_str = 'LD' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  170. value)
  171. odds_only = Helper.genearte_MD5(odds_str, pt)
  172. condition = concedes_dict_rule[key]
  173. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  174. "odds_only", "sole", "source", "type", "team"]
  175. concedes_dict_value = [match_id, league_id, key, '0', 0, 'LD', value, condition,
  176. odds_only, sole, 'hg3535', '0', '']
  177. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  178. data_list.append(concedes_dict_data)
  179. # 让局
  180. if bureaus_dict:
  181. for key, value in bureaus_dict.items():
  182. sole_str = 'LB' + str(key) + str(match_id) + 'hg3535'
  183. sole = Helper.genearte_MD5(sole_str, pt)
  184. odds_str = 'LB' + str(key) + str(match_id) + 'hg3535' + str(bureaus_dict_rule[key]) + str(
  185. value)
  186. odds_only = Helper.genearte_MD5(odds_str, pt)
  187. condition = bureaus_dict_rule[key]
  188. bureaus_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  189. "odds_only", "sole", "source", "type", "team"]
  190. bureaus_dict_value = [match_id, league_id, key, '0', 0, 'LB', value, condition,
  191. odds_only, sole, 'hg3535', '0', '']
  192. bureaus_dict_data = dict(zip(bureaus_dict_key, bureaus_dict_value))
  193. data_list.append(bureaus_dict_data)
  194. # 总局数大小
  195. if total_number_dict:
  196. for key, value in total_number_dict.items():
  197. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  198. sole = Helper.genearte_MD5(sole_str, pt)
  199. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  200. total_number_dict_rule[key]) + str(value)
  201. odds_only = Helper.genearte_MD5(odds_str, pt)
  202. condition = total_number_dict_rule[key]
  203. total_number_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  204. "condition",
  205. "odds_only", "sole", "source", "type", "team"]
  206. total_number_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  207. odds_only, sole, 'hg3535', '0', '']
  208. total_number_dict_data = dict(zip(total_number_dict_key, total_number_dict_value))
  209. data_list.append(total_number_dict_data)
  210. # 总局数单双
  211. if odd_evens_dict:
  212. for key, value in odd_evens_dict.items():
  213. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  214. sole = Helper.genearte_MD5(sole_str, pt)
  215. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(odd_evens_dict_rule[key]) + str(
  216. value)
  217. odds_only = Helper.genearte_MD5(odds_str, pt)
  218. condition = odd_evens_dict_rule[key]
  219. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  220. "odds_only", "sole", "source", "type", "team"]
  221. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  222. odds_only, sole, 'hg3535', '0', '']
  223. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  224. data_list.append(odd_evens_dict_data)
  225. # 冠军
  226. if kemps_dict:
  227. for key, value in kemps_dict.items():
  228. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  229. sole = Helper.genearte_MD5(sole_str, pt)
  230. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + '2' + str(value)
  231. odds_only = Helper.genearte_MD5(odds_str, pt)
  232. kemps_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  233. "odds_only", "sole", "source", "type", "team"]
  234. kemps_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  235. odds_only, sole, 'hg3535', '0', '']
  236. kemps_dict_data = dict(zip(kemps_dict_key, kemps_dict_value))
  237. data_list.append(kemps_dict_data)
  238. if pt == '3':
  239. ris_stringscene = 1
  240. else:
  241. ris_stringscene = 0
  242. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  243. "is_stringscene", "utime", "pt", 'match_identity']
  244. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  245. ris_stringscene, utime, pt, match_identity]
  246. odds_dict = dict(zip(odds_key, odds_value))
  247. if data_list:
  248. res = Helper.async_post(ODDS_URL, odds_dict)
  249. if res:
  250. if res.get('status') == 1:
  251. logger.info('网球赔率提交成功, {}'.format(res))
  252. # logger.info(odds_dict)
  253. else:
  254. logger.warning('网球赔率提交失败, {}'.format(res))
  255. # logger.warning(odds_dict)
  256. else:
  257. logger.warning('网球详细接口赔率返回异常, {}'.format(res))
  258. # logger.warning(odds_dict)
  259. else:
  260. logger.info('网球详细赔率列表为空, 不提交')
  261. reactor.callFromThread(out.callback, item)