jieshu.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import datetime
  2. import pymongo
  3. import logging
  4. from twisted.internet import defer, reactor
  5. import redis
  6. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, MATCH_STATUS
  7. from ..utils.helper import Helper
  8. from ..settings import R_HOST, R_PASSWORD, R_POST, R_DB
  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. self.rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  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 _do_calculation(self, item, out):
  22. # def process_item(self, item, spider):
  23. logger = logging.getLogger(__name__)
  24. ball = item['ball']
  25. match_id = item['match_id']
  26. status = item['status']
  27. home = item['home']
  28. away = item['away']
  29. date = item['date']
  30. if ball == 1:
  31. game_code = "zq"
  32. elif ball == 2:
  33. game_code = "lq"
  34. elif ball == 3:
  35. game_code = "wq"
  36. else:
  37. game_code = "bq"
  38. dates = str(date.strip(" "))
  39. status_dict = {"game_code": game_code, "title": "match_status", "source": "hg3535"}
  40. update = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  41. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  42. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  43. month = month_dict[dates[1]]
  44. day = str(dates[0])
  45. data_list = []
  46. if status:
  47. ostatus = 1
  48. else:
  49. ostatus = 2
  50. us_time = '{}-{}-{}'.format(datetime.datetime.now().year, month, day)
  51. match_identity = Helper.genearte_uuid(home + away + us_time)
  52. data = {'match_id': match_id, 'status': ostatus, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  53. "is_stringscene": 0, "is_horn": 0, 'game_code': game_code, 'update': update,
  54. "match_identity": match_identity}
  55. data_list.append(data)
  56. status_dict['data'] = data_list
  57. res = Helper.async_post(MATCH_STATUS, status_dict)
  58. if res:
  59. if res.get('status') == 1:
  60. self.db.match_status35.insert(data)
  61. # self.db.match_status35.update({'match_id': match_id}, {'$set': data}, upsert=True)
  62. if ostatus == 2:
  63. self.rls.srem('hg3535.gunqiu.ids', match_id)
  64. logger.info('{},赛事结果状态交成功, {}'.format(game_code, res))
  65. logger.info(status_dict)
  66. print(status_dict)
  67. else:
  68. logger.warning('{},赛事结果状态交失败, {}'.format(game_code, res))
  69. logger.warning(status_dict)
  70. else:
  71. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format(game_code, res))
  72. logger.warning(status_dict)
  73. reactor.callFromThread(out.callback, item)