Skip to content

Commit dfe46f0

Browse files
committed
Bump version: 0.9.4 → 0.10.0
Added API key to the fichub request headers
1 parent 62aeb3f commit dfe46f0

File tree

7 files changed

+34
-14
lines changed

7 files changed

+34
-14
lines changed

.git-utils-bump.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.9.4
2+
current_version = 0.10.0
33
commit = True
44
tag = False
55

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ fichub_cli -i urls.txt --changelog
122122
"filename_format": "[title] by [author]"
123123
```
124124

125+
- You can also add API keys to the `api_key_v0` which will include it in the header when making API calls to fichub
126+
125127
- To locate the config file, run `fichub_cli --config-info` and open the `config.json` file in an editor and make the necessary changes.
126128

127129
## Notes

fichub_cli/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.9.4"
1+
__version__ = "0.10.0"

fichub_cli/utils/fichub.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
16+
import os
17+
import sys
1518
import requests
1619
from requests.adapters import HTTPAdapter
1720
from requests.packages.urllib3.util.retry import Retry
@@ -22,10 +25,8 @@
2225
from tqdm import tqdm
2326
from loguru import logger
2427
from fichub_cli import __version__
28+
from platformdirs import PlatformDirs
2529

26-
headers = {
27-
'User-Agent': f'fichub_cli/{__version__}'
28-
}
2930

3031
retry_strategy = Retry(
3132
total=3,
@@ -43,6 +44,22 @@ def __init__(self, debug, automated, exit_status):
4344
self.http = requests.Session()
4445
self.http.mount("https://", adapter)
4546
self.http.mount("http://", adapter)
47+
self.files = {}
48+
self.response = ""
49+
self.file_format = []
50+
self.cache_hash = {}
51+
self.headers = { 'User-Agent': f'fichub_cli/{__version__}' }
52+
self.api_key = None
53+
try:
54+
app_dirs = PlatformDirs("fichub_cli", "fichub")
55+
with open(os.path.join(app_dirs.user_data_dir, 'config.json'), "r") as f:
56+
config = json.load(f)
57+
self.api_key = config.get('api_key_v0')
58+
except FileNotFoundError:
59+
pass
60+
61+
if self.api_key:
62+
self.headers['Authorization'] = f'Bearer {self.api_key}'
4663

4764
def get_fic_metadata(self, url: str, format_type: list):
4865
"""
@@ -59,7 +76,7 @@ def get_fic_metadata(self, url: str, format_type: list):
5976
try:
6077
response = self.http.get(
6178
"https://fichub.net/api/v0/epub", params=params,
62-
allow_redirects=True, headers=headers, timeout=(6.1, 300)
79+
allow_redirects=True, headers=self.headers, timeout=(6.1, 300)
6380
)
6481
if self.debug:
6582
logger.debug(
@@ -78,10 +95,11 @@ def get_fic_metadata(self, url: str, format_type: list):
7895

7996
try:
8097
self.response = response.json()
81-
self.file_format = []
82-
self.cache_hash = {}
83-
cache_urls = {}
98+
if response.status_code == 403:
99+
tqdm.write("\n" + Fore.RED + "API Key was invalid! Please recheck & use a valid key!" + Style.RESET_ALL)
100+
sys.exit(3)
84101

102+
cache_urls = {}
85103
for format in format_type:
86104
if format == 0:
87105
cache_urls['epub'] = self.response['urls']['epub']
@@ -103,7 +121,6 @@ def get_fic_metadata(self, url: str, format_type: list):
103121
self.cache_hash['zip'] = self.response['hashes']['epub']
104122
self.file_format.append(".zip")
105123

106-
self.files = {}
107124
self.files["meta"] = self.response['meta']
108125
for file_format in self.file_format:
109126
self.files[self.response['urls']['epub'].split(
@@ -140,7 +157,7 @@ def get_fic_data(self, download_url: str):
140157
for _ in range(2):
141158
try:
142159
self.response_data = self.http.get(
143-
download_url, allow_redirects=True, headers=headers,
160+
download_url, allow_redirects=True, headers=self.headers,
144161
params=params, timeout=(6.1, 300))
145162
if self.debug:
146163
logger.debug(

fichub_cli/utils/processing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def appdir_builder(app_dirs, show_output = False):
182182
base_config= {'db_up_time_format': r'%Y-%m-%dT%H:%M:%S%z',
183183
'fic_up_time_format': r'%Y-%m-%dT%H:%M:%S',
184184
'delete_output_log': '',
185-
"filename_format":''}
185+
"filename_format":'',
186+
"api_key_v0": ''}
186187

187188
if os.path.exists(config_file):
188189
if show_output:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
description="A CLI for the fichub.net API",
1111
long_description=long_description,
1212
long_description_content_type="text/markdown",
13-
version="0.9.4",
13+
version="0.10.0",
1414
license='Apache License',
1515
url="https://github.com/FicHub/fichub-cli",
1616
packages=find_packages(include=['fichub_cli', 'fichub_cli.*']),

tests/test_cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ def test_cli_version():
6161

6262
assert not result.exception
6363
assert result.exit_code == 0
64-
assert result.output.strip() == 'fichub-cli: v0.9.4'
64+
assert result.output.strip() == 'fichub-cli: v0.10.0'

0 commit comments

Comments
 (0)