#100DaysOfCode in Python Transcripts
Chapter: Days 67-69: Copy and Paste with Pyperclip
Lecture: Demo: Affiliate script
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
It's time to create something useful. This is a script we use, Bob and I use for PyBites, to actually put our affiliate code
0:10
into some of the Amazon links that we use on the website. This is really cool because it's a great demonstration
0:16
of how you can make a script for yourself that is useful day to day and is really just a nice, cool, dirty script.
0:26
You know, it doesn't have to be pretty and it doesn't have to have all the cool Pythonic formation around it. It's not going to be any functions.
0:35
It's not going to be anything, just a couple of lines of code. So let's start off by importing pyperclip,
0:44
and what this code's going to do is it's going to take a URL and it's going to tack on our Amazon affiliate code onto the end of your URL.
0:55
So it's going to paste it in, edit it, and then copy it back out to the clipboard. So the first thing we need is our affiliate code.
1:04
That's a constant, it's not going to change. Let me just copy that. Now you'll notice that the affiliate code is this here, right?
1:16
But this here, if you were to inspect any of the affiliate links that we have, you need to tack this onto the end of the URL,
1:26
or somewhere in the URL, and this will then provide the correct affiliate link. You can't just put this in there.
1:34
You have to have this there too, okay? Alright, so we'll move on. Now we want the URL, so the URL you're bringing in
1:43
is going to be the pyperclip.paste, right? Now obviously, we're not going to do any huge amount of testing here,
1:55
because this is just, again, a dirty script. I'm not going to sit here and say, well, if it's not a link that's being pasted in,
2:01
you know, and all that sort of rubbish. We just want to go quick and dirty, okay? So the most we're going to do is check for the word Amazon
2:10
in the URL, so if Amazon is not in our URL, well then, let's just return a message. What are we going to say? Sorry, invalid link, okay?
2:24
Nice and simple, don't want to go over the top, and now what are we going to do? This is where we're going to manipulate the URL,
2:33
so we've called in the URL with pyperclip.paste. We've checked to see if it has the word Amazon in it, and if it does have Amazon in it, we can then,
2:43
let's create a new variable called new_link. And it's just going to be simple string manipulation. We're going to go URL, which is the paste,
2:55
plus affiliate code. That's it, so pyperclip.copy as we saw in the other video, new_link, and then print.
3:14
Alright, we're going to say affiliate link generated and copied to clipboard. Oops, close that off. Alright, so not much to it.
3:31
It's actually very, very simple, isn't it? So we like that, so we're pasting it in. We're checking to see if it has Amazon in it.
3:39
If it does, we're appending the affiliate code on, then we're copying it back to the clipboard, okay? Let's save that.
3:48
Now if we need to run that, we just have to copy something to the clipboard, so let's copy my Gmail link,
3:57
mail.google.com, and let's run the script, see what happens. Alright, it says sorry, invalid link. That's exactly what we wanted.
4:07
And now I've just copied an Amazon link for one of those Fire Stick devices, and we're going to run the script again.
4:16
And it'll go affiliate link generated and copied to clipboard, so let's just paste that in here. And there's all the standard Amazon URL,
4:26
and then right on the end there we see end tag equals 0fPython20, which is our affiliate code. And then just to be sure, we can go check that
4:36
in on Amazon and see if it works, and it does. I don't expect you to do that. This isn't some sort of cheap sales trick.
4:44
Yeah, so it all works really well, and this is, again, a nice simple script. Obviously there's a lot of checking that could be done.
4:52
For example, if I take amazon.com.au, copy that to clipboard, and then run this again, it's going to say it's generated, but when I hit paste,
5:05
you can see we've just got amazon.com.au and the tag, which is not a valid link, so obviously this isn't something
5:13
for production use for the rest of the world. This is just something that you're going to use just as a little hack job when you know you have
5:20
the proper type of URL copied to the clipboard. So there you have it, nice little use case of.