jieshu.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if status:
  39. ostatus = 1
  40. # dates = date.split("/")
  41. # month = dates[1].strip()
  42. # day = str(dates[0].strip())
  43. else:
  44. ostatus = 2
  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. # dates = date.split(" ")
  48. # month = month_dict[dates[1].strip()]
  49. # day = str(dates[0].strip())
  50. status_dict = {"game_code": game_code, "title": "match_status", "source": "hg3535"}
  51. update = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  52. data_list = []
  53. # us_time = '{}-{}-{}'.format(datetime.datetime.now().year, month, day)
  54. match_identity = Helper.genearte_uuid(home + away + date)
  55. data = {'match_id': match_id, 'status': ostatus, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  56. "is_stringscene": 0, "is_horn": 0, 'game_code': game_code, 'update': update,
  57. "match_identity": match_identity}
  58. data_list.append(data)
  59. status_dict['data'] = data_list
  60. res = Helper.async_post(MATCH_STATUS, status_dict)
  61. if res:
  62. if res.get('status') == 1:
  63. self.db.match_status35.insert(data)
  64. # self.db.match_status35.update({'match_id': match_id}, {'$set': data}, upsert=True)
  65. if ostatus == 2:
  66. self.rls.hdel('hg3535.GunQiu.ids', match_id)
  67. logger.info('{},赛事结果状态交成功, {}'.format(game_code, res))
  68. # logger.info(status_dict)
  69. print(status_dict)
  70. else:
  71. logger.warning('{},赛事结果状态交失败, {}'.format(game_code, res))
  72. # logger.warning(status_dict)
  73. else:
  74. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format(game_code, res))
  75. # logger.warning(status_dict)
  76. reactor.callFromThread(out.callback, item)