Hey guys this is Kayla with slopes and in this video we're going to access the flicker API.
That's what we're going to use to download photos by the way.
If you didn't know already you probably did because I've said it like 100 times.
Anyway we're going to access the Flickr API.
We're going to get ourselves an API key and then I'm going to show you how the Flickr API is you are
l scheme works so that you know how you can download photos exactly the way you want to.
So go ahead and pull open your Safari browser and we're actually going to go ahead and go to Flickr
dot com slash services slash developer.
And this is the Flickr developer guide.
There's all kinds of information.
They have a pretty good community business.
They have all this stuff.
The only thing you need to know is that actually you need to log into a Yahoo account because Flickr
is owned by Yahoo.
So once you log into your Yahoo account you can go here and click on API.
Once that load you can click on request an API key K..
And we're gonna go to get your API.
The number one option.
Now there are two options here.
Commercial commercial or noncommercial.
Now that's for us our app does not make money.
You could also use this though if your app does make money but you're a family run smaller independent
business.
You're developing a product which is not currently commercial but might be in the future or you're building
a personal web site or blog where you're only using your own images but our app doesn't make money.
That's us.
So let's apply for a noncommercial key.
OK.
Now of course this shows you're the owner of the account.
The name of our app is pixel city and we're building an app that downloads photos from the internet
and displays them for a location.
Pretty simple little description.
And let's go ahead and let's check this.
I acknowledge that flicker members own all rights to their content and it's my responsibility to make
sure that my project does not contravene those rights.
That's where that's what we're doing we're not using the photos for anything other than displaying them.
And we also agreed to comply with the flicker terms of use press submit.
And as soon as it loads boom there's our API key there is our secret and all we're going to actually
mean is the API key.
So we are now at a point where we can create a Constans file on our project and we can use that to basically
store our API key for use.
So let's go ahead and pull open our tax code project really quickly.
We'll go back to the API and we'll look at how you work in just a second.
But for now we're going to go into the utilities folder and create a new file click swift file and we're
going to call this Constance and these are going to be used for things that maybe we use a lot and you
know when we go back we can actually use this photocell identifier as a constant.
We use it more than one time drop a pin.
We use that more than one time.
We can create a constant that basically makes our app a little less strongly.
When you retyped strings that can cause problems if you mistype it here or mis type it here it's better
to create one constant variable and then call that variable because you can't mistype the variable things
to auto complete.
But anyway let's go create one for our API.
So in here.
Go ahead and just type let API key.
And of course we're going to need to paste it from here so select your API key.
Copy it and paste it.
And that's that.
There's your API.
Awesome.
So that's saved in our app Let's go ahead and head back to flicker and we're going to go to what's called
the flicker API Explorer which you can access by going to this very long you l flicker dot com slash
services slash API slash explore slash flicker dot photos that search flicker stuff Flickr photos that
search is actually the part of the API that we're going to use.
The reason for that being is that you can pass in a latitude and longitude and a radius and it will
allow you to find all photos within that latitudinal radius really really cool.
Now the API Explorer here allows you to choose which parts of the potential options you want to add
in the API call which is very cool.
You can see there's a lot of different options here.
You can sort photos by the minimum upload date.
Maybe you only want photos that are from this month.
Or you can sort them by content type.
Maybe you actually only want video.
You can download those too.
But for us we just want a Latitude Longitude a radius and we have to give the radius in a certain unit.
So for example I'm just going to put in random coordinates here maybe 40 2.8 by one hundred twenty two
point three and the radius we're going to make it a one mile radius.
Whoops a one mile radius.
And let's just say that the unit will be Miles am I.
If you want to use kilometers you can K.M. meters just an it's up to you.
You can set it up to be whatever radius you want.
That's the cool thing about the API.
So we have now given it all of the properties we want and we want to see the output.
Now in this app we're going to be working with Jason.
That's just really easy to use with with iOS and so with Jaison we can get that output.
Now something that you can see here that's very very cool.
We can call this API we can make a call and download all the Jaison without even needing to sign it
with my permissions or a user token or anything we can we can send it as anybody which is cool.
So click do not sign call and then click call method.
Now if we scroll down we'll see what happened.
Looks like it's loading and it froze we're making too many API calls at once.
No that's not true.
I'm actually not sure why it's not loading.
Let's go ahead and try reloading this bad boy.
OK.
Really just not like me.
This is weird.
Cathro one more time just try reloading it and it's just going to keep getting stuck right.
Looks like I'm bringing in the big dogs and bringing in Chrome let's copy this URL.
Let's throw it in Chrome here and let's try it again.
Sometimes when Safari does not work I pick brick.
I have to go to Chrome.
So sad.
OK.
Let's try it again.
Latitude forty two point eight I think 122.
We want that that one twenty two point eight The radius is one.
The unit is Miles.
We want this to output in Jaison and we're not going to sign the call.
Go ahead and click call method and when you scroll down you'll see right here that you get a call.
Now of course the photo data is obscured for the moment.
But when you actually call this in your app all of the photo information will be inside of this dictionary
which is very cool.
So you can see that this is working.
We get a lot of photos.
We get all kinds of information.
And you know what.
The interesting thing is I actually want to set a certain number of results.
So there is another one here that allows us to set the number of results that we get.
And let's say per page let's say per page we want to download 40 photos.
So if I call that method you'll see we get a successful return and look at this.
We get a U R L we can see how it works.
To see this better I'm actually going to copy it and I'm going to paste it into X code.
So I'm just going to paste it here so we can take a look at what we're what we're dealing with here.
So API flicker dot com slash services slash rest and the method here is flickery photos like that search.
Like I said our API key is here but does that match.
No it doesn't.
So that's just a generic API that they're showing.
We'll fix that in just a second.
Now the Latitude has passed in the longitude the radius is one.
The unit is miles per page we return 40 items and the format is Jaison.
OK don't worry about no Jason callback but this is a u r l that we are going to use to download all
the photos that we want but we need to write a function that allows us to pass in the relevant data
like our API key the latitude and longitude the number of items that we want returned.
So let's do that now let's go ahead and write a function called phunk flicker you RL and I'm doing this
in my Constans file because I want to be able to access this anywhere and Constans file allows me to
do that.
So we're going to pass in a couple things into this function.
OK let's go in brackets so it stops yelling at us.
We need to pass in our API key.
We need to pass in an annotation.
And that's the same same as the pin that gets dropped.
We're going to pass in that annotation and pull out the latitude and longitude.
We're also going to pass in a number of photos.
OK.
So we're going to say flicker yourself for API key and we're going to just create an internal parameter
here called Key of type String.
We're going to say with annotation with an internal parameter of annotation.
Now we already have a custom subclass called drop a pen.
We'll use that and number of photos for a number.
And that's going to be of type int.
Now at the end of this we want this to return to us a string of our finished and perfected you r l.
So in order to return that we're going to actually just use return and now we can pass in this you are
l.
So go ahead and cut this u r l and we're going to modify it to fit our needs.
So return a string with this you r l.
Now all of this is good.
Let's go ahead and minimize this we can see more of it.
This is good right API to flicker services rest.
Flickr photo search.
Nothing changes but the first thing that changes is the API key we can pass that in using string encapsulation.
So let's go ahead and use our little special string syntax here.
And I know it's hard to see because of the length of the string but we're going to pass in API key and
where the old key was.
Now we're passing in our own key now for the latitude.
We can do the same thing.
OK.
We can pass in the annotation and then we can get the coordinate and we can pull out the latitude.
Same with the longitude.
We can do the same thing inside of those parentheses we can go to the annotation coordinate that longitude
and we can pull that out.
Now the radius the unit that never changes.
But what if we want to change the number of results we get.
So we're going to pass in using our concatenation we're going to pass in our sorry concatenation.
The encapsulation we're going to pass in the number of results that we want.
So now when we call this function it's going to return to us a perfect U r l.
Now I'm actually going to go ahead and I'm going to make this a constant for now.
And I want to print it first just so we can see that it works print u r l and then return you r l k.
Let's go ahead and let's find a space to call this and see how we do.
Let's see if we can get a nice nice fancy u r l I think a good place to call it would be here.
Now let's go ahead and call that WIPs.
We're going to print it because it returns a string to us.
And let's go ahead and try it now let's build and run a whoops of course we forgot to call all of our
fancy stuff.
What's wrong with us.
OK so we are going to go ahead and pass in the API key from our Constans file our annotation.
We do not yet have.
So let's actually move this down below because then we have our annotation pass in the annotation then
number of photos let's say 40.
So let's go ahead and build and run.
When we tap to drop a pin the screen should slide up and we should see this print out a nice u r l and
we can actually go ahead and check it to see if it properly is pulling from flicker.
So pull open the simulator.
There we go.
Let's drop a pin and let's see what prints out.
All right look at that.
Very cool.
So yeah.
Looks like it's working there's a nice you Arel let's copy it and paste it over to Chrome and let's
look at it.
Yeah we have our API.
That's ours.
That is the latitude that is the longitude and 40 results when we call it.
Look at that we get an array of all the photos all the data from that exact GPS GPS coordinate.
So so cool we're going to dive into this in much greater detail in the next video and we're going to
actually start retrieving the you or else for each image and then downloading the images using Alamo
fire image.
Very very awesome work guys.
So yeah.
Get ready for the next video you can of course delete this print function and we can of course fix this
back to where it was before we were printing out and it's going to undo that we're just returning that
value and.
Yeah let's move on to the next video let's start downloading some images.
I'll see in the next video.