Skip to content

Commit

Permalink
Merge pull request #1 from Jalkhov:api-key-support
Browse files Browse the repository at this point in the history
Api-key-support
  • Loading branch information
Jalkhov authored May 10, 2023
2 parents aac6402 + 14fe409 commit a021032
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
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

0 comments on commit a021032

Please sign in to comment.