Exemplo n.º 1
0
    def export_playerscore_template(self, data):
        """导出球员得分模板表
        """
        players = self.get_team_all_players()
        entries = []
        for record in data:
            uid = common.generate_uid(record['id'], self.sportType)
            entry = self.PlayerMatchScoreEntryTemplate % (
                        uid,
                        common.generate_uid(record['mid'], self.sportType),
                        record['score']
                    )
            entries.append(entry)
            try:
                players.remove(uid)
            except Exception as e:
                print("PlayerId: %d in mid: %d and PlayerTemplate does not match" % (record['id'], self.mid))

        for ID in players:
            entry = self.PlayerMatchScoreEntryTemplate % (
                        common.generate_uid(ID, self.sportType),
                        common.generate_uid(record['mid'], self.sportType),
                        0
                    )
            entries.append(entry)

        codecs.open(self.output, 'w', encoding='utf-8').write(
                    self.PlayerMatchScoreTemplate % '\n'.join(entries)
                )
Exemplo n.º 2
0
def generate_player_template_item(players, sport_type):
    """生成球员模板表记录

    :players:           由爬虫抓取,经简单剥离后的球员数据

    :returns: TODO
    """
    entries = []
    for player in players:
        # 计算球员ID
        uid = int(player['id'])
        oid = common.generate_uid(uid, sport_type)

        try:
            entry = PlayerEntryTemplate % (
                    oid,
                    oid,
                    player['name'],
                    get_img_url(sport_type, uid),
                    common.generate_uid(player['club_id'], sport_type),
                    common.get_position(sport_type, player['posi'])
                ) 
        except Exception as e:
            print(e)
            print("uid: %d name: %s" %( uid, player['name']))
            continue

        entries.append(entry)

    return entries
Exemplo n.º 3
0
    def export_playerscore_template(self, data):
        """导出球员得分模板表
        """
        players = self.get_team_all_players()
        entries = []
        for record in data:
            uid = common.generate_uid(record['id'], self.sportType)
            entry = self.PlayerMatchScoreEntryTemplate % (
                uid, common.generate_uid(record['mid'],
                                         self.sportType), record['score'])
            entries.append(entry)
            try:
                players.remove(uid)
            except Exception as e:
                print(
                    "PlayerId: %d in mid: %d and PlayerTemplate does not match"
                    % (record['id'], self.mid))

        for ID in players:
            entry = self.PlayerMatchScoreEntryTemplate % (
                common.generate_uid(ID, self.sportType),
                common.generate_uid(record['mid'], self.sportType), 0)
            entries.append(entry)

        codecs.open(self.output, 'w', encoding='utf-8').write(
            self.PlayerMatchScoreTemplate % '\n'.join(entries))
Exemplo n.º 4
0
    def export_playerscore_to_db(self, data, tbl, cursor):
        """将球员得分数据导入到数据库中

        :data:      赛事球员得分数据
        :cursor:    数据库游标
        """
        SQLTemplate = "INSERT INTO %s (player_id, match_id, score, match_start_time) VALUES %s"

        now = datetime.datetime.now().strftime("%s")
        scores = []
        for record in data:
            score = '(%d, %d, %.2f, %d)' % (
                common.generate_uid(record['id'], self.sportType),
                common.generate_uid(record['mid'],
                                    self.sportType), record['score'], now)
            scores.append(score)

        sql = SQLTemplate % (tbl, ','.join(values))
        cursor.execute(sql)
Exemplo n.º 5
0
    def export_playerscore_to_db(self, data, tbl, cursor):
        """将球员得分数据导入到数据库中

        :data:      赛事球员得分数据
        :cursor:    数据库游标
        """
        SQLTemplate = "INSERT INTO %s (player_id, match_id, score, match_start_time) VALUES %s"

        now = datetime.datetime.now().strftime("%s")
        scores = []
        for record in data:
            score = '(%d, %d, %.2f, %d)' % (
                        common.generate_uid(record['id'], self.sportType),
                        common.generate_uid(record['mid'], self.sportType),
                        record['score'],
                        now
                    )
            scores.append(score)

        sql = SQLTemplate % (tbl, ','.join(values))
        cursor.execute(sql)
Exemplo n.º 6
0
def export_fixtures_to_template(args):

    condition = dict()
    try:
        if args.match:
            condition['compid'] = str(args.match)
    except Exception as e:
        pass
    try:
        if args.season:
            condition['season'] = str(args.season)
    except Exception as e:
        pass

    sport_type = enum.ST_FOOTBALL
    try:
        sport_type = int(args.type)
    except Exception as e:
        pass

    if not (args.host and args.port and args.db and args.collection):
        assert False
        print("导出赛程失败!")
        return

    client = pymongo.MongoClient(args.host, int(args.port))
    database = client.get_database(args.db)
    collection = database.get_collection(args.collection)

    content = []
    content_classify = dict()
    for record in collection.find(condition):
        start_time = datetime.datetime.strptime(
            "%s %s" % (record['date'], record['time']),
            "%Y-%m-%d %H:%M").strftime('%s')

        if record['date'] in content_classify:
            content_classify[record['date']][0].append(record)
            if int(start_time) < content_classify[record['date']][1]:
                content_classify[record['date']][1] = int(start_time)
        else:
            content_classify[record['date']] = [[record], int(start_time)]

    for k, v in content_classify.iteritems():
        # 最早开赛前30分钟
        valide_time = str(v[1] - FINAL_TIME)
        for record in v[0]:

            now = datetime.datetime.now().strftime('%s')
            start_time = datetime.datetime.strptime(
                "%s %s" % (record['date'], record['time']),
                "%Y-%m-%d %H:%M").strftime('%s')
            homescore = awayscore = -1
            if int(now) > int(start_time):
                try:
                    homescore = int(record['homescore'])
                    awayscore = int(record['awayscore'])
                except Exception as e:
                    print("%s 比赛有结果有误!")
                    pass

            mid = int(record['id'])
            mid = common.generate_uid(mid, sport_type)
            homeid = common.generate_uid(int(record['homeid'][1:]), sport_type)
            awayid = common.generate_uid(int(record['awayid'][1:]), sport_type)

            row = ROW_TEMPLATE % (
                mid,
                int(record['compid']),
                str(sport_type),
                record['round'],
                start_time,
                #record['date'],
                valide_time,
                homeid,
                awayid,
                homescore,
                awayscore)
            content.append(row)

    open(args.output, 'w').write(DOCUMENT % '\n'.join(content))
Exemplo n.º 7
0
def export_fixtures_to_template(args):

    condition = dict()
    try:
        if args.match:
            condition["compid"] = str(args.match)
    except Exception as e:
        pass
    try:
        if args.season:
            condition["season"] = str(args.season)
    except Exception as e:
        pass

    sport_type = enum.ST_FOOTBALL
    try:
        sport_type = int(args.type)
    except Exception as e:
        pass

    if not (args.host and args.port and args.db and args.collection):
        assert False
        print("导出赛程失败!")
        return

    client = pymongo.MongoClient(args.host, int(args.port))
    database = client.get_database(args.db)
    collection = database.get_collection(args.collection)

    content = []
    content_classify = dict()
    for record in collection.find(condition):
        start_time = datetime.datetime.strptime("%s %s" % (record["date"], record["time"]), "%Y-%m-%d %H:%M").strftime(
            "%s"
        )

        if record["date"] in content_classify:
            content_classify[record["date"]][0].append(record)
            if int(start_time) < content_classify[record["date"]][1]:
                content_classify[record["date"]][1] = int(start_time)
        else:
            content_classify[record["date"]] = [[record], int(start_time)]

    for k, v in content_classify.iteritems():
        # 最早开赛前30分钟
        valide_time = str(v[1] - FINAL_TIME)
        for record in v[0]:

            now = datetime.datetime.now().strftime("%s")
            start_time = datetime.datetime.strptime(
                "%s %s" % (record["date"], record["time"]), "%Y-%m-%d %H:%M"
            ).strftime("%s")
            homescore = awayscore = -1
            if int(now) > int(start_time):
                try:
                    homescore = int(record["homescore"])
                    awayscore = int(record["awayscore"])
                except Exception as e:
                    print("%s 比赛有结果有误!")
                    pass

            mid = int(record["id"])
            mid = common.generate_uid(mid, sport_type)
            homeid = common.generate_uid(int(record["homeid"][1:]), sport_type)
            awayid = common.generate_uid(int(record["awayid"][1:]), sport_type)

            row = ROW_TEMPLATE % (
                mid,
                int(record["compid"]),
                str(sport_type),
                record["round"],
                start_time,
                # record['date'],
                valide_time,
                homeid,
                awayid,
                homescore,
                awayscore,
            )
            content.append(row)

    open(args.output, "w").write(DOCUMENT % "\n".join(content))