roll_lanqiu.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import pymongo
  2. import time
  3. import logging
  4. from twisted.internet import defer, reactor
  5. from .ball_func import fuhao
  6. from ..utils.helper import Helper
  7. # from .ball_func import get_pcode, new_time
  8. class Roll_Lanqiupipeline(object):
  9. def open_spider(self, spider):
  10. self.mongo = pymongo.MongoClient(host="192.168.2.200", username="kaiyou", password="kaiyou", port=27017)
  11. self.db = self.mongo['kaiyou']
  12. def process_item(self, item, spider):
  13. out = defer.Deferred()
  14. reactor.callInThread(self._do_calculation, item, out)
  15. yield out
  16. defer.returnValue(item)
  17. def _do_calculation(self, item, out):
  18. url = 'http://admin.5gogo.com'
  19. roll_lq = []
  20. # 使用twisted将mysql插入变成异步执行
  21. logger = logging.getLogger(__name__)
  22. # 联赛id
  23. league_id = item['league_id']
  24. # 联赛名
  25. league_name = item['league_name']
  26. # result = item['result']
  27. # 比赛id
  28. match_id = item['game_id']
  29. # 球队1 #home_team
  30. team_home = item['team_home']
  31. # 球队2 # guest_team
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. number = item['number']
  35. # 比赛状态
  36. zhuangtai = item['zhuangtai']
  37. # 日期
  38. # data_game = item['data_game']
  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. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  55. r_ctime = "2019" + "-" + month + "-" + day
  56. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60)) # 过期时间
  57. # 现在时间,时间戳
  58. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  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 = 4
  68. match_score = "{}:{}".format(score_home, score_guest)
  69. concedes_dict = item['concede']
  70. concedes_dict_rule = item['concede_rule']
  71. odd_evens_dict = item['odd_even']
  72. odd_evens_dict_rule = item['odd_even_rule']
  73. total_sizes_dict = item['total_size']
  74. total_sizes_dict_rule = item['total_size_rule']
  75. last_numbers_dict = item['last_number']
  76. capots_dict = item['capot']
  77. team_scores_dict = item['team_score']
  78. team_scores_dict_rule = item['team_score_rule']
  79. last_time = "2019-12-31 23:59:59"
  80. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  81. league_list = []
  82. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  83. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  84. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  85. league_data = dict(zip(league_key, league_value))
  86. league_list.append(league_data)
  87. league_dict['data'] = league_list
  88. if self.db.hg3535_lq_league.find({'lg_id': league_id}).count() < 1:
  89. response_data = Helper.async_post(url + '/setLeague', league_dict)
  90. print(response_data)
  91. self.db.hg3535_lq_league.insert(league_list)
  92. competition = self.db.hg3535_lq_competition.find_one({"match_id": match_id})
  93. match_list = []
  94. if competition:
  95. print('篮球赛事已存在,修改篮球赛事四状态')
  96. is_morningplate = competition['is_morningplate']
  97. is_rollball = competition['is_rollball']
  98. is_stringscene = competition['is_stringscene']
  99. is_today = competition['is_today']
  100. status = competition['status']
  101. match_date = competition['match_date']
  102. match_time = competition['match_time']
  103. us_time = competition['us_time']
  104. if is_rollball != 1 or status != 1:
  105. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  106. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  107. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  108. "half_match_id", "is_today"]
  109. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, number,
  110. "hg3535", 1, is_morningplate, is_stringscene, us_time, uuid, 0, is_today]
  111. match_data = dict(zip(match_kay, match_value))
  112. match_list.append(match_data)
  113. match_dict['data'] = match_list
  114. response_data = Helper.async_post(url + '/setMatch', match_dict)
  115. self.db.hg3535_lq_competition.insert(match_list)
  116. print(response_data)
  117. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  118. {"$set": {"is_rollball": 1, "status": 1}},
  119. upsert=True)
  120. else:
  121. print("篮球赛事已存在,篮球赛事状态时间未改变,不做修改")
  122. else:
  123. is_rollball = 1
  124. is_today = 0
  125. is_morningplate = 0
  126. is_stringscene = 0
  127. # 赛事接口写入
  128. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  129. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  130. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  131. "half_match_id", "is_today"]
  132. match_value = [team_home, team_guest, league_id, 1, match_id, r_ctime, "00:00", number, "hg3535",
  133. is_rollball, is_morningplate, is_stringscene, utime, uuid, 0, is_today]
  134. match_data = dict(zip(match_kay, match_value))
  135. match_list.append(match_data)
  136. match_dict['data'] = match_list
  137. response_data = Helper.async_post(url + '/setMatch', match_dict)
  138. self.db.hg3535_lq_competition.insert(match_list)
  139. print(response_data)
  140. # 赔率
  141. try:
  142. result = self.db.hg3535_lq_odds.find_one({'match_id': match_id, 'pt': pt})
  143. uuid_list = result['odds_only']
  144. except:
  145. uuid_list = []
  146. result = ''
  147. data_list = []
  148. odds_onlys = []
  149. if concedes_dict:
  150. for key, value in concedes_dict.items():
  151. if value:
  152. for index, concede_value in enumerate(value):
  153. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  154. concede_value) + "hg3535" + str(match_id)
  155. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  156. odds_only = Helper.genearte_MD5(hash_str, pt)
  157. sole = Helper.genearte_MD5(sole_str, pt)
  158. odds_onlys.append(odds_only)
  159. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  160. "condition", "odds_only", "sole", "source", "type", "team"]
  161. condition = fuhao(concedes_dict_rule[key][index])
  162. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  163. odds_only, sole, "hg3535", "0", ""]
  164. concede_data = dict(zip(concede_key, concede_value))
  165. if odds_only not in uuid_list:
  166. data_list.append(concede_data)
  167. if total_sizes_dict:
  168. for key, value in total_sizes_dict.items():
  169. if value:
  170. for index, total_sizes_value in enumerate(value):
  171. condition = fuhao(total_sizes_dict_rule[key][index])
  172. hash_str = "total_size" + str(key) + str(index) + str(condition) + str(
  173. total_sizes_value) + "hg3535" + str(match_id)
  174. sole_str = "total_size" + str(key) + str(index) + str(match_id) + "hg3535"
  175. odds_only = Helper.genearte_MD5(hash_str, pt)
  176. sole = Helper.genearte_MD5(sole_str, pt)
  177. odds_onlys.append(odds_only)
  178. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  179. "condition", "odds_only", "sole", "source", "type", "team"]
  180. total_sizes_value = [match_id, league_id, key, "0", index, 'total_size', total_sizes_value,
  181. condition,
  182. odds_only, sole, "hg3535", "0", ""]
  183. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  184. if odds_only not in uuid_list:
  185. data_list.append(total_sizes_data)
  186. if odd_evens_dict:
  187. for key, value in odd_evens_dict.items():
  188. if value:
  189. condition = odd_evens_dict_rule[key]
  190. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  191. value) + "hg3535" + str(match_id)
  192. sole_str = "TS" + 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. odds_onlys.append(odds_only)
  196. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  197. "condition", "odds_only", "sole", "source", "type", "team"]
  198. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  199. condition, odds_only, sole, "hg3535", "0", ""]
  200. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  201. if odds_only not in uuid_list:
  202. data_list.append(odd_evens_data)
  203. lastnumber_dict = {'0或5': '_zero_five', '1或6': '_one_six', '2或7': '_two_seven', '3或8': '_three_eight',
  204. '4或9': '_four_nine'}
  205. last_home = last_numbers_dict['last_home']
  206. if last_home:
  207. for key, value in last_home.items():
  208. # pass
  209. odds_code = 'last_home' + lastnumber_dict[key]
  210. condition = lastnumber_dict[key]
  211. hash_str = "LN" + odds_code + '0' + str(condition) + str(
  212. value) + "hg3535" + str(match_id)
  213. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  214. odds_only = Helper.genearte_MD5(hash_str, pt)
  215. sole = Helper.genearte_MD5(sole_str, pt)
  216. odds_onlys.append(odds_only)
  217. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  218. "condition", "odds_only", "sole", "source", "type", "team"]
  219. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  220. condition, odds_only, sole, "hg3535", "0", ""]
  221. last_home_data = dict(zip(last_home_key, last_home_value))
  222. if odds_only not in uuid_list:
  223. data_list.append(last_home_data)
  224. last_guest = last_numbers_dict['last_guest']
  225. if last_guest:
  226. for key, value in last_guest.items():
  227. odds_code = 'last_guest' + lastnumber_dict[key]
  228. condition = lastnumber_dict[key]
  229. hash_str = "LN" + odds_code + '0' + str(condition) + str(value) + "hg3535" + str(match_id)
  230. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  231. odds_only = Helper.genearte_MD5(hash_str, pt)
  232. sole = Helper.genearte_MD5(sole_str, pt)
  233. odds_onlys.append(odds_only)
  234. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  235. "condition", "odds_only", "sole", "source", "type", "team"]
  236. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  237. condition, odds_only, sole, "hg3535", "0", ""]
  238. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  239. if odds_only not in uuid_list:
  240. data_list.append(last_guest_data)
  241. if capots_dict:
  242. for key, value in capots_dict.items():
  243. if value:
  244. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  245. sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535"
  246. odds_only = Helper.genearte_MD5(hash_str, pt)
  247. sole = Helper.genearte_MD5(sole_str, pt)
  248. odds_onlys.append(odds_only)
  249. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  250. "condition", "odds_only", "sole", "source", "type", "team"]
  251. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  252. "", odds_only, sole, "hg3535", "0", ""]
  253. capots_data = dict(zip(capots_key, capots_value))
  254. if odds_only not in uuid_list:
  255. data_list.append(capots_data)
  256. if team_scores_dict:
  257. for key, team_scores_value in team_scores_dict.items():
  258. if team_scores_value:
  259. for index, value in enumerate(team_scores_value):
  260. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  261. value) + "hg3535" + str(match_id)
  262. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  263. odds_only = Helper.genearte_MD5(hash_str, pt)
  264. sole = Helper.genearte_MD5(sole_str, pt)
  265. odds_onlys.append(odds_only)
  266. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  267. "condition", "odds_only", "sole", "source", "type", "team"]
  268. condition = team_scores_dict_rule[key][index]
  269. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  270. odds_only, sole, "hg3535", "0", ""]
  271. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  272. if odds_only not in uuid_list:
  273. data_list.append(team_scores_data)
  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, number, uuid,
  277. 0, utime, pt]
  278. odds_dict = dict(zip(odds_key, odds_value))
  279. if data_list:
  280. # print(odds_dict)
  281. response_data = Helper.async_post(url + '/setOdds', odds_dict)
  282. # print(response_data)
  283. print('篮球球赔率请求成功', response_data)
  284. if "成功" in response_data:
  285. if result:
  286. self.db.hg3535_lq_odds.update({'match_id': match_id, 'pt': pt}, {
  287. '$set': {"data": data_list, "odds_only": odds_onlys, "utime": utime}}, upsert=True)
  288. else:
  289. self.db.hg3535_lq_odds.insert(odds_dict)
  290. data_list = []
  291. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  292. "lg_id": league_id, "home_rate": 0,
  293. "guest_rate": 0, "home_score": score_home,
  294. "guest_score": score_guest, "all_goal": "", "status": 1,
  295. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  296. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  297. "match_process": jijie, "tag": number, "result_mark": '',
  298. "match_id": match_id, "p_code": ""}
  299. data_list.append(lq_rball)
  300. r_data_dict = {
  301. "game_code": "lq",
  302. "title": "match_result_r",
  303. "source": "hg3535",
  304. "data": data_list
  305. }
  306. if data_list:
  307. try:
  308. response_data = Helper.async_post(url + '/setMatchResult', r_data_dict)
  309. # print(r_data_dict)
  310. print("滚球篮球赛事结果post提交", response_data)
  311. except Exception as e:
  312. logger.warning('滚球数据接口异常,提交失败')
  313. logger.warning(e)
  314. reactor.callFromThread(out.callback, item)