lanqiu.py 16 KB

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