wangqiu.py 12 KB

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