main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3from pathlib import Path
 4
 5from quickchart import QuickChart
 6
 7from config import DOWNLOAD_DIR
 8from networking import download_file
 9from utils import number, rand_string, ts_to_dt
10
11
12async def generate_chart(klines: list[list], interval: str, title: str, subtitle: str) -> str:
13    """Generate a candlestick chart.
14
15    Docs: https://quickchart.io/documentation
16    """
17    qc = QuickChart()
18    qc.width = 3000  # max 3000
19    qc.height = 1080
20    qc.version = "3"
21    qc.format = "png"
22    qc.device_pixel_ratio = 1.0
23    data_str = [f"{{ x: new Date('{ts_to_dt(x[0]):%Y-%m-%dT%H:%M:%S}+08:00').getTime(), o: {number(x[1])}, h: {number(x[2])}, l: {number(x[3])}, c: {number(x[4])} }}" for x in klines]
24    config_str = f"""
25    {{
26        type: 'candlestick',
27        data: {{
28            datasets: [{{
29                data: [{",".join(data_str)}],
30            }}],
31        }},
32        options: {{
33            scales:{{
34                x: {{
35                    adapters: {{ date: {{ zone: 'UTC+8' }} }},
36                    type: 'time',
37                    time: {{
38                        unit: '{"minute" if interval[-1] in ["m", "h"] else "day"}',
39                        stepSize: 1,
40                        displayFormats: {{
41                            minute: 'HH:mm',
42                            hour: 'HH:mm',
43                            day: 'MM-dd',
44                            month: 'MM-dd',
45                            year: 'MM-dd',
46                        }}
47                    }},
48                    ticks: {{
49                        autoSkip: false,
50                        font: {{
51                            size: 16,
52                            weight: 'bold',
53                        }},
54                    }}
55                }},
56                y: {{
57                    ticks: {{
58                        font: {{
59                            size: 24,
60                            weight: 'bold',
61                        }},
62                    }}
63                }},
64            }},
65            plugins: {{
66                title: {{
67                    display: true,
68                    text: '{title}',
69                    color: 'rgb(0, 0, 0)',
70                    font: {{
71                        size: 24,
72                        weight: 'bold',
73                    }},
74                }},
75                subtitle: {{
76                    display: true,
77                    text: '{subtitle}',
78                    font: {{
79                        size: 20,
80                        weight: 'bold',
81                    }},
82                }},
83                legend: {{
84                    display: false,
85                }},
86            }},
87        }},
88    }}"""
89    qc.config = config_str.replace("\n", "").replace("  ", "")  # type: ignore
90    path = Path(DOWNLOAD_DIR) / f"{rand_string()}.png"
91    try:
92        return await download_file(qc.get_url(), path=path)
93    except Exception:
94        return ""