Skip to content

Commit

Permalink
wikicommons logger added
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMQaisar committed Mar 17, 2024
1 parent 1363d89 commit 4445cc7
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions wikicommons/wikicommons_scratcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

# Standard library
import datetime as dt
import logging
import os
import sys
import traceback

# Third-party
import requests
Expand All @@ -21,6 +21,14 @@
f"{CWD}" f"/data_wikicommons_{today.year}_{today.month}_{today.day}.csv"
)

# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.FileHandler("logfile.log"), logging.StreamHandler()],
)
logger = logging.getLogger(__name__)


def get_content_request_url(license):
"""Provides the API Endpoint URL for specified parameters' WikiCommons
Expand Down Expand Up @@ -100,15 +108,16 @@ def get_subcategories(license, session):
return category_list
except Exception as e:
if "queries" not in search_data:
print(
logger.error(
(
f"search data is: \n{search_data} for license {license}"
f"This query will not be processed due to empty result."
),
file=sys.stderr,
)
)

sys.exit(1)
else:
logger.error(f"Error occurred during request: {e}")
raise e


Expand Down Expand Up @@ -147,15 +156,16 @@ def get_license_contents(license, session):
return search_data_dict
except Exception as e:
if "queries" not in search_data:
print(
logger.error(
(
f"search data is: \n{search_data} for license {license}"
f"This query will not be processed due to empty result."
),
file=sys.stderr,
)
)

sys.exit(1)
else:
logger.error(f"Error occurred during request: {e}")
raise e


Expand Down Expand Up @@ -242,9 +252,8 @@ def main():
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
print("INFO (130) Halted via KeyboardInterrupt.", file=sys.stderr)
logger.info("Halted via KeyboardInterrupt.")
sys.exit(130)
except Exception:
print("ERROR (1) Unhandled exception:", file=sys.stderr)
print(traceback.print_exc(), file=sys.stderr)
logger.exception("Unhandled exception:")
sys.exit(1)

0 comments on commit 4445cc7

Please sign in to comment.