Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api-key-support #1

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $ octodir
* **Example:** `https://github.com/Jalkhov/octodir/tree/stable/octodir`
* **Output folder**: Absolute path of the output directory
* You can enter a dot to download in the current working directory
* **API key**: Personal Github Token for prevent requests limit

## In code

Expand All @@ -33,8 +34,9 @@ from octodir import Octodir

target = 'https://github.com/Jalkhov/Octodir/tree/stable/octodir'
folder = '.' # Current working directory
api_key = '<PERSONAL_GITHUB_TOKEN>'

Octo = Octodir(target, folder)
Octo = Octodir(target, folder, api_key=api_key)
Octo.dowload_folder()
```

Expand Down
5 changes: 3 additions & 2 deletions octodir/octodir_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
@click.command()
@click.option("--folder_url", prompt="Full folder url", help="Full url from the folder to download")
@click.option("--output_folder", prompt="Output folder", help="Folder where files will be downloaded")
def octodir_cli(folder_url, output_folder):
x = Octodir(folder_url, output_folder)
@click.option("--api-key", prompt=True, hide_input=True, help="API key for authentication")
def octodir_cli(folder_url, output_folder, api_key):
x = Octodir(folder_url, output_folder, api_key)
x.dowload_folder()


Expand Down
8 changes: 4 additions & 4 deletions octodir/octodir_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def mkdirs(path):

class Octodir(object):

def __init__(self, folder_url, output_folder):
def __init__(self, folder_url, output_folder, api_key):
super(Octodir, self).__init__()
self.folder_url = folder_url
self.output_folder = output_folder

self.headers = {"Authorization": f"Token {api_key}"}
self.repo = None
self.target_dir = None
self.branch = None
Expand All @@ -48,7 +48,7 @@ def __get_raw_url(self, file_path, url):

def __get_repo_tree(self):
api = requests.get(
api_urls.recursive.format(self.repo, self.branch)).text
api_urls.recursive.format(self.repo, self.branch), headers=self.headers).text
files = json.loads(api)

output = []
Expand Down Expand Up @@ -91,7 +91,7 @@ def __scrutinize_url(self, folder_url):
def __api_response(self):
repo_data = self.__scrutinize_url(self.folder_url)
api = requests.get(api_urls.no_recursive.format(
repo_data.repo, repo_data.branch)).text
repo_data.repo, repo_data.branch), headers=self.headers).text
response = json.loads(api)

return response
Expand Down