# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: scraping
#     language: python
#     name: scraping
# ---

# %load_ext nb_black

from bs4 import BeautifulSoup
from lxml import etree
import requests
from lxml.etree import tostring
from collections import defaultdict

mapping = {
    "mahmood & blanco": "mahmood e blanco",
    "rettore & ditonellapiaga": "ditonellapiaga con donatella rettore",
    "highsnub & hu": "highsnob e hu",
    "aka7even": "aka 7even",
    "aka7ven": "aka 7even",
    "ditonellapiaga con rettore": "ditonellapiaga con donatella rettore",
}

bets_map = defaultdict(list)

# ### Unibet

bets_page = open("bets.html", "r").read()
bets_soup = BeautifulSoup(bets_page, "html.parser")

dom = etree.HTML(str(bets_soup))
rows = dom.xpath("/html/body/table/tbody//tr")
for row in rows:
    singer = row.xpath("td[3]/a/text()")[0].lower()
    if singer in mapping:
        singer = mapping[singer]
    bet = float(row.xpath("td[5]/text()")[0])
    bets_map[singer].append(bet)

# ### Goldbet

bets_page = open("bets_2.html", "r").read()
bets_soup = BeautifulSoup(bets_page, "html.parser")

dom = etree.HTML(str(bets_soup))
rows = dom.xpath("//td[contains(@class,'match aview')]")
for row in rows:
    singer = row.xpath("span[1]/text()")[0].lower()
    if singer in mapping:
        singer = mapping[singer]
    bet = float(row.xpath("span[2]/text()")[0])
    bets_map[singer].append(bet)

bets_map

singers_page = open("singers.html", "r").read()
soup_singers = BeautifulSoup(singers_page, "html.parser")

singers_map = {}

dom_singers = etree.HTML(str(soup_singers))
rows_singers = dom_singers.xpath("/html/body/div//div[@class='song-card']")
for row in rows_singers:
    singer = row.xpath("div/div[2]/div[1]/div/h1/text()")[0].lower().replace('’',"'")
    soup_temp = BeautifulSoup(tostring(row.xpath("div/div[2]/h1[2]")[0]), "html.parser")
    quotation = float(soup_temp.getText().replace('Baudi','').strip())
    singers_map[singer] = quotation


singers_map

# +
singers = []
costs = []
bets = []
scores = []

for k, v in singers_map.items():
    singers.append(k)
    costs.append(v)
    current_bet = sum(bets_map[k]) / len(bets_map[k])
    bets.append(current_bet)
    scores.append((1 / current_bet) / v)
# -

list(zip(singers, costs, bets, scores))

# +
from pulp import LpProblem, LpMaximize, LpVariable, LpInteger


def maximization_scores(singers, costs, bets, scores):
    P = range(len(singers))
    # Declare problem instance, maximization problem
    prob = LpProblem("Portfolio", LpMaximize)
    # Declare decision variable x, which is 1 if a
    # singer is part of the portfolio and 0 else
    x = LpVariable.matrix("x", list(P), 0, 1, LpInteger)
    # Objective function -> Maximize scores
    prob += sum(scores[p] * x[p] for p in P)
    ### Constraint definition

    # constraint for the number of singers in the team
    prob += sum(x[p] for p in P) == 5
    # constraint on the total amount of credits
    prob += sum(costs[p] * x[p] for p in P) <= 100
    # constraint for having every player only once in the final team
    for elem in singers:
        prob += sum(x[p] for p in P if singers[p] == elem) <= 1
    # Start solving the problem instance
    prob.solve()
    # Extract solution
    portfolio = [(singers[p], costs[p], bets[p], scores[p]) for p in P if x[p].varValue]
    portfolio = sorted(portfolio, key=lambda x: x[3], reverse=True)
    return portfolio


# -

portfolio = maximization_scores(singers, costs, bets, scores)

portfolio

sorted(list(zip(singers, costs, scores)), key=lambda x: x[2], reverse=True)



StarcoderdataJupyterScriptsDedupFiltered