Python Fundamentals

App Solution 2: Searching the Github API

Nina Zakharenko

Nina Zakharenko

Microsoft
Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "App Solution 2: Searching the Github API" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to apply search parameters to the Github API query, and starts to divide the code into separate functions.

Preview
Close

Transcript from the "App Solution 2: Searching the Github API" Lesson

[00:00:00]
>> Nina Zakharenko: Instead of just working with this big blob of printed out text, let's get the json back, so I'll say response_json = response.json||.
>> Nina Zakharenko: And, I can kind of examine this object that we get back, so a good way of kind of looking at new APIs, working with new data types, is just printing out for a larger dictionary, you can just print out the keys.

[00:00:35]
So I'm gonna say response_json.keys, I'm gonna print that out, and let's see what I get back. So there's a few keys here, I know the total count is the total number of items that came back. Incomplete results has to do with the GitHub search API. The way that it works, it always return, kind of, the most accurate values, it just runs your query and returns the first results that came back before a timeout occurs.

[00:01:03]
So this key here will tell you if there were incomplete results or not. But just by looking at the GitHub search API, I know that this key items is the one that we're actually interested in.
>> Nina Zakharenko: So I'm gonna take this out, and I'm going to use that dictionary, get the item's key from the dictionary.

[00:01:38]
I know that this is a dictionary and I'm going to get the items key from it. Now, the items key contains a list of other dictionaries, so narrow kind of thinking about more involved data structures, and I'm going to return this response_json.
>> Nina Zakharenko: Now in my main method, I'm gonna assign a variable to this function call because I know that it now is gonna return a list of items.

[00:02:18]
And just to kind of incrementally test that this is all working, I can print out the length of the results.
>> Nina Zakharenko: Okay, 30 of them, things are working as expected.
>> Nina Zakharenko: If I was kind of writing this on my own and doing a little bit more exploring, I might want to maybe look at the first result by saying, first_result equals results 0.

[00:02:56]
Seeing what keys might be on that, maybe printing that out, and getting to know this data structure a little bit better. But because this is available in the documentation for the GitHub search API, I know some of the other keys that are available on every item, in that items dictionary there.

[00:03:19]
So I'm going to loop over them and say for result in results:, that the language is going to be the result with the key language, and the number of stars that project has, this GitHub project, is gonna be something called the stargazers_count. And that the name of the project is going to be in a key called name.

[00:03:45]
And now I can print this all out and I want the print to be nested in my for loop, not outside of it, so that it prints for every single project, or every single result in that results dictionary. So for this f string, I'm just going to print out some nicer formatting here, and say {name} is a {language} repo with {stars}, number of stars, and I can take this comment out.

[00:04:21]
Okay let's run this and see if it works.
>> Nina Zakharenko: Okay I have just got a whole bunch of results about 30 of them. And we will see that it's just various languages here JavaScript, C++, None, looks like there's no value set there. Shell repo, TypeScript repo, we have more JavaScripts, CSS, Dart, and a Python repo here.

[00:04:54]
But I wanna do some filtering, I don't necessarily care about all of these languages, I just care about a few specific ones. So in order to do that, we're gonna have to build up this query and be very, very specific about, what languages we want results for. So I'm gonna make a new method here, and I'm gonna call it create_query.

[00:05:23]
And it's gonna take a list of languages along with the minimum number of stars. I'm gonna start at 50,000, and there's some more documentation in this file in the GitHub repository on my personal GitHub at nnja, that you can access by git.io/python3. It has more documentation, more comments, but for now, I know what the query structure look likes, so I'm just gonna make an f string.

[00:05:57]
And I know that the stars, I want those to be greater than or equal to the minimum number of stars, and something important to add here is a space after words, but it's just kind of the query that GitHub expects. And then for each language in languages, we're gonna loop over them,

[00:06:23]
>> Nina Zakharenko: And I'm just going to append to this query and say I want the language to,
>> Nina Zakharenko: Be that and I have to include a space that's what GitHub expects, and then I'm gonna return this query.
>> Nina Zakharenko: So let's test this method out quickly,
>> Nina Zakharenko: Kinda here at the bottom.

[00:06:54]
I'm gonna say that my languages should let me do that at the top here, I'm gonna say my languages are Python and JavaScript and let's see.
>> Nina Zakharenko: Let's do Ruby as well.
>> Nina Zakharenko: So if I call create_query,
>> Nina Zakharenko: With my list of languages and I print that out,

[00:07:35]
>> Nina Zakharenko: Let's see what it looks like.
>> Nina Zakharenko: Okay, so my terminal, I'm just gonna have to scroll up here to the very top, and here's my query. It's a space separated list of parameters with a value that the GitHub API expects, so this query will look for repositories in Python, JavaScript or Ruby with more than 50,000 stars.

[00:08:05]
Now that we know the query works, we're going to want to move this call up to where we'll actually be using it outside of the main method. So, I'm gonna remove this print statement, and I'm gonna move this list of languages above the results. Because I want the repos_with_most_stars for these specific languages.

[00:08:32]
So if I want to pass languages into the repos_with_most_stars function, I will pass that in, and then I also have to change that function signature so that it accepts a new parameter.
>> Nina Zakharenko: Now, I'm gonna move this call up here.
>> Nina Zakharenko: So now we have this query, this q key, and I'm just gonna put that here, all right?

[00:09:12]
The query is now being created by this function, there are a few other parameters that I wanna pass in along with the query. The first is the sort, what do I wanna sort for, or sort by? I think you can sort by forks or some other variables, in this case, I want to sort by the number of stars.

[00:09:41]
>> Nina Zakharenko: And I want the order to be descending.
>> Nina Zakharenko: Let me clean up some white space and then let's run this and see. So just scrolling down to the bottom here, we'll see that my results are now just filtered to JavaScript, some Python, doesn't look like any Ruby repos made it here.

[00:10:15]
>> Nina Zakharenko: Okay,
>> Nina Zakharenko: Some other improvements that we can make, just some clean up here.
>> Nina Zakharenko: It's pretty likely that anybody using this function, they might want to pass in their own sort order, and they might want to pass in their own parameter for sorting by. So I would say it's probably useful to make these into default parameters.

[00:10:52]
So I would say my function also accepts a sort, but the default is stars, and it also accepts an order that the default is descending. And now I can update these to use those defaults.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now