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\$
3
  • \$\begingroup\$ as a side note, if you want to pursue the project further, see the pygithub project ! \$\endgroup\$ Commented Aug 11, 2018 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\$ Commented Aug 13, 2018 at 22:42
  • \$\begingroup\$ yes docs are lacking \$\endgroup\$ Commented Aug 14, 2018 at 10:37

1 Answer 1

2
\$\begingroup\$

Just a note: instead of using string.split() to check for spaces, you can directly check with in.

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 multiline strings: """...""" or '''...'''.

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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.