roll_lanqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # encoding: utf-8
  2. import datetime
  3. import pymongo
  4. import time
  5. import logging
  6. import redis
  7. from twisted.internet import defer, reactor
  8. from ..utils.helper import Helper
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, POST_URL, R_HOST, R_POST, R_DB, R_PASSWORD
  10. class Roll_Lanqiupipeline(object):
  11. def open_spider(self, spider):
  12. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource=M_DB)
  13. self.db = self.mongo[M_DB]
  14. self.rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  15. @defer.inlineCallbacks
  16. def process_item(self, item, spider):
  17. out = defer.Deferred()
  18. reactor.callInThread(self._do_calculation, item, out)
  19. yield out
  20. # defer.returnValue(item)
  21. # def process_item(self, item, spider):
  22. def _do_calculation(self, item, out):
  23. # 使用twisted将mysql插入变成异步执行
  24. logger = logging.getLogger(__name__)
  25. # 联赛id
  26. league_id = item['league_id']
  27. # 联赛名
  28. league_name = item['league_name']
  29. # 比赛id
  30. match_id = item['game_id']
  31. hash_date = self.rls.hget("hg3535.GunQiu.ids", match_id)
  32. # 球队1 #home_team
  33. team_home = item['team_home']
  34. # 球队2 # guest_team
  35. team_guest = item['team_guest']
  36. # 数量(97>)
  37. number = item['number']
  38. # 比赛状态
  39. result_mark = item['score_dict']
  40. time_game = str(item['time_game'])
  41. # 现在时间,时间戳
  42. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 43200))
  43. cdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  44. match_date = cdate.split(' ')[0]
  45. match_time = cdate.split(' ')[1]
  46. # 队1分数
  47. score_home = item['score_home']
  48. # 队2分数
  49. score_guest = item['score_guest']
  50. # 第几节
  51. jijie = item['jijie']
  52. # 球队得分
  53. pt = 4
  54. match_score = "{}:{}".format(score_home, score_guest)
  55. concedes_dict = item['concede']
  56. concedes_dict_rule = item['concede_rule']
  57. odd_evens_dict = item['odd_even']
  58. odd_evens_dict_rule = item['odd_even_rule']
  59. total_sizes_dict = item['total_size']
  60. total_sizes_dict_rule = item['total_size_rule']
  61. last_numbers_dict = item['last_number']
  62. capots_dict = item['capot']
  63. team_scores_dict = item['team_score']
  64. team_scores_dict_rule = item['team_score_rule']
  65. last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year)
  66. uuid = Helper.genearte_uuid(league_name)
  67. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  68. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  69. league_list = []
  70. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  71. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  72. league_data = dict(zip(league_key, league_value))
  73. league_list.append(league_data)
  74. league_dict['data'] = league_list
  75. res = Helper.async_post('{}/setLeague'.format(POST_URL), league_dict)
  76. if res:
  77. if res.get('status') == 1:
  78. logger.info('篮球滚球联赛提交成功, {}'.format(res))
  79. self.db.lq_league35.insert(league_data)
  80. else:
  81. logger.warning('篮球滚球联赛提交失败, {}, {}'.format(res, league_dict))
  82. else:
  83. logger.warning('篮球滚球联赛接口, 提交失败, {}, {}'.format(res, league_dict))
  84. else:
  85. logger.info('篮球滚球联赛已存在')
  86. match_identity = Helper.genearte_uuid(team_home + team_guest + hash_date.decode())
  87. if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  88. match_list = []
  89. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  90. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  91. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  92. "half_match_id", "is_today", "is_horn", 'match_identity']
  93. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, number,
  94. "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0, match_identity]
  95. match_data = dict(zip(match_kay, match_value))
  96. match_list.append(match_data)
  97. match_dict['data'] = match_list
  98. res = Helper.async_post('{}/setMatch'.format(POST_URL), match_dict)
  99. if res:
  100. if res.get('status') == 1:
  101. logger.info('篮球滚球赛事提交成功, {}'.format(res))
  102. self.db.lq_competition35.insert(match_data)
  103. # logger.info(match_dict)
  104. else:
  105. logger.warning('篮球滚球赛事提交失败, {}, {}'.format(res, match_dict))
  106. else:
  107. logger.warning('篮球滚球赛事接口异常,提交失败, {}, {}'.format(res, match_dict))
  108. else:
  109. logger.info('篮球滚球赛事已存在')
  110. # 赔率
  111. data_list = []
  112. odds_onlys = []
  113. if concedes_dict:
  114. for key, value in concedes_dict.items():
  115. if value:
  116. for index, concede_value in enumerate(value):
  117. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  118. concede_value) + "hg3535" + str(match_id)
  119. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  120. odds_only = Helper.genearte_MD5(hash_str, pt)
  121. sole = Helper.genearte_MD5(sole_str, pt)
  122. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  123. "condition", "odds_only", "sole", "source", "type", "team"]
  124. condition = concedes_dict_rule[key][index]
  125. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  126. odds_only, sole, "hg3535", "0", ""]
  127. concede_data = dict(zip(concede_key, concede_value))
  128. data_list.append(concede_data)
  129. if total_sizes_dict:
  130. for key, value in total_sizes_dict.items():
  131. if value:
  132. for index, total_sizes_value in enumerate(value):
  133. condition = total_sizes_dict_rule[key][index]
  134. hash_str = "TN" + str(key) + str(index) + str(condition) + str(
  135. total_sizes_value) + "hg3535" + str(match_id)
  136. sole_str = "TN" + str(key) + str(index) + str(match_id) + "hg3535"
  137. odds_only = Helper.genearte_MD5(hash_str, pt)
  138. sole = Helper.genearte_MD5(sole_str, pt)
  139. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  140. "condition", "odds_only", "sole", "source", "type", "team"]
  141. total_sizes_value = [match_id, league_id, key, "0", index, 'TN', total_sizes_value,
  142. condition, odds_only, sole, "hg3535", "0", ""]
  143. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  144. data_list.append(total_sizes_data)
  145. if odd_evens_dict:
  146. for key, value in odd_evens_dict.items():
  147. if value:
  148. condition = odd_evens_dict_rule[key]
  149. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  150. value) + "hg3535" + str(match_id)
  151. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  152. odds_only = Helper.genearte_MD5(hash_str, pt)
  153. sole = Helper.genearte_MD5(sole_str, pt)
  154. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  155. "condition", "odds_only", "sole", "source", "type", "team"]
  156. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  157. condition, odds_only, sole, "hg3535", "0", ""]
  158. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  159. data_list.append(odd_evens_data)
  160. lasthome_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  161. last_home = last_numbers_dict['last_home']
  162. if last_home:
  163. for key, value in last_home.items():
  164. odds_code =lasthome_dict[key]
  165. hash_str = "LN" + odds_code + '0' + str(key) + str(
  166. value) + "hg3535" + str(match_id)
  167. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  168. odds_only = Helper.genearte_MD5(hash_str, pt)
  169. sole = Helper.genearte_MD5(sole_str, pt)
  170. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  171. "condition", "odds_only", "sole", "source", "type", "team"]
  172. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  173. key, odds_only, sole, "hg3535", "0", ""]
  174. last_home_data = dict(zip(last_home_key, last_home_value))
  175. data_list.append(last_home_data)
  176. last_guest = last_numbers_dict['last_guest']
  177. lastguest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  178. if last_guest:
  179. for key, value in last_guest.items():
  180. odds_code = lastguest_dict[key]
  181. # condition = lastnumber_dict[key]
  182. hash_str = "LN" + odds_code + '0' + str(key) + str(value) + "hg3535" + str(match_id)
  183. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  184. odds_only = Helper.genearte_MD5(hash_str, pt)
  185. sole = Helper.genearte_MD5(sole_str, pt)
  186. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  187. "condition", "odds_only", "sole", "source", "type", "team"]
  188. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  189. key, odds_only, sole, "hg3535", "0", ""]
  190. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  191. data_list.append(last_guest_data)
  192. if capots_dict:
  193. for key, value in capots_dict.items():
  194. if value:
  195. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  196. sole_str = "C" + 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. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  200. "condition", "odds_only", "sole", "source", "type", "team"]
  201. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  202. "", odds_only, sole, "hg3535", "0", ""]
  203. capots_data = dict(zip(capots_key, capots_value))
  204. data_list.append(capots_data)
  205. if team_scores_dict:
  206. for key, team_scores_value in team_scores_dict.items():
  207. if team_scores_value:
  208. for index, value in enumerate(team_scores_value):
  209. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  210. value) + "hg3535" + str(match_id)
  211. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  212. odds_only = Helper.genearte_MD5(hash_str, pt)
  213. sole = Helper.genearte_MD5(sole_str, pt)
  214. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  215. "condition", "odds_only", "sole", "source", "type", "team"]
  216. condition = team_scores_dict_rule[key][index]
  217. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  218. odds_only, sole, "hg3535", "0", ""]
  219. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  220. data_list.append(team_scores_data)
  221. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  222. "is_stringscene", "utime", "pt", 'match_identity']
  223. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid,
  224. 0, cdate, pt, match_identity]
  225. odds_dict = dict(zip(odds_key, odds_value))
  226. if data_list:
  227. res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict)
  228. if res:
  229. if res.get('status') == 1:
  230. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  231. else:
  232. logger.warning('篮球滚球详细赔率提交失败, {}, {}'.format(res, odds_dict))
  233. else:
  234. logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}, {}'.format(res, odds_dict))
  235. else:
  236. logger.info('篮球滚球详细赔率列表为空, 不提交')
  237. data_list = []
  238. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  239. "lg_id": league_id, "home_rate": 0, 'match_identity': match_identity,
  240. "guest_rate": 0, "home_score": score_home,
  241. "guest_score": score_guest, "all_goal": "", "status": 1,
  242. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  243. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  244. "match_process": jijie, "tag": number, "result_mark": result_mark,
  245. "match_id": match_id, "p_code": ""}
  246. data_list.append(lq_rball)
  247. r_data_dict = {
  248. "game_code": "lq",
  249. "title": "match_result_r",
  250. "source": "hg3535",
  251. "data": data_list
  252. }
  253. if data_list:
  254. try:
  255. res = Helper.async_post('{}/setMatchResult'.format(POST_URL), r_data_dict)
  256. if res.get('status') == 1:
  257. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  258. self.db.lq_match_result35.insert(lq_rball)
  259. else:
  260. logger.warning('篮球滚球赛事结果记录提交失败, {}, {}'.format(res, r_data_dict))
  261. # logger.warning(r_data_dict)
  262. except Exception as e:
  263. logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
  264. else:
  265. logger.info('篮球滚球赛事结果数据,为空不提交')
  266. reactor.callFromThread(out.callback, item)