Fixed typo. Dont start not valid workers

This commit is contained in:
Joan
2023-07-04 15:09:56 +02:00
parent 3dfc7c6358
commit ab68730aab
3 changed files with 17 additions and 17 deletions

View File

@@ -135,7 +135,7 @@ async def send_article(article, product):
found_by = f"*🔍 Encontrado por la búsqueda de:* {telegram_escape_characters(product['product_name'])}"
created_at = f"*📅 Fecha de publicación:* {telegram_escape_characters(get_publish_date(article))}"
modified_at = f"*📅 Fecha de modificación:* {telegram_escape_characters(get_modified_date(article))}"
location = f"📍 *Lugar:* {telegram_escape_characters(article['location']['city'])} {telegram_escape_characters('(' + article['location']['postal_code']) + ')' } {telegram_escape_characters('- (' + article['location']['country_code']) + ')'}"
location = f"📍 *Lugar:* {telegram_escape_characters(article['location']['city'])} {telegram_escape_characters('(' + article['location']['postal_code'] + ')')} {telegram_escape_characters('- (' + article['location']['country_code'] + ')')}"
if article['shipping']['user_allows_shipping']:
user_ships = f"📦 *Envío:* ✅"
else:
@@ -310,3 +310,12 @@ def send_to_nr(article, product):
logging.error(f"Error sending to NR: {e}")
response.raise_for_status()
def is_valid_request(product):
is_valid = False
if walladb.get_product(product):
if not walladb.is_user_expired(product['telegram_user_id']):
if walladb.is_user_valid(product['telegram_user_id']):
if walladb.is_user_premium(product['telegram_user_id']) or \
walladb.is_user_testing(product['telegram_user_id']):
is_valid = True
return is_valid

View File

@@ -439,6 +439,7 @@ def main()->None:
products = walladb.get_all_products()
for product in products:
if helpers.is_valid_request(product):
logging.info(product)
p = threading.Thread(target=Worker.run, args=(product, ))
p.start()

View File

@@ -17,16 +17,6 @@ class Worker:
_stop = False
def is_valid_request(self, product):
is_valid = False
if walladb.get_product(product):
if not walladb.is_user_expired(product['telegram_user_id']):
if walladb.is_user_valid(product['telegram_user_id']):
if walladb.is_user_premium(product['telegram_user_id']) or \
walladb.is_user_testing(product['telegram_user_id']):
is_valid = True
return is_valid
def request(self, product_name, n_articles, latitude=constants.LATITUDE, longitude=constants.LONGITUDE, distance='0', condition='all', min_price=0, max_price=10000000, category="", subcategories=[]):
distance = str(int(distance) * 1000)
url = (f"https://api.wallapop.com/api/v3/general/search?keywords={product_name}"
@@ -72,7 +62,7 @@ class Worker:
for i in range(5):
helpers.random_wait()
list = []
if not self.is_valid_request(product):
if not helpers.is_valid_request(product):
return list
if product['category'] == '':
articles = self.request(product['product_name'], 0, product['latitude'], product['longitude'], product['distance'], product['condition'], product['min_price'], product['max_price'], product['category'])
@@ -103,7 +93,7 @@ class Worker:
helpers.random_wait() # Random wait to make requests separated in time in order to prevent API rate limit
exec_times = []
while True:
if not self.is_valid_request(product) or self._stop == True:
if not helpers.is_valid_request(product) or self._stop == True:
logging.info(f"{product['product_name']} not valid anymore, exiting worker")
break # Exits and ends worker thread
start_time = time.time()