📺 Develpreneur YouTube Episode

Video + transcript

How Do I Create A URL Shortener Solution in Python?

2023-07-18 •Youtube

Detailed Notes

We start a new series of bite-sized solutions to common problems with a larger problem where we want to take a URL and return a short form of one as seen on many sites. We will be using Python as our example language, but the application will be described a we build it in a way for you to choose the development language best-suited to your needs.

You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.

Transcript Text
foreign
[Music]
well hello and we are focusing we are in
like a series of questions here this
specific episode we're going to talk
about this question
how do we allow a user to send us a URL
and we're going to give them back a
short one
now we decided we're going to call this
thing
shorty so we're just going to go with
it's gonna be a new file and we are in
pycharm there's some questions around
that that we will come back to but we're
going to call this uh what's called
shorty.py
so we're going to have a new script but
I do want to do is I want to go back to
my application design and I want to do
pre-questions
because we will want to come back to
this or you may want to go look for it
and those are going to be how do I
actually
where do I download
and IDE for python
or pie chart
let's just call it
let's do this why do I download the pie
charm IDE for python
and then
probably will get us where we need to
we'll see if we need to do extras okay
back to the this question problem at
hand so shorty what we want to do here
is we're going to allow our user send us
URL and give a new one so let's come to
shorty
and we're just going to start with it so
our first thing is
this is what we're doing so we're going
to find a little method and a function
and we're going to call it a shortener
and
as we always do in Python starts with
Define we're not going to do a we want
to do a class that's a good question
we might so let's go ahead and let's do
a class and we're going to call it
shortener
and it's going to be
I don't know if it has any properties
yet so the first thing we're going to do
is let's see
retrieve
new URL
and we're going to get the old URL
and what we want to do is so we're just
going to have a counter let's do this
um
and so he's going to have
so he's gonna have a counter
is going to be equal to one
and so this will be something we're
going to start very simple so first all
we need to do is we're going to say
let's say we'll even store this so this
will be a source URL
let's just call it source so Source
equals this and
short
let's see we'll say new
link
equals that
so that means they're going to give us
the older URL
which means source is going to be equal
to Old URL because we're going to store
it
and then
he may not like that
it's like this
uh maybe not let's go real quick
so real quick we're going to go just
because it's one of those things we
forget sometimes we talk about our
classes so class yeah that's right no
it's right the first time so we can do
it like this
oh I forgot that because this this means
it has to be self otherwise it's going
to be static oops period comma so that's
R2 uh let's do this
clean it up so now we're going to do is
we're going to store
our old URL
so so the new
let's say what we call it result hour
and call return value
turn value
equals
oh here let's do this
base equals and we're just going to do
we're going to do this really simple
right now so it's going to be localhost
and that's going to be our base
and so return value equals
base plus
it's got to be a string because it's a
number
that's going to be self.counter
and then self dot counter
I'll just say plus equals one so we've
got to increment our counter this is a
Brute Force way to do it but that's okay
and then we're going to do a return
return value
and we really don't need well we'll keep
this for now because we probably will
find a useful so self Dot
link
equals
turn value
and so for our little test here
we'll do it like this Shorty 1.0
[Music]
replication
just so people know who it is
let's do this let's do this
in case people need any questions about
it
info number
dot com
es whoops
and then here
I'm going to do input
give me a URL
sorry uh
equals
that
s is going to be an interest instance
of the shortener class
and then I'm going to print
your new URL is
I'll just do it this way up s dot
retrieve new URL for
source
let's see how this runs
so now if I do python shorty
give me a URL let's take this URL
so this URL here
I said oh base is not defined
uh oops right there
oh and let's do this
um
so I'm also going to need
as part of this so let's see
let's see we're going to call it
navigate
and that's going to be self
I call it URL
and then we're going to return
the
New Link
so we're just going to say if
URL
equals
self dot source
nope
um let's do this for now we're not going
to check that so we're going to do this
that's your new URL
well let's do that for now and we'll
navigate we'll fix that one in a little
bit we'll cover that next time because
that's going to be coming in
so let's go back to this
do I have that yes okay so if I give
them this big URL it's going to come
back and say this is your new URL so
technically
if this other pieces are worked then
I'll be able to do it now that does
require us so this is send us a URL
we're going to give them a short one
now let's do
um
this will be a URL list
and
get a little more complicated here so we
don't need the
New Link doesn't need this but New Link
counter
equals the return value oh it's going to
be self.counter
and so navigate
here what I'm going to do is I'm going
to do url.split
on
uh let's even do it this way we're going
to make it really simple
uh
yeah let's do it like this
and let's do this so short and then
that'll make it real easy so this is
going to be our new link and then
that means that
if we split on the question mark this
makes it super simple
then our value the let's see the code
let's call that
the index equals that
and then we're going to do a URL list
idx
give me URL list not a new link that's
going to be your own list equals that
we don't need new Link at all yeah
because we don't have it turn value
return value okay
so now
let's do this
give me a URL a
short URL
I'm going to call it short URL
and then we're going to do front
s dot navigate
short URL okay so let's see how this
works
do I have I don't have that thing doing
nope so let's go back to here
and let's get our
baby thing so let's see if this works
so now give me a URL all right
now I'm going to give you back whoops
um
let's do this
do it that way
I'll just keep it simple
oh I need to do this
instead of that
um that'll actually work
I'm just gonna pin
this is not the most awesome way to do
this but we're going to do it this way
anyway so okay so now so it's going to
come back and it says hey if you give me
zero
I send it back
list indices must be integers or slices
not a string oh this needs to be
that's an ant because that was a oh
actually let's do it this way
idx that needs to be
now if I take it
and I give it this guy
he's going to give me that back
he gave me the wrong thing I need to
give him
uh it's not the return value
it's the old URL
if we do this
and we come in here
and now we take that
boom it gives us back
our address
which we can do here and guess what it's
the exact same address so we have solved
that problem
we now have
we have a way for a user to give us an a
URL and then we're going to send them a
short one but we have some more work to
do because at this point
the next thing we want to do is come in
here and say okay they've got this one
so this would be like a you know check
off of this all right let's do something
that's a little more resilient so we're
going to create a database and talk
about a table and probably within that
we're going to connect so we've got a
couple of things to do and that will be
our next episode
thanks a lot for listening in and let us
know if you have any questions you can
shoot questions as always to info
developmentware.com and until next time
keep working on becoming a better
developer
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.development or we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
thank you
Transcript Segments
0.42

