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