Skip to content

Commit a021032

Browse files
authored
Merge pull request #1 from Jalkhov:api-key-support
Api-key-support
2 parents aac6402 + 14fe409 commit a021032

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $ octodir
2525
* **Example:** `https://github.com/Jalkhov/octodir/tree/stable/octodir`
2626
* **Output folder**: Absolute path of the output directory
2727
* You can enter a dot to download in the current working directory
28+
* **API key**: Personal Github Token for prevent requests limit
2829

2930
## In code
3031

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

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

37-
Octo = Octodir(target, folder)
39+
Octo = Octodir(target, folder, api_key=api_key)
3840
Octo.dowload_folder()
3941
```
4042

octodir/octodir_cli.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
@click.command()
66
@click.option("--folder_url", prompt="Full folder url", help="Full url from the folder to download")
77
@click.option("--output_folder", prompt="Output folder", help="Folder where files will be downloaded")
8-
def octodir_cli(folder_url, output_folder):
9-
x = Octodir(folder_url, output_folder)
8+
@click.option("--api-key", prompt=True, hide_input=True, help="API key for authentication")
9+
def octodir_cli(folder_url, output_folder, api_key):
10+
x = Octodir(folder_url, output_folder, api_key)
1011
x.dowload_folder()
1112

1213

octodir/octodir_core.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def mkdirs(path):
2828

2929
class Octodir(object):
3030

31-
def __init__(self, folder_url, output_folder):
31+
def __init__(self, folder_url, output_folder, api_key):
3232
super(Octodir, self).__init__()
3333
self.folder_url = folder_url
3434
self.output_folder = output_folder
35-
35+
self.headers = {"Authorization": f"Token {api_key}"}
3636
self.repo = None
3737
self.target_dir = None
3838
self.branch = None
@@ -48,7 +48,7 @@ def __get_raw_url(self, file_path, url):
4848

4949
def __get_repo_tree(self):
5050
api = requests.get(
51-
api_urls.recursive.format(self.repo, self.branch)).text
51+
api_urls.recursive.format(self.repo, self.branch), headers=self.headers).text
5252
files = json.loads(api)
5353

5454
output = []
@@ -91,7 +91,7 @@ def __scrutinize_url(self, folder_url):
9191
def __api_response(self):
9292
repo_data = self.__scrutinize_url(self.folder_url)
9393
api = requests.get(api_urls.no_recursive.format(
94-
repo_data.repo, repo_data.branch)).text
94+
repo_data.repo, repo_data.branch), headers=self.headers).text
9595
response = json.loads(api)
9696

9797
return response

0 commit comments

Comments
 (0)