def set_player_data(player_id=None, playername=None): """ Sets prices and points for all the players of the user :param player_id: Id of the football player :param playername: Football player name :return: Players of the user id """ days_left = days_wo_price(player_id) prices, points = list(), list() if days_left: dates, prices, points = get_player_data(player_id=player_id) if days_left >= 365: days_left = len(dates) if not db.rowcount('SELECT idp FROM players WHERE idp=%s' % player_id): playername, position, club_id, price = com.get_player_info(player_id) set_new_player(player_id, playername, position, club_id) db.many_commit_query('INSERT IGNORE INTO prices (idp,date,price) VALUES (%s' % player_id + ',%s,%s)', zip(dates[:days_left], prices[:days_left])) for point in points: db.nocommit_query('INSERT INTO points (idp,gameday,points) VALUES (%s,%s,%s) \ ON DUPLICATE KEY UPDATE points=%s' % (player_id, point[0], point[1], point[1])) db.commit() if len(dates) != len(prices): print "%sThe prices arrays and dates haven't the same size.%s" % (RED, ENDC) return prices, points
def set_user_players(user_id=None, username=None): """ Set the players of the user. :param user_id: Id of the user. :param username: Name of the user. :return: [[player_id, playername, club_id, club_name, value, player_points, position]] """ user_players = com.get_user_players(user_id) print 'Updating players of %s =>' % username, for player in user_players: player_id, playername, club_id, club_name, value, player_points, position = player db.nocommit_query('INSERT IGNORE INTO clubs (idcl, name) VALUES (%s, "%s")' % (club_id, club_name)) db.nocommit_query('INSERT IGNORE INTO players (idp, name, position, idcl) VALUES (%s, "%s", "%s", %s)' % ( player_id, playername, position, club_id)) db.nocommit_query('INSERT IGNORE INTO owners (idp, idu) VALUES (%s, %s)' % (player_id, user_id)) db.commit() print '%sdone%s.' % (GREEN, ENDC) return user_players
def set_users_data(): """ Gets the last data of the users from Comunio and saves it to database. :return: {{user_id: username, user_points, teamvalue, money, maxbid}} """ users_data = dict() users_info = com.get_users_info() print 'Updating users data =>', for user in users_info: [user_name, user_id, user_points, teamvalue, money, maxbid] = user db.nocommit_query('INSERT IGNORE INTO users (idu, name) VALUES (%s, "%s")' % (user_id, user_name)) db.nocommit_query('INSERT IGNORE INTO user_data (idu, date, points, money, teamvalue, maxbid) \ VALUES (%s, "%s", %s, %s, %s, %s)' % (user_id, today, user_points, money, teamvalue, maxbid)) db.nocommit_query('DELETE FROM owners WHERE idu="%s"' % user_id) users_data[user_id] = [user_name, user_points, teamvalue, money, maxbid] db.commit() print '%sdone%s.' % (GREEN, ENDC) return users_data
def get_all_players(): ''' Get all players ''' req = requests.get('http://stats.comunio.es/squad', headers=headers).content soup = bs4(req) clubs = soup.find('td', {'class': 'clubPics'}).find_all('a') for club in clubs: link = club['href'] club_num = re.search("([0-9]+)-(.*)", link).group(1) club_name = re.search("([0-9]+)-(.*)", link).group(2).replace("+", " ") db.commit_query('INSERT IGNORE INTO clubs (idcl, name) VALUES (%s, "%s")' % (club_num, club_name)) soup = bs4(requests.get('http://stats.comunio.es' + link, headers=headers).content) for row in soup.find('table', {'class': 'rangliste'}).find_all('tr', re.compile(r"r[1-2]"))[1:-1]: nuna = re.search("([0-9]+)-(.*)", row.find('a', {'class': 'nowrap'})['href']) number = nuna.group(1) name = nuna.group(2).replace("+", " ").strip() position = _position_translation(row.contents[5].text) db.nocommit_query(' INSERT INTO players (idp, name, position, idcl) VALUES (%s, "%s", %s, %s) ' % ( number, name, position, club_num)) db.commit()
def get_all_prices(name=None, incremental=True, clean=False): ''' Get all prices from a player ''' sql = 'SELECT idp,name FROM players' if name != None: sql += ' WHERE name = "%s" ' % name.title() players = db.simple_query(sql) if incremental: for player in players: idp = player[0] name = player[1] days_wo_price = _array_days_wo_price(name) for day in days_wo_price: try: # Consultamos el webservice de comunio price = int(soapclient.service.getquote(idp, day)) db.nocommit_query('INSERT IGNORE INTO prices (idp,date,price) VALUES (%s,"%s",%s)' % ( idp, day.replace("-", ""), price)) except WebFault: continue db.commit() else: if clean: day_ini = date.today() players = map(list, players) for player in players: idp = player[0] name = player[1] days = 0 first = str(db.simple_query('SELECT date FROM prices WHERE idp=%s ORDER BY date ASC LIMIT 1' % idp)[0][0]) day_ini = date(int(first[:4]), int(first[4:6]), int(first[6:8])) while True: try: day = day_ini - timedelta(days=days) # Consultamos el webservice de comunio price = int(soapclient.service.getquote(idp, day.strftime('%Y-%m-%d'))) db.nocommit_query('INSERT IGNORE INTO prices (idp,date,price) VALUES (%s,"%s",%s)' % ( idp, day.strftime('%Y%m%d'), price)) days += 1 except WebFault: # Cuando no exista más precios para ese jugador romperemos el while db.commit() break except: db.commit() days += 1 continue