roll_lanqiu.py 17 KB

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