Docfile commited on
Commit
4854cab
·
verified ·
1 Parent(s): 4a102e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -5,7 +5,6 @@ import os
5
  import json
6
  import time
7
  import random
8
- import string
9
  from datetime import datetime
10
  import multiprocessing
11
  from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
@@ -15,7 +14,7 @@ import sys
15
  from functools import partial
16
  from jinja2 import Undefined # Pour gérer les valeurs Undefined
17
 
18
- # Définition d'une fonction pour convertir les objets Undefined en chaîne vide
19
  def default_json(o):
20
  if isinstance(o, Undefined):
21
  return ''
@@ -34,14 +33,17 @@ logger = logging.getLogger('website_requester')
34
 
35
  app = Flask(__name__)
36
 
37
- # Définir un encodeur JSON personnalisé pour Flask
38
- from flask.json import JSONEncoder
39
- class CustomJSONEncoder(JSONEncoder):
40
- def default(self, obj):
41
- if isinstance(obj, Undefined):
42
- return ''
43
- return super().default(obj)
44
- app.json_encoder = CustomJSONEncoder
 
 
 
45
 
46
  # Variables globales pour stocker l'état, utilisation d'un gestionnaire multiprocessing
47
  manager = multiprocessing.Manager()
@@ -97,7 +99,7 @@ def get_random_referrer():
97
  "https://www.gabon.ga/",
98
  "https://www.gov.ga/",
99
  "https://www.youtube.com/",
100
- "", # Aucun référent (accès direct)
101
  ]
102
  return random.choice(referrers)
103
 
@@ -110,7 +112,7 @@ async def with_retries(func, *args, max_retries=MAX_RETRIES, **kwargs):
110
  if attempt == max_retries - 1:
111
  logger.error(f"Échec définitif après {max_retries} tentatives: {str(e)}")
112
  raise
113
- delay = RETRY_DELAY * (2 ** attempt) # Backoff exponentiel
114
  logger.warning(f"Tentative {attempt+1} échouée: {str(e)}. Nouvel essai dans {delay:.2f}s")
115
  await asyncio.sleep(delay)
116
 
 
5
  import json
6
  import time
7
  import random
 
8
  from datetime import datetime
9
  import multiprocessing
10
  from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
 
14
  from functools import partial
15
  from jinja2 import Undefined # Pour gérer les valeurs Undefined
16
 
17
+ # Pour gérer la sérialisation des objets Undefined lors de l'utilisation de json.dump
18
  def default_json(o):
19
  if isinstance(o, Undefined):
20
  return ''
 
33
 
34
  app = Flask(__name__)
35
 
36
+ # Définition d'un fournisseur JSON personnalisé en étendant DefaultJSONProvider
37
+ from flask.json.provider import DefaultJSONProvider
38
+
39
+ class CustomJSONProvider(DefaultJSONProvider):
40
+ def default(self, o):
41
+ if isinstance(o, Undefined):
42
+ return ""
43
+ return super().default(o)
44
+
45
+ # Assigner le fournisseur JSON personnalisé à l'application
46
+ app.json = CustomJSONProvider(app)
47
 
48
  # Variables globales pour stocker l'état, utilisation d'un gestionnaire multiprocessing
49
  manager = multiprocessing.Manager()
 
99
  "https://www.gabon.ga/",
100
  "https://www.gov.ga/",
101
  "https://www.youtube.com/",
102
+ "" # Aucun référent (accès direct)
103
  ]
104
  return random.choice(referrers)
105
 
 
112
  if attempt == max_retries - 1:
113
  logger.error(f"Échec définitif après {max_retries} tentatives: {str(e)}")
114
  raise
115
+ delay = RETRY_DELAY * (2 ** attempt)
116
  logger.warning(f"Tentative {attempt+1} échouée: {str(e)}. Nouvel essai dans {delay:.2f}s")
117
  await asyncio.sleep(delay)
118