This script will show you how to build an Amazon affiliate link with image from the new API that generates HTML that can be inserted into a blog. I made this as an example for my wife. She ended up using a plugin in WordPress. It uses python-amazon-paapi to do all the heavy lifting. You can enter the ASIN in the var and run without Flet or, if you prefer a GUI and wish to use Flet, you can install it.
A good IDE for Python is PyCharm. It’s free and will automatically create a virtual environment. It’s below the 30 day trial of pro edition.
If you are not a programmer.
Install Python, copy the program below into a text editor like Notepad, save as get_affiliate_link.py
Be sure to fill in the API key, secret, your affiliate id, along with the asin for the product you want to display.
I did not put a lot of work into the HTML so change anything there you think will make it fit your design. Be sure not to remove anything starting with $ or ${} in the HTML.
To run, open the terminal or command prompt and run: python get_affiliate_link.py OR python3 get_affiliate_link.py
Code:
#!/usr/bin/env python3 from amazon_paapi import AmazonApi from string import Template try: import flet as ft term = False except: term = True # Flet not installed. Run without gui. ''' Quick example to get Amazon affiliate data. Best to create a Python virtual environment to keep it all contained. PyCharm does this for you. Install Amazon API module (https://pypi.org/project/python-amazon-paapi/): pip install python-amazon-paapi --upgrade Install flet (https://pypi.org/project/flet/), or not: pip install flet --upgrade ''' # **** User configs: access_key = "API Key" # Enter your Amazon API key. secret_key = "API Secret" # Enter your Amazon API secret. affiliate_tag = "affiliate-id" # Enter your affiliate id. country = "US" # Country. ASIN = "B074MN9DBB" # Enter Product ASIN. Only used if Flet is not installed. def get_item(asin): amazon = AmazonApi(access_key, secret_key, affiliate_tag, country) item = amazon.get_items(asin)[0] return item.to_dict() def html(prod_url, prod_img, img_width, prod_title, div_pad=4, border_color="#000000;", background_color="#ffffff;"): div_width = img_width ad_html = Template(''' <div style="width: ${ad_width}px; background-color: ${bg_color} border: 1px solid ${bord_color} padding: ${div_padding}px; margin: 6px;"> <div> <a href="$link" title="$link_title" target="_blank"> <img src="$img_url" width="${img_width}px"> <p font-size="9px">$title</p> </a> </div> </div> ''') advert = ad_html.substitute(ad_width=div_width, title=prod_title, link=prod_url, link_title=prod_title, img_url=prod_img, img_width=img_width, div_padding=div_pad, bord_color=border_color, bg_color=background_color) return advert if __name__ == '__main__': if term: # Flet not installed. amz_data = get_item(ASIN) product_url = amz_data['detail_page_url'] product_img_details = amz_data['images']['primary']['medium'] product_img_url = product_img_details['url'] product_img_width = product_img_details['width'] product_title = amz_data['item_info']['title']['display_value'] product_html = html(product_url, product_img_url, product_img_width, product_title) print(product_html) else: def main(page: ft.Page): page.title = "Amazon Affiliate Generator" try: page.window_width = 500 page.window_height = 620 except: pass # known issue in Flutter, with Wayland? def amz_data(e): tb.disabled = True amz_data = get_item(tb.value) product_url = amz_data['detail_page_url'] product_img_details = amz_data['images']['primary']['medium'] product_img_url = product_img_details['url'] product_img_width = product_img_details['width'] product_title = amz_data['item_info']['title']['display_value'] product_html = html(product_url, product_img_url, product_img_width, product_title) to.value = product_html tb.disabled = False page.update() to = ft.TextField(multiline=True, width=500, height=500) tb = ft.TextField(label="ASIN", width=435) btn = ft.IconButton(ft.icons.ARROW_FORWARD, on_click=amz_data) top_row = ft.Row([tb, btn]) top_textedit = ft.Container(content=top_row, alignment=ft.alignment.top_left) bottom_textedit = ft.Container(content=to, alignment=ft.alignment.top_left) page.add(top_textedit, bottom_textedit) ft.app(target=main)
Enjoy, and have fun learning!