lanqiu.py 16 KB

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