From 13522a3e52666025f249ca1335aa993785ad8a96 Mon Sep 17 00:00:00 2001 From: Nils Herrmann Date: Thu, 23 May 2024 18:58:19 +0200 Subject: [PATCH] #112 Refactor --- pubmed_parser/pubmed_oa_parser.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pubmed_parser/pubmed_oa_parser.py b/pubmed_parser/pubmed_oa_parser.py index eb05892..5090af9 100644 --- a/pubmed_parser/pubmed_oa_parser.py +++ b/pubmed_parser/pubmed_oa_parser.py @@ -88,14 +88,15 @@ def parse_article_meta(tree): def parse_date(tree, date_type): - """Parse ppub and epub dates.""" - pub_year_node = tree.find(f".//pub-date[@pub-type='{date_type}']/year") - pub_year = pub_year_node.text if pub_year_node is not None else "" - pub_month_node = tree.find(f".//pub-date[@pub-type='{date_type}']/month") - pub_month = pub_month_node.text if pub_month_node is not None else "01" - pub_day_node = tree.find(f".//pub-date[@pub-type='{date_type}']/day") - pub_day = pub_day_node.text if pub_day_node is not None else "01" - + """Parse publication dates based on the provided date type.""" + def get_text_or_default(xpath, default): + node = tree.find(xpath) + return node.text if node is not None else default + + pub_date_path = f".//pub-date[@pub-type='{date_type}']" + pub_year = get_text_or_default(f"{pub_date_path}/year", "") + pub_month = get_text_or_default(f"{pub_date_path}/month", "01") + pub_day = get_text_or_default(f"{pub_date_path}/day", "01") return {'year': pub_year, 'month': pub_month, 'day': pub_day}