-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
364 lines (311 loc) · 12.9 KB
/
config.py
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""Module to handle bot configuration and affiliate link processing."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
import logging
from pathlib import Path
from typing import Any
import requests # type: ignore[import-untyped]
import yaml # type: ignore[import-untyped]
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ConfigurationManager:
"""Class to manage bot configuration and affiliate link processing."""
CONFIG_PATH = Path("data/config.yaml")
CREATORS_CONFIG_PATH = Path("creators_affiliates.yaml")
TIMEOUT = 10
def __init__(self) -> None:
"""Initialize the configuration manager."""
# Telegram settings
self.bot_token: str = ""
self.delete_messages: bool = True
self.excluded_users: list[str] = []
self.discount_keywords: list[str] = []
# Messages
self.msg_affiliate_link_modified: str = ""
self.msg_reply_provided_by_user: str = ""
# Affiliate settings
self.creator_percentage: int = 10
# Logging
self.log_level: str = "INFO"
# Internal data
self.domain_percentage_table: dict[str, list[dict[str, Any]]] = {}
self.all_users_configurations: dict[str, dict] = {}
self.last_load_time: datetime | None = None
def _load_user_configuration(
self, user: str, creator_percentage: int, user_data: dict
) -> dict:
"""Load user-specific configuration for affiliate programs and settings.
Args:
----
user (str): User ID.
creator_percentage (int): Percentage of creator's influence.
user_data (dict): User-specific configuration data.
Returns:
-------
dict: Processed user configuration.
"""
return {
"user": user,
"percentage": creator_percentage,
"amazon": {
"advertisers": user_data.get("amazon", {}),
},
"awin": {
"publisher_id": user_data.get("awin", {}).get("publisher_id", None),
"advertisers": user_data.get("awin", {}).get("advertisers", {}),
},
"admitad": {
"publisher_id": user_data.get("admitad", {}).get("publisher_id", None),
"advertisers": user_data.get("admitad", {}).get("advertisers", {}),
},
"tradedoubler": {
"publisher_id": user_data.get("tradedoubler", {}).get(
"publisher_id", None
),
"advertisers": user_data.get("tradedoubler", {}).get("advertisers", {}),
},
"aliexpress": {
"discount_codes": user_data.get("aliexpress", {}).get(
"discount_codes", None
),
"app_key": user_data.get("aliexpress", {}).get("app_key", None),
"app_secret": user_data.get("aliexpress", {}).get("app_secret", None),
"tracking_id": user_data.get("aliexpress", {}).get("tracking_id", None),
},
}
def _load_user_configuration_from_url(
self, user_id: str, percentage: int, url: str
) -> dict | None:
"""Load user-specific configuration from a URL.
Args:
----
user_id (str): ID of the user.
percentage (int): User's affiliate percentage.
url (str): URL to fetch the user's configuration YAML file.
Returns:
-------
dict | None: User configuration data or None if an error occurs.
"""
try:
response = requests.get(url, timeout=self.TIMEOUT)
response.raise_for_status()
user_data = yaml.safe_load(response.text)
return self._load_user_configuration(
user_id, percentage, user_data.get("configuration", {})
)
except requests.RequestException:
logger.exception("Error loading configuration for %s from %s", user_id, url)
return None
def _add_to_domain_table(
self, domain: str, user_id: str, affiliate_id: str | None, percentage: int
) -> None:
"""Add a user to the domain percentage table.
Args:
----
domain (str): Domain name (e.g., "amazon").
user_id (str): User ID (e.g., "user", "HectorziN").
affiliate_id (str | None): Affiliate ID for the domain.
percentage (int): User's percentage share for the domain.
"""
if affiliate_id:
if domain not in self.domain_percentage_table:
self.domain_percentage_table[domain] = []
if not any(
entry["user"] == user_id
for entry in self.domain_percentage_table[domain]
):
self.domain_percentage_table[domain].append(
{"user": user_id, "percentage": percentage}
)
def _add_affiliate_stores_domains(
self,
user_id: str,
advertisers: dict[str, str],
platform_key: str,
percentage: int,
) -> None:
"""Process multiple domains for affiliate platforms.
Args:
----
user_id (str): User ID.
advertisers (dict[str, str]): Advertiser data.
platform_key (str): Platform key (e.g., "awin").
percentage (int): User's percentage share.
"""
if not advertisers:
logger.info("No advertisers for %s. Skipping %s.", platform_key, user_id)
return
for domain, affiliate_id in advertisers.items():
if affiliate_id:
self._add_to_domain_table(domain, user_id, affiliate_id, percentage)
def _add_user_to_domain_percentage_table(
self, user_id: str, user_data: dict, percentage: int
) -> None:
"""Add a user to the domain percentage table based on their affiliate configurations.
Args:
----
user_id (str): User ID (e.g., "HectorziN").
user_data (dict): User-specific configuration data.
percentage (int): Percentage of user influence.
"""
logger.debug("Adding %s with percentage %s", user_id, percentage)
if user_data.get("aliexpress", {}).get("discount_codes", None):
self._add_to_domain_table(
"aliexpress.com",
user_id,
"Discount",
percentage,
)
self._add_to_domain_table(
"aliexpress.com",
user_id,
user_data.get("aliexpress", {}).get("app_key", None),
percentage,
)
self._add_affiliate_stores_domains(
user_id,
user_data.get("amazon", {}).get("advertisers", {}),
"amazon",
percentage,
)
self._add_affiliate_stores_domains(
user_id,
user_data.get("awin", {}).get("advertisers", {}),
"awin",
percentage,
)
self._add_affiliate_stores_domains(
user_id,
user_data.get("admitad", {}).get("advertisers", {}),
"admitad",
percentage,
)
self._add_affiliate_stores_domains(
user_id,
user_data.get("tradedoubler", {}).get("advertisers", {}),
"tradedoubler",
percentage,
)
def _adjust_domain_affiliate_percentages(
self, domain: str, creator_percentage: int
) -> None:
"""Adjust percentages in domain_percentage_table to ensure they sum to 100%.
Args:
----
domain (str): Domain name (e.g., "amazon").
creator_percentage (int): Percentage allocated to creators.
"""
logger.debug("Adjusting percentages for domain: %s", domain)
domain_data = self.domain_percentage_table.get(domain, [])
user_entry = None
creator_entries = []
for entry in domain_data:
if entry["user"] == "main":
user_entry = entry
else:
creator_entries.append(entry)
user_percentage = 100 - creator_percentage
if user_entry:
user_entry["percentage"] = user_percentage
logger.debug(
"Set user percentage for domain %s to %s", domain, user_percentage
)
else:
user_percentage = 0
creator_percentage = 100
total_creator_percentage = sum(entry["percentage"] for entry in creator_entries)
if total_creator_percentage > 0:
for creator_entry in creator_entries:
weighted_creator_percentage = creator_entry["percentage"] * (
creator_percentage / total_creator_percentage
)
creator_entry["percentage"] = weighted_creator_percentage
logger.debug(
"Set creator percentage for domain %s to %s",
domain,
weighted_creator_percentage,
)
elif user_entry:
user_entry["percentage"] = 100
index_offset = 0
if user_entry:
domain_data[0] = user_entry
index_offset = 1
for i in range(len(creator_entries)):
domain_data[i + index_offset] = creator_entries[i]
log_message = f"Adjusted percentages for domain {domain}: "
# log percentages in a unique line for easy reding
log_entries = [
f"{entry['user']}:{entry['percentage']:.2f}%" for entry in domain_data
]
log_message += " ".join(log_entries)
logger.info(log_message)
def _should_reload_configuration(self) -> bool:
"""Check if the configuration should be reloaded.
Returns
-------
bool: True if the configuration should be reloaded, False otherwise.
"""
if self.last_load_time is None:
return True # first time must Load always
return datetime.now(timezone.utc) - self.last_load_time >= timedelta(seconds=60)
def load_configuration(self) -> None:
"""Load and process the configuration files."""
if not self._should_reload_configuration():
return
logger.info("Loading configuration")
self.domain_percentage_table.clear()
self.all_users_configurations.clear()
with self.CONFIG_PATH.open(encoding="utf-8") as file:
config_file_data = yaml.safe_load(file)
with self.CREATORS_CONFIG_PATH.open(encoding="utf-8") as file:
creators_file_data = yaml.safe_load(file)
# Telegram settings
telegram_config = config_file_data.get("telegram", {})
self.bot_token = telegram_config.get("bot_token", "")
self.delete_messages = telegram_config.get("delete_messages", True)
self.excluded_users = telegram_config.get("excluded_users", [])
self.discount_keywords = telegram_config.get("discount_keywords", [])
# Messages
messages_config = config_file_data.get("messages", {})
self.msg_affiliate_link_modified = messages_config.get(
"affiliate_link_modified",
"Here is the modified link with our affiliate program:",
)
self.msg_reply_provided_by_user = messages_config.get(
"reply_provided_by_user", "Reply provided by"
)
# Affiliate settings
affiliate_settings = config_file_data.get("affiliate_settings", {})
self.creator_percentage = affiliate_settings.get(
"creator_affiliate_percentage", 10
)
# Logging
self.log_level = config_file_data.get("log_level", "INFO")
# Load user configurations
self.all_users_configurations["main"] = self._load_user_configuration(
"main", 100 - self.creator_percentage, config_file_data
)
for creator in creators_file_data.get("users", []):
creator_id = creator.get("id")
creator_percentage = creator.get("percentage", 0)
creator_url = creator.get(
"url"
) # Assuming you have a field 'url' for each creator
if creator_url:
user_data = self._load_user_configuration_from_url(
creator_id, creator_percentage, creator_url
)
if user_data:
self.all_users_configurations[creator_id] = user_data
# Add users to the domain percentage table
for user_id, user_data in self.all_users_configurations.items():
user_percentage = user_data.get("percentage", 0)
self._add_user_to_domain_percentage_table(
user_id, user_data, user_percentage
)
# Adjust percentages for each domain
for domain in self.domain_percentage_table:
self._adjust_domain_affiliate_percentages(domain, self.creator_percentage)
self.last_load_time = datetime.now(timezone.utc)