From 3340ad79c7a4bb681ea1f15d589f7f508996780a Mon Sep 17 00:00:00 2001 From: gian3194 Date: Fri, 30 Aug 2024 17:06:01 +0200 Subject: [PATCH] Use affiliate id (#8) Co-authored-by: Alberto Pasqualetto <39854348+albertopasqualetto@users.noreply.github.com> --- .env.example | 1 + amazon-deals-telegram-bot/__main__.py | 22 ++++++++++++++++--- .../amazon_page_analyser.py | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 9594366..d71dc4f 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ # Put here your tokens AMAZON_DEALS_TG_BOT_TOKEN= AMAZON_DEALS_TG_CHANNEL_ID= +AMAZON_DEALS_TG_AFFILIATE_ID= diff --git a/amazon-deals-telegram-bot/__main__.py b/amazon-deals-telegram-bot/__main__.py index bdf1c2c..02a5386 100644 --- a/amazon-deals-telegram-bot/__main__.py +++ b/amazon-deals-telegram-bot/__main__.py @@ -11,6 +11,8 @@ import json +from urllib.parse import urlparse, parse_qs, urlencode + def get_random_product_info(deals_ids, already_sent_products_ids): if(len(deals_ids) == 0): @@ -21,8 +23,7 @@ def get_random_product_info(deals_ids, already_sent_products_ids): while True: # get new product until the selected one is valid if (selected_product_info is not None) and ( - selected_product_info[ - "product_id"] not in already_sent_products_ids): # product valid and not already sent + selected_product_info["product_id"] not in already_sent_products_ids): # product valid and not already sent break deals_ids.remove(selected_deal_id) # remove invalid product to not encounter it in the next iteration @@ -44,6 +45,21 @@ def get_random_product_info(deals_ids, already_sent_products_ids): return selected_product_info, already_sent_products_ids +''' + Add affiliate id ('tag') parameter in url. + + If a previous one was present it is removed. + If an empty string is passed (default), affiliate id is not added. +''' +def add_affiliate_id(url, affiliate_id=''): + url_parts = urlparse(url) # parse url parts + query = parse_qs(url_parts.query, keep_blank_values=True) # get parameters from url + query.update({'tag': affiliate_id}) # add affiliate id + if affiliate_id == '': # if no affiliate id provided, do not add it + query.pop('tag') + return url_parts._replace(query=urlencode(query, doseq=True)).geturl() # rebuild url + + def send_deal(bot, product_info, chat_id): if(product_info == None): return @@ -53,7 +69,7 @@ def send_deal(bot, product_info, chat_id): comparison_text = ['invece di ', 'al posto di ', 'piuttosto che '] caption = "" + product_info["title"] + "" + "\n\n" - caption += "\U0001F449 " + apa.url_from_id(product_info["product_id"]) + "\n\n" # add affiliate code here if any + caption += "\U0001F449 " + add_affiliate_id(apa.url_from_id(product_info["product_id"]), os.environ.get("AMAZON_DEALS_TG_AFFILIATE_ID")) + "\n\n" caption += random.choice(emoticon) + product_info["discount_rate"] + "\n" caption += "\U0001F4B6 " + random.choice(starting_text) + product_info["new_price"] + ", " + random.choice(comparison_text) + \ "" + product_info["old_price"] + " " + random.choice(emoticon) diff --git a/amazon-deals-telegram-bot/amazon_page_analyser.py b/amazon-deals-telegram-bot/amazon_page_analyser.py index 760c93d..9bf0213 100644 --- a/amazon-deals-telegram-bot/amazon_page_analyser.py +++ b/amazon-deals-telegram-bot/amazon_page_analyser.py @@ -47,7 +47,7 @@ def get_all_deals_ids(): # Checking for url change would not work, because it changes before the new deals are loaded WebDriverWait(selenium_driver, 60).until( EC.presence_of_element_located((By.CSS_SELECTOR, "[data-testid='product-card']"))) # timeout connect after 60 seconds - + elements_urls = [] # scroll the page little by little to load more deals. Take the deals present after each scroll for _ in range(100):