lanqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 .ball_func import new_time
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL
  10. class 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)
  13. self.db = self.mongo[M_DB]
  14. @defer.inlineCallbacks
  15. def process_item(self, item, spider):
  16. out = defer.Deferred()
  17. reactor.callInThread(self._do_calculation, item, out)
  18. yield out
  19. # defer.returnValue(item)
  20. # def process_item(self, item, spider):
  21. def _do_calculation(self, item, out):
  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
  30. team_home = item['team_home']
  31. # 球队2
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. tag_number = item['number']
  35. # 日期
  36. try:
  37. data_game = item['data_game'].split("/")
  38. month = str(data_game[1].strip())
  39. day = str(data_game[0].strip())
  40. except Exception as e:
  41. logger.warning(e)
  42. data_game = item['data_game'].split(" ")
  43. months = str(data_game[1].strip())
  44. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  45. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  46. month = month_dict[months]
  47. day = str(data_game[0].strip())
  48. # 比赛时间
  49. time_game = str(item['time_game'])
  50. # 比赛时间,时间戳
  51. us_time = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  52. # us_date = "{}-{}-{}".format(datetime.datetime.now().year, month, day)
  53. # 现在时间,时间戳
  54. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  55. pt = str(item['pt'])
  56. # 让球
  57. concedes_dict = item['concede']
  58. concedes_dict_rule = item['concede_rule']
  59. # 总得分单双
  60. odd_evens_dict = item['odd_even']
  61. odd_evens_dict_rule = item['odd_even_rule']
  62. # 总比分大小
  63. total_sizes_dict = item['total_size']
  64. total_sizes_dict_rule = item['total_size_rule']
  65. # 球队得分最后一位数
  66. last_numbers_dict = item['last_number']
  67. # 独赢
  68. capots_dict = item['capot']
  69. # 球队得分大小
  70. team_scores_dict = item['team_score']
  71. team_scores_dict_rule = item['team_score_rule']
  72. match_date, match_time, time3 = new_time(us_time)
  73. uuid = Helper.genearte_uuid(league_name)
  74. # 让球 数据插入数据库
  75. """联赛"""
  76. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  77. if self.db.lq_league35.find({'lg_id': league_id}).count() < 1:
  78. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  79. league_list = []
  80. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  81. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  82. league_data = dict(zip(league_key, league_value))
  83. league_list.append(league_data)
  84. league_dict['data'] = league_list
  85. res = Helper.async_post(LEAGUE_URL, league_dict)
  86. if res:
  87. if res.get('status') == 1:
  88. logger.info('篮球联赛提交成功, {}'.format(res))
  89. self.db.lq_league35.insert(league_data)
  90. # self.db.lq_league35.update({'lg_id': league_id}, {'$set': league_data}, upsert=True)
  91. # logger.info(league_dict)
  92. else:
  93. logger.warning('篮球联赛提交失败, {}'.format(res))
  94. else:
  95. logger.warning('篮球联赛接口, 提交失败, {}'.format(res))
  96. logger.warning(league_dict)
  97. else:
  98. logger.info('篮球联赛已存在')
  99. """赛事"""
  100. if pt == '3':
  101. is_rollball = 0
  102. is_today = 0
  103. is_morningplate = 0
  104. is_stringscene = 1
  105. elif pt == '2':
  106. is_rollball = 0
  107. is_today = 0
  108. is_morningplate = 1
  109. is_stringscene = 0
  110. else:
  111. is_today = 1
  112. is_rollball = 0
  113. is_morningplate = 0
  114. is_stringscene = 0
  115. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  116. pt_status = pt_dict[str(pt)]
  117. match_identity = Helper.genearte_uuid(team_home + team_guest + match_date)
  118. if self.db.lq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  119. match_list = []
  120. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  121. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  122. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  123. "half_match_id", "is_today", 'pt', "is_horn", 'match_identity']
  124. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  125. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, pt, 0, match_identity]
  126. match_data = dict(zip(match_kay, match_value))
  127. match_list.append(match_data)
  128. match_dict['data'] = match_list
  129. res = Helper.async_post(MATCH_URL, match_dict)
  130. if res:
  131. if res.get('status') == 1:
  132. logger.info('篮球赛事提交成功, {}'.format(res))
  133. self.db.lq_competition35.insert(match_data)
  134. # self.db.lq_competition35.update({'match_id': match_id, pt_status: 1}, {'$set': match_data}, upsert=True)
  135. # logger.info(match_dict)
  136. else:
  137. logger.warning('篮球赛事提交失败, {}'.format(res))
  138. # logger.warning(match_dict)
  139. else:
  140. logger.warning('篮球赛事接口异常,提交失败, {}'.format(res))
  141. # logger.warning(match_dict)
  142. else:
  143. logger.info('篮球赛事已存在')
  144. """赔率"""
  145. data_list = []
  146. odds_onlys = []
  147. # 让球
  148. if concedes_dict:
  149. for key, value in concedes_dict.items():
  150. if value:
  151. for index, concedes_value in enumerate(value):
  152. sole_str = 'CO' + str(key) + str(index) + str(match_id) + 'hg3535'
  153. sole = Helper.genearte_MD5(sole_str, pt)
  154. condition = str(concedes_dict_rule[key][index])
  155. hash_str = "CO" + key + str(index) + condition + str(value) + "hg3535" + str(match_id)
  156. odds_only = Helper.genearte_MD5(hash_str, pt)
  157. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  158. "condition", "odds_only", "sole", "source", "type", "team"]
  159. concedes_dict_value = [match_id, league_id, key, '0', index, 'CO', concedes_value, condition,
  160. odds_only, sole, 'hg3535', '0', '']
  161. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  162. data_list.append(concedes_dict_data)
  163. # 总得分单双
  164. if odd_evens_dict:
  165. for key, value in odd_evens_dict.items():
  166. sole_str = 'TS' + str(key) + '0' + str(match_id) + 'hg3535'
  167. sole = Helper.genearte_MD5(sole_str, pt)
  168. condition = str(odd_evens_dict_rule[key])
  169. hash_str = "TS" + key + '0' + condition + str(value) + "hg3535" + str(match_id)
  170. odds_only = Helper.genearte_MD5(hash_str, pt)
  171. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  172. "condition", "odds_only", "sole", "source", "type", "team"]
  173. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  174. odds_only, sole, 'hg3535', '0', '']
  175. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  176. data_list.append(odd_evens_dict_data)
  177. # 总比分大小
  178. if total_sizes_dict:
  179. for key, value in total_sizes_dict.items():
  180. if value:
  181. for index, total_sizes_value in enumerate(value):
  182. sole_str = 'TN' + str(key) + str(index) + str(match_id) + 'hg3535'
  183. sole = Helper.genearte_MD5(sole_str, pt)
  184. condition = str(total_sizes_dict_rule[key][index])
  185. hash_str = "TN" + str(key) + str(index) + condition + str(total_sizes_value) + "hg3535" + str(match_id)
  186. odds_only = Helper.genearte_MD5(hash_str, pt)
  187. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  188. "condition", "odds_only", "sole", "source", "type", "team"]
  189. total_sizes_value = [match_id, league_id, key, '0', index, 'TN', total_sizes_value, condition,
  190. odds_only, sole, 'hg3535', '0', '']
  191. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  192. data_list.append(total_sizes_data)
  193. # 球队得分大小
  194. if team_scores_dict:
  195. for key, value in team_scores_dict.items():
  196. if value:
  197. for index, odd in enumerate(value):
  198. sole_str = "TB" + str(key) + '0' + str(match_id) + "hg3535"
  199. sole = Helper.genearte_MD5(sole_str, pt)
  200. condition = str(team_scores_dict_rule[key][index])
  201. hash_str = "TB" + key + str(index) + condition + str(odd) + "hg3535" + str(match_id)
  202. odds_only = Helper.genearte_MD5(hash_str, pt)
  203. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  204. "condition", "odds_only", "sole", "source", "type", "team"]
  205. team_scores_value = [match_id, league_id, key, '0', 0, 'TB', odd, condition,
  206. odds_only, sole, 'hg3535', '0', '']
  207. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  208. data_list.append(team_scores_data)
  209. # 球队得分最后一位数
  210. last_home_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  211. last_home = last_numbers_dict['last_home']
  212. if last_home:
  213. for key, value in last_home.items():
  214. sole_str = "LN" + last_home_dict[key] + '0' + str(match_id) + "hg3535"
  215. sole = Helper.genearte_MD5(sole_str, pt)
  216. hash_str = "LN" + last_home_dict[key] + '0' + key + str(value) + "hg3535" + str(match_id)
  217. odds_only = Helper.genearte_MD5(hash_str, pt)
  218. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  219. "condition", "odds_only", "sole", "source", "type", "team"]
  220. last_home_value = [match_id, league_id, last_home_dict[key], '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  221. last_home_data = dict(zip(last_home_key, last_home_value))
  222. data_list.append(last_home_data)
  223. last_guest = last_numbers_dict['last_guest']
  224. last_guest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  225. if last_guest:
  226. for key, value in last_guest.items():
  227. sole_str = "LN" + last_guest_dict[key] + '0' + str(match_id) + "hg3535"
  228. sole = Helper.genearte_MD5(sole_str, pt)
  229. hash_str = "LN" + last_guest_dict[key] + '0' + key + str(value) + "hg3535" + str(match_id)
  230. odds_only = Helper.genearte_MD5(hash_str, pt)
  231. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  232. "condition", "odds_only", "sole", "source", "type", "team"]
  233. last_guest_value = [match_id, league_id, last_guest_dict[key], '0', 0, 'LN', value, key, odds_only, sole, 'hg3535', '0', '']
  234. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  235. data_list.append(last_guest_data)
  236. # 独赢
  237. if capots_dict:
  238. for key, value in capots_dict.items():
  239. sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535"
  240. sole = Helper.genearte_MD5(sole_str, pt)
  241. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  242. odds_only = Helper.genearte_MD5(hash_str, pt)
  243. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  244. "condition", "odds_only", "sole", "source", "type", "team"]
  245. capots_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  246. odds_only, sole, 'hg3535', '0', '']
  247. capots_data = dict(zip(capots_key, capots_value))
  248. data_list.append(capots_data)
  249. if pt == '3':
  250. ris_stringscene = 1
  251. else:
  252. ris_stringscene = 0
  253. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  254. "is_stringscene", "utime", "pt", 'match_identity']
  255. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  256. ris_stringscene, utime, pt, match_identity]
  257. odds_dict = dict(zip(odds_key, odds_value))
  258. if data_list:
  259. res = Helper.async_post(ODDS_URL, odds_dict)
  260. if res:
  261. if res.get('status') == 1:
  262. logger.info('篮球详细赔率提交成功, {}'.format(res))
  263. # logger.info(odds_dict)
  264. else:
  265. logger.warning('篮球详细赔率提交成功, {}'.format(res))
  266. # logger.warning(odds_dict)
  267. else:
  268. logger.warning('篮球详细赔率接口异常, 提交成功, {}'.format(res))
  269. # logger.warning(odds_dict)
  270. else:
  271. logger.info('详细赔率列表为空')
  272. reactor.callFromThread(out.callback, item)