Exemplo n.º 1
0
def get_games():
    startDate = dt.date(2018, 1, 1)
    last_update = queries.check_last_update(Game)
    print(last_update)
    if last_update == None: last_update = dt.date(2018, 1, 1)
    print(f'Games last_update: {last_update}')
    for i in range(last_update.year, yesterday.year + 1):
        print(i)
        if last_update > dt.date(i, 9, 28):
            pass
        elif (i < yesterday.year):
            dfGames = update_played_games(i, f'{i}-01-01', f'{i}-10-15')
            queries.insert_data(df=dfGames,
                                table='game',
                                replace_append='append')

        elif (i == yesterday.year) and (yesterday > dt.date(2020, 3, 26)):
            fromDate = last_update + dt.timedelta(days=1)
            dfGames = update_played_games(
                i, f'{i}-{fromDate.month}-{fromDate.day}', yesterday)
            queries.insert_data(df=dfGames,
                                table='game',
                                replace_append='append')

        else:
            print('games up to date')
            return
Exemplo n.º 2
0
def extract_attr(id_user,
                 id_doc,
                 attributes,
                 status=0,
                 descript=str(datetime.datetime)):
    text = ''
    for a_name, a_value in attributes.items():
        text = text + str(a_name) + ' = ' + str(a_value) + ', '
    temp = '\'' + str(id_user) + '\', \'' + str(
        id_doc
    ) + '\', \'' + text + '\', \'' + descript + '\', \'' + status + '\''
    insert_data(table_name='attributes', data=temp)
    return True
