sports.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. import copy
  3. import datetime
  4. import time
  5. import scrapy
  6. import lxml.etree
  7. import pycomm
  8. import json
  9. from collectSports.biz import getMongo
  10. from collectSports.items import Odds
  11. class SportsSpider(scrapy.Spider):
  12. name = 'sports'
  13. allowed_domains = ['hg0088.com']
  14. # start_urls = ['http://hg0088.com/']
  15. custom_settings = {
  16. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  17. "Accept-Encoding": "gzip, deflate",
  18. "Accept-Language": "zh-CN,zh;q=0.8",
  19. "Cache-Control": "max-age=0",
  20. "Connection": "keep-alive",
  21. # "Cookie": "OddType@21627573=H; protocolstr=http; gamePoint_21627573=2019-05-10%2A0%2A0; _ga=GA1.4.601418716.1557495256; _gid=GA1.4.1118061739.1557495256",
  22. "Cookie": "OddType@21627573=H; _ga=GA1.4.773413111.1560825258; _gid=GA1.4.1960743904.1560825258; protocolstr=https; gamePoint_21627573=2019-06-18%2A2%2A",
  23. "Host": "205.201.4.177",
  24. "USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36",
  25. # "ITEM_PIPELINES": {
  26. # "collectSports.pipelines.sports.SportsPipeline": 200,
  27. # }
  28. }
  29. start_url = 'https://205.201.4.177/app/member/get_game_allbets.php'
  30. def start_requests(self):
  31. url = self.start_url
  32. mongo = getMongo()
  33. zq_competitions = mongo.changeSet('zq_competition').find()
  34. for zq_competition in zq_competitions:
  35. match_id = str(zq_competition['match_id'])
  36. current_time = datetime.datetime.now()
  37. mongo.changeSet('zq_competition').update({}, {'$set': {'current_time': current_time}}, upsert=True)
  38. # mongo.collection.update({}, {$set: {update_time: ""}}, {multi: 1})
  39. # mongo.changeSet('zq_league').insert(dict(zq_league))
  40. # mongo.changeSet({"name":"zhangsan"}, {"$set":{"age":"25"}})
  41. uuid = zq_competition['uuid']
  42. form_data = {
  43. "uid": "iobou83m21627573l357852",
  44. "langx": "zh-cn",
  45. "gtype": "FT",
  46. "showtype": "FT",
  47. "gid": match_id,
  48. "ltype": "4",
  49. "date": pycomm.gmdate()
  50. }
  51. request = scrapy.FormRequest(url, formdata=form_data, callback=self.parse, dont_filter=True, meta={'match_id': match_id, 'uuid': uuid})
  52. yield request
  53. def parse(self, response):
  54. game_list = []
  55. games = response.xpath('/serverresponse/game')
  56. match_id = response.meta['match_id']
  57. uuid = response.meta['uuid']
  58. for game in games:
  59. game_odds = {}
  60. game = lxml.etree.fromstring(game.extract())
  61. for i in game.getchildren():
  62. game_odds[i.tag] = i.text
  63. game_list.append(game_odds)
  64. print(game_list)
  65. with open('../collectSports/conf/hg0088.json', 'r', encoding='utf8') as hg:
  66. hg0088 = json.load(hg)['root']
  67. print(111)
  68. odd_list = []
  69. for x in hg0088:
  70. try:
  71. x['enabled'] = game_list[0][x['prodds']]
  72. except:
  73. pass
  74. items = x['items']
  75. new_items = []
  76. for item in items:
  77. try:
  78. item['oddsv'] = game_list[0][item['rodds']]
  79. except:
  80. item['oddsv'] = 0
  81. try:
  82. item['ratio'] = game_list[0][item['ratio_name']]
  83. except:
  84. item['ratio'] = ""
  85. new_items.append(item)
  86. n_i = copy.deepcopy(x)
  87. n_i['items'] = new_items
  88. odd_list.append(n_i)
  89. item = Odds()
  90. item['match_id'] = match_id
  91. item['uuid'] = uuid
  92. item['source'] = "hg0088"
  93. item['updata'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  94. item['content'] = odd_list
  95. yield item