roll_lanqiu.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  9. class Roll_Lanqiupipeline(object):
  10. def open_spider(self, spider):
  11. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST)
  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. # self._do_calculation(item)
  20. # return
  21. def _do_calculation(self, item, out):
  22. url = 'http://admin.5gogo.com'
  23. roll_lq = []
  24. # 使用twisted将mysql插入变成异步执行
  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 #home_team
  34. team_home = item['team_home']
  35. # 球队2 # guest_team
  36. team_guest = item['team_guest']
  37. # 数量(97>)
  38. number = item['number']
  39. # 比赛状态
  40. zhuangtai = item['zhuangtai']
  41. # 日期
  42. # data_game = item['data_game']
  43. result_mark = item['score_dict']
  44. try:
  45. data_game = item['data_game'].split("/")
  46. month = str(data_game[1].strip())
  47. day = str(data_game[0])
  48. except Exception as e:
  49. logger.warning(e)
  50. data_game = item['data_game'].split(" ")
  51. months = str(data_game[1].strip())
  52. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  53. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  54. month = month_dict[months]
  55. day = str(data_game[0])
  56. # 比赛时间
  57. time_game = str(item['time_game'])
  58. # 比赛时间,时间戳
  59. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  60. r_ctime = "2019" + "-" + month + "-" + day
  61. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60)) # 过期时间
  62. # 现在时间,时间戳
  63. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  64. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  65. # 队1分数
  66. score_home = item['score_home']
  67. # 队2分数
  68. score_guest = item['score_guest']
  69. # 第几节
  70. jijie = item['jijie']
  71. # 球队得分
  72. qiudui = item['qiudui']
  73. pt = 4
  74. match_score = "{}:{}".format(score_home, score_guest)
  75. concedes_dict = item['concede']
  76. concedes_dict_rule = item['concede_rule']
  77. odd_evens_dict = item['odd_even']
  78. odd_evens_dict_rule = item['odd_even_rule']
  79. total_sizes_dict = item['total_size']
  80. total_sizes_dict_rule = item['total_size_rule']
  81. last_numbers_dict = item['last_number']
  82. capots_dict = item['capot']
  83. team_scores_dict = item['team_score']
  84. team_scores_dict_rule = item['team_score_rule']
  85. last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year)
  86. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  87. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  88. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  89. league_list = []
  90. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  91. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  92. league_data = dict(zip(league_key, league_value))
  93. league_list.append(league_data)
  94. league_dict['data'] = league_list
  95. res = Helper.async_post(LEAGUE_URL, league_dict)
  96. if res:
  97. if '成功' in res:
  98. logger.info('篮球滚球联赛提交成功, {}'.format(res))
  99. self.db.hg3535_lq_league.insert(league_list)
  100. logger.info(league_dict)
  101. else:
  102. logger.warning('篮球滚球联赛提交失败, {}'.format(res))
  103. else:
  104. logger.warning('篮球滚球联赛接口, 提交失败, {}'.format(res))
  105. logger.warning(league_dict)
  106. else:
  107. logger.info('篮球滚球联赛已存在')
  108. if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  109. match_list = []
  110. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  111. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  112. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  113. "half_match_id", "is_today"]
  114. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, "00:00", number,
  115. "hg3535", 1, 0, 0, utime, uuid, 0, 0]
  116. match_data = dict(zip(match_kay, match_value))
  117. match_list.append(match_data)
  118. match_dict['data'] = match_list
  119. res = Helper.async_post(MATCH_URL, match_dict)
  120. if res:
  121. if '成功' in res:
  122. logger.info('篮球滚球赛事提交成功, {}'.format(res))
  123. self.db.hg3535_lq_competition.insert(match_list)
  124. logger.info(match_dict)
  125. else:
  126. logger.warning('篮球滚球赛事提交失败, {}'.format(res))
  127. logger.warning(match_dict)
  128. else:
  129. logger.warning('篮球滚球赛事接口异常,提交失败, {}'.format(res))
  130. logger.warning(match_dict)
  131. else:
  132. logger.info('篮球滚球赛事已存在')
  133. # 赔率
  134. data_list = []
  135. odds_onlys = []
  136. if concedes_dict:
  137. for key, value in concedes_dict.items():
  138. if value:
  139. for index, concede_value in enumerate(value):
  140. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  141. concede_value) + "hg3535" + str(match_id)
  142. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  143. odds_only = Helper.genearte_MD5(hash_str, pt)
  144. sole = Helper.genearte_MD5(sole_str, pt)
  145. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  146. "condition", "odds_only", "sole", "source", "type", "team"]
  147. condition = fuhao(concedes_dict_rule[key][index])
  148. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  149. odds_only, sole, "hg3535", "0", ""]
  150. concede_data = dict(zip(concede_key, concede_value))
  151. data_list.append(concede_data)
  152. if total_sizes_dict:
  153. for key, value in total_sizes_dict.items():
  154. if value:
  155. for index, total_sizes_value in enumerate(value):
  156. condition = fuhao(total_sizes_dict_rule[key][index])
  157. hash_str = "total_size" + str(key) + str(index) + str(condition) + str(
  158. total_sizes_value) + "hg3535" + str(match_id)
  159. sole_str = "total_size" + str(key) + str(index) + str(match_id) + "hg3535"
  160. odds_only = Helper.genearte_MD5(hash_str, pt)
  161. sole = Helper.genearte_MD5(sole_str, pt)
  162. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  163. "condition", "odds_only", "sole", "source", "type", "team"]
  164. total_sizes_value = [match_id, league_id, key, "0", index, 'total_size', total_sizes_value,
  165. condition, odds_only, sole, "hg3535", "0", ""]
  166. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  167. data_list.append(total_sizes_data)
  168. if odd_evens_dict:
  169. for key, value in odd_evens_dict.items():
  170. if value:
  171. condition = odd_evens_dict_rule[key]
  172. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  173. value) + "hg3535" + str(match_id)
  174. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  175. odds_only = Helper.genearte_MD5(hash_str, pt)
  176. sole = Helper.genearte_MD5(sole_str, pt)
  177. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  178. "condition", "odds_only", "sole", "source", "type", "team"]
  179. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  180. condition, odds_only, sole, "hg3535", "0", ""]
  181. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  182. data_list.append(odd_evens_data)
  183. lastnumber_dict = {'0或5': '_zero_five', '1或6': '_one_six', '2或7': '_two_seven', '3或8': '_three_eight',
  184. '4或9': '_four_nine'}
  185. last_home = last_numbers_dict['last_home']
  186. if last_home:
  187. for key, value in last_home.items():
  188. odds_code = 'last_home' + lastnumber_dict[key]
  189. condition = lastnumber_dict[key]
  190. hash_str = "LN" + odds_code + '0' + str(condition) + str(
  191. value) + "hg3535" + str(match_id)
  192. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  193. odds_only = Helper.genearte_MD5(hash_str, pt)
  194. sole = Helper.genearte_MD5(sole_str, pt)
  195. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  196. "condition", "odds_only", "sole", "source", "type", "team"]
  197. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  198. condition, odds_only, sole, "hg3535", "0", ""]
  199. last_home_data = dict(zip(last_home_key, last_home_value))
  200. data_list.append(last_home_data)
  201. last_guest = last_numbers_dict['last_guest']
  202. if last_guest:
  203. for key, value in last_guest.items():
  204. odds_code = 'last_guest' + lastnumber_dict[key]
  205. condition = lastnumber_dict[key]
  206. hash_str = "LN" + odds_code + '0' + str(condition) + str(value) + "hg3535" + str(match_id)
  207. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  208. odds_only = Helper.genearte_MD5(hash_str, pt)
  209. sole = Helper.genearte_MD5(sole_str, pt)
  210. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  211. "condition", "odds_only", "sole", "source", "type", "team"]
  212. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  213. condition, odds_only, sole, "hg3535", "0", ""]
  214. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  215. data_list.append(last_guest_data)
  216. if capots_dict:
  217. for key, value in capots_dict.items():
  218. if value:
  219. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  220. sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535"
  221. odds_only = Helper.genearte_MD5(hash_str, pt)
  222. sole = Helper.genearte_MD5(sole_str, pt)
  223. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  224. "condition", "odds_only", "sole", "source", "type", "team"]
  225. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  226. "", odds_only, sole, "hg3535", "0", ""]
  227. capots_data = dict(zip(capots_key, capots_value))
  228. data_list.append(capots_data)
  229. if team_scores_dict:
  230. for key, team_scores_value in team_scores_dict.items():
  231. if team_scores_value:
  232. for index, value in enumerate(team_scores_value):
  233. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  234. value) + "hg3535" + str(match_id)
  235. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  236. odds_only = Helper.genearte_MD5(hash_str, pt)
  237. sole = Helper.genearte_MD5(sole_str, pt)
  238. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  239. "condition", "odds_only", "sole", "source", "type", "team"]
  240. condition = team_scores_dict_rule[key][index]
  241. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  242. odds_only, sole, "hg3535", "0", ""]
  243. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  244. data_list.append(team_scores_data)
  245. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  246. "is_stringscene", "utime", "pt"]
  247. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid,
  248. 0, utime, pt]
  249. odds_dict = dict(zip(odds_key, odds_value))
  250. if data_list:
  251. res = Helper.async_post(ODDS_URL, odds_dict)
  252. if res:
  253. if "成功" in res:
  254. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  255. else:
  256. logger.warning('篮球滚球详细赔率提交失败, {}'.format(res))
  257. logger.warning(odds_dict)
  258. else:
  259. logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}'.format(res))
  260. logger.warning(odds_dict)
  261. else:
  262. logger.info('篮球滚球详细赔率列表, 不提交, {}'.format(res))
  263. data_list = []
  264. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  265. "lg_id": league_id, "home_rate": 0,
  266. "guest_rate": 0, "home_score": score_home,
  267. "guest_score": score_guest, "all_goal": "", "status": 1,
  268. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  269. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  270. "match_process": jijie, "tag": number, "result_mark": result_mark,
  271. "match_id": match_id, "p_code": ""}
  272. data_list.append(lq_rball)
  273. r_data_dict = {
  274. "game_code": "lq",
  275. "title": "match_result_r",
  276. "source": "hg3535",
  277. "data": data_list
  278. }
  279. if data_list:
  280. try:
  281. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  282. if "成功" in res:
  283. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  284. else:
  285. logger.warning('篮球滚球赛事结果记录提交失败, {}'.format(res))
  286. logger.warning(r_data_dict)
  287. except Exception as e:
  288. logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
  289. else:
  290. logger.info('篮球滚球赛事结果数据,为空不提交')
  291. reactor.callFromThread(out.callback, item)