Skip to content

Commit

Permalink
Bump version: 0.9.4 → 0.10.0
Browse files Browse the repository at this point in the history
Added API key to the fichub request headers
  • Loading branch information
arzkar committed Jul 15, 2024
1 parent 62aeb3f commit dfe46f0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .git-utils-bump.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.9.4
current_version = 0.10.0
commit = True
tag = False

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ fichub_cli -i urls.txt --changelog
"filename_format": "[title] by [author]"
```

- You can also add API keys to the `api_key_v0` which will include it in the header when making API calls to fichub

- To locate the config file, run `fichub_cli --config-info` and open the `config.json` file in an editor and make the necessary changes.

## Notes
Expand Down
2 changes: 1 addition & 1 deletion fichub_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.4"
__version__ = "0.10.0"
35 changes: 26 additions & 9 deletions fichub_cli/utils/fichub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import sys
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
Expand All @@ -22,10 +25,8 @@
from tqdm import tqdm
from loguru import logger
from fichub_cli import __version__
from platformdirs import PlatformDirs

headers = {
'User-Agent': f'fichub_cli/{__version__}'
}

retry_strategy = Retry(
total=3,
Expand All @@ -43,6 +44,22 @@ def __init__(self, debug, automated, exit_status):
self.http = requests.Session()
self.http.mount("https://", adapter)
self.http.mount("http://", adapter)
self.files = {}
self.response = ""
self.file_format = []
self.cache_hash = {}
self.headers = { 'User-Agent': f'fichub_cli/{__version__}' }
self.api_key = None
try:
app_dirs = PlatformDirs("fichub_cli", "fichub")
with open(os.path.join(app_dirs.user_data_dir, 'config.json'), "r") as f:
config = json.load(f)
self.api_key = config.get('api_key_v0')
except FileNotFoundError:
pass

if self.api_key:
self.headers['Authorization'] = f'Bearer {self.api_key}'

def get_fic_metadata(self, url: str, format_type: list):
"""
Expand All @@ -59,7 +76,7 @@ def get_fic_metadata(self, url: str, format_type: list):
try:
response = self.http.get(
"https://fichub.net/api/v0/epub", params=params,
allow_redirects=True, headers=headers, timeout=(6.1, 300)
allow_redirects=True, headers=self.headers, timeout=(6.1, 300)
)
if self.debug:
logger.debug(
Expand All @@ -78,10 +95,11 @@ def get_fic_metadata(self, url: str, format_type: list):

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

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

self.files = {}
self.files["meta"] = self.response['meta']
for file_format in self.file_format:
self.files[self.response['urls']['epub'].split(
Expand Down Expand Up @@ -140,7 +157,7 @@ def get_fic_data(self, download_url: str):
for _ in range(2):
try:
self.response_data = self.http.get(
download_url, allow_redirects=True, headers=headers,
download_url, allow_redirects=True, headers=self.headers,
params=params, timeout=(6.1, 300))
if self.debug:
logger.debug(
Expand Down
3 changes: 2 additions & 1 deletion fichub_cli/utils/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def appdir_builder(app_dirs, show_output = False):
base_config= {'db_up_time_format': r'%Y-%m-%dT%H:%M:%S%z',
'fic_up_time_format': r'%Y-%m-%dT%H:%M:%S',
'delete_output_log': '',
"filename_format":''}
"filename_format":'',
"api_key_v0": ''}

if os.path.exists(config_file):
if show_output:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
description="A CLI for the fichub.net API",
long_description=long_description,
long_description_content_type="text/markdown",
version="0.9.4",
version="0.10.0",
license='Apache License',
url="https://github.com/FicHub/fichub-cli",
packages=find_packages(include=['fichub_cli', 'fichub_cli.*']),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ def test_cli_version():

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

0 comments on commit dfe46f0

Please sign in to comment.