import copy
import json

import requests

from collectSports.biz import getMongo


class SportsPipeline(object):
    def process_item(self, item, spider):
        mongo = getMongo()
        url = 'http://admin.5gogo.com/setSports'
        match_id = item['match_id']
        gidm = item['gidm']
        # mongo.changeSet('zq_odds').insert(dict(item))
        if item['content']:
            odds_list = []
            result = mongo.changeSet('zq_odds').find({'uuid': item['uuid']}).count()
            if result >= 1:
                pass
            if result < 1:
                contents = item['content']
                for content in contents:
                    if content['plodds'] and content['enabled']:
                        for y in content['items']:
                            condition = y['latio']
                            if condition == None:
                                condition = ''
                            data = {"game_code": "zq", "title": "odds",
                                    "data": {"match_id": match_id, "lg_id": gidm, "odds_code": y['lodds'], "status": 0,
                                             "sort": 0, "p_code": content['plodds'], "odds": y['oddsv'],
                                             "condition": condition, "odds_only": y['uid'], "sole": y['uid'],
                                             "source": "hg0088", "type": 0,
                                             "team": ""}
                                    }
                            data1 = copy.deepcopy(data)
                            odds_list.append(data1)
            mongo.changeSet('zq_odds').insert(dict(item))
            # if odds_list:
            if odds_list:
                r = requests.post(url, data={"data": json.dumps(odds_list)})
                print(r.text)
                print(odds_list)
        # return item
