4
\$\begingroup\$

I've created a basic Python CLI script, which takes a Github username, and returns some basic information associated with said username. I use the requests for HTTP requests (I'm new to this area) and docopt for command-line handling.

#!/usr/bin/env python3

"""Github User Info

Get basic information on a Github user

Usage:
  github_user_info.py USERNAME
  github_user_info.py (-h | --help)
  github_user_info.py (-v | --version)

Arguments:
  USERNAME     Github username

Options:
  -h --help     Show this screen.
  -v --version  Show version.
"""

from docopt import docopt
import requests

import sys


def main():
    if len(sys.argv) == 1:
        sys.argv.append('-h')  # Show help screen if no arguments are passed

    cmd_arguments = docopt(__doc__, version='Github User Info v1.0')

    username = cmd_arguments["USERNAME"]

    if len(username.split()) != 1:
        print('Github usernames must be one word in length! Exiting!')
        return

    response = requests.get(f'https://api.github.com/users/{username}')

    if response.status_code != requests.codes.ok:
        if response.status_code == 404:  # No such user was found
            print(f'No Github user was found with username {username}!')
        else:
            print(f'An unexpected error occured! Exiting with error code: '
                  f'{response.status_code}')
        return

    responses_json = response.json()

    print(f'The following information was found on user: {username}',
          end='\n\n')
    print(f'Name: {responses_json["name"]}')
    print(f'Bio: {responses_json["bio"]}')
    print(f'Followers: {responses_json["followers"]}')
    print(f'Following: {responses_json["following"]}')


if __name__ == '__main__':
    main()

I'd mainly like to know if this is the correct way to handle HTTP requests (in terms of getting them, error handling, etc.). I'd also like to make sure that I follow good Python conventions.

\$\endgroup\$
  • \$\begingroup\$ as a side note, if you want to pursue the project further, see the pygithub project ! \$\endgroup\$ – Abdur-Rahmaan Janhangeer Aug 11 '18 at 6:19
  • \$\begingroup\$ @Abdur-RahmaanJanhangeer Yeah, I've seen that project, but I personally don't want to use it, as my goal is to learn more about HTTP requests, not the Github API specifically. Plus, the documentation is sparse, at best. \$\endgroup\$ – Arnav Borborah Aug 13 '18 at 22:42
  • \$\begingroup\$ yes docs are lacking \$\endgroup\$ – Abdur-Rahmaan Janhangeer Aug 14 '18 at 10:37
1
\$\begingroup\$

just a note instead of using .split to check for spaces, you can directly check with ' '

from

if len(username.split()) != 1:
        print('Github usernames must be one word in length! Exiting!')
        return

to

if ' ' in username:
        print('Github usernames cannot contain spaces! Exiting!')
        return

also instead of multiple print statements you can use """ """

from

print(f'The following information was found on user: {username}',
      end='\n\n')
print(f'Name: {responses_json["name"]}')
print(f'Bio: {responses_json["bio"]}')
print(f'Followers: {responses_json["followers"]}')
print(f'Following: {responses_json["following"]}')

to

print(f'''
The following information was found on user: {username}

Name: {responses_json["name"]}
Bio: {responses_json["bio"]}
Followers: {responses_json["followers"]}
Following: {responses_json["following"]}''')
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.