roll_lanqiu.py 16 KB

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