Restucture code to make it better maintainable
Remove unnecessary whitespace
This commit is contained in:
230
charts.py
230
charts.py
@@ -65,9 +65,6 @@ class Charts:
|
|||||||
self.site = pywikibot.Site()
|
self.site = pywikibot.Site()
|
||||||
self.summary = "Bot: Aktualisiere Übersichtsseite Nummer-eins-Hits"
|
self.summary = "Bot: Aktualisiere Übersichtsseite Nummer-eins-Hits"
|
||||||
|
|
||||||
# Set attribute to detect wether there was a real change
|
|
||||||
self.changed = None
|
|
||||||
|
|
||||||
# Set locale to 'de_DE.UTF-8'
|
# Set locale to 'de_DE.UTF-8'
|
||||||
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
|
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
|
||||||
|
|
||||||
@@ -113,7 +110,7 @@ class Charts:
|
|||||||
botflag=True):
|
botflag=True):
|
||||||
"""Update the given page with new text."""
|
"""Update the given page with new text."""
|
||||||
# only save if something was changed (and not just revision)
|
# only save if something was changed (and not just revision)
|
||||||
if text != page.get() and self.changed:
|
if text != page.get():
|
||||||
# Show the title of the page we're working on.
|
# Show the title of the page we're working on.
|
||||||
# Highlight the title in purple.
|
# Highlight the title in purple.
|
||||||
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
|
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
|
||||||
@@ -146,18 +143,70 @@ entry %s'
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def parse_charts_list( self, page, belgien=False ):
|
def parse_overview( self, text ):
|
||||||
"""
|
"""
|
||||||
Handles the parsing process
|
Parses the given Charts-Overview-Page and returns the updated version
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Parse text with mwparser to get access to nodes
|
||||||
|
wikicode = mwparser.parse( text )
|
||||||
|
|
||||||
|
# Get mwparser.template objects for Template "/Eintrag"
|
||||||
|
for entry in wikicode.ifilter_templates( matches="/Eintrag" ):
|
||||||
|
|
||||||
|
# Maybe complete entry template
|
||||||
|
self.entry_template_complete( entry )
|
||||||
|
|
||||||
|
# Extract saved revision_id
|
||||||
|
ref_list_revid = int(str( entry.get( "Liste Revision" ).value ))
|
||||||
|
|
||||||
|
# Parse ref list
|
||||||
|
data = self.parse_ref_list( self.get_entry_ref_list( entry ),
|
||||||
|
ref_list_revid )
|
||||||
|
|
||||||
|
# Check that parsing was not short circuited
|
||||||
|
if data:
|
||||||
|
data = self.calculate_chartein( entry, data )
|
||||||
|
|
||||||
|
entry = self.entry_changed( entry, data )
|
||||||
|
|
||||||
|
#~ # Check if saved revid is unequal to current revid
|
||||||
|
#~ if( str( country.get( "Liste Revision" ).value ) !=
|
||||||
|
#~ list_page.latest_revision_id ):
|
||||||
|
#~
|
||||||
|
#~ country = self.update_overview( country, list_page )
|
||||||
|
|
||||||
|
# If any param of any occurence of Template "/Eintrag" has changed,
|
||||||
|
# Save new version
|
||||||
|
# We need to convert mwparser-objects to string before saving
|
||||||
|
return str( wikicode )
|
||||||
|
|
||||||
|
def parse_ref_list( self, ref_list_link , ref_list_revid):
|
||||||
|
"""
|
||||||
|
Handles the parsing process of ref list
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Create Page-Object for Chartslist
|
||||||
|
ref_list_page = pywikibot.Page( self.site, ref_list_link.title )
|
||||||
|
|
||||||
|
# Short circuit if current revision is same than saved
|
||||||
|
if( ref_list_page.latest_revision_id == ref_list_revid ):
|
||||||
|
return False
|
||||||
|
|
||||||
|
# We need the year related to ref_list_link
|
||||||
|
year = int(ref_list_page.title()[-5:-1])
|
||||||
|
|
||||||
# Parse charts list with mwparser
|
# Parse charts list with mwparser
|
||||||
wikicode = mwparser.parse( page.text )
|
wikicode = mwparser.parse( ref_list_page.text )
|
||||||
|
|
||||||
|
# Detect if we are on begian list
|
||||||
|
belgian = self.detect_belgium( ref_list_link )
|
||||||
|
|
||||||
# Select the section "Singles"
|
# Select the section "Singles"
|
||||||
if belgien:
|
# For belgian list we need to select subsection of country
|
||||||
|
if belgian:
|
||||||
singles_section = wikicode.get_sections(
|
singles_section = wikicode.get_sections(
|
||||||
matches=belgien )[0].get_sections( matches="Singles" )[0]
|
matches=belgian )[0].get_sections( matches="Singles" )[0]
|
||||||
else:
|
else:
|
||||||
singles_section = wikicode.get_sections( matches="Singles" )[0]
|
singles_section = wikicode.get_sections( matches="Singles" )[0]
|
||||||
|
|
||||||
@@ -176,6 +225,15 @@ entry %s'
|
|||||||
# "Chartein"
|
# "Chartein"
|
||||||
if( last.get("Chartein").value.strip().isnumeric() ):
|
if( last.get("Chartein").value.strip().isnumeric() ):
|
||||||
chartein = last.get("Chartein").value.strip()
|
chartein = last.get("Chartein").value.strip()
|
||||||
|
|
||||||
|
# Maybe there is a year correction for weeknumber
|
||||||
|
if last.has( "Jahr" ):
|
||||||
|
if last.get("Jahr").value.strip() == "+1":
|
||||||
|
year = year + 1
|
||||||
|
elif last.get("Jahr").value.strip() == "-1":
|
||||||
|
year = year - 1
|
||||||
|
|
||||||
|
chartein = ( year, chartein )
|
||||||
else:
|
else:
|
||||||
chartein = datetime.strptime( last.get("Chartein").value.strip(),
|
chartein = datetime.strptime( last.get("Chartein").value.strip(),
|
||||||
"%Y-%m-%d" )
|
"%Y-%m-%d" )
|
||||||
@@ -184,43 +242,21 @@ entry %s'
|
|||||||
interpret = last.get("Interpret").value.strip()
|
interpret = last.get("Interpret").value.strip()
|
||||||
|
|
||||||
# Return collected data as tuple
|
# Return collected data as tuple
|
||||||
return ( chartein, title, interpret )
|
return ( chartein, title, interpret, ref_list_page.latest_revision_id )
|
||||||
|
|
||||||
def parse_overview( self, text ):
|
def detect_belgium( self, ref_list_link ):
|
||||||
"""
|
"""
|
||||||
Parses the given Charts-Overview-Page and returns the updated version
|
Detect wether current entry is on of the belgian (Belgien/Wallonien)
|
||||||
"""
|
"""
|
||||||
|
# Parse linked charts list for the country
|
||||||
# Parse text with mwparser to get access to nodes
|
if "Wallonien" in str( ref_list_link.text ) \
|
||||||
wikicode = mwparser.parse( text )
|
or "Wallonien" in str( ref_list_link.title):
|
||||||
|
return "Wallonie"
|
||||||
# Get mwparser.template objects for Template "/Eintrag"
|
elif "Flandern" in str( ref_list_link.text ) \
|
||||||
for country in wikicode.ifilter_templates( matches="/Eintrag" ):
|
or "Flandern" in str( ref_list_link.title):
|
||||||
|
return "Flandern"
|
||||||
# Get mwparser.wikilink object
|
else:
|
||||||
for link in country.get("Liste").value.ifilter_wikilinks():
|
return None
|
||||||
# Create Page-Object for Chartslist
|
|
||||||
list_page = pywikibot.Page( self.site, link.title )
|
|
||||||
# Only use first wikilink in Template Param "Liste"
|
|
||||||
break
|
|
||||||
|
|
||||||
# Check if we have a saved revid
|
|
||||||
if not country.has( "Liste Revision" ):
|
|
||||||
try:
|
|
||||||
country.add( "Liste Revision", 0, before="Interpret" )
|
|
||||||
except ValueError:
|
|
||||||
country.add( "Liste Revision", 0 )
|
|
||||||
|
|
||||||
# Check if saved revid is unequal to current revid
|
|
||||||
if( str( country.get( "Liste Revision" ).value ) !=
|
|
||||||
list_page.latest_revision_id ):
|
|
||||||
|
|
||||||
country = self.update_overview( country, list_page )
|
|
||||||
|
|
||||||
# If any param of any occurence of Template "/Eintrag" has changed,
|
|
||||||
# Save new version
|
|
||||||
# We need to convert mwparser-objects to string before saving
|
|
||||||
return str( wikicode )
|
|
||||||
|
|
||||||
def update_overview( self, country, list_page ): # noqa
|
def update_overview( self, country, list_page ): # noqa
|
||||||
"""
|
"""
|
||||||
@@ -232,74 +268,96 @@ entry %s'
|
|||||||
@returns wikicode-object with updated Template for country
|
@returns wikicode-object with updated Template for country
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Parse linked charts list for the country
|
data = self.parse_charts_list( ref_list_link, belgien )
|
||||||
if "Wallonien" in str( country.get( "Liste" ).value ):
|
|
||||||
belgien = "Wallonie"
|
|
||||||
elif "Flandern" in str( country.get( "Liste" ).value ):
|
|
||||||
belgien = "Flandern"
|
|
||||||
else:
|
|
||||||
belgien = None
|
|
||||||
|
|
||||||
data = self.parse_charts_list( list_page, belgien )
|
def get_entry_ref_list( self, entry ):
|
||||||
|
"""
|
||||||
# Update "Liste Revision" param
|
"""
|
||||||
country.get( "Liste Revision" ).value = str(
|
# Get mwparser.wikilink object
|
||||||
list_page.latest_revision_id )
|
return next( entry.get("Liste").value.ifilter_wikilinks() )
|
||||||
|
|
||||||
|
def calculate_chartein( self, entry, data ):
|
||||||
|
"""
|
||||||
|
Calculates the correct value for param chartein in entry
|
||||||
|
"""
|
||||||
# If param Korrektur is present extract the value
|
# If param Korrektur is present extract the value
|
||||||
if( country.has( "Korrektur" ) and
|
if( entry.has( "Korrektur" ) ):
|
||||||
str( country.get( "Korrektur" ).value ).isnumeric() ):
|
# If Korrektur is (after striping) castable to int use it
|
||||||
days = int( str( country.get( "Korrektur" ).value ) )
|
try:
|
||||||
|
days = int( str( entry.get( "Korrektur" ).value ).strip() )
|
||||||
|
# Otherwise, if casting fails, ignore it
|
||||||
|
except ValueError:
|
||||||
|
days = 0
|
||||||
else:
|
else:
|
||||||
days = 0
|
days = 0
|
||||||
|
|
||||||
# For some countries we have weeknumbers instead of dates
|
# For some countries we have weeknumbers instead of dates
|
||||||
if( isinstance( data[0], str ) ):
|
if( isinstance( data[0], tuple ) ):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Calculate date of monday in given week and add number of
|
# Calculate date of monday in given week and add number of
|
||||||
# days given in Template parameter "Korrektur" with monday
|
# days given in Template parameter "Korrektur" with monday
|
||||||
# as day (zero)
|
# as day (zero)
|
||||||
date = ( Week( year, int( data[0] ) ).monday() +
|
date = ( Week( data[0][0], int( data[0][1] ) ).monday() +
|
||||||
timedelta( days=days ) )
|
timedelta( days=days ) )
|
||||||
|
|
||||||
# Param Chartein contains a regular date
|
# Param Chartein contains a regular date
|
||||||
else:
|
else:
|
||||||
date = data[0] + timedelta( days=days )
|
date = data[0] + timedelta( days=days )
|
||||||
|
|
||||||
# Check if param "Chartein" is present
|
return (date,)+data[1:]
|
||||||
if not country.has( "Chartein" ):
|
|
||||||
try:
|
|
||||||
country.add( "Chartein", "", before="Korrektur" )
|
|
||||||
except ValueError:
|
|
||||||
country.add( "Chartein", "" )
|
|
||||||
|
|
||||||
# Check if date has changed
|
def entry_template_complete( self, entry ):
|
||||||
if( date.strftime( "%d. %B" ).lstrip( "0" ) !=
|
"""
|
||||||
country.get("Chartein").value ):
|
Checks wether given entry template is complete, otherwise adds missing
|
||||||
country.get("Chartein").value = date.strftime( "%d. %B"
|
params
|
||||||
).lstrip( "0" )
|
"""
|
||||||
self.changed = True
|
|
||||||
|
# Check if param "Chartein" is present
|
||||||
|
if not entry.has( "Chartein" ):
|
||||||
|
try:
|
||||||
|
entry.add( "Chartein", "", before="Korrektur" )
|
||||||
|
except ValueError:
|
||||||
|
entry.add( "Chartein", "" )
|
||||||
|
|
||||||
# Check if param "Titel" is present
|
# Check if param "Titel" is present
|
||||||
if not country.has( "Titel" ):
|
if not entry.has( "Titel" ):
|
||||||
country.add( "Titel", "", before="Chartein" )
|
entry.add( "Titel", "", before="Chartein" )
|
||||||
|
|
||||||
# Check if Titel has changed
|
|
||||||
if( data[1] != country.get( "Titel" ).value ):
|
|
||||||
country.get( "Titel" ).value = data[1]
|
|
||||||
self.changed = True
|
|
||||||
|
|
||||||
# Check if param "Intepret" is present
|
# Check if param "Intepret" is present
|
||||||
if not country.has( "Interpret" ):
|
if not entry.has( "Interpret" ):
|
||||||
country.add( "Interpret", "", before="Titel" )
|
entry.add( "Interpret", "", before="Titel" )
|
||||||
|
|
||||||
|
# Check if we have a saved revid
|
||||||
|
if not entry.has( "Liste Revision" ):
|
||||||
|
entry.add( "Liste Revision", 0, before="Interpret" )
|
||||||
|
|
||||||
|
return entry
|
||||||
|
|
||||||
|
def entry_changed( self, entry, data ):
|
||||||
|
"""
|
||||||
|
Checks wether given entry has changed
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Check if date has changed
|
||||||
|
if( data[0].strftime( "%d. %B" ).lstrip( "0" ) !=
|
||||||
|
entry.get("Chartein").value ):
|
||||||
|
|
||||||
|
entry.get("Chartein").value = data[0].strftime( "%d. %B"
|
||||||
|
).lstrip( "0" )
|
||||||
|
|
||||||
|
# Check if Titel has changed
|
||||||
|
if( data[1] != entry.get( "Titel" ).value ):
|
||||||
|
entry.get( "Titel" ).value = data[1]
|
||||||
|
|
||||||
# Check if Interpret has changed
|
# Check if Interpret has changed
|
||||||
if( data[2] != country.get( "Interpret" ).value ):
|
if( data[2] != entry.get( "Interpret" ).value ):
|
||||||
country.get( "Interpret" ).value = data[2]
|
entry.get( "Interpret" ).value = data[2]
|
||||||
self.changed = True
|
|
||||||
|
|
||||||
|
# Update "Liste Revision" param
|
||||||
|
entry.get( "Liste Revision" ).value = str(
|
||||||
|
data[3] )
|
||||||
|
|
||||||
|
return entry
|
||||||
|
|
||||||
class ChartsError( Exception ):
|
class ChartsError( Exception ):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user