aboutsummaryrefslogtreecommitdiffstats
path: root/sql/table_builder.py
blob: e5fbb5776d66d950e7926b58f2aa61bed29fa6b7 (plain) (blame)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import mysql.connector
from mysql.connector import errorcode

def sql_to_html_table(host, username, password, database_name, table_name,
                      diff_table="24h_historical", headers=["Rank", "Liver", "Subscribers", "Difference (24hr)"], root_url="https://nijitracker.com"):
    try:
        cnx = mysql.connector.connect(user=username, password=password,
                                      host=host, database=database_name)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user name or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Database does not exist")
        else:
            print(err)

    cursor = cnx.cursor()
    query = f"SELECT id, channel_id, name, subscriber_count, timestamp, profile_pic FROM {table_name} ORDER by subscriber_count DESC"
    cursor.execute(query)
    data = cursor.fetchall()
    table = "<table>"
    header = "<tr>"
    header_cols = headers
    for col in header_cols:
        header += f"<th>{col}</th>"
    header += "</tr>"
    table += header
    rank = 1
    for row in data:
        table += "<tr>"
        table += f"<td>{rank}</td>"
        rank += 1
        for i, col in enumerate(row):
            if cursor.description[i][0] == "name":
                channel_url = f"{root_url}/stats/{row[2]}"
                profile_pic_url = row[5]
                table += f"<td><a href='{channel_url}'><img src='{profile_pic_url}' height='50px' width='50px'>{col}</a></td>"
            elif cursor.description[i][0] == "subscriber_count":
                formatted_sub_count = "{:,.0f}".format(int(col))
                table += f"<td>{formatted_sub_count}</td>"
            elif cursor.description[i][0] == "timestamp":
                query = f"SELECT sub_diff FROM {diff_table} WHERE channel_id = '{row[1]}'"
                try:
                    diff_cursor = cnx.cursor()
                    diff_cursor.execute(query)
                    diff_data = diff_cursor.fetchall()
                    old_sub_count = int(diff_data[0][0])
                    current_sub_count = int(row[3])
                    if old_sub_count > current_sub_count:
                        difference = f"-{old_sub_count - current_sub_count}"
                    else:
                        difference = f"+{current_sub_count - old_sub_count}"
                    table += f"<td>{difference}</td>"
                except IndexError:
                    raise Exception("Are you trying to use a new set of channels?\nPlease delete last_refresh.txt in data folder first!")
            elif cursor.description[i][0] not in ["id", "channel_id", "profile_pic"]:
                table += f"<td>{col}</td>"

                
        table += "</tr>"
    table += "</table>"
    style = "<style>\
            table {\
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;\
                font-size: 16px;\
                border-collapse: separate;\
                border-spacing: 0;\
                width: 100%;\
                max-width: 1570px;\
                margin: 0 auto;\
                background-color: #fff;\
                border-radius: 5px;\
                overflow: hidden;\
                box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);\
            }\
            th, td {\
                text-align: left;\
                padding: 12px 15px;\
                font-size: 18px;\
                border-bottom: 1px solid #ddd;\
            }\
            th {\
                background-color: #2a4b71;\
                color: #fff;\
                font-weight: bold;\
                text-transform: uppercase;\
                letter-spacing: 0.03em;\
            }\
            td:nth-child(3), td:nth-child(4) {\
                text-align: left;\
                padding: 12px 15px;\
                font-size: 18px;\
                border-bottom: 1px solid #ddd;\
            }\
            tbody tr:nth-child(even) {\
                background-color: #f2f2f2;\
            }\
            tbody tr:hover {\
                background-color: #ddd;\
            }\
            a {\
                color: #3c8dbc;\
                text-decoration: none;\
            }\
            </style>"
    style += "<style>\
        @media screen and (max-width: 1024px) {\
            table {\
                font-size: 14px;\
            }\
            th, td {\
                padding: 8px 10px;\
                font-size: 16px;\
            }\
        }\
        @media screen and (max-width: 768px) {\
            th, td {\
                padding: 5px 8px;\
                font-size: 14px;\
            }\
        }\
        @media screen and (max-width: 600px) {\
            th, td {\
                padding: 3px 5px;\
                font-size: 10px;\
            }\
        }\
        @media screen and (max-width: 400px) {\
            th, td {\
                padding: 2px 4px;\
                font-size: 8px;\
            }\
        }\
        </style>"


    cursor.close()
    cnx.close()
    return table + style


def generate_individual_table(host, username, password, database_name, table_name, param="LIMIT 7"):
    try:
        cnx = mysql.connector.connect(user=username, password=password,
                                      host=host, database=database_name)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user name or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Database does not exist")
        else:
            print(err)

    cursor = cnx.cursor()
    query = f"SELECT subscriber_count, timestamp FROM {table_name} GROUP BY DATE(timestamp) ORDER by timestamp DESC " + param
    
    cursor.execute(query)
    data = cursor.fetchall()
    table = "<table>"
    header = "<tr>"
    header_cols = ["Subscribers", "Timestamp"]
    for col in header_cols:
        header += f"<th>{col}</th>"
    header += "</tr>"
    table += header
    for row in data:
        table += "<tr>"
        for i, col in enumerate(row):
            if cursor.description[i][0] == "subscriber_count":
                formatted_sub_count = "{:,.0f}".format(int(col))
                table += f"<td>{formatted_sub_count}</td>"
            else:
                table += f"<td>{col}</td>"
        table += "</tr>"
    table += "</table>"
    style = "<style>\
            table {\
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;\
                font-size: 16px;\
                border-collapse: separate;\
                border-spacing: 0;\
                width: 100%;\
                max-width: 1570px;\
                margin: 0 auto;\
                background-color: #fff;\
                border-radius: 5px;\
                overflow: hidden;\
                box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);\
            }\
            th, td {\
                text-align: left;\
                padding: 12px 15px;\
                font-size: 18px;\
                border-bottom: 1px solid #ddd;\
            }\
            th {\
                background-color: #2a4b71;\
                color: #fff;\
                font-weight: bold;\
                text-transform: uppercase;\
                letter-spacing: 0.03em;\
            }\
            td:nth-child(3), td:nth-child(4) {\
                text-align: left;\
                padding: 12px 15px;\
                font-size: 18px;\
                border-bottom: 1px solid #ddd;\
            }\
            tbody tr:nth-child(even) {\
                background-color: #f2f2f2;\
            }\
            tbody tr:hover {\
                background-color: #ddd;\
            }\
            a {\
                color: #3c8dbc;\
                text-decoration: none;\
            }\
            </style>"
    style += "<style>\
        @media screen and (max-width: 1024px) {\
            table {\
                font-size: 14px;\
            }\
            th, td {\
                padding: 8px 10px;\
                font-size: 16px;\
            }\
        }\
        @media screen and (max-width: 768px) {\
            th, td {\
                padding: 5px 8px;\
                font-size: 14px;\
            }\
        }\
        @media screen and (max-width: 600px) {\
            th, td {\
                padding: 3px 5px;\
                font-size: 12px;\
            }\
        }\
        @media screen and (max-width: 400px) {\
            th, td {\
                padding: 2px 4px;\
                font-size: 10px;\
            }\
        }\
        </style>"

    cursor.close()
    cnx.close()
    return table + style
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage