lanqiu.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import logging
  2. from twisted.internet import defer,reactor
  3. from ..utils.helper import Helper
  4. from ..settings import LEAGUE_URL,MATCH_URL
  5. import pymongo,time
  6. from ..settings import M_HOST,M_USER,M_PASSWORD,M_POST,M_DB,ODDS_URL
  7. class LanqiuPipeline(object):
  8. def open_spider(self, spider):
  9. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  10. authSource=M_DB)
  11. self.db = self.mongo[M_DB]
  12. @defer.inlineCallbacks
  13. def process_item(self,item,spider):
  14. logger=logging.getLogger(__name__)
  15. logger.info("进入管道")
  16. out=defer.Deferred()
  17. reactor.callInThread(self._do_calculation,item,out)
  18. yield out
  19. def _do_calculation(self,item,out):
  20. #先保存联赛
  21. league_name = item['league']
  22. uuid = Helper.genearte_uuid(league_name)
  23. type=item['showtype']
  24. is_rollball,is_today,is_morningplate = 0,0,0
  25. if type=="FT":
  26. is_today=1
  27. elif type=="FU":
  28. is_morningplate=1
  29. elif type=="RB":
  30. is_rollball=1
  31. else:
  32. is_stringscene=1
  33. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid","is_rollball","is_today","is_morningplate","is_stringscene"]
  34. league_value = [league_name, "1", "1", "0", item['datetime'], item['match_id'], "hgg070", uuid,is_rollball,is_today,is_morningplate,is_stringscene]
  35. #赛事
  36. childer = dict(zip(league_key, league_value))
  37. #联赛
  38. obj = {"game_code": "lq", "title": "league", "source": "hgg070","data":[childer]}
  39. res=Helper.async_post(LEAGUE_URL,obj)
  40. if res:
  41. if res.get('status')==1:
  42. logging.warning("联赛提交成功,{}1".format(res))
  43. #提交赛事
  44. lres=Helper.async_post(MATCH_URL,childer)
  45. print('333333333333333333333333333333333333333333',lres)
  46. if lres.get('status')==1:
  47. logging.warning("赛事提交成功,{}2".format(res))
  48. #保存赔率
  49. # 是否串场
  50. if item['isP'] == 'P':
  51. ris_stringscene = 1
  52. else:
  53. ris_stringscene = 0
  54. # 现在时间,时间戳
  55. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  56. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  57. "is_stringscene", "utime", "pt", 'match_identity']
  58. odds_value = ["lq", "odds", item['match_id'], item['league_id'], item["content"], "hgg070", [],
  59. item['more_count'], uuid,
  60. ris_stringscene, utime, item['isP'], item["match_identity"]]
  61. # 赛事
  62. odderlist = dict(zip(odds_key, odds_value))
  63. res = Helper.async_post(ODDS_URL, odderlist)
  64. if res:
  65. if res.get('status') == 1:
  66. logging.warning("赔率提交成功,{}3".format(res))
  67. else:
  68. logging.warning("赔率提交失败,{}4".format(res))
  69. else:
  70. logging.warning("赔率提交失败,{}5".format(res))
  71. else:
  72. logging.warning("赛事提交失败,{}6".format(res))
  73. else:
  74. logging.warning("联赛提交失败,{}7".format(res))
  75. else:
  76. logging.warning("联赛提交失败,{}8".format(res))