saiguo.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # import datetime
  2. import pymongo
  3. # import time
  4. import logging
  5. # import redis
  6. from twisted.internet import defer, reactor
  7. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, SAIGUO_RESULT
  8. from ..utils.helper import Helper
  9. # 滚球足球 插入
  10. class Jieshuqiupipeline(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. # cls = redis.Redis(host='192.168.0.110', port=6379, db=0)
  15. @defer.inlineCallbacks
  16. def process_item(self, item, spider):
  17. out = defer.Deferred()
  18. reactor.callInThread(self._do_calculation, item, out)
  19. yield out
  20. defer.returnValue(item)
  21. # def process_item(self, item, spider):
  22. def _do_calculation(self, item, out):
  23. logger = logging.getLogger(__name__)
  24. pt = item['pt']
  25. league_id = item["league_id"]
  26. league_name = item["league_name"]
  27. match_id = item["match_id"]
  28. match_date = item["match_date"]
  29. match_time = item["match_time"]
  30. h_name = item["home_team"]
  31. a_name = item["guest_team"]
  32. play_data = item["play_data"]
  33. if pt == 1:
  34. h_score = item["score_half"]
  35. f_score = item["score_full"]
  36. post_data = {'game_code': 'zq', "title": "result", "source": "hg3535", "data": []}
  37. data = {"league_id": league_id, "league_name": league_name, "match_date": match_date,
  38. "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_half": h_score,
  39. "score_full": f_score, "play_data": play_data, "match_id": match_id}
  40. post_data['data'].append(data)
  41. res = Helper.async_post(SAIGUO_RESULT, post_data)
  42. if res:
  43. if res.get('status') == 1:
  44. self.db.zq_saiguo35.insert(post_data)
  45. logger.info('{},赛事结果状态交成功, {}'.format('zq', res))
  46. logger.info(post_data)
  47. else:
  48. logger.warning('{},赛事结果状态交失败, {}'.format('zq', res))
  49. logger.warning(post_data)
  50. else:
  51. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('zq', res))
  52. logger.warning(post_data)
  53. elif pt == 2:
  54. h_score = item["score_half"]
  55. f_score = item["score_result"]
  56. x_score = item["score_below"]
  57. post_data = {'game_code': 'lq', "title": "result", "source": "hg3535", "data": []}
  58. data = {"league_id": league_id, "league_name": league_name, "match_date": match_date,
  59. "match_time": match_time, "home_team": h_name, "match_id": match_id,
  60. "guest_team": a_name, "score_half": h_score, "score_below": x_score, "score_result": f_score,
  61. "play_data": play_data}
  62. post_data['data'].append(data)
  63. res = Helper.async_post(SAIGUO_RESULT, post_data)
  64. if res:
  65. if res.get('status') == 1:
  66. self.db.lq_saiguo35.insert(post_data)
  67. logger.info('{},赛事结果状态交成功, {}'.format('lq', res))
  68. logger.info(post_data)
  69. else:
  70. logger.warning('{},赛事结果状态交失败, {}'.format('lq', res))
  71. logger.warning(post_data)
  72. else:
  73. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('lq', res))
  74. logger.warning(post_data)
  75. elif pt == 3:
  76. f_score = item["score_result"]
  77. post_data = {'game_code': 'wq', "title": "result", "source": "hg3535", "data": []}
  78. data = {"league_id": league_id, "league_name": league_name, "match_date": match_date,
  79. "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_result": f_score,
  80. "play_data": play_data, "match_id": match_id}
  81. post_data['data'].append(data)
  82. res = Helper.async_post(SAIGUO_RESULT, post_data)
  83. if res:
  84. if res.get('status') == 1:
  85. self.db.wq_saiguo35.insert(post_data)
  86. logger.info('{},赛事结果状态交成功, {}'.format('wq', res))
  87. logger.info(post_data)
  88. else:
  89. logger.warning('{},赛事结果状态交失败, {}'.format('wq', res))
  90. logger.warning(post_data)
  91. else:
  92. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('wq', res))
  93. logger.warning(post_data)
  94. else:
  95. h_score = item["score_half"]
  96. f_score = item["score_full"]
  97. post_data = {'game_code': 'bq', "title": "result", "source": "hg3535", "data": []}
  98. data = {"league_id": league_id, "league_name": league_name, "match_date": match_date,
  99. "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_half": h_score,
  100. "score_full": f_score, "play_data": play_data, "match_id": match_id}
  101. post_data['data'].append(data)
  102. res = Helper.async_post(SAIGUO_RESULT, post_data)
  103. if res:
  104. if res.get('status') == 1:
  105. self.db.bq_saiguo35.insert(post_data)
  106. logger.info('{},赛事结果状态交成功, {}'.format('bq', res))
  107. logger.info(post_data)
  108. else:
  109. logger.warning('{},赛事结果状态交失败, {}'.format('bq', res))
  110. logger.warning(post_data)
  111. else:
  112. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('bq', res))
  113. logger.warning(post_data)
  114. reactor.callFromThread(out.callback, item)