foreign

18.89

[Music]

27.599

well hello and we are focusing we are in

31.14

like a series of questions here this

32.88

specific episode we're going to talk

34.2

about this question

36.96

how do we allow a user to send us a URL

39.059

and we're going to give them back a

40.86

short one

42

now we decided we're going to call this

44.52

thing

45.6

shorty so we're just going to go with

47.94

it's gonna be a new file and we are in

50.899

pycharm there's some questions around

53.28

that that we will come back to but we're

55.62

going to call this uh what's called

57.059

shorty.py

59.219

so we're going to have a new script but

61.32

I do want to do is I want to go back to

62.64

my application design and I want to do

64.559

pre-questions

67.74

because we will want to come back to

69.119

this or you may want to go look for it

70.979

and those are going to be how do I

77.939

actually

80.4

where do I download

83.6

and IDE for python

88.799

or pie chart

92.159

let's just call it

96.299

let's do this why do I download the pie

99.36

charm IDE for python

103.32

and then

106.74

probably will get us where we need to

108.6

we'll see if we need to do extras okay

110.28

back to the this question problem at

112.5

hand so shorty what we want to do here

114.96

is we're going to allow our user send us

117.659

URL and give a new one so let's come to

120.06

shorty

121.799

and we're just going to start with it so

124.439

our first thing is

126.42

this is what we're doing so we're going

128.099

to find a little method and a function

130.679

and we're going to call it a shortener

136.379

and

137.879

as we always do in Python starts with

140.04

Define we're not going to do a we want

142.62

to do a class that's a good question

144.78

we might so let's go ahead and let's do

