roll_lanqiu.py 16 KB

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