#100DaysOfCode in Python Transcripts
Chapter: Days 61-63: Using the Github API with Python
Lecture: Ranking user's repos by popularity
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
All right. Enough introduction. Let's write some code and get something usable working. So, I mentioned the git repos method before.
0:11
And we're now going to use it on the PyBites or pb object to get all the repos and sort them by most popular. And I define popularity as
0:20
the amount of stars the repo has at this moment. So we're going to use get_repos. And the GitHub API tells me it's in paginated list.
0:37
Let's define a namedtuple as best practice to better describe what we're working with. So we have a repo that holds a name, stars and forks.
0:48
Let's write a method, get_repo_stats. Let's collect the repos in a list which we then can easily sort.
1:12
Let's ignore the forks. Right. There's some stuff to take in. Just run quickly over it. So we keep a list of repos, appending namedtuples,
1:30
which we initialize with data we're getting back from the get_repos. So every element in that paginated list
1:39
contains attributes of which we want name, stars, which are called stargazers in the GitHub API, and the fork count. Then we return to repos list
1:49
and we give it to sorted and the great thing about sorted is that it takes an optional key argument which can receive any callable.
1:59
Now a lambda is a quick way to define an inline function or anonymous function that we use here
2:06
to indicate that we want to sort on the number of stars. And we want the high stars up at the top, most popular, so we say reversed=True.
2:15
That gives the whole list. I put a second argument to this function, the number of items we want to see, so I can then slice the list like this.
2:25
So, take the first up until n. So this will give me item 0, 1, 2, 3, 4. And let's call it. get_repo_stats on pb user and I leave n off
2:40
so it will default to five. An error. All right, this already looked weird to me and stargazers does not have a count object. That's me mistyping it.
2:52
Repo has a stargazers_count. And forks is with an s. All right, so here we see our most popular repos. The challenge is, by far,
3:09
wins and, look at that, 100 Days Of Code, very applicable in this course, and, if you want to see what we did in our 100 Days Of Code, we kept a log.
3:21
We saving that the number's correct. And here we logged what we did every day. And I encourage you to do the same actually
3:27
because when you log it and tweet it out, you make yourself accountable and it's very likely that you make it to the end
3:33
and you have a great collection of scripts to later use in your further Python career. Let's look at another user, for example, Mike Kennedy.
3:44
And his user is mikeckennedy. So, I create an object and I call get_repo_stats on mk.
4:02
Now look at that. You can see from this output that his Jumpstart course is popular, as well as Write Pythonic Code. So that's pretty cool.
4:10
And next, we're going to post to the API, creating a gist.