roll_lanqiu.py 15 KB

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