wangqiu.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. from .ball_func import fuhao
  7. from ..utils.helper import Helper
  8. from .ball_func import get_pcode, new_time, out_time
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  10. class Wangqiupipeline(object):
  11. def open_spider(self, spider):
  12. # self.connection = AsyncIOMotorClient("mongodb://{}:{}@{}:{}/database?authSource={}".format('kaiyou', 'kaiyou', '192.168.2.200', 27017, 'kaiyou'))
  13. # session = aiohttp.ClientSession()
  14. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST)
  15. self.db = self.mongo[M_DB]
  16. @defer.inlineCallbacks
  17. def process_item(self, item, spider):
  18. out = defer.Deferred()
  19. reactor.callInThread(self._do_calculation, item, out)
  20. yield out
  21. defer.returnValue(item)
  22. def _do_calculation(self, item, out):
  23. wq_odds = []
  24. logger = logging.getLogger(__name__)
  25. url = 'http://admin.5gogo.com'
  26. # logger.warning(query.addErrback(self.handle_error, item, spider))
  27. # 联赛id
  28. league_id = item['league_id']
  29. # 联赛名
  30. league_name = item['league_name']
  31. # result = item['result']
  32. # 比赛id
  33. match_id = item['game_id']
  34. # print(game_id)
  35. # 球队1
  36. team_home = item['team_home']
  37. # 球队2
  38. team_guest = item['team_guest']
  39. # 数量(97>)
  40. tag_number = item['number']
  41. # 比赛状态
  42. zhuangtai = item['zhuangtai']
  43. # 日期
  44. # data_game = item['data_game']
  45. try:
  46. data_game = item['data_game'].split("/")
  47. month = str(data_game[1].strip())
  48. day = str(data_game[0])
  49. except Exception as e:
  50. logger.warning(e)
  51. data_game = item['data_game'].split(" ")
  52. months = str(data_game[1].strip())
  53. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  54. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  55. month = month_dict[months]
  56. day = str(data_game[0])
  57. # 比赛时间
  58. time_game = str(item['time_game'])
  59. # 比赛时间,时间戳
  60. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  61. r_ctime = "2019" + "-" + month + "-" + day
  62. # 现在时间,时间戳
  63. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  64. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  65. # 队1分数
  66. score_home = item['score_home']
  67. # 队2分数
  68. score_guest = item['score_guest']
  69. # 第几节
  70. jijie = item['jijie']
  71. # 球队得分
  72. qiudui = item['qiudui']
  73. pt = item['pt']
  74. # 让盘
  75. concedes_dict = item['concedes_dict']
  76. concedes_dict_rule = item['concedes_dict_rule']
  77. # 冠军
  78. kemps_dict = item['kemps_dict']
  79. # 让局
  80. bureaus_dict = item['bureaus_dict']
  81. bureaus_dict_rule = item['bureaus_dict_rule']
  82. # 总局数大小
  83. total_number_dict = item['total_number_dict']
  84. total_number_dict_rule = item['total_number_dict_rule']
  85. # 总决赛单双
  86. odd_evens_dict = item['odd_evens_dict']
  87. odd_evens_dict_rule = item['odd_evens_dict_rule']
  88. us_time = ctime
  89. match_date, match_time, time3 = new_time(ctime)
  90. n_time = out_time(time3, 3)
  91. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  92. """联赛"""
  93. last_time = '2019-12-31 23:59:59'
  94. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  95. league_list = []
  96. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  97. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  98. league_data = dict(zip(league_key, league_value))
  99. league_list.append(league_data)
  100. league_dict['data'] = league_list
  101. # print(league_list)
  102. # print(league_dict)
  103. if self.db.hg3535_wq_league.find({'lg_id': league_id}).count() < 1:
  104. res = Helper.async_post(url + '/setLeague', league_dict)
  105. print(res)
  106. self.db.hg3535_wq_league.insert(league_list)
  107. """赛事"""
  108. competition = self.db.wq_competition.find_one({"match_id": match_id})
  109. match_list = []
  110. if competition:
  111. print('赛事已存在,修改赛事四状态')
  112. is_morningplate = competition['is_morningplate']
  113. is_rollball = competition['is_rollball']
  114. is_stringscene = competition['is_stringscene']
  115. is_today = competition['is_today']
  116. lmtime = competition['match_time']
  117. if pt == 3:
  118. if is_stringscene != 1 or lmtime != match_time:
  119. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  120. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  121. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  122. "half_match_id", "is_today", 'rule']
  123. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  124. "hg3535", is_rollball, is_morningplate, 1, us_time, uuid, 0, is_today, '']
  125. match_data = dict(zip(match_key, match_value))
  126. match_list.append(match_data)
  127. match_dict['data'] = match_list
  128. # print(match_list)
  129. # print(match_dict)
  130. res = Helper.async_post(url + '/setMatch', match_dict)
  131. print(res)
  132. self.db.hg3535_wq_competition.update({'match_id': match_id},
  133. {"$set": {"is_stringscene": 1, 'match_time': match_time}},
  134. upsert=True)
  135. if pt == 2:
  136. if is_morningplate != 1 or lmtime != match_time:
  137. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  138. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  139. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  140. "half_match_id", "is_today", 'rule']
  141. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  142. "hg3535", is_rollball, 1, is_stringscene, us_time, uuid, 0, is_today, '']
  143. match_data = dict(zip(match_key, match_value))
  144. match_list.append(match_data)
  145. match_dict['data'] = match_list
  146. # print(match_list)
  147. # print(match_dict)
  148. res = Helper.async_post(url + '/setMatch', match_dict)
  149. print(res)
  150. self.db.hg3535_wq_competition.update({'match_id': match_id},
  151. {"$set": {"is_morningplate": 1, 'match_time': match_time}},
  152. upsert=True)
  153. if pt == 1:
  154. if is_today != 1 or lmtime != match_time:
  155. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  156. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  157. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  158. "half_match_id", "is_today", 'rule']
  159. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  160. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, 1, '']
  161. match_data = dict(zip(match_kay, match_value))
  162. match_list.append(match_data)
  163. match_dict['data'] = match_list
  164. # print(match_list)
  165. # print(match_dict)
  166. res = Helper.async_post(url + '/setMatch', match_dict)
  167. print(res)
  168. self.db.hg3535_wq_competition.update({'match_id': match_id},
  169. {"$set": {"is_today": 1, 'match_time': match_time}},
  170. upsert=True)
  171. else:
  172. print("赛事已存在,赛事状态时间未改变,不做修改")
  173. else:
  174. if pt == 3:
  175. is_rollball = 0
  176. is_today = 0
  177. is_morningplate = 0
  178. is_stringscene = 1
  179. elif pt == 2:
  180. is_rollball = 0
  181. is_today = 0
  182. is_morningplate = 1
  183. is_stringscene = 0
  184. else:
  185. is_today = 1
  186. is_rollball = 0
  187. is_morningplate = 0
  188. is_stringscene = 0
  189. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  190. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  191. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  192. "half_match_id", "is_today", 'rule']
  193. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  194. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '']
  195. match_data = dict(zip(match_kay, match_value))
  196. match_list.append(match_data)
  197. match_dict['data'] = match_list
  198. res = Helper.async_post(url + '/setMatch', match_dict)
  199. print(res)
  200. self.db.hg3535_wq_competition.insert(match_list)
  201. """赔率"""
  202. try:
  203. result = self.db.hg3535_wq_odds.find_one({'match_id': match_id, 'pt': pt})
  204. odds_only_list = result['odds_only']
  205. except:
  206. result = ''
  207. odds_only_list = []
  208. wangqiu = item['wangqiu']
  209. data_list = []
  210. odds_onlys = []
  211. # 让盘
  212. if concedes_dict:
  213. for key, value in concedes_dict.items():
  214. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  215. sole = Helper.genearte_MD5(sole_str)
  216. odds_str = 'LD' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  217. value)
  218. odds_only = Helper.genearte_MD5(odds_str)
  219. condition = fuhao(concedes_dict_rule[key])
  220. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  221. "odds_only", "sole", "source", "type", "team"]
  222. concedes_dict_value = [match_id, league_id, key, '0', 0, 'LD', value, condition,
  223. odds_only, sole, 'hg3535', '0', '']
  224. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  225. odds_onlys.append(odds_only)
  226. if odds_only not in odds_only_list:
  227. data_list.append(concedes_dict_data)
  228. # 让局
  229. if bureaus_dict:
  230. for key, value in bureaus_dict.items():
  231. sole_str = 'LB' + str(key) + str(match_id) + 'hg3535'
  232. sole = Helper.genearte_MD5(sole_str)
  233. odds_str = 'LB' + str(key) + str(match_id) + 'hg3535' + str(bureaus_dict_rule[key]) + str(
  234. value)
  235. odds_only = Helper.genearte_MD5(odds_str)
  236. condition = fuhao(bureaus_dict_rule[key])
  237. bureaus_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  238. "odds_only", "sole", "source", "type", "team"]
  239. bureaus_dict_value = [match_id, league_id, key, '0', 0, 'LB', value, condition,
  240. odds_only, sole, 'hg3535', '0', '']
  241. bureaus_dict_data = dict(zip(bureaus_dict_key, bureaus_dict_value))
  242. odds_onlys.append(odds_only)
  243. if odds_only not in odds_only_list:
  244. data_list.append(bureaus_dict_data)
  245. # 总局数大小
  246. if total_number_dict:
  247. for key, value in total_number_dict.items():
  248. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  249. sole = Helper.genearte_MD5(sole_str)
  250. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  251. total_number_dict_rule[key]) + str(value)
  252. odds_only = Helper.genearte_MD5(odds_str)
  253. condition = fuhao(total_number_dict_rule[key])
  254. total_number_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  255. "condition",
  256. "odds_only", "sole", "source", "type", "team"]
  257. total_number_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  258. odds_only, sole, 'hg3535', '0', '']
  259. total_number_dict_data = dict(zip(total_number_dict_key, total_number_dict_value))
  260. odds_onlys.append(odds_only)
  261. if odds_only not in odds_only_list:
  262. data_list.append(total_number_dict_data)
  263. # 总局数单双
  264. if odd_evens_dict:
  265. for key, value in odd_evens_dict.items():
  266. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  267. sole = Helper.genearte_MD5(sole_str)
  268. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(odd_evens_dict_rule[key]) + str(
  269. value)
  270. odds_only = Helper.genearte_MD5(odds_str)
  271. condition = fuhao(odd_evens_dict_rule[key])
  272. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  273. "odds_only", "sole", "source", "type", "team"]
  274. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  275. odds_only, sole, 'hg3535', '0', '']
  276. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  277. odds_onlys.append(odds_only)
  278. if odds_only not in odds_only_list:
  279. data_list.append(odd_evens_dict_data)
  280. # 冠军
  281. if kemps_dict:
  282. for key, value in kemps_dict.items():
  283. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  284. sole = Helper.genearte_MD5(sole_str)
  285. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + '2' + str(value)
  286. odds_only = Helper.genearte_MD5(odds_str)
  287. kemps_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  288. "odds_only", "sole", "source", "type", "team"]
  289. kemps_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  290. odds_only, sole, 'hg3535', '0', '']
  291. kemps_dict_data = dict(zip(kemps_dict_key, kemps_dict_value))
  292. odds_onlys.append(odds_only)
  293. if odds_only not in odds_only_list:
  294. data_list.append(kemps_dict_data)
  295. if pt == 3:
  296. ris_stringscene = 1
  297. else:
  298. ris_stringscene = 0
  299. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  300. "is_stringscene", "utime", "pt"]
  301. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  302. ris_stringscene, utime, pt]
  303. odds_dict = dict(zip(odds_key, odds_value))
  304. # print(odds_dict)
  305. # print(123)
  306. if data_list:
  307. res = Helper.async_post(url + '/setOdds', odds_dict)
  308. print(res)
  309. if '成功' in res:
  310. if result:
  311. self.db.hg3535_wq_odds.update({'match_id': match_id, 'pt': pt}, {
  312. '$set': {"data": data_list, "odds_only": odds_onlys, "utime": utime}}, upsert=True)
  313. else:
  314. self.db.hg3535_wq_odds.insert(odds_dict)