File size: 5,503 Bytes
78fdef7
 
 
 
 
 
 
 
f8aae21
78fdef7
 
 
f8aae21
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
6c28794
 
78fdef7
 
 
 
 
01ae5e3
 
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
 
f8aae21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b9b023
f8aae21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b9b023
f8aae21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
f8aae21
 
 
 
 
78fdef7
f8aae21
78fdef7
f8aae21
 
78fdef7
f8aae21
 
 
78fdef7
f8aae21
 
 
 
78fdef7
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
scrape.py

This script automates the process of opening TradingView for all chosen global markets,
injecting their specific data source IDs, automatically switching the UI into Dark Mode, 
and exporting/downloading high-resolution Heatmap PNG images into the assets/cache_png directory.
"""

import asyncio
import json
import os
import urllib.parse
from playwright.async_api import async_playwright
from datetime import datetime
import zoneinfo

def is_market_open(iso, trading_hours, holidays):
    tz_name = trading_hours.get('timezone')
    open_str = trading_hours.get('open')
    close_str = trading_hours.get('close')
    
    if not tz_name or not open_str or not close_str:
        return False
        
    try:
        tz = zoneinfo.ZoneInfo(tz_name)
    except Exception as e:
        print(f"Error loading timezone {tz_name} for {iso}: {e}")
        return False
        
    now = datetime.now(tz)
    
    # Check weekend
    weekend_days = trading_hours.get('weekend_days', ['Saturday', 'Sunday'])
    if now.strftime('%A') in weekend_days:
        return False
        
    # Check holiday
    date_str = now.strftime('%Y-%m-%d')
    if iso in holidays and date_str in holidays[iso]:
        return False
        
    # Check hours
    current_time = now.strftime('%H:%M')
    if open_str < close_str:
        return open_str <= current_time <= close_str
    else:
        return current_time >= open_str or current_time <= close_str

async def process_market(market, browser, market_details, holidays, semaphore):
    iso = market.get('iso', '')
    chosen_code = market.get('chosen_code', '')
    if not iso or not chosen_code:
        return

    details = market_details.get(iso, {})
    trading_hours = details.get('trading_hours', {})
    
    if not is_market_open(iso, trading_hours, holidays):
        print(f"Skipping {market.get('name', iso)} (Closed or Holiday)")
        return

    async with semaphore:
        print(f"\nProcessing {market['name']} (ISO: {iso}, Code: {chosen_code})...")
        
        # Use a fresh context and page for every iteration
        context = await browser.new_context(viewport={'width': 1920, 'height': 1080})
        page = await context.new_page()
        
        # Construct URL
        params = {
            "dataSource": chosen_code,
            "blockColor": "change",
            "blockSize": "market_cap_basic",
            "grouping": "no_group"
        }
        json_str = json.dumps(params, separators=(',', ':'))
        encoded = urllib.parse.quote(json_str)
        url = f'https://www.tradingview.com/heatmap/stock/#{encoded}'
        
        try:
            await page.goto(url, wait_until='networkidle')
            await asyncio.sleep(10) # Wait for canvas to render
            
            # Perform dark mode click sequence for EVERY page
            print(f"[{iso}] Setting dark mode...")
            menu_btn = page.locator("xpath=/html/body/div[3]/div[3]/div[2]/div[3]/button[2]")
            await menu_btn.click(timeout=5000)
            await asyncio.sleep(2)
            
            toggle = page.locator('input[data-name="header-user-menu-switch-theme"]')
            await toggle.click(force=True, timeout=5000)
            print(f"[{iso}] Clicked dark mode toggle successfully!")
            
            await page.keyboard.press("Escape")
            await asyncio.sleep(3)
            
            print(f"[{iso}] Clicking the Share/Export menu...")
            share_btn = page.locator('[data-qa-id="heatmap-top-bar_share"]')
            await share_btn.click(force=True, timeout=5000)
            await asyncio.sleep(5)
            
            print(f"[{iso}] Clicking 'Download image'...")
            async with page.expect_download(timeout=15000) as download_info:
                download_btn = page.locator('[data-qa-id="heatmap-top-bar_share_download_snapshot"]')
                await download_btn.click(force=True, timeout=5000)
            
            download = await download_info.value
            out_path = f"assets/cache_png/{iso}.png"
            await download.save_as(out_path)
            print(f"[{iso}] Successfully saved to {out_path}!")

        except Exception as e:
            print(f"[{iso}] Failed to process {iso}:", e)
            
        # Close context to clean up memory
        await context.close()

async def run_async():
    print("Starting async Playwright...")
    os.makedirs('assets/cache_png', exist_ok=True)
    
    with open('assets/geo/market_chosen.json', encoding='utf-8') as f:
        chosen_markets = json.load(f)
        
    with open('assets/geo/markets.json', encoding='utf-8') as f:
        raw_markets = json.load(f)
        
    with open('assets/geo/holidays.json', encoding='utf-8') as f:
        holidays = json.load(f)
        
    market_details = { m['iso']: m for m in raw_markets }
        
    # Create semaphore for max 5 concurrent tabs
    semaphore = asyncio.Semaphore(5)
        
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        
        tasks = []
        for market in chosen_markets:
            task = asyncio.create_task(process_market(market, browser, market_details, holidays, semaphore))
            tasks.append(task)
            
        # Wait for all tasks to complete
        if tasks:
            await asyncio.gather(*tasks)
            
        await browser.close()

def run():
    asyncio.run(run_async())

if __name__ == '__main__':
    run()