From a250074caa3c9e2c64611f1ad119dbbd62142d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 28 Nov 2015 17:26:27 +0100 Subject: [PATCH] CountryList-module: Search current year via regex to also make parsing older lists possible --- countrylist.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/countrylist.py b/countrylist.py index 3a7eeeb..d222cd5 100644 --- a/countrylist.py +++ b/countrylist.py @@ -25,6 +25,7 @@ Provides a class for handling charts list per country and year """ +import re import locale from datetime import datetime @@ -96,15 +97,15 @@ class CountryList(): def find_year( self ): """ - Try to find the year related to CountryList + Try to find the year related to CountryList using regex """ - self.year = datetime.now().year + match = re.search( r"^.+\((\d{4})\)", self.page.title() ) - # Check if year is in page.title, if not try last year - if str( self.year ) not in self.page.title(): - self.year -= 1 - # If last year does not match, raise YearError - if str( self.year ) not in self.page.title(): + # We matched something + if match: + self.year = match.group() + + else: raise CountryListError( "CountryList year is errorneous!" ) def parse( self ):