12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import json
16
+ import os
17
+ import sys
15
18
import requests
16
19
from requests .adapters import HTTPAdapter
17
20
from requests .packages .urllib3 .util .retry import Retry
22
25
from tqdm import tqdm
23
26
from loguru import logger
24
27
from fichub_cli import __version__
28
+ from platformdirs import PlatformDirs
25
29
26
- headers = {
27
- 'User-Agent' : f'fichub_cli/{ __version__ } '
28
- }
29
30
30
31
retry_strategy = Retry (
31
32
total = 3 ,
@@ -43,6 +44,22 @@ def __init__(self, debug, automated, exit_status):
43
44
self .http = requests .Session ()
44
45
self .http .mount ("https://" , adapter )
45
46
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 } '
46
63
47
64
def get_fic_metadata (self , url : str , format_type : list ):
48
65
"""
@@ -59,7 +76,7 @@ def get_fic_metadata(self, url: str, format_type: list):
59
76
try :
60
77
response = self .http .get (
61
78
"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 )
63
80
)
64
81
if self .debug :
65
82
logger .debug (
@@ -78,10 +95,11 @@ def get_fic_metadata(self, url: str, format_type: list):
78
95
79
96
try :
80
97
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 )
84
101
102
+ cache_urls = {}
85
103
for format in format_type :
86
104
if format == 0 :
87
105
cache_urls ['epub' ] = self .response ['urls' ]['epub' ]
@@ -103,7 +121,6 @@ def get_fic_metadata(self, url: str, format_type: list):
103
121
self .cache_hash ['zip' ] = self .response ['hashes' ]['epub' ]
104
122
self .file_format .append (".zip" )
105
123
106
- self .files = {}
107
124
self .files ["meta" ] = self .response ['meta' ]
108
125
for file_format in self .file_format :
109
126
self .files [self .response ['urls' ]['epub' ].split (
@@ -140,7 +157,7 @@ def get_fic_data(self, download_url: str):
140
157
for _ in range (2 ):
141
158
try :
142
159
self .response_data = self .http .get (
143
- download_url , allow_redirects = True , headers = headers ,
160
+ download_url , allow_redirects = True , headers = self . headers ,
144
161
params = params , timeout = (6.1 , 300 ))
145
162
if self .debug :
146
163
logger .debug (
0 commit comments