147.78

a class and we're going to call it

149.4

shortener

153.08

and it's going to be

156.48

I don't know if it has any properties

157.86

yet so the first thing we're going to do

159.18

is let's see

162.84

retrieve

165.18

new URL

167.34

and we're going to get the old URL

172.92

and what we want to do is so we're just

175.2

going to have a counter let's do this

178.62

um

182.76

and so he's going to have

187.56

so he's gonna have a counter

191.159

is going to be equal to one

195.72

and so this will be something we're

197.159

going to start very simple so first all

199.5

we need to do is we're going to say

203.12

let's say we'll even store this so this

207.54

will be a source URL

211.26

let's just call it source so Source

212.76

equals this and

216.12

short

217.86

let's see we'll say new

221.879

link

224.159

equals that

227.22

so that means they're going to give us

229.14

the older URL

231.12

which means source is going to be equal

234.239

to Old URL because we're going to store

236.28

it

238.319

and then

242.099

he may not like that

244.98

it's like this

250.799

uh maybe not let's go real quick

254.04

so real quick we're going to go just

256.019

because it's one of those things we

258.239

forget sometimes we talk about our

259.5

classes so class yeah that's right no

261.54

it's right the first time so we can do

263.16

it like this

265.68

oh I forgot that because this this means

268.56

it has to be self otherwise it's going

270.24

to be static oops period comma so that's

273.96

R2 uh let's do this

276.24

clean it up so now we're going to do is

278.16

we're going to store

279.38

our old URL

281.46

so so the new

284.639

let's say what we call it result hour

287.22

and call return value

288.96

turn value

290.759

equals

292.56

oh here let's do this

294.9

base equals and we're just going to do

297.419

we're going to do this really simple

298.86

right now so it's going to be localhost

301.199

and that's going to be our base

304.259

and so return value equals

308.16

base plus

311.4

it's got to be a string because it's a

312.84

number

316.68

that's going to be self.counter

320.28

and then self dot counter

324.9

I'll just say plus equals one so we've

327.6

got to increment our counter this is a

329.58

Brute Force way to do it but that's okay

331.32

and then we're going to do a return

336.479

return value

339.8

and we really don't need well we'll keep

342.419

this for now because we probably will

344.699

find a useful so self Dot

348.12

link

349.979

equals

351.96

turn value

355.8

and so for our little test here

361.979

we'll do it like this Shorty 1.0

364.78

[Music]

368.1

replication

370.38

just so people know who it is

373.8

let's do this let's do this

378.72

in case people need any questions about

381.3

it

382.38

info number

386.46

dot com

388.919

es whoops

397.68

and then here

401.039

I'm going to do input

406.86

give me a URL

412.979

sorry uh

415.08

equals

416.699

that

419.639

s is going to be an interest instance

427.08

of the shortener class

430.319

and then I'm going to print

437.52

your new URL is

441.9

I'll just do it this way up s dot

446.16

retrieve new URL for

449.4

source

450.9

let's see how this runs

453.66

so now if I do python shorty

459.06

give me a URL let's take this URL

464.4

so this URL here

469.979

I said oh base is not defined

476.639

uh oops right there

478.74

oh and let's do this

482.52

um

486.9

so I'm also going to need

489.419

as part of this so let's see

493.56

let's see we're going to call it

495.36

navigate

499.74

and that's going to be self

502.979

I call it URL

508.5

and then we're going to return

511.86

the

515.3

New Link

524.82

so we're just going to say if

528.06

URL

530.339

equals

532.5

self dot source

537.24

nope

544.019

um let's do this for now we're not going

546.06

to check that so we're going to do this

547.26

that's your new URL

551.339

well let's do that for now and we'll

553.68

navigate we'll fix that one in a little

555.06

bit we'll cover that next time because

556.38

that's going to be coming in

558.48

so let's go back to this

561.54

do I have that yes okay so if I give

563.519

them this big URL it's going to come

565.38

back and say this is your new URL so

567.66

technically

568.98

if this other pieces are worked then

571.68

I'll be able to do it now that does

