Skip to content

Commit

Permalink
Merge pull request #93 from chaosAD/master
Browse files Browse the repository at this point in the history
Generate human friendly error message
  • Loading branch information
alexgand authored May 9, 2020
2 parents 6cd22c6 + b663de2 commit 854dd9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
30 changes: 14 additions & 16 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,25 @@ def indices_of_categories(categories, books):
return books.index[t].tolist(), invalid_categories


def download_book(url, book_path):
if not os.path.exists(book_path):
with requests.get(url, stream=True) as req:
path = create_path('./tmp')
tmp_file = os.path.join(path, '_-_temp_file_-_.bak')
with open(tmp_file, 'wb') as out_file:
shutil.copyfileobj(req.raw, out_file)
out_file.close()
shutil.move(tmp_file, book_path)


def download_book_if_exists(request, output_file, patch):
def download_book(request, output_file, patch):
new_url = request.url.replace('%2F','/').replace('/book/', patch['url']) + patch['ext']
request = requests.get(new_url, stream=True)
if request.status_code == 200:
download_book(new_url, output_file)
with requests.get(new_url, stream=True) as req:
if req.status_code == 200:
if not os.path.exists(output_file):
path = create_path('./tmp')
tmp_file = os.path.join(path, '_-_temp_file_-_.bak')
with open(tmp_file, 'wb') as out_file:
shutil.copyfileobj(req.raw, out_file)
out_file.close()
shutil.move(tmp_file, output_file)


def download_books(books, folder, patches):
assert MAX_FILENAME_LEN >= MIN_FILENAME_LEN, \
'Please change MAX_FILENAME_LEN to a value greater than 50'
'Please change MAX_FILENAME_LEN to a value greater than {}'.format(
MIN_FILENAME_LEN
)
max_length = get_max_filename_length(folder)
longest_name = books[CATEGORY].map(len).max()
if max_length - longest_name < MIN_FILENAME_LEN:
Expand Down Expand Up @@ -126,7 +124,7 @@ def download_books(books, folder, patches):
output_file = get_book_path_if_new(dest_folder, bookname, patch)
if output_file is not None:
request = requests.get(url) if request is None else request
download_book_if_exists(request, output_file, patch)
download_book(request, output_file, patch)
except (OSError, IOError) as e:
print(e)
title = title.encode('ascii', 'ignore').decode('ascii')
Expand Down
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@
table = 'table_' + table_url.split('/')[-1] + '.xlsx'
table_path = os.path.join(folder, table)
if not os.path.exists(table_path):
books = pd.read_excel(table_url)
try:
books = pd.read_excel(table_url)
except (OSError, IOError) as e:
if e.__class__.__name__ == 'HTTPError' and e.getcode() == 404:
print('Error: {} URL page not found. '.format(table_url) +
'Fix the URL in the Python script, or get someone to help.')
else:
print(e)
exit(-1)
# Save table in the download folder
books.to_excel(table_path)
else:
Expand Down

0 comments on commit 854dd9b

Please sign in to comment.