sportslst.py 8.1 KB

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