File size: 4,439 Bytes
abcb943
 
 
 
 
 
 
 
 
7f1dc32
abcb943
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c94d1f
 
 
 
 
7f1dc32
8c94d1f
 
132b34b
7f1dc32
8c94d1f
 
 
 
 
 
 
 
abcb943
 
7f1dc32
 
abcb943
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19e44c2
 
abcb943
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.core.os_manager import ChromeType
from helper import convert_percentage_to_decimal

def setup_driver():
    """Setup and return a configured Chrome WebDriver with optimized settings"""
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--disable-logging")
    chrome_options.add_argument("--log-level=3")
    chrome_options.add_argument("--silent")
    chrome_options.page_load_strategy = 'eager'
    chrome_options.add_argument(
        "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    )

    # Check if we're running in Hugging Face Spaces or locally
    if 'HF_SPACE' in os.environ:
        # Hugging Face Space is detected, handle accordingly (example for versioning)
        print("Running on Hugging Face Space.")
        chromedriver_path = ChromeDriverManager().install()
    else:
        # Local environment setup
        print("Running chrome webdriver.")
        chromedriver_path = ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()

    # Create the Service object using the installed chromedriver
    service = Service(executable_path=chromedriver_path)

    # Return the configured WebDriver instance
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    return driver

def get_weekly_meta():

    print("======================== IN get_weekly_meta() ===============================\n")
    BASE_URL = "https://www.op.gg/statistics/champions?tier=challenger&period=week&mode=ranked"
    driver = setup_driver()
    
    try:
        driver.get(BASE_URL)
        table = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#content-container > div:nth-child(2) > table"))
        )
        
        # Extract table rows
        rows = table.find_elements(By.TAG_NAME, "tr")
        
        # Define the column order
        columns = ["rank", "champion", "games", "KDA", "WR", "pick", "ban", "cs", "gold"]
        
        data = []
        for row in rows[1:]:  # Skip the header row
            cells = row.find_elements(By.TAG_NAME, "td")
            row_data = [cell.text for cell in cells]
            
            if len(row_data) >= len(columns):
                # Remove ":1" from KDA format
                row_data[3] = row_data[3].replace(":1", "")
                # Convert WR, pick, and ban percentages to decimals
                row_data[4] = convert_percentage_to_decimal(row_data[4])
                row_data[5] = convert_percentage_to_decimal(row_data[5])
                row_data[6] = convert_percentage_to_decimal(row_data[6])
                # Remove commas from the gold values
                row_data[8] = int(row_data[8].replace(",", ""))
                
                data.append(row_data[:len(columns)])
        
        # Create a DataFrame with the extracted data
        df = pd.DataFrame(data, columns=columns)
        
        # Ensure the directory exists
        os.makedirs('./util/data', exist_ok=True)
        
        # Define the save path
        save_path = "./util/data/weekly_meta_stats.csv"
        
        # Automatically save the DataFrame to a CSV file in the specified directory
        df.to_csv(save_path, index=False)
        
        # Print confirmation message
        print(f"Saved weekly meta to {save_path}")

        print("======================== Exiting: IN get_weekly_meta() ===============================")
        
        return df
    
    except Exception as e:
        print(f"Error: {e}")
        return None
    
    finally:
        driver.quit()

# if __name__ == "__main__":
#     weekly_meta_data = get_weekly_meta()
    
#     if weekly_meta_data is not None:
#         print(weekly_meta_data)