<jupyter_start><jupyter_text>Contents API  Retrieving a File's Contents<jupyter_code>import github3
repo = github3.repository('sigmavirus24', 'github3.py')
contents = repo.file_contents('github3/repos/contents.py')<jupyter_output><empty_output><jupyter_text>Now that we have the contents of the file at `github3/repos/contents.py` we can get information about it. We can find the name, the SHA of the commit that most recently updated it, etc.<jupyter_code>print(contents.name)
print(contents.sha)<jupyter_output>ec9f27e0cc18a5440952ebb7dc9b3b9402b93702<jupyter_text>We can also get the contents of the file:<jupyter_code>print(contents.content)<jupyter_output><KEY>...]<jupyter_text>But that's base64 encoded text. So github3.py provides the content decoded as well:<jupyter_code>print(contents.decoded)<jupyter_output># -*- coding: utf-8 -*-
"""
github3.repos.contents
======================

This module contains the Contents object pertaining to READMEs and other files
that can be accessed via the GitHub API.

"""
from __future__ import unicode_literals

from json import dumps
from base64 import b64decode, b64encode
from ..git import Commit
from ..models import GitHubCore
from ..decorators import requires_auth


class Contents(GitHubCore):
    """The :class:`Contents <Contents>` object. It holds the information
    concerning any content in a repository requested via the API.

    Two content instances can be checked like so::

        c1 == c2
        c1 != c2

    And is equivalent to::

        c1.sha == c2.sha
        c1.sha != c2.sha

    See also: http://developer.github.com/v3/repos/contents/
    """
    def _update_attributes(self, content):
        # links
        self._api = content.get('url')
        #: Dictionary of links
        self.links = content.get('_links')

        #: URL of the [...]<jupyter_text>github3.py tries to preserve original names of data it receives so that users can access that easily and without having to guess. From here, (if we were logged in) we could easily update or even delete this file altogether.  Retrieving a directory's contents<jupyter_code>import github3
repo = github3.repository('sigmavirus24', 'github3.py')
dir_contents = repo.directory_contents('github3/search/')
dir_contents
dc = dict(dir_contents)
dc['user.py'].decoded<jupyter_output><empty_output><jupyter_text>In many instances when GitHub returns more than one instance of a resource, they don't return the entirety of each resource. Here's we have an example of this. The file bodies would be expensive to return for a directory with a lot of files, so GitHub omits them. We can still retrieve it. We just need to make another call to the API for the information.<jupyter_code>dc['user.py'].refresh()
print(dc['user.py'].decoded)<jupyter_output># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from ..models import GitHubCore
from ..users import User


class UserSearchResult(GitHubCore):
    def _update_attributes(self, data):
        result = data.copy()
        #: Score of this search result
        self.score = result.pop('score')
        #: Text matches
        self.text_matches = result.pop('text_matches', [])
        #: User object matching the search
        self.user = User(result, self)

    def _repr(self):
        return '<UserSearchResult [{0}]>'.format(self.user)
{'fraction_non_alphanumeric': 0.10137972405518897, 'fraction_numerical': 0.01829634073185363, 'mean_word_length': 4.178571428571429, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '467673', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1034, 'n_tokens_neox': 966, 'n_words': 334, 'path': 'example-notebooks/contents-api.ipynb', 'repo_name': 'zkdev/github3.py', 'size': '4330'}