lanqiu.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import datetime
  2. import json
  3. import pymongo
  4. import time
  5. import logging
  6. import requests
  7. from .ball_func import fuhao
  8. from ..utils.helper import Helper
  9. from .ball_func import get_pcode, new_time, out_time
  10. from concurrent.futures import ThreadPoolExecutor
  11. from requests_futures.sessions import FuturesSession
  12. class Lanqiupipeline(object):
  13. def open_spider(self, spider):
  14. self.mongo = pymongo.MongoClient(host="192.168.2.200", username="kaiyou", password="kaiyou", port=27017)
  15. self.db = self.mongo['kaiyou']
  16. self.session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))
  17. def process_item(self, item, spider):
  18. # 使用twisted将mysql插入变成异步执行
  19. lq_odds = []
  20. url = 'http://admin.5gogo.com'
  21. logger = logging.getLogger(__name__)
  22. # # logger.warning(query.addErrback(self.handle_error, item, spider))
  23. # 联赛id
  24. league_id = item['league_id']
  25. # 联赛名
  26. league_name = item['league_name']
  27. # result = item['result']
  28. # 比赛id
  29. match_id = item['game_id']
  30. # 球队1
  31. team_home = item['team_home']
  32. # 球队2
  33. team_guest = item['team_guest']
  34. # 数量(97>)
  35. tag_number = item['number']
  36. # 比赛状态
  37. zhuangtai = item['zhuangtai']
  38. # 日期
  39. # data_game = item['data_game']
  40. try:
  41. data_game = item['data_game'].split("/")
  42. month = str(data_game[1].strip())
  43. day = str(data_game[0])
  44. except Exception as e:
  45. logger.warning(e)
  46. data_game = item['data_game'].split(" ")
  47. months = str(data_game[1].strip())
  48. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  49. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  50. month = month_dict[months]
  51. day = str(data_game[0])
  52. # 比赛时间
  53. time_game = str(item['time_game'])
  54. # 比赛时间,时间戳
  55. us_time = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  56. r_ctime = "2019" + "-" + month + "-" + day
  57. # 现在时间,时间戳
  58. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  59. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  60. # 队1分数
  61. score_home = item['score_home']
  62. # 队2分数
  63. score_guest = item['score_guest']
  64. # 第几节
  65. jijie = item['jijie']
  66. # 球队得分
  67. qiudui = item['qiudui']
  68. pt = item['pt']
  69. # 让球
  70. concedes_dict = item['concede']
  71. concedes_dict_rule = item['concede_rule']
  72. # 总得分单双
  73. odd_evens_dict = item['odd_even']
  74. odd_evens_dict_rule = item['odd_even_rule']
  75. # 总比分大小
  76. total_sizes_dict = item['total_size']
  77. total_sizes_dict_rule = item['total_size_rule']
  78. # 球队得分最后一位数
  79. last_numbers_dict = item['last_number']
  80. # 独赢
  81. capots_dict = item['capot']
  82. # 球队得分大小
  83. team_scores_dict = item['team_score']
  84. team_scores_dict_rule = item['team_score_rule']
  85. # 让球
  86. lanqiu = item['lanqiu']
  87. match_date, match_time, time3 = new_time(us_time)
  88. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  89. # 让球 数据插入数据库
  90. """联赛"""
  91. last_time = '2019-12-31 23:59:59'
  92. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  93. league_list = []
  94. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  95. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  96. league_data = dict(zip(league_key, league_value))
  97. league_list.append(league_data)
  98. league_dict['data'] = league_list
  99. # print(league_list)
  100. # print(league_dict)
  101. if self.db.hg3535_lq_league.find({'lg_id': league_id}).count() < 1:
  102. res = Helper.async_post(url + '/setLeague', league_dict)
  103. print(res)
  104. self.db.hg3535_lq_league.insert(league_list)
  105. """赛事"""
  106. competition = self.db.hg3535_lq_competition.find_one({"match_id": str(match_id)})
  107. match_list = []
  108. if competition:
  109. print('赛事已存在,修改赛事四状态')
  110. is_morningplate = competition['is_morningplate']
  111. is_rollball = competition['is_rollball']
  112. is_stringscene = competition['is_stringscene']
  113. is_today = competition['is_today']
  114. lmtime = competition['match_time']
  115. if pt == 3:
  116. if is_stringscene != 1 or lmtime != match_time:
  117. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  118. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  119. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  120. "half_match_id", "is_today", 'pt']
  121. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  122. "hg3535", is_rollball, is_morningplate, 1, us_time, uuid, 0, is_today, pt]
  123. match_data = dict(zip(match_key, match_value))
  124. match_list.append(match_data)
  125. match_dict['data'] = match_list
  126. # print(match_list)
  127. # print(match_dict)
  128. res = Helper.async_post(url + '/setMatch', match_dict)
  129. print(res)
  130. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  131. {"$set": {"is_stringscene": 1, 'match_time': match_time}},
  132. upsert=True)
  133. if pt == 2:
  134. if is_morningplate != 1 or lmtime != match_time:
  135. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  136. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  137. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  138. "half_match_id", "is_today", 'pt']
  139. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  140. "hg3535", is_rollball, 1, is_stringscene, us_time, uuid, 0, is_today, pt]
  141. match_data = dict(zip(match_key, match_value))
  142. match_list.append(match_data)
  143. match_dict['data'] = match_list
  144. # print(match_list)
  145. # print(match_dict)
  146. res = Helper.async_post(url + '/setMatch', match_dict)
  147. print(res)
  148. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  149. {"$set": {"is_morningplate": 1, 'match_time': match_time}},
  150. upsert=True)
  151. if pt == 1:
  152. if is_today != 1 or lmtime != match_time:
  153. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  154. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  155. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  156. "half_match_id", "is_today", 'pt']
  157. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  158. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, 1, pt]
  159. match_data = dict(zip(match_kay, match_value))
  160. match_list.append(match_data)
  161. match_dict['data'] = match_list
  162. # print(match_list)
  163. # print(match_dict)
  164. res = Helper.async_post(url + '/setMatch', match_dict)
  165. print(res)
  166. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  167. {"$set": {"is_today": 1, 'match_time': match_time}},
  168. upsert=True)
  169. else:
  170. print("赛事已存在,赛事状态时间未改变,不做修改")
  171. else:
  172. print('赛事不存在,插入新赛事')
  173. if pt == 3:
  174. is_rollball = 0
  175. is_today = 0
  176. is_morningplate = 0
  177. is_stringscene = 1
  178. elif pt == 2:
  179. is_rollball = 0
  180. is_today = 0
  181. is_morningplate = 1
  182. is_stringscene = 0
  183. else:
  184. is_today = 1
  185. is_rollball = 0
  186. is_morningplate = 0
  187. is_stringscene = 0
  188. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  189. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  190. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  191. "half_match_id", "is_today", 'pt']
  192. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  193. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, pt]
  194. match_data = dict(zip(match_kay, match_value))
  195. match_list.append(match_data)
  196. match_dict['data'] = match_list
  197. res = Helper.async_post(url + '/setMatch', match_dict)
  198. print(res)
  199. self.db.hg3535_lq_competition.insert(match_list)
  200. """赔率"""
  201. try:
  202. result = self.db.hg3535_lq_odds.find_one({'match_id': str(match_id), 'pt': pt})
  203. odds_only_list = result['odds_only']
  204. except:
  205. result = ''
  206. odds_only_list = []
  207. data_list = []
  208. odds_onlys = []
  209. # 让球
  210. if concedes_dict:
  211. for key, value in concedes_dict.items():
  212. if value:
  213. for index, concedes_value in enumerate(value):
  214. sole_str = 'CO' + str(index) + str(match_id) + 'hg3535'
  215. sole = Helper.genearte_MD5(sole_str)
  216. odds_str = 'CO' + str(index) + str(match_id) + 'hg3535' + str(
  217. concedes_dict_rule[key][index]) + str(concedes_value)
  218. odds_only = Helper.genearte_MD5(odds_str)
  219. condition = fuhao(str(concedes_dict_rule[key][index]))
  220. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  221. "condition", "odds_only", "sole", "source", "type", "team"]
  222. concedes_dict_value = [match_id, league_id, key, '0', index, 'CO', concedes_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. else:
  229. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  230. sole = Helper.genearte_MD5(sole_str)
  231. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(
  232. concedes_dict_rule[key]) + str(value)
  233. odds_only = Helper.genearte_MD5(odds_str)
  234. condition = fuhao(str(concedes_dict_rule[key]))
  235. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  236. "condition", "odds_only", "sole", "source", "type", "team"]
  237. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  238. odds_only, sole, 'hg3535', '0', '']
  239. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  240. odds_onlys.append(odds_only)
  241. if odds_only not in odds_only_list:
  242. data_list.append(concedes_dict_data)
  243. # 总得分单双
  244. if odd_evens_dict:
  245. for key, value in odd_evens_dict.items():
  246. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  247. sole = Helper.genearte_MD5(sole_str)
  248. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  249. odd_evens_dict_rule[key]) + str(value)
  250. odds_only = Helper.genearte_MD5(odds_str)
  251. condition = fuhao(str(odd_evens_dict_rule[key]))
  252. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  253. "condition", "odds_only", "sole", "source", "type", "team"]
  254. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  255. odds_only, sole, 'hg3535', '0', '']
  256. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  257. odds_onlys.append(odds_only)
  258. if odds_only not in odds_only_list:
  259. data_list.append(odd_evens_dict_data)
  260. # 总比分大小
  261. if total_sizes_dict:
  262. for key, value in total_sizes_dict.items():
  263. if value:
  264. for index, total_sizes_value in enumerate(value):
  265. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  266. sole = Helper.genearte_MD5(sole_str)
  267. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  268. total_sizes_dict_rule[key][index]) + str(total_sizes_value)
  269. odds_only = Helper.genearte_MD5(odds_str)
  270. condition = fuhao(str(total_sizes_dict_rule[key][index]))
  271. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  272. "condition", "odds_only", "sole", "source", "type", "team"]
  273. total_sizes_value = [match_id, league_id, key, '0', index, 'TN', total_sizes_value, condition,
  274. odds_only, sole, 'hg3535', '0', '']
  275. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  276. odds_onlys.append(odds_only)
  277. if odds_only not in odds_only_list:
  278. data_list.append(total_sizes_data)
  279. else:
  280. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  281. sole = Helper.genearte_MD5(sole_str)
  282. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  283. total_sizes_dict_rule[key]) + str(value)
  284. odds_only = Helper.genearte_MD5(odds_str)
  285. condition = fuhao(str(total_sizes_dict_rule[key]))
  286. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  287. "condition", "odds_only", "sole", "source", "type", "team"]
  288. total_sizes_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  289. odds_only, sole, 'hg3535', '0', '']
  290. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  291. odds_onlys.append(odds_only)
  292. if odds_only not in odds_only_list:
  293. data_list.append(total_sizes_data)
  294. # 球队得分大小
  295. if team_scores_dict:
  296. for key, value in team_scores_dict.items():
  297. sole_str = 'TB' + str(key) + str(match_id) + 'hg3535'
  298. sole = Helper.genearte_MD5(sole_str)
  299. odds_str = 'TB' + str(key) + str(match_id) + 'hg3535' + str(
  300. team_scores_dict_rule[key]) + str(value)
  301. odds_only = Helper.genearte_MD5(odds_str)
  302. condition = fuhao(str(team_scores_dict_rule[key]))
  303. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  304. "condition", "odds_only", "sole", "source", "type", "team"]
  305. team_scores_value = [match_id, league_id, key, '0', 0, 'TB', value, condition,
  306. odds_only, sole, 'hg3535', '0', '']
  307. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  308. odds_onlys.append(odds_only)
  309. if odds_only not in odds_only_list:
  310. data_list.append(team_scores_data)
  311. # 球队得分最后一位数
  312. last_home_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  313. last_home = last_numbers_dict['last_home']
  314. if last_home:
  315. for key, value in last_home.items():
  316. for index, last_home_value in value.items():
  317. sole_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535'
  318. sole = Helper.genearte_MD5(sole_str)
  319. odds_str = 'LN' + last_home_dict[key] + str(match_id) + 'hg3535' + '0' + str(last_home_value)
  320. odds_only = Helper.genearte_MD5(odds_str)
  321. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  322. "condition", "odds_only", "sole", "source", "type", "team"]
  323. last_home_value = [match_id, league_id, last_home_dict[key], '0', index, 'LN', last_home_value, '',odds_only, sole, 'hg3535', '0', '']
  324. last_home_data = dict(zip(last_home_key, last_home_value))
  325. odds_onlys.append(odds_only)
  326. if odds_only not in odds_only_list:
  327. data_list.append(last_home_data)
  328. last_guest = last_numbers_dict['last_guest']
  329. last_guest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'}
  330. if last_guest:
  331. for key, value in last_numbers_dict.items():
  332. for index, last_guest_value in value.items():
  333. sole_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535'
  334. sole = Helper.genearte_MD5(sole_str)
  335. odds_str = 'LN' + last_guest_dict[key] + str(match_id) + 'hg3535' + '0' + str(
  336. last_guest_value)
  337. odds_only = Helper.genearte_MD5(odds_str)
  338. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  339. "condition", "odds_only", "sole", "source", "type", "team"]
  340. last_guest_value = [match_id, league_id, key, '0', index, 'LN', last_guest_value, '',
  341. odds_only, sole, 'hg3535', '0', '']
  342. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  343. odds_onlys.append(odds_only)
  344. if odds_only not in odds_only_list:
  345. data_list.append(last_guest_data)
  346. # 独赢
  347. if capots_dict:
  348. for key, value in capots_dict.items():
  349. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  350. sole = Helper.genearte_MD5(sole_str)
  351. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  352. odds_only = Helper.genearte_MD5(odds_str)
  353. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  354. "condition", "odds_only", "sole", "source", "type", "team"]
  355. capots_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  356. odds_only, sole, 'hg3535', '0', '']
  357. capots_data = dict(zip(capots_key, capots_value))
  358. odds_onlys.append(odds_only)
  359. if odds_only not in odds_only_list:
  360. data_list.append(capots_data)
  361. if pt == 3:
  362. ris_stringscene = 1
  363. else:
  364. ris_stringscene = 0
  365. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  366. "is_stringscene", "utime", "pt"]
  367. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  368. ris_stringscene, utime, pt]
  369. odds_dict = dict(zip(odds_key, odds_value))
  370. # print(odds_dict)
  371. # print(123)
  372. if data_list:
  373. res = Helper.async_post(url + '/setOdds', odds_dict)
  374. print(res)
  375. if '成功' in res:
  376. if result:
  377. self.db.hg3535_lq_odds.update({'match_id': match_id, 'pt': pt}, {
  378. '$set': {"data": data_list, "odds_only": odds_onlys, "utime": utime}}, upsert=True)
  379. else:
  380. self.db.hg3535_lq_odds.insert(odds_dict)