sportslst.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. # Define your item pipelines here
  3. #
  4. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  5. # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
  6. import json
  7. import datetime
  8. import re
  9. import pymongo
  10. import requests
  11. from collectSports.biz import getMongo
  12. from collectSports.utils.helper import Helper
  13. class SportslstPipeline(object):
  14. def open_spider(self, spider):
  15. self.host = '192.168.2.200'
  16. self.port = 27017
  17. self.dbname = 'kaiyou' # 数据库名
  18. self.client = pymongo.MongoClient(host=self.host, port=self.port)
  19. self.tdb = self.client[self.dbname]
  20. self.port = self.tdb['zq_league'] # 表名
  21. # competition
  22. self.port2 = self.tdb['zq_competition'] # 表名
  23. def process_item(self, item, spider):
  24. mongo = getMongo()
  25. url = 'http://admin.5gogo.com/setSports'
  26. # print(mongo)
  27. # zaopan = item['csource']
  28. # if zaopan == 'zaopan':
  29. # zq_leagues = set(item['zq_league'])
  30. # zq_matchs = set(item['zq_match'])
  31. # for zq_league in zq_leagues:
  32. # mongo.changeSet('zq_league').insert(dict(zq_league))
  33. # for zq_match in zq_matchs:
  34. # mongo.changeSet('zq_competition').insert(dict(zq_match))
  35. # return item
  36. zb = item['csource']
  37. if zb == 'zhibo':
  38. zb_lists = item['zq_zhibo']
  39. # print(zb_lists)
  40. list = []
  41. for i in zb_lists:
  42. # print(i)
  43. dic = {}
  44. dic['title'] = 'broadcast'
  45. dic['game_code'] = 'zq'
  46. # dic['data'] = i
  47. dic['data'] = {}
  48. dic['data']['doing'] = i['doing']
  49. dic['data']['game_type'] = i['game_type']
  50. dic['data']['guest_team'] = i['guest_team']
  51. dic['data']['host_team'] = i['host_team']
  52. dic['data']['league_name'] = i['league_name']
  53. dic['data']['shower'] = i['shower']
  54. dic['data']['showid'] = i['showid']
  55. dic['data']['start_time'] = i['start_time']
  56. # print(dic)
  57. if dic['data']['showid'] == mongo.changeSet('zq_zhibo').find({'showid': dic['data']['showid']}):
  58. mongo.changeSet('zq_zhibo').update(dic)
  59. else:
  60. mongo.changeSet('zq_zhibo').insert(dic)
  61. list.append(dic)
  62. # print(list)
  63. r = requests.post(url='http://admin.5gogo.com/setSports', data={"data": json.dumps(list)})
  64. print(r.text)
  65. elif zb == 'zaopan':
  66. zq_leagues = set(item['zq_league'])
  67. zq_matchs = set(item['zq_match'])
  68. current_time = datetime.datetime.now()
  69. # 联赛
  70. for zq_league in zq_leagues:
  71. league_list = []
  72. payload_key = ['game_code', 'title']
  73. payload_value = ['zq', 'league']
  74. payload = Helper.get_zip_data(payload_key, payload_value)
  75. data_key = ['name_chinese', 'kind', 'match_mode', 'if_stop', 'belong', 'last_time', 'lg_id',
  76. 'source', 'uuid']
  77. data_value = ['name_chinese', '0', '0', '0', '0', current_time.strftime("%Y-%m-%d %H:%M:%S"),
  78. zq_league.get('league_id'), 'hg0088', str(zq_league.get('uuid'))]
  79. data = Helper.get_zip_data(data_key, data_value)
  80. payload['data'] = data
  81. league_list.append(payload)
  82. response_data = Helper.post(url, league_list)
  83. print(response_data.text)
  84. mongo.changeSet('zq_league').insert(dict(zq_league))
  85. # 赛事
  86. for zq_match in zq_matchs:
  87. match_list = []
  88. payload_key = ['game_code', 'title']
  89. payload_value = ['zq', 'competition']
  90. payload = Helper.get_zip_data(payload_key, payload_value)
  91. match_date = zq_match.get('mdate')
  92. match_time = re.search(r'(\d{1,2}):(\d{1,2})', zq_match.get('mtime'))
  93. # 日期时间地区差异转换
  94. if 0 <= int(match_time.group(1)) <= 12:
  95. if int(match_time.group(1)) == 12:
  96. r_match_time = '00:' + match_time.group(2)
  97. else:
  98. r_match_time = str(int(match_time.group(1)) + 12) + ':' + match_time.group(2)
  99. else:
  100. m_time = int(match_time.group(1)) + 12 - 24
  101. if m_time < 10:
  102. r_match_time = '0' + str(m_time) + ':' + match_time.group(2)
  103. else:
  104. r_match_time = str(m_time) + ':' + match_time.group(2)
  105. if int(match_time.group(1)) + 12 >= 24:
  106. match_date = (datetime.datetime.strptime(match_date, '%Y-%m-%d') + datetime.timedelta(
  107. days=1)).strftime('%Y-%m-%d')
  108. old_time = match_date + ' ' + match_time.group(0) + ':00'
  109. if current_time - datetime.datetime.strptime(old_time, "%Y-%m-%d %H:%M:%S") >= datetime.timedelta(
  110. hours=12):
  111. status = 1
  112. else:
  113. status = 0
  114. us_time = match_date + ' ' + r_match_time
  115. data_key = ['home_team', 'guest_team', 'lg_id', 'status', 'match_id', 'match_date', 'match_time',
  116. 'tag', 'source', 'is_rollball', 'is_today', 'is_morningplate', 'is_stringscene', 'us_time',
  117. 'uuid']
  118. data_value = [zq_match.get('host_name'), zq_match.get('guest_name'), zq_match.get('league_id'),
  119. status, zq_match.get('match_id'), match_date, r_match_time,
  120. '0', 'hg0088', zq_match.get('is_roll'), '0', '0', '0', us_time, str(zq_match.get('uuid'))]
  121. data = Helper.get_zip_data(data_key, data_value)
  122. payload['data'] = data
  123. match_list.append(payload)
  124. response_data = Helper.post(url, match_list)
  125. print(response_data.text)
  126. mongo.changeSet('zq_competition').insert(dict(zq_match))
  127. # 冠军
  128. elif zb == 'chain':
  129. zq_chains = set(item['zq_chain'])
  130. current_time = datetime.datetime.now()
  131. for zq_chain in zq_chains:
  132. chain_list = []
  133. payload_key = ['game_code', 'title']
  134. payload_value = ['zq', 'odds']
  135. payload = Helper.get_zip_data(payload_key, payload_value)
  136. data_key = ['match_id', 'lg_id', 'odds_code', 'status', 'sort', 'p_code', 'odds',
  137. 'condition', 'odds_only', 'sole', 'source', 'type', 'team']
  138. data_value = [zq_chain.get('league_id'), zq_chain.get('league_id'), zq_chain.get('league_subname'),
  139. zq_chain.get('enabled'), '0', zq_chain.get('game_type'), zq_chain.get('odds'),
  140. '0', '0', '0', 'c22123', '0', zq_chain.get('name')
  141. ]
  142. data = Helper.get_zip_data(data_key, data_value)
  143. payload['data'] = data
  144. chain_list.append(payload)
  145. response_data = Helper.post(url, chain_list)
  146. print(response_data.text)
  147. mongo.changeSet('zq_chain').insert(dict(zq_chain))
  148. return item
  149. def close_spider(self, spider):
  150. self.client.close()
  151. # def open_spider(self, spider):
  152. # self.host = '192.168.2.200'
  153. # self.port = 27017
  154. # self.dbname = 'kaiyou' # 数据库名
  155. # self.client = pymongo.MongoClient(host=self.host, port=self.port)
  156. # self.tdb = self.client[self.dbname]
  157. # self.port = self.tdb['zq_league'] # 表名
  158. # # competition
  159. # self.port2 = self.tdb['zq_competition'] # 表名