diff --git a/wallamanta/helpers.py b/wallamanta/helpers.py index ec83cc8..20fbda3 100644 --- a/wallamanta/helpers.py +++ b/wallamanta/helpers.py @@ -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 \ No newline at end of file diff --git a/wallamanta/wallamanta.py b/wallamanta/wallamanta.py index 01abc41..fefb6f9 100644 --- a/wallamanta/wallamanta.py +++ b/wallamanta/wallamanta.py @@ -439,10 +439,11 @@ def main()->None: products = walladb.get_all_products() for product in products: - logging.info(product) - p = threading.Thread(target=Worker.run, args=(product, )) - p.start() - SEARCH_THREADS_LIST.append((product, p)) + if helpers.is_valid_request(product): + logging.info(product) + p = threading.Thread(target=Worker.run, args=(product, )) + p.start() + SEARCH_THREADS_LIST.append((product, p)) """Start the bot.""" # Create the Application and pass it your bot's token. diff --git a/wallamanta/worker.py b/wallamanta/worker.py index 8368591..0c89ffa 100644 --- a/wallamanta/worker.py +++ b/wallamanta/worker.py @@ -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()