Exemplo n.º 3
0
def on_request(ch, method, props, body):
    """loading data recieved from rpc client
    """
    data = json.loads(str(body)[2:-1])
    print('Recieved -> {}'.format(data))

    # Display what request we got
    logger.info('Recieved -> {}'.format(data))

    # Inthis section as per client request calling the coresponding method which return desired output
    if data['method'] == 'GET':
        response = Q.display_data()
    elif data['method'] == 'POST':
        response = Q.insert_data(data['username'], data['comment'])

    elif data['method'] == 'PUT':
        response = Q.update_data(data['id'], data['username'], data['comment'])

    elif data['method'] == 'DELETE':
        response = Q.delete_data(data['id'])

    else:
        response = "wrong api"

    # sending response back to client
    ch.basic_publish(
        exchange='',
        routing_key=props.reply_to,
        properties=pika.BasicProperties(correlation_id=props.correlation_id),
        body=json.dumps(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)
Exemplo n.º 4
0
def on_request(ch, method, props, body):

    data = json.loads(str(body)[2:-1])
    print('Recieved -> {}'.format(data))

    logger.info('Recieved -> {}'.format(data))

    if data['method'] == 'GET':
        response = Q.display_data()
    elif data['method'] == 'POST':
        response = Q.insert_data(data['username'], data['comment'])

    elif data['method'] == 'PUT':
        response = Q.update_data(data['id'], data['username'], data['comment'])

    elif data['method'] == 'DELETE':
        response = Q.delete_data(data['id'])

    else:
        response = "wrong api"

    ch.basic_publish(
        exchange='',
        routing_key=props.reply_to,
        properties=pika.BasicProperties(correlation_id=props.correlation_id),
        body=json.dumps(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)
Exemplo n.º 5
0
def get_teams():
    num_ids = session.query(Team.id).count()
    session.commit()
    if num_ids > 0:
        print('teams exists')
    else:
        print('Fetching teams')
        data = fetch.get_data(f'{constants.API_URL}/teams')
        list_of_teams = []
        for i in data['teams']:
            if i['sport']['name'] == 'Major League Baseball':
                list_of_teams.append(i)
        teams_df = pd.DataFrame.from_dict(list_of_teams)
        teams_df = utils.clean_teams(teams_df)
        print('Inserting Teams')
        queries.insert_data(teams_df, 'team', 'replace')
        print('Teams inserted')
def distrib_docs(rubric_name, user_id_list):
    doc_list = set_all_where(table_name='documents',
                             field='id_doc',
                             key_field='rubric',
                             key=str(rubric_name),
                             type_comparison='=')
    i = 0
    max_i = len(user_id_list)
    for d in doc_list:
        rel = '\'' + str(
            user_id_list[i]) + '\',  \'' + str(d) + '\',  \'' + 'executor\''
        insert_data(table_name='relation', data=rel)
        i = i + 1
        if i == max_i:
            i = 0
        rel = '\'' + str(
            user_id_list[i]) + '\',  \'' + str(d) + '\',  \'' + 'executor\''
        insert_data(table_name='relation', data=rel)
    return True
Exemplo n.º 7
0
def on_message(client, userdata, msg):
  print_log(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') + " | message received!")
  try:
    message = json.loads(msg.payload)
  except ValueError:
    print_log("Invalid message format!")
    return
  station_secret = message["id_secret"]
  sensor = message["sensor"]
  value = message["value"]
  format_date = message["datetime"].split("+")[0]
  timestamp = datetime.strptime(format_date, '%Y-%m-%dT%H:%M:%S')
  sid = db.get_id_from_secret(station_secret)
  if (sid and sensor != "ACC" and sensor != "STR"):
    db.insert_data(sid, sensor, value, timestamp)
  if (not sid):
    sid = "-"
    #sid = db.new_station(station_secret)
    #print_log("Add new sensor with ID " + str(sid))
  print_log("receive: sid: {} | sensor: {} | value: {} | timestamp: {}".format(sid,sensor,value,timestamp))
Exemplo n.º 8
0
def update_player_table():
    last_update = queries.check_last_update(Player, is_roster=True)

    if last_update == None: last_update = dt.date(2018, 1, 1)
    next_update = last_update + dt.timedelta(6)
    print(type(last_update))

    if next_update < constants.today:
        print(f'updating, last update: {last_update}')
        team_ids = session.query(Team.id).all()
        session.commit()
        team_ids = [x.id for x in team_ids]
        # print(team_ids)
        df = fetch.get_rosters(team_ids)
        df.reset_index(drop=True, inplace=True)
        df.jerseynumber.replace("", -1, inplace=True)
        df.position_code.replace('O', 9, inplace=True)
        #print(df.shape)
        queries.insert_data(df, 'player')
    else:
        print(f'rosters up to date, next update: {next_update}')
Exemplo n.º 9
0
def get_game_stats(game_data):
    team_stats = []
    player_stats = []
    for i in game_data:
        print(i['gamedate'])
        gm = get_data(i['boxscore'])
        gamepk = re.search(r'/game/([^/]+)', i['boxscore']).group(1)

        for home_away in gm['teams']:
            team_stats.append({
                'gamepk':
                gamepk,
                'gamedate':
                i['gamedate'],
                'homegame':
                True if home_away == 'home' else False,
                'team_name':
                gm['teams'][home_away]['team']['name'],
                'team_id':
                gm['teams'][home_away]['team']['id'],
                'batting':
                gm['teams'][home_away]['teamStats']['batting'],
                'pitching':
                gm['teams'][home_away]['teamStats']['pitching'],
                'fielding':
                gm['teams'][home_away]['teamStats']['fielding']
            })

            for playerID in gm['teams'][home_away]['players']:
                player_info = {
                    'gamepk':
                    gamepk,
                    'gamedate':
                    i['gamedate'],
                    'homegame':
                    True if home_away == 'home' else False,
                    'team_name':
                    gm['teams'][home_away]['team']['name'],
                    'team_id':
                    gm['teams'][home_away]['team']['id'],
                    'player_id':
                    gm['teams'][home_away]['players'][playerID]['person']
                    ['id'],
                    'player_fullname':
                    gm['teams'][home_away]['players'][playerID]['person']
                    ['fullName'],
                    'player_link':
                    gm['teams'][home_away]['players'][playerID]['person']
                    ['link'],
                    'position_code':
                    gm['teams'][home_away]['players'][playerID]['position']
                    ['code'],
                    'position_name':
                    gm['teams'][home_away]['players'][playerID]['position']
                    ['name'],
                    'position_type':
                    gm['teams'][home_away]['players'][playerID]['position']
                    ['type'],
                    'batting':
                    gm['teams'][home_away]['players'][playerID]['stats']
                    ['batting'],
                    'fielding':
                    gm['teams'][home_away]['players'][playerID]['stats']
                    ['fielding'],
                    'pitching':
                    gm['teams'][home_away]['players'][playerID]['stats']
                    ['pitching']
                }
                player_stats.append(player_info)

    df_team_stats = clean_game_data(pd.DataFrame.from_dict(team_stats))
    df_player_stats = clean_game_data(pd.DataFrame.from_dict(player_stats),
                                      isPlayer=True)

    queries.insert_data(df_team_stats, 'teamstat')

    queries.insert_data(df_player_stats, 'playerstat')

    return True
Exemplo n.º 10
0
def choice_attr(attrs1, attrs2):  # выбор между 2мя вариантами аттрибутов
    #doc = attrs1.id_doc
    doc = set_field(table_name='attributes',
                    field='id_doc',
                    key_field='id_attr',
                    key=str(attrs1))
    #score = similarity(attrs1.text, attrs2.text)
    text1 = set_field(table_name='attributes',
                      field='attr_text',
                      key_field='id_attr',
                      key=str(attrs1))
    text2 = set_field(table_name='attributes',
                      field='attr_text',
                      key_field='id_attr',
                      key=str(attrs2))
    score = similarity(text1, text2)

    #r1 = attrs1.id_user.rating
    #r2 = attrs2.id_user.rating

    u1 = set_field(table_name='attributes',
                   field='id_user',
                   key_field='id_attr',
                   key=str(attrs1))
    u2 = set_field(table_name='attributes',
                   field='id_user',
                   key_field='id_attr',
                   key=str(attrs2))
    r1 = int(
        set_field(table_name='users',
                  field='rating',
                  key_field='id_user',
                  key=str(u1)))
    r2 = int(
        set_field(table_name='users',
                  field='rating',
                  key_field='id_user',
                  key=str(u2)))

    #TODO: запросы
    if score > 0.2:
        #doc.status = 'yellow'
        upd_field(table_name='documents',
                  field='status',
                  value='yellow',
                  key_field='id_doc',
                  key=str(doc))
    if score < 0.15:
        #doc.status = 'red'
        upd_field(table_name='documents',
                  field='status',
                  value='red',
                  key_field='id_doc',
                  key=str(doc))
    if score == 1:
        #doc.status = 'green'
        upd_field(table_name='documents',
                  field='status',
                  value='green',
                  key_field='id_doc',
                  key=str(doc))  # изменяем статус документа
        upd_field(table_name='users',
                  field='rating',
                  value=str(r1 + 1),
                  key_field='id_user',
                  key=str(u1))  # повышаем рейтинг
        upd_field(table_name='users',
                  field='rating',
                  value=str(r2 + 1),
                  key_field='id_user',
                  key=str(u2))
        upd_field(table_name='attributes',
                  field='status',
                  value='1',
                  key_field='id_attr',
                  key=str(attrs1))  # изменяем статус атрибута
        drop_data(table_name='attributes',
                  key_field='id_attr',
                  key=str(attrs2))  # удаляем ненужные атрибуты

        return True

    if r1 > r2:
        #attrs1.status = 1
        upd_field(table_name='attributes',
                  field='status',
                  value='1',
                  key_field='id_attr',
                  key=str(attrs1))
        #attrs1.id_user.rating = r1 + 1
        upd_field(table_name='users',
                  field='rating',
                  value=str(r1 + 1),
                  key_field='id_user',
                  key=str(u1))
        #attrs2.id_user.rating = r2 - 1
        upd_field(table_name='users',
                  field='rating',
                  value=str(r2 - 1),
                  key_field='id_user',
                  key=str(u2))  # понижаем рейтинг
        return True
    if r1 < r2:
        #attrs2.status = 1
        upd_field(table_name='attributes',
                  field='status',
                  value='1',
                  key_field='id_attr',
                  key=str(attrs2))
        #attrs1.id_user.rating = r1 - 1
        upd_field(table_name='users',
                  field='rating',
                  value=str(r1 - 1),
                  key_field='id_user',
                  key=str(u1))
        #attrs2.id_user.rating = r2 + 1
        upd_field(table_name='users',
                  field='rating',
                  value=str(r2 + 1),
                  key_field='id_user',
                  key=str(u2))
        return True

    if r1 == r2:
        #TODO: переадресация документа
        user_list = set_all_where(table_name='users',
                                  field='id_user',
                                  key_field='rating',
                                  key=str(r1),
                                  type_comparison='>')
        if len(user_list) == 0:
            i = random.randint(0, 1)
            if i:
                upd_field(table_name='attributes',
                          field='status',
                          value='1',
                          key_field='id_attr',
                          key=str(attrs2))
                upd_field(table_name='users',
                          field='rating',
                          value=str(r1 - 1),
                          key_field='id_user',
                          key=str(u1))
                upd_field(table_name='users',
                          field='rating',
                          value=str(r2 + 1),
                          key_field='id_user',
                          key=str(u2))
            else:
                upd_field(table_name='attributes',
                          field='status',
                          value='1',
                          key_field='id_attr',
                          key=str(attrs1))
                upd_field(table_name='users',
                          field='rating',
                          value=str(r1 + 1),
                          key_field='id_user',
                          key=str(u1))
                upd_field(table_name='users',
                          field='rating',
                          value=str(r2 - 1),
                          key_field='id_user',
                          key=str(u2))
            return True
        i = random.randint(0, len(user_list) - 1)
        id_user = user_list[i]
        #пусть будет три типа связи: author, executor, checker
        rel = '\'' + str(id_user) + '\',  \'' + str(
            doc) + '\',  \'' + 'checker\''
        insert_data(table_name='relation', data=rel)