Last active
December 17, 2020 15:21
-
-
Save pybites/c4b688fdf69a9f517086ac8cb2ba6b61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import pyperclip | |
def gen_affiliation_link(url): | |
if not re.search(r"amazon.*/dp/", url): | |
raise ValueError(f"{url} is not a valid Amazon product link") | |
asin = re.sub(r".*/dp/([^/]+).*", r"\1", url) | |
code = os.environ.get("AMAZON_AFFILIATE_CODE", "pyb0f-20") | |
return f"http://www.amazon.com/dp/{asin}/?tag={code}" | |
def copy_affiliation_link(): | |
url = pyperclip.paste() | |
link = gen_affiliation_link(url) | |
pyperclip.copy(link) | |
if __name__ == '__main__': | |
copy_affiliation_link() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment