def process_item(self, item, spider): if isinstance(item, TeamItem): team = Team() if not get_team_by_url(item['url'], item['sport']): team.url = item['url'] team.name = item['name'] team.sport = item['sport'] session.add(team) session.commit() elif isinstance(item, ScheduleItem): team_id = get_team_by_url(item['team']).id schedule = get_schedule(team_id, item['date']) if not schedule: schedule = Schedule() schedule.team = team_id schedule.date = item['date'] schedule.matchup_url = item['matchup_url'] schedule.location = item['location'] schedule.opponent = get_team_by_url(item['opponent']).id schedule.result = item['result'] matched = re.match('\w (\d*)-(\d*)', item['result']) if matched: schedule.status = 'ended' schedule.score = int(matched.group(1)) schedule.opponent_score = int(matched.group(2)) schedule.score_result = schedule.score-schedule.opponent_score else: schedule.status = 'not started' session.add(schedule) session.commit() elif isinstance(item, StatsItem): if item['stat_type'] == 'team_id': self._save_team_id(item) elif item['stat_type'] == 'schedule': self._save_schedule(item) elif item['stat_type'] == 'ast_results': self._save_ast_results(item) elif item['stat_type'] == 'over_under_trends': self._save_over_under_trends(item) return item