Example #1
0
def lookup_teams():

    global users_dict
    global games_dict

    json = requests.get(teams_api_url, headers=headers).json()

    for t in json.get('teams'):
        team_api_url = teams_api_url + str(t.get('name'))
        team = requests.get(team_api_url, headers=headers).json()
        team_db = Team()
        team_db.name = team.get('display_name')
        team_db.id = team.get('_id')
        team_db.info = team.get('info')
        team_db.image_url = team.get('logo')
        if team_db.image_url == None:
            team_db.image_url = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png'  # This is Twitch's default logo
        team_db.created = team.get('created_at')
        team_db.updated = team.get('updated_at')
        user_ids = []
        game_ids = []

        for user in team.get('users'):
            user_id = user.get('_id')
            user_ids.append(user_id)
            game_id = lookup_user(team_db, user_id)

            # for k in users_dict:
            # 	users_d[k] = users_dict[k]

            if game_id != None:
                game_ids.append(game_id)

            # for key in games_dict:
            # 	games_d[key] = games_dict[key]

        team_db.user_ids = user_ids
        team_db.game_ids = game_ids

        try:
            db.session.add(team_db)
            db.session.commit()
            db.session.close()
        except Exception as e:
            print(str(e) + '\n')
            db.session.rollback()
Example #2
0
    def test_get_team_search_match(self):
        # Test API GET method api/team?q=<searchjson> with a match

        valid_team = None
        test_id = -1
        test_name = 'API TEST GET TEAM THROUGH SEARCH'
        test_info = 'Made Up Team Info'

        # Insert test user into database to get using the API
        valid_team = Team()
        valid_team.id = test_id
        valid_team.name = test_name
        valid_team.info = test_info
        try:
            db.session.add(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()

        filters = [dict(name='info', op='ilike', val='made up team info')]
        params = dict(q=json.dumps(dict(filters=filters)))
        response = requests.get(self.team_url,
                                params=params,
                                headers=self.headers)
        json_response = json.loads(response.text)

        # Make sure API call searches and matches the test user just entered into the db above
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json_response.get('num_results'), 1)
        self.assertEqual(json_response.get('objects')[0].get('id'), test_id)
        self.assertEqual(
            json_response.get('objects')[0].get('name'), test_name)
        self.assertEqual(
            json_response.get('objects')[0].get('info'), test_info)

        # Delte the test user that was inserted earlier in this
        try:
            db.session.delete(valid_team)
            db.session.commit()
            db.session.close()
        except:
            db.session.rollback()