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