lanqiu.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. concedes_dict = item['concede']
  70. concedes_dict_rule = item['concede_rule']
  71. odd_evens_dict = item['odd_even']
  72. odd_evens_dict_rule = item['odd_even_rule']
  73. total_sizes_dict = item['total_size']
  74. total_sizes_dict_rule = item['total_size_rule']
  75. last_numbers_dict = item['last_number']
  76. capots_dict = item['capot']
  77. team_scores_dict = item['team_score']
  78. team_scores_dict_rule = item['team_score_rule']
  79. # 让球
  80. lanqiu = item['lanqiu']
  81. match_date, match_time, time3 = new_time(us_time)
  82. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  83. # 让球 数据插入数据库
  84. """联赛"""
  85. # last_time = '2019-12-31 23:59:59'
  86. # league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  87. # league_list = []
  88. # league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  89. # league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  90. # league_data = dict(zip(league_key, league_value))
  91. # league_list.append(league_data)
  92. # league_dict['data'] = league_list
  93. # # print(league_list)
  94. # # print(league_dict)
  95. # if self.db.hg3535_lq_league.find({'lg_id': league_id}).count() < 1:
  96. # res = Helper.async_post(url + '/setLeague', league_dict)
  97. # print(res)
  98. # self.db.hg3535_lq_league.insert(league_list)
  99. """赛事"""
  100. competition = self.db.hg3535_lq_competition.find_one({"match_id": str(match_id)})
  101. match_list = []
  102. if competition:
  103. print('赛事已存在,修改赛事四状态')
  104. is_morningplate = competition['is_morningplate']
  105. is_rollball = competition['is_rollball']
  106. is_stringscene = competition['is_stringscene']
  107. is_today = competition['is_today']
  108. lmtime = competition['match_time']
  109. if pt == 3:
  110. if is_stringscene != 1 or lmtime != match_time:
  111. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  112. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  113. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  114. "half_match_id", "is_today", 'pt']
  115. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  116. "hg3535", is_rollball, is_morningplate, 1, us_time, uuid, 0, is_today, pt]
  117. match_data = dict(zip(match_key, match_value))
  118. match_list.append(match_data)
  119. match_dict['data'] = match_list
  120. # print(match_list)
  121. # print(match_dict)
  122. res = Helper.async_post(url + '/setMatch', match_dict)
  123. print(res)
  124. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  125. {"$set": {"is_stringscene": 1, 'match_time': match_time}},
  126. upsert=True)
  127. if pt == 2:
  128. if is_morningplate != 1 or lmtime != match_time:
  129. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  130. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  131. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  132. "half_match_id", "is_today", 'pt']
  133. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  134. "hg3535", is_rollball, 1, is_stringscene, us_time, uuid, 0, is_today, pt]
  135. match_data = dict(zip(match_key, match_value))
  136. match_list.append(match_data)
  137. match_dict['data'] = match_list
  138. # print(match_list)
  139. # print(match_dict)
  140. res = Helper.async_post(url + '/setMatch', match_dict)
  141. print(res)
  142. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  143. {"$set": {"is_morningplate": 1, 'match_time': match_time}},
  144. upsert=True)
  145. if pt == 1:
  146. if is_today != 1 or lmtime != match_time:
  147. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  148. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  149. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  150. "half_match_id", "is_today", 'pt']
  151. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  152. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, 1, pt]
  153. match_data = dict(zip(match_kay, match_value))
  154. match_list.append(match_data)
  155. match_dict['data'] = match_list
  156. # print(match_list)
  157. # print(match_dict)
  158. res = Helper.async_post(url + '/setMatch', match_dict)
  159. print(res)
  160. self.db.hg3535_lq_competition.update({'match_id': str(match_id)},
  161. {"$set": {"is_today": 1, 'match_time': match_time}},
  162. upsert=True)
  163. else:
  164. print("赛事已存在,赛事状态时间未改变,不做修改")
  165. else:
  166. print('赛事不存在,插入新赛事')
  167. if pt == 3:
  168. is_rollball = 0
  169. is_today = 0
  170. is_morningplate = 0
  171. is_stringscene = 1
  172. elif pt == 2:
  173. is_rollball = 0
  174. is_today = 0
  175. is_morningplate = 1
  176. is_stringscene = 0
  177. else:
  178. is_today = 1
  179. is_rollball = 0
  180. is_morningplate = 0
  181. is_stringscene = 0
  182. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  183. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  184. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  185. "half_match_id", "is_today", 'pt']
  186. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  187. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, pt]
  188. match_data = dict(zip(match_kay, match_value))
  189. match_list.append(match_data)
  190. match_dict['data'] = match_list
  191. res = Helper.async_post(url + '/setMatch', match_dict)
  192. print(res)
  193. self.db.hg3535_lq_competition.insert(match_list)