wangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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])
  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 res.get('status') == 1:
  103. self.db.wq_league35.insert(league_data)
  104. # self.db.wq_league35.update({'lg_id': league_id}, {'$set': league_data},
  105. # upsert=True)
  106. logger.info('网球联赛提交成功, {}'.format(res))
  107. else:
  108. logger.warning('网球联赛提交失败, {}'.format(res))
  109. logger.warning(league_dict)
  110. else:
  111. logger.warning('网球联赛接口异常,提交失败, {}'.format(res))
  112. logger.warning(league_dict)
  113. else:
  114. logger.info('网球联赛已存在, 不提交')
  115. """赛事"""
  116. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  117. pt_status = pt_dict[pt]
  118. if pt == '3':
  119. is_rollball = 0
  120. is_today = 0
  121. is_morningplate = 0
  122. is_stringscene = 1
  123. elif pt == '2':
  124. is_rollball = 0
  125. is_today = 0
  126. is_morningplate = 1
  127. is_stringscene = 0
  128. else:
  129. is_today = 1
  130. is_rollball = 0
  131. is_morningplate = 0
  132. is_stringscene = 0
  133. if self.db.wq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  134. match_list = []
  135. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  136. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  137. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  138. "half_match_id", "is_today", 'rule', "is_horn"]
  139. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  140. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '', 0]
  141. match_data = dict(zip(match_key, match_value))
  142. match_list.append(match_data)
  143. match_dict['data'] = match_list
  144. res = Helper.async_post(MATCH_URL, match_dict)
  145. if res:
  146. if res.get('status') == 1:
  147. self.db.wq_competition35.insert(match_data)
  148. # self.db.wq_competition35.update({'match_id': match_id, pt_status: 1}, {'$set': match_data},
  149. # upsert=True)
  150. logger.info('网球赛事提交成功, {}'.format(res))
  151. else:
  152. logger.warning('网球赛事提交失败, {}'.format(res))
  153. logger.warning(match_data)
  154. else:
  155. logger.warning('网球赛事接口异常,提交失败, {}'.format(res))
  156. logger.warning(match_dict)
  157. else:
  158. logging.info('网球赛事已存在, 不提交')
  159. """赔率"""
  160. # try:
  161. # result = self.db.hg3535_wq_odds.find_one({'match_id': match_id, 'pt': pt})
  162. # odds_only_list = result['odds_only']
  163. # except:
  164. # result = ''
  165. # odds_only_list = []
  166. # wangqiu = item['wangqiu']
  167. data_list = []
  168. odds_onlys = []
  169. # 让盘
  170. if concedes_dict:
  171. for key, value in concedes_dict.items():
  172. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  173. sole = Helper.genearte_MD5(sole_str, pt)
  174. odds_str = 'LD' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  175. value)
  176. odds_only = Helper.genearte_MD5(odds_str, pt)
  177. condition = concedes_dict_rule[key]
  178. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  179. "odds_only", "sole", "source", "type", "team"]
  180. concedes_dict_value = [match_id, league_id, key, '0', 0, 'LD', value, condition,
  181. odds_only, sole, 'hg3535', '0', '']
  182. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  183. data_list.append(concedes_dict_data)
  184. # 让局
  185. if bureaus_dict:
  186. for key, value in bureaus_dict.items():
  187. sole_str = 'LB' + str(key) + str(match_id) + 'hg3535'
  188. sole = Helper.genearte_MD5(sole_str, pt)
  189. odds_str = 'LB' + str(key) + str(match_id) + 'hg3535' + str(bureaus_dict_rule[key]) + str(
  190. value)
  191. odds_only = Helper.genearte_MD5(odds_str, pt)
  192. condition = bureaus_dict_rule[key]
  193. bureaus_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  194. "odds_only", "sole", "source", "type", "team"]
  195. bureaus_dict_value = [match_id, league_id, key, '0', 0, 'LB', value, condition,
  196. odds_only, sole, 'hg3535', '0', '']
  197. bureaus_dict_data = dict(zip(bureaus_dict_key, bureaus_dict_value))
  198. data_list.append(bureaus_dict_data)
  199. # 总局数大小
  200. if total_number_dict:
  201. for key, value in total_number_dict.items():
  202. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  203. sole = Helper.genearte_MD5(sole_str, pt)
  204. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  205. total_number_dict_rule[key]) + str(value)
  206. odds_only = Helper.genearte_MD5(odds_str, pt)
  207. condition = total_number_dict_rule[key]
  208. total_number_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  209. "condition",
  210. "odds_only", "sole", "source", "type", "team"]
  211. total_number_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  212. odds_only, sole, 'hg3535', '0', '']
  213. total_number_dict_data = dict(zip(total_number_dict_key, total_number_dict_value))
  214. data_list.append(total_number_dict_data)
  215. # 总局数单双
  216. if odd_evens_dict:
  217. for key, value in odd_evens_dict.items():
  218. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  219. sole = Helper.genearte_MD5(sole_str, pt)
  220. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(odd_evens_dict_rule[key]) + str(
  221. value)
  222. odds_only = Helper.genearte_MD5(odds_str, pt)
  223. condition = odd_evens_dict_rule[key]
  224. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  225. "odds_only", "sole", "source", "type", "team"]
  226. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  227. odds_only, sole, 'hg3535', '0', '']
  228. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  229. data_list.append(odd_evens_dict_data)
  230. # 冠军
  231. if kemps_dict:
  232. for key, value in kemps_dict.items():
  233. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  234. sole = Helper.genearte_MD5(sole_str, pt)
  235. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + '2' + str(value)
  236. odds_only = Helper.genearte_MD5(odds_str, pt)
  237. kemps_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  238. "odds_only", "sole", "source", "type", "team"]
  239. kemps_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  240. odds_only, sole, 'hg3535', '0', '']
  241. kemps_dict_data = dict(zip(kemps_dict_key, kemps_dict_value))
  242. data_list.append(kemps_dict_data)
  243. if pt == '3':
  244. ris_stringscene = 1
  245. else:
  246. ris_stringscene = 0
  247. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  248. "is_stringscene", "utime", "pt"]
  249. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  250. ris_stringscene, utime, pt]
  251. odds_dict = dict(zip(odds_key, odds_value))
  252. if data_list:
  253. res = Helper.async_post(ODDS_URL, odds_dict)
  254. if res:
  255. if res.get('status') == 1:
  256. logger.info('网球赔率提交成功, {}'.format(res))
  257. logger.info(odds_dict)
  258. else:
  259. logger.warning('网球赔率提交失败, {}'.format(res))
  260. logger.warning(odds_dict)
  261. else:
  262. logger.warning('网球详细接口赔率返回异常, {}'.format(res))
  263. logger.warning(odds_dict)
  264. else:
  265. logger.info('网球详细赔率列表为空, 不提交')
  266. reactor.callFromThread(out.callback, item)