573.12

require us so this is send us a URL

576.06

we're going to give them a short one

577.98

now let's do

580.38

um

582.12

this will be a URL list

589.14

and

592.32

get a little more complicated here so we

595.5

don't need the

597.66

New Link doesn't need this but New Link

604.92

counter

607.5

equals the return value oh it's going to

610.38

be self.counter

618.779

and so navigate

621.18

here what I'm going to do is I'm going

623.279

to do url.split

625.86

on

629.82

uh let's even do it this way we're going

632.22

to make it really simple

635.279

uh

638.76

yeah let's do it like this

648.3

and let's do this so short and then

650.76

that'll make it real easy so this is

652.14

going to be our new link and then

656.88

that means that

659.519

if we split on the question mark this

662.22

makes it super simple

664.14

then our value the let's see the code

667.86

let's call that

669.66

the index equals that

672.959

and then we're going to do a URL list

680.519

idx

687.12

give me URL list not a new link that's

689.339

going to be your own list equals that

692.88

we don't need new Link at all yeah

695.22

because we don't have it turn value

697.14

return value okay

699.6

so now

705.54

let's do this

708.72

give me a URL a

713.88

short URL

718.68

I'm going to call it short URL

723

and then we're going to do front

727.22

s dot navigate

732.54

short URL okay so let's see how this

735.779

works

736.86

do I have I don't have that thing doing

738.48

nope so let's go back to here

741

and let's get our

743.76

baby thing so let's see if this works

748.32

so now give me a URL all right

751.68

now I'm going to give you back whoops

754.8

um

756.18

let's do this

759.36

do it that way

760.86

I'll just keep it simple

768

oh I need to do this

770.94

instead of that

776.76

um that'll actually work

783.72

I'm just gonna pin

787.5

this is not the most awesome way to do

790.62

this but we're going to do it this way

791.639

anyway so okay so now so it's going to

793.079

come back and it says hey if you give me

794.7

zero

802.32

I send it back

806.579

list indices must be integers or slices

809.339

not a string oh this needs to be

818.04

that's an ant because that was a oh

820.98

actually let's do it this way

823.139

idx that needs to be

830.579

now if I take it

833.1

and I give it this guy

838.079

he's going to give me that back

845.579

he gave me the wrong thing I need to

847.2

give him

848.82

uh it's not the return value

853.079

it's the old URL

856.62

if we do this

860.22

and we come in here

864.72

and now we take that

871.8

boom it gives us back

874.019

our address

875.88

which we can do here and guess what it's

878.16

the exact same address so we have solved

881.1

that problem

882.839

we now have

884.639

we have a way for a user to give us an a

886.56

URL and then we're going to send them a

888.06

short one but we have some more work to

891

do because at this point

893.699

the next thing we want to do is come in

895.92

here and say okay they've got this one

898.199

so this would be like a you know check

899.519

off of this all right let's do something

902.399

that's a little more resilient so we're

904.079

going to create a database and talk

906.3

about a table and probably within that

908.76

we're going to connect so we've got a

910.62

couple of things to do and that will be

912.42

our next episode

914.639

thanks a lot for listening in and let us

917.339

know if you have any questions you can

918.48

shoot questions as always to info

919.92

developmentware.com and until next time

923.459

keep working on becoming a better

925.199

developer

927.48

hello this is Rob with developmentor

929.88

also known as building better developers

931.98

wanted to announce that we have

934.339

school.developmentor.com feel free to

936.66

check it out if you like any of this

938.82

information any of the content that

940.74

we've sent and you would like to see

942

more you can come out you can enroll for

944.04

free we have free courses we've got

946.32

places for you to get better at just

948.959

learning a technology our how to's you

951.66

can work on your business skills we can

953.76

help you with becoming a better

955.139

developer as encoding and things like

957.54

that a lot of the stuff you've seen on

959.279

YouTube we also have out at

961.399

school.development or we just have it a

963.66

little more of a educational format and

966.18

a way for you to track your progress as

968.699

you move forward becoming a better

970.86

developer

981.139

thank you