lanqiu.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. # def process_item(self, item, spider):
  21. def _do_calculation(self, item, out):
  22. logger = logging.getLogger(__name__)
  23. # 联赛id
  24. league_id = item['league_id']
  25. # 联赛名
  26. league_name = item['league_name']
  27. # 比赛id
  28. match_id = item['game_id']
  29. # 球队1
  30. team_home = item['team_home']
  31. # 球队2
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. tag_number = item['number']
  35. # 日期
  36. try:
  37. data_game = item['data_game'].split("/")
  38. month = str(data_game[1].strip())
  39. day = str(data_game[0])
  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])
  48. # 比赛时间
  49. time_game = str(item['time_game'])
  50. # 比赛时间,时间戳
  51. us_time = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  52. # 现在时间,时间戳
  53. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  54. pt = str(item['pt'])
  55. # 让球
  56. concedes_dict = item['concede']
  57. concedes_dict_rule = item['concede_rule']
  58. # 总得分单双
  59. odd_evens_dict = item['odd_even']
  60. odd_evens_dict_rule = item['odd_even_rule']
  61. # 总比分大小
  62. total_sizes_dict = item['total_size']
  63. total_sizes_dict_rule = item['total_size_rule']
  64. # 球队得分最后一位数
  65. last_numbers_dict = item['last_number']
  66. # 独赢
  67. capots_dict = item['capot']
  68. # 球队得分大小
  69. team_scores_dict = item['team_score']
  70. team_scores_dict_rule = item['team_score_rule']
  71. match_date, match_time, time3 = new_time(us_time)
  72. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  73. # 让球 数据插入数据库
  74. """联赛"""
  75. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  76. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  77. league_dict = {"game_code": "lq", "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 '成功' in res:
  87. logger.info('篮球联赛提交成功, {}'.format(res))
  88. # self.db.hg3535_lq_league.insert(league_data)
  89. self.db.lq_league35.update({'lg_id': league_id}, {'$set': league_data}, upsert=True)
  90. logger.info(league_dict)
  91. else:
  92. logger.warning('篮球联赛提交失败, {}'.format(res))
  93. else:
  94. logger.warning('篮球联赛接口, 提交失败, {}'.format(res))
  95. logger.warning(league_dict)
  96. else:
  97. logger.info('篮球联赛已存在')
  98. """赛事"""
  99. if pt == '3':
  100. is_rollball = 0
  101. is_today = 0
  102. is_morningplate = 0
  103. is_stringscene = 1
  104. elif pt == '2':
  105. is_rollball = 0
  106. is_today = 0
  107. is_morningplate = 1
  108. is_stringscene = 0
  109. else:
  110. is_today = 1
  111. is_rollball = 0
  112. is_morningplate = 0
  113. is_stringscene = 0
  114. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  115. pt_status = pt_dict[str(pt)]
  116. if self.db.lq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  117. match_list = []
  118. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  119. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  120. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  121. "half_match_id", "is_today", 'pt', "is_horn"]
  122. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  123. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, pt, 0]
  124. match_data = dict(zip(match_kay, match_value))
  125. match_list.append(match_data)
  126. match_dict['data'] = match_list
  127. res = Helper.async_post(MATCH_URL, match_dict)
  128. if res:
  129. if '成功' in res:
  130. logger.info('篮球赛事提交成功, {}'.format(res))
  131. # self.db.hg3535_lq_competition.insert(match_data)
  132. self.db.lq_competition35.update({'match_id': match_id, pt_status: 1}, {'$set': match_data}, upsert=True)
  133. logger.info(match_dict)
  134. else:
  135. logger.warning('篮球赛事提交失败, {}'.format(res))
  136. logger.warning(match_dict)
  137. else:
  138. logger.warning('篮球赛事接口异常,提交失败, {}'.format(res))
  139. logger.warning(match_dict)
  140. else:
  141. logger.info('篮球赛事已存在')
  142. """赔率"""
  143. data_list = []
  144. odds_onlys = []
  145. # 让球
  146. if concedes_dict:
  147. for key, value in concedes_dict.items():
  148. if value:
  149. for index, concedes_value in enumerate(value):
  150. sole_str = 'CO' + str(index) + str(match_id) + 'hg3535'
  151. sole = Helper.genearte_MD5(sole_str, pt)
  152. odds_str = 'CO' + str(index) + str(match_id) + 'hg3535' + str(
  153. concedes_dict_rule[key][index]) + str(concedes_value)
  154. odds_only = Helper.genearte_MD5(odds_str, pt)
  155. condition = str(concedes_dict_rule[key][index])
  156. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  157. "condition", "odds_only", "sole", "source", "type", "team"]
  158. concedes_dict_value = [match_id, league_id, key, '0', index, 'CO', concedes_value, condition,
  159. odds_only, sole, 'hg3535', '0', '']
  160. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  161. data_list.append(concedes_dict_data)
  162. else:
  163. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  164. sole = Helper.genearte_MD5(sole_str, pt)
  165. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(
  166. concedes_dict_rule[key]) + str(value)
  167. odds_only = Helper.genearte_MD5(odds_str, pt)
  168. condition = str(concedes_dict_rule[key])
  169. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  170. "condition", "odds_only", "sole", "source", "type", "team"]
  171. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  172. odds_only, sole, 'hg3535', '0', '']
  173. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  174. data_list.append(concedes_dict_data)
  175. # 总得分单双
  176. if odd_evens_dict:
  177. for key, value in odd_evens_dict.items():
  178. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  179. sole = Helper.genearte_MD5(sole_str, pt)
  180. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  181. odd_evens_dict_rule[key]) + str(value)
  182. odds_only = Helper.genearte_MD5(odds_str, pt)
  183. condition = str(odd_evens_dict_rule[key])
  184. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  185. "condition", "odds_only", "sole", "source", "type", "team"]
  186. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  187. odds_only, sole, 'hg3535', '0', '']
  188. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  189. data_list.append(odd_evens_dict_data)
  190. # 总比分大小
  191. if total_sizes_dict:
  192. for key, value in total_sizes_dict.items():
  193. if value:
  194. for index, total_sizes_value in enumerate(value):
  195. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  196. sole = Helper.genearte_MD5(sole_str, pt)
  197. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  198. total_sizes_dict_rule[key][index]) + str(total_sizes_value)
  199. odds_only = Helper.genearte_MD5(odds_str, pt)
  200. condition = str(total_sizes_dict_rule[key][index])
  201. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  202. "condition", "odds_only", "sole", "source", "type", "team"]
  203. total_sizes_value = [match_id, league_id, key, '0', index, 'TN', total_sizes_value, condition,
  204. odds_only, sole, 'hg3535', '0', '']
  205. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  206. data_list.append(total_sizes_data)
  207. else:
  208. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  209. sole = Helper.genearte_MD5(sole_str, pt)
  210. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  211. total_sizes_dict_rule[key]) + str(value)
  212. odds_only = Helper.genearte_MD5(odds_str, pt)
  213. condition = str(total_sizes_dict_rule[key])
  214. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  215. "condition", "odds_only", "sole", "source", "type", "team"]
  216. total_sizes_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  217. odds_only, sole, 'hg3535', '0', '']
  218. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  219. data_list.append(total_sizes_data)
  220. # 球队得分大小
  221. if team_scores_dict:
  222. for key, value in team_scores_dict.items():
  223. sole_str = 'TB' + str(key) + str(match_id) + 'hg3535'
  224. sole = Helper.genearte_MD5(sole_str, pt)
  225. odds_str = 'TB' + str(key) + str(match_id) + 'hg3535' + str(
  226. team_scores_dict_rule[key]) + str(value)
  227. odds_only = Helper.genearte_MD5(odds_str, pt)
  228. condition = str(team_scores_dict_rule[key])
  229. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  230. "condition", "odds_only", "sole", "source", "type", "team"]
  231. team_scores_value = [match_id, league_id, key, '0', 0, 'TB', value, condition,
  232. odds_only, sole, 'hg3535', '0', '']
  233. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  234. data_list.append(team_scores_data)
  235. # 球队得分最后一位数
  236. last_home_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  237. last_home = last_numbers_dict['last_home']
  238. if last_home:
  239. for key, value in last_home.items():
  240. sole_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535'
  241. sole = Helper.genearte_MD5(sole_str, pt)
  242. odds_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535' + '0' + str(value)
  243. odds_only = Helper.genearte_MD5(odds_str, pt)
  244. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  245. "condition", "odds_only", "sole", "source", "type", "team"]
  246. last_home_value = [match_id, league_id, last_home_dict[key], '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  247. last_home_data = dict(zip(last_home_key, last_home_value))
  248. data_list.append(last_home_data)
  249. last_guest = last_numbers_dict['last_guest']
  250. last_guest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  251. if last_guest:
  252. for key, value in last_guest.items():
  253. sole_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535'
  254. sole = Helper.genearte_MD5(sole_str, pt)
  255. odds_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535' + '0' + str(value)
  256. odds_only = Helper.genearte_MD5(odds_str, pt)
  257. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  258. "condition", "odds_only", "sole", "source", "type", "team"]
  259. last_guest_value = [match_id, league_id, key, '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  260. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  261. data_list.append(last_guest_data)
  262. # 独赢
  263. if capots_dict:
  264. for key, value in capots_dict.items():
  265. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  266. sole = Helper.genearte_MD5(sole_str, pt)
  267. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  268. odds_only = Helper.genearte_MD5(odds_str, pt)
  269. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  270. "condition", "odds_only", "sole", "source", "type", "team"]
  271. capots_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  272. odds_only, sole, 'hg3535', '0', '']
  273. capots_data = dict(zip(capots_key, capots_value))
  274. data_list.append(capots_data)
  275. if pt == '3':
  276. ris_stringscene = 1
  277. else:
  278. ris_stringscene = 0
  279. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  280. "is_stringscene", "utime", "pt"]
  281. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  282. ris_stringscene, utime, pt]
  283. odds_dict = dict(zip(odds_key, odds_value))
  284. if data_list:
  285. res = Helper.async_post(ODDS_URL, odds_dict)
  286. if res:
  287. if '成功' in res:
  288. logger.info('篮球详细赔率提交成功, {}'.format(res))
  289. logger.info(odds_dict)
  290. else:
  291. logger.warning('篮球详细赔率提交成功, {}'.format(res))
  292. logger.warning(odds_dict)
  293. else:
  294. logger.warning('篮球详细赔率接口异常, 提交成功, {}'.format(res))
  295. logger.warning(odds_dict)
  296. else:
  297. logger.info('详细赔率列表为空')
  298. reactor.callFromThread(out.callback, item)