wangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, POST_URL
  10. class Wangqiupipeline(object):
  11. def open_spider(self, spider):
  12. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource=M_DB)
  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 _do_calculation(self, item, out):
  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. # print(game_id)
  31. # 球队1
  32. team_home = item['team_home']
  33. # 球队2
  34. team_guest = item['team_guest']
  35. # 数量(97>)
  36. tag_number = item['number']
  37. try:
  38. data_game = item['data_game'].split("/")
  39. month = str(data_game[1].strip())
  40. day = str(data_game[0]).strip()
  41. except Exception as e:
  42. logger.warning(e)
  43. data_game = item['data_game'].split(" ")
  44. months = str(data_game[1].strip())
  45. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  46. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  47. month = month_dict[months]
  48. day = str(data_game[0]).strip()
  49. year = datetime.datetime.now().year
  50. # 比赛时间
  51. time_game = str(item['time_game'])
  52. # 比赛时间,时间戳
  53. ctime = str(year) + "-" + month + "-" + day + " " + time_game + ":00"
  54. # 现在时间,时间戳
  55. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  56. pt = str(item['pt'])
  57. # 让盘
  58. concedes_dict = item['concedes_dict']
  59. concedes_dict_rule = item['concedes_dict_rule']
  60. # 冠军
  61. kemps_dict = item['kemps_dict']
  62. # 让局
  63. bureaus_dict = item['bureaus_dict']
  64. bureaus_dict_rule = item['bureaus_dict_rule']
  65. # 总局数大小
  66. total_number_dict = item['total_number_dict']
  67. total_number_dict_rule = item['total_number_dict_rule']
  68. # 总决赛单双
  69. odd_evens_dict = item['odd_evens_dict']
  70. odd_evens_dict_rule = item['odd_evens_dict_rule']
  71. us_time = ctime
  72. match_date, match_time, time3 = new_time(ctime)
  73. # n_time = out_time(time3, 3)
  74. uuid = Helper.genearte_uuid(league_name)
  75. """联赛"""
  76. last_time = '{}-12-31 23:59:59'.format(year)
  77. if self.db.wq_league35.find({'lg_id': league_id}).count() < 1:
  78. league_dict = {"game_code": "wq", "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('{}/setLeague'.format(POST_URL), league_dict)
  86. if res:
  87. if res.get('status') == 1:
  88. self.db.wq_league35.insert(league_data)
  89. # self.db.wq_league35.update({'lg_id': league_id}, {'$set': league_data},
  90. # upsert=True)
  91. logger.info('网球联赛提交成功, {}'.format(res))
  92. else:
  93. logger.warning('网球联赛提交失败, {}'.format(res))
  94. logger.warning(league_dict)
  95. else:
  96. logger.warning('网球联赛接口异常,提交失败, {}'.format(res))
  97. logger.warning(league_dict)
  98. else:
  99. logger.info('网球联赛已存在, 不提交')
  100. """赛事"""
  101. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  102. pt_status = pt_dict[pt]
  103. if pt == '3':
  104. is_rollball = 0
  105. is_today = 0
  106. is_morningplate = 0
  107. is_stringscene = 1
  108. elif pt == '2':
  109. is_rollball = 0
  110. is_today = 0
  111. is_morningplate = 1
  112. is_stringscene = 0
  113. else:
  114. is_today = 1
  115. is_rollball = 0
  116. is_morningplate = 0
  117. is_stringscene = 0
  118. match_identity = Helper.genearte_uuid(team_home + team_guest + match_date)
  119. if self.db.wq_competition35.find({'match_id': match_id, pt_status: 1}).count() < 1:
  120. match_list = []
  121. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  122. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  123. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  124. "half_match_id", "is_today", 'rule', "is_horn", "match_identity"]
  125. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  126. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '', 0, match_identity]
  127. match_data = dict(zip(match_key, match_value))
  128. match_list.append(match_data)
  129. match_dict['data'] = match_list
  130. res = Helper.async_post('{}/setMatch'.format(POST_URL), match_dict)
  131. if res:
  132. if res.get('status') == 1:
  133. self.db.wq_competition35.insert(match_data)
  134. logger.info('网球赛事提交成功, {}'.format(res))
  135. else:
  136. logger.warning('网球赛事提交失败, {}'.format(res))
  137. logger.warning(match_data)
  138. else:
  139. logger.warning('网球赛事接口异常,提交失败, {}'.format(res))
  140. logger.warning(match_dict)
  141. else:
  142. logging.info('网球赛事已存在, 不提交')
  143. """赔率"""
  144. data_list = []
  145. odds_onlys = []
  146. # 让盘
  147. if concedes_dict:
  148. for key, value in concedes_dict.items():
  149. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  150. sole = Helper.genearte_MD5(sole_str, pt)
  151. odds_str = 'LD' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  152. value)
  153. odds_only = Helper.genearte_MD5(odds_str, pt)
  154. condition = concedes_dict_rule[key]
  155. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  156. "odds_only", "sole", "source", "type", "team"]
  157. concedes_dict_value = [match_id, league_id, key, '0', 0, 'LD', value, condition,
  158. odds_only, sole, 'hg3535', '0', '']
  159. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  160. data_list.append(concedes_dict_data)
  161. # 让局
  162. if bureaus_dict:
  163. for key, value in bureaus_dict.items():
  164. sole_str = 'LB' + str(key) + str(match_id) + 'hg3535'
  165. sole = Helper.genearte_MD5(sole_str, pt)
  166. odds_str = 'LB' + str(key) + str(match_id) + 'hg3535' + str(bureaus_dict_rule[key]) + str(
  167. value)
  168. odds_only = Helper.genearte_MD5(odds_str, pt)
  169. condition = bureaus_dict_rule[key]
  170. bureaus_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  171. "odds_only", "sole", "source", "type", "team"]
  172. bureaus_dict_value = [match_id, league_id, key, '0', 0, 'LB', value, condition,
  173. odds_only, sole, 'hg3535', '0', '']
  174. bureaus_dict_data = dict(zip(bureaus_dict_key, bureaus_dict_value))
  175. data_list.append(bureaus_dict_data)
  176. # 总局数大小
  177. if total_number_dict:
  178. for key, value in total_number_dict.items():
  179. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  180. sole = Helper.genearte_MD5(sole_str, pt)
  181. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  182. total_number_dict_rule[key]) + str(value)
  183. odds_only = Helper.genearte_MD5(odds_str, pt)
  184. condition = total_number_dict_rule[key]
  185. total_number_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  186. "condition",
  187. "odds_only", "sole", "source", "type", "team"]
  188. total_number_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  189. odds_only, sole, 'hg3535', '0', '']
  190. total_number_dict_data = dict(zip(total_number_dict_key, total_number_dict_value))
  191. data_list.append(total_number_dict_data)
  192. # 总局数单双
  193. if odd_evens_dict:
  194. for key, value in odd_evens_dict.items():
  195. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  196. sole = Helper.genearte_MD5(sole_str, pt)
  197. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(odd_evens_dict_rule[key]) + str(
  198. value)
  199. odds_only = Helper.genearte_MD5(odds_str, pt)
  200. condition = odd_evens_dict_rule[key]
  201. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  202. "odds_only", "sole", "source", "type", "team"]
  203. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  204. odds_only, sole, 'hg3535', '0', '']
  205. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  206. data_list.append(odd_evens_dict_data)
  207. # 冠军
  208. if kemps_dict:
  209. for key, value in kemps_dict.items():
  210. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  211. sole = Helper.genearte_MD5(sole_str, pt)
  212. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + '2' + str(value)
  213. odds_only = Helper.genearte_MD5(odds_str, pt)
  214. kemps_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  215. "odds_only", "sole", "source", "type", "team"]
  216. kemps_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  217. odds_only, sole, 'hg3535', '0', '']
  218. kemps_dict_data = dict(zip(kemps_dict_key, kemps_dict_value))
  219. data_list.append(kemps_dict_data)
  220. if pt == '3':
  221. ris_stringscene = 1
  222. else:
  223. ris_stringscene = 0
  224. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  225. "is_stringscene", "utime", "pt", 'match_identity']
  226. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  227. ris_stringscene, utime, pt, match_identity]
  228. odds_dict = dict(zip(odds_key, odds_value))
  229. if data_list:
  230. res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict)
  231. if res:
  232. if res.get('status') == 1:
  233. logger.info('网球赔率提交成功, {}'.format(res))
  234. # logger.info(odds_dict)
  235. else:
  236. logger.warning('网球赔率提交失败, {}'.format(res))
  237. logger.warning(odds_dict)
  238. else:
  239. logger.warning('网球详细接口赔率返回异常, {}'.format(res))
  240. logger.warning(odds_dict)
  241. else:
  242. logger.info('网球详细赔率列表为空, 不提交')
  243. reactor.callFromThread(out.callback, item)