roll_lanqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. # logger.info(match_dict)
  101. else:
  102. logger.warning('篮球滚球赛事提交失败, {}, {}'.format(res, match_dict))
  103. else:
  104. logger.warning('篮球滚球赛事接口异常,提交失败, {}, {}'.format(res, match_dict))
  105. else:
  106. logger.info('篮球滚球赛事已存在')
  107. # 赔率
  108. data_list = []
  109. odds_onlys = []
  110. if concedes_dict:
  111. for key, value in concedes_dict.items():
  112. if value:
  113. for index, concede_value in enumerate(value):
  114. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  115. concede_value) + "hg3535" + str(match_id)
  116. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  117. odds_only = Helper.genearte_MD5(hash_str, pt)
  118. sole = Helper.genearte_MD5(sole_str, pt)
  119. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  120. "condition", "odds_only", "sole", "source", "type", "team"]
  121. condition = concedes_dict_rule[key][index]
  122. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  123. odds_only, sole, "hg3535", "0", ""]
  124. concede_data = dict(zip(concede_key, concede_value))
  125. data_list.append(concede_data)
  126. if total_sizes_dict:
  127. for key, value in total_sizes_dict.items():
  128. if value:
  129. for index, total_sizes_value in enumerate(value):
  130. condition = total_sizes_dict_rule[key][index]
  131. hash_str = "TN" + str(key) + str(index) + str(condition) + str(
  132. total_sizes_value) + "hg3535" + str(match_id)
  133. sole_str = "TN" + str(key) + str(index) + str(match_id) + "hg3535"
  134. odds_only = Helper.genearte_MD5(hash_str, pt)
  135. sole = Helper.genearte_MD5(sole_str, pt)
  136. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  137. "condition", "odds_only", "sole", "source", "type", "team"]
  138. total_sizes_value = [match_id, league_id, key, "0", index, 'TN', total_sizes_value,
  139. condition, odds_only, sole, "hg3535", "0", ""]
  140. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  141. data_list.append(total_sizes_data)
  142. if odd_evens_dict:
  143. for key, value in odd_evens_dict.items():
  144. if value:
  145. condition = odd_evens_dict_rule[key]
  146. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  147. value) + "hg3535" + str(match_id)
  148. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  149. odds_only = Helper.genearte_MD5(hash_str, pt)
  150. sole = Helper.genearte_MD5(sole_str, pt)
  151. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  152. "condition", "odds_only", "sole", "source", "type", "team"]
  153. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  154. condition, odds_only, sole, "hg3535", "0", ""]
  155. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  156. data_list.append(odd_evens_data)
  157. lasthome_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  158. last_home = last_numbers_dict['last_home']
  159. if last_home:
  160. for key, value in last_home.items():
  161. odds_code =lasthome_dict[key]
  162. hash_str = "LN" + odds_code + '0' + str(key) + str(
  163. value) + "hg3535" + str(match_id)
  164. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  165. odds_only = Helper.genearte_MD5(hash_str, pt)
  166. sole = Helper.genearte_MD5(sole_str, pt)
  167. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  168. "condition", "odds_only", "sole", "source", "type", "team"]
  169. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  170. key, odds_only, sole, "hg3535", "0", ""]
  171. last_home_data = dict(zip(last_home_key, last_home_value))
  172. data_list.append(last_home_data)
  173. last_guest = last_numbers_dict['last_guest']
  174. lastguest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  175. if last_guest:
  176. for key, value in last_guest.items():
  177. odds_code = lastguest_dict[key]
  178. # condition = lastnumber_dict[key]
  179. hash_str = "LN" + odds_code + '0' + str(key) + str(value) + "hg3535" + str(match_id)
  180. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  181. odds_only = Helper.genearte_MD5(hash_str, pt)
  182. sole = Helper.genearte_MD5(sole_str, pt)
  183. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  184. "condition", "odds_only", "sole", "source", "type", "team"]
  185. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  186. key, odds_only, sole, "hg3535", "0", ""]
  187. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  188. data_list.append(last_guest_data)
  189. if capots_dict:
  190. for key, value in capots_dict.items():
  191. if value:
  192. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  193. sole_str = "C" + 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. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  197. "condition", "odds_only", "sole", "source", "type", "team"]
  198. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  199. "", odds_only, sole, "hg3535", "0", ""]
  200. capots_data = dict(zip(capots_key, capots_value))
  201. data_list.append(capots_data)
  202. if team_scores_dict:
  203. for key, team_scores_value in team_scores_dict.items():
  204. if team_scores_value:
  205. for index, value in enumerate(team_scores_value):
  206. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  207. value) + "hg3535" + str(match_id)
  208. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  209. odds_only = Helper.genearte_MD5(hash_str, pt)
  210. sole = Helper.genearte_MD5(sole_str, pt)
  211. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  212. "condition", "odds_only", "sole", "source", "type", "team"]
  213. condition = team_scores_dict_rule[key][index]
  214. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  215. odds_only, sole, "hg3535", "0", ""]
  216. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  217. data_list.append(team_scores_data)
  218. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  219. "is_stringscene", "utime", "pt", 'match_identity']
  220. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid,
  221. 0, cdate, pt, match_identity]
  222. odds_dict = dict(zip(odds_key, odds_value))
  223. if data_list:
  224. res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict)
  225. if res:
  226. if res.get('status') == 1:
  227. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  228. else:
  229. logger.warning('篮球滚球详细赔率提交失败, {}, {}'.format(res, odds_dict))
  230. else:
  231. logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}, {}'.format(res, odds_dict))
  232. else:
  233. logger.info('篮球滚球详细赔率列表为空, 不提交')
  234. data_list = []
  235. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  236. "lg_id": league_id, "home_rate": 0, 'match_identity': match_identity,
  237. "guest_rate": 0, "home_score": score_home,
  238. "guest_score": score_guest, "all_goal": "", "status": 1,
  239. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  240. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  241. "match_process": jijie, "tag": number, "result_mark": result_mark,
  242. "match_id": match_id, "p_code": ""}
  243. data_list.append(lq_rball)
  244. r_data_dict = {
  245. "game_code": "lq",
  246. "title": "match_result_r",
  247. "source": "hg3535",
  248. "data": data_list
  249. }
  250. if data_list:
  251. try:
  252. res = Helper.async_post('{}/setMatchResult'.format(POST_URL), r_data_dict)
  253. if res.get('status') == 1:
  254. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  255. self.db.lq_match_result35.insert(lq_rball)
  256. else:
  257. logger.warning('篮球滚球赛事结果记录提交失败, {}, {}'.format(res, r_data_dict))
  258. # logger.warning(r_data_dict)
  259. except Exception as e:
  260. logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
  261. else:
  262. logger.info('篮球滚球赛事结果数据,为空不提交')
  263. # reactor.callFromThread(out.callback, item)