lanqiu.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  10. class Lanqiupipeline(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. self.db = self.mongo[M_DB]
  14. @defer.inlineCallbacks
  15. def process_item(self, item, spider):
  16. out = defer.Deferred()
  17. reactor.callInThread(self._do_calculation, item, out)
  18. yield out
  19. defer.returnValue(item)
  20. # self._do_calculation(item)
  21. # return
  22. # def process_item(self, item, spider):
  23. def _do_calculation(self, item, out):
  24. logger = logging.getLogger(__name__)
  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. # 球队1
  33. team_home = item['team_home']
  34. # 球队2
  35. team_guest = item['team_guest']
  36. # 数量(97>)
  37. tag_number = item['number']
  38. # 比赛状态
  39. # zhuangtai = item['zhuangtai']
  40. # 日期
  41. try:
  42. data_game = item['data_game'].split("/")
  43. month = str(data_game[1].strip())
  44. day = str(data_game[0])
  45. except Exception as e:
  46. logger.warning(e)
  47. data_game = item['data_game'].split(" ")
  48. months = str(data_game[1].strip())
  49. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  50. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  51. month = month_dict[months]
  52. day = str(data_game[0])
  53. # 比赛时间
  54. time_game = str(item['time_game'])
  55. # 比赛时间,时间戳
  56. us_time = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  57. # r_ctime = "2019" + "-" + month + "-" + day
  58. # 现在时间,时间戳
  59. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  60. # expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  61. # # 队1分数
  62. # score_home = item['score_home']
  63. # # 队2分数
  64. # score_guest = item['score_guest']
  65. # # 第几节
  66. # jijie = item['jijie']
  67. # # 球队得分
  68. # qiudui = item['qiudui']
  69. pt = item['pt']
  70. # 让球
  71. concedes_dict = item['concede']
  72. concedes_dict_rule = item['concede_rule']
  73. # 总得分单双
  74. odd_evens_dict = item['odd_even']
  75. odd_evens_dict_rule = item['odd_even_rule']
  76. # 总比分大小
  77. total_sizes_dict = item['total_size']
  78. total_sizes_dict_rule = item['total_size_rule']
  79. # 球队得分最后一位数
  80. last_numbers_dict = item['last_number']
  81. # 独赢
  82. capots_dict = item['capot']
  83. # 球队得分大小
  84. team_scores_dict = item['team_score']
  85. team_scores_dict_rule = item['team_score_rule']
  86. # 让球
  87. # lanqiu = item['lanqiu']
  88. match_date, match_time, time3 = new_time(us_time)
  89. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  90. # 让球 数据插入数据库
  91. """联赛"""
  92. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  93. if self.db.hg3535_lq_league.find({'lg_id': league_id}).count() < 1:
  94. league_dict = {"game_code": "lq", "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 '成功' in res:
  104. logger.info('篮球联赛提交成功, {}'.format(res))
  105. self.db.hg3535_lq_league.insert(league_list)
  106. logger.info(league_dict)
  107. else:
  108. logger.warning('篮球联赛提交失败, {}'.format(res))
  109. else:
  110. logger.warning('篮球联赛接口, 提交失败, {}'.format(res))
  111. logger.warning(league_dict)
  112. else:
  113. logger.info('篮球联赛已存在')
  114. """赛事"""
  115. if pt == 3:
  116. is_rollball = 0
  117. is_today = 0
  118. is_morningplate = 0
  119. is_stringscene = 1
  120. elif pt == 2:
  121. is_rollball = 0
  122. is_today = 0
  123. is_morningplate = 1
  124. is_stringscene = 0
  125. else:
  126. is_today = 1
  127. is_rollball = 0
  128. is_morningplate = 0
  129. is_stringscene = 0
  130. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  131. pt_status = pt_dict[str(pt)]
  132. if self.db.lq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  133. match_list = []
  134. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  135. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  136. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  137. "half_match_id", "is_today", 'pt']
  138. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  139. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, pt]
  140. match_data = dict(zip(match_kay, match_value))
  141. match_list.append(match_data)
  142. match_dict['data'] = match_list
  143. res = Helper.async_post(MATCH_URL, match_dict)
  144. if res:
  145. if '成功' in res:
  146. logger.info('篮球赛事提交成功, {}'.format(res))
  147. self.db.hg3535_lq_competition.insert(match_list)
  148. logger.info(match_dict)
  149. else:
  150. logger.warning('篮球赛事提交失败, {}'.format(res))
  151. logger.warning(match_dict)
  152. else:
  153. logger.warning('篮球赛事接口异常,提交失败, {}'.format(res))
  154. logger.warning(match_dict)
  155. else:
  156. logger.info('篮球赛事已存在')
  157. """赔率"""
  158. data_list = []
  159. odds_onlys = []
  160. # 让球
  161. if concedes_dict:
  162. for key, value in concedes_dict.items():
  163. if value:
  164. for index, concedes_value in enumerate(value):
  165. sole_str = 'CO' + str(index) + str(match_id) + 'hg3535'
  166. sole = Helper.genearte_MD5(sole_str, pt)
  167. odds_str = 'CO' + str(index) + str(match_id) + 'hg3535' + str(
  168. concedes_dict_rule[key][index]) + str(concedes_value)
  169. odds_only = Helper.genearte_MD5(odds_str, pt)
  170. condition = str(concedes_dict_rule[key][index])
  171. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  172. "condition", "odds_only", "sole", "source", "type", "team"]
  173. concedes_dict_value = [match_id, league_id, key, '0', index, 'CO', concedes_value, condition,
  174. odds_only, sole, 'hg3535', '0', '']
  175. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  176. data_list.append(concedes_dict_data)
  177. else:
  178. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  179. sole = Helper.genearte_MD5(sole_str, pt)
  180. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(
  181. concedes_dict_rule[key]) + str(value)
  182. odds_only = Helper.genearte_MD5(odds_str, pt)
  183. condition = str(concedes_dict_rule[key])
  184. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  185. "condition", "odds_only", "sole", "source", "type", "team"]
  186. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  187. odds_only, sole, 'hg3535', '0', '']
  188. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  189. data_list.append(concedes_dict_data)
  190. # 总得分单双
  191. if odd_evens_dict:
  192. for key, value in odd_evens_dict.items():
  193. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  194. sole = Helper.genearte_MD5(sole_str, pt)
  195. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  196. odd_evens_dict_rule[key]) + str(value)
  197. odds_only = Helper.genearte_MD5(odds_str, pt)
  198. condition = str(odd_evens_dict_rule[key])
  199. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  200. "condition", "odds_only", "sole", "source", "type", "team"]
  201. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  202. odds_only, sole, 'hg3535', '0', '']
  203. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  204. data_list.append(odd_evens_dict_data)
  205. # 总比分大小
  206. if total_sizes_dict:
  207. for key, value in total_sizes_dict.items():
  208. if value:
  209. for index, total_sizes_value in enumerate(value):
  210. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  211. sole = Helper.genearte_MD5(sole_str, pt)
  212. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  213. total_sizes_dict_rule[key][index]) + str(total_sizes_value)
  214. odds_only = Helper.genearte_MD5(odds_str, pt)
  215. condition = str(total_sizes_dict_rule[key][index])
  216. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  217. "condition", "odds_only", "sole", "source", "type", "team"]
  218. total_sizes_value = [match_id, league_id, key, '0', index, 'TN', total_sizes_value, condition,
  219. odds_only, sole, 'hg3535', '0', '']
  220. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  221. data_list.append(total_sizes_data)
  222. else:
  223. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  224. sole = Helper.genearte_MD5(sole_str, pt)
  225. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  226. total_sizes_dict_rule[key]) + str(value)
  227. odds_only = Helper.genearte_MD5(odds_str, pt)
  228. condition = str(total_sizes_dict_rule[key])
  229. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  230. "condition", "odds_only", "sole", "source", "type", "team"]
  231. total_sizes_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  232. odds_only, sole, 'hg3535', '0', '']
  233. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  234. data_list.append(total_sizes_data)
  235. # 球队得分大小
  236. if team_scores_dict:
  237. for key, value in team_scores_dict.items():
  238. sole_str = 'TB' + str(key) + str(match_id) + 'hg3535'
  239. sole = Helper.genearte_MD5(sole_str, pt)
  240. odds_str = 'TB' + str(key) + str(match_id) + 'hg3535' + str(
  241. team_scores_dict_rule[key]) + str(value)
  242. odds_only = Helper.genearte_MD5(odds_str, pt)
  243. condition = str(team_scores_dict_rule[key])
  244. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  245. "condition", "odds_only", "sole", "source", "type", "team"]
  246. team_scores_value = [match_id, league_id, key, '0', 0, 'TB', value, condition,
  247. odds_only, sole, 'hg3535', '0', '']
  248. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  249. data_list.append(team_scores_data)
  250. # 球队得分最后一位数
  251. last_home_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  252. last_home = last_numbers_dict['last_home']
  253. if last_home:
  254. for key, value in last_home.items():
  255. sole_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535'
  256. sole = Helper.genearte_MD5(sole_str, pt)
  257. odds_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535' + '0' + str(value)
  258. odds_only = Helper.genearte_MD5(odds_str, pt)
  259. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  260. "condition", "odds_only", "sole", "source", "type", "team"]
  261. last_home_value = [match_id, league_id, last_home_dict[key], '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  262. last_home_data = dict(zip(last_home_key, last_home_value))
  263. data_list.append(last_home_data)
  264. last_guest = last_numbers_dict['last_guest']
  265. last_guest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  266. if last_guest:
  267. for key, value in last_guest.items():
  268. sole_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535'
  269. sole = Helper.genearte_MD5(sole_str, pt)
  270. odds_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535' + '0' + str(value)
  271. odds_only = Helper.genearte_MD5(odds_str, pt)
  272. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  273. "condition", "odds_only", "sole", "source", "type", "team"]
  274. last_guest_value = [match_id, league_id, key, '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  275. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  276. data_list.append(last_guest_data)
  277. # 独赢
  278. if capots_dict:
  279. for key, value in capots_dict.items():
  280. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  281. sole = Helper.genearte_MD5(sole_str, pt)
  282. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  283. odds_only = Helper.genearte_MD5(odds_str, pt)
  284. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  285. "condition", "odds_only", "sole", "source", "type", "team"]
  286. capots_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  287. odds_only, sole, 'hg3535', '0', '']
  288. capots_data = dict(zip(capots_key, capots_value))
  289. data_list.append(capots_data)
  290. if pt == 3:
  291. ris_stringscene = 1
  292. else:
  293. ris_stringscene = 0
  294. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  295. "is_stringscene", "utime", "pt"]
  296. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  297. ris_stringscene, utime, pt]
  298. odds_dict = dict(zip(odds_key, odds_value))
  299. if data_list:
  300. res = Helper.async_post(ODDS_URL, odds_dict)
  301. if res:
  302. if '成功' in res:
  303. logger.info('篮球详细赔率提交成功, {}'.format(res))
  304. logger.info(odds_dict)
  305. else:
  306. logger.warning('篮球详细赔率提交成功, {}'.format(res))
  307. logger.warning(odds_dict)
  308. else:
  309. logger.warning('篮球详细赔率接口异常, 提交成功, {}'.format(res))
  310. logger.warning(odds_dict)
  311. else:
  312. logger.info('详细赔率列表为空')
  313. reactor.callFromThread(out.callback, item)