roll_lanqiu.py 15 KB

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