Use structure of pywikibot basic.py for handling reading and saving page
This commit is contained in:
171
charts.py
171
charts.py
@@ -30,37 +30,119 @@ import locale
|
|||||||
|
|
||||||
from isoweek import Week
|
from isoweek import Week
|
||||||
|
|
||||||
import pywikibot # noqa
|
import pywikibot
|
||||||
|
from pywikibot import pagegenerators
|
||||||
import mwparserfromhell as mwparser
|
import mwparserfromhell as mwparser
|
||||||
|
|
||||||
|
# This is required for the text that is shown when you run this script
|
||||||
|
# with the parameter -help.
|
||||||
|
docuReplacements = {
|
||||||
|
'¶ms;': pagegenerators.parameterHelp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Charts:
|
class Charts:
|
||||||
"""
|
"""
|
||||||
Class for handling chart lists
|
Class for handling chart lists
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__( self ):
|
def __init__( self, generator, dry ):
|
||||||
"""
|
"""
|
||||||
Generate a new ChartsList object based on given pywikibot page object
|
Constructor.
|
||||||
|
|
||||||
@param page page Pywikibot/MediaWiki page object for page
|
@param generator: The page generator that determines on which pages
|
||||||
|
to work.
|
||||||
|
@type generator: generator.
|
||||||
|
@param dry: If True, doesn't do any real changes, but only shows
|
||||||
|
what would have been changed.
|
||||||
|
@type dry: boolean.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
self.generator = generator
|
||||||
|
self.dry = dry
|
||||||
|
|
||||||
|
# Set the edit summary message
|
||||||
|
self.site = pywikibot.Site()
|
||||||
|
self.summary = "Bot: Aktualisiere Übersichtsseite Nummer-eins-Hits"
|
||||||
|
|
||||||
# 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')
|
||||||
|
|
||||||
self.site = pywikibot.Site()
|
|
||||||
self.changed = None
|
|
||||||
# Safe the pywikibot page object
|
|
||||||
# self.page = page
|
|
||||||
|
|
||||||
self.open_overview()
|
|
||||||
|
|
||||||
self.parse_overview()
|
|
||||||
|
|
||||||
if self.changed:
|
|
||||||
self.save_overview()
|
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""Process each page from the generator."""
|
||||||
|
for page in self.generator:
|
||||||
|
self.treat(page)
|
||||||
|
|
||||||
|
def treat(self, page):
|
||||||
|
"""Load the given page, does some changes, and saves it."""
|
||||||
|
text = self.load(page)
|
||||||
|
if not text:
|
||||||
|
return
|
||||||
|
|
||||||
|
################################################################
|
||||||
|
# NOTE: Here you can modify the text in whatever way you want. #
|
||||||
|
################################################################
|
||||||
|
|
||||||
|
# If you find out that you do not want to edit this page, just return.
|
||||||
|
# Example: This puts the text 'Test' at the beginning of the page.
|
||||||
|
|
||||||
|
text = self.parse_overview( text )
|
||||||
|
|
||||||
|
if not self.save(text, page, self.summary):
|
||||||
|
pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))
|
||||||
|
|
||||||
|
def load(self, page):
|
||||||
|
"""Load the text of the given page."""
|
||||||
|
try:
|
||||||
|
# Load the page
|
||||||
|
text = page.get()
|
||||||
|
except pywikibot.NoPage:
|
||||||
|
pywikibot.output(u"Page %s does not exist; skipping."
|
||||||
|
% page.title(asLink=True))
|
||||||
|
except pywikibot.IsRedirectPage:
|
||||||
|
pywikibot.output(u"Page %s is a redirect; skipping."
|
||||||
|
% page.title(asLink=True))
|
||||||
|
else:
|
||||||
|
return text
|
||||||
|
return None
|
||||||
|
|
||||||
|
def save(self, text, page, comment=None, minorEdit=True,
|
||||||
|
botflag=True):
|
||||||
|
"""Update the given page with new text."""
|
||||||
|
# only save if something was changed
|
||||||
|
if text != page.get():
|
||||||
|
# Show the title of the page we're working on.
|
||||||
|
# Highlight the title in purple.
|
||||||
|
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
|
||||||
|
% page.title())
|
||||||
|
# show what was changed
|
||||||
|
pywikibot.showDiff(page.get(), text)
|
||||||
|
pywikibot.output(u'Comment: %s' % comment)
|
||||||
|
if not self.dry:
|
||||||
|
if pywikibot.input_yn(
|
||||||
|
u'Do you want to accept these changes?',
|
||||||
|
default=False, automatic_quit=False):
|
||||||
|
try:
|
||||||
|
page.text = text
|
||||||
|
# Save the page
|
||||||
|
page.save(summary=comment or self.comment,
|
||||||
|
minor=minorEdit, botflag=botflag)
|
||||||
|
except pywikibot.LockedPage:
|
||||||
|
pywikibot.output(u"Page %s is locked; skipping."
|
||||||
|
% page.title(asLink=True))
|
||||||
|
except pywikibot.EditConflict:
|
||||||
|
pywikibot.output(
|
||||||
|
u'Skipping %s because of edit conflict'
|
||||||
|
% (page.title()))
|
||||||
|
except pywikibot.SpamfilterError as error:
|
||||||
|
pywikibot.output(
|
||||||
|
u'Cannot change %s because of spam blacklist \
|
||||||
|
entry %s'
|
||||||
|
% (page.title(), error.url))
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def parse_charts_list( self, page ):
|
def parse_charts_list( self, page ):
|
||||||
"""
|
"""
|
||||||
Handles the parsing process
|
Handles the parsing process
|
||||||
@@ -93,13 +175,13 @@ class Charts:
|
|||||||
# Return collected data as tuple
|
# Return collected data as tuple
|
||||||
return ( chartein, title, interpret )
|
return ( chartein, title, interpret )
|
||||||
|
|
||||||
def parse_overview( self ):
|
def parse_overview( self, text ):
|
||||||
"""
|
"""
|
||||||
Parses the given Charts-Overview-Page and returns the updated version
|
Parses the given Charts-Overview-Page and returns the updated version
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Parse text with mwparser to get access to nodes
|
# Parse text with mwparser to get access to nodes
|
||||||
wikicode = mwparser.parse( self.overview_text )
|
wikicode = mwparser.parse( text )
|
||||||
|
|
||||||
# Get mwparser.template objects for Template "/Eintrag"
|
# Get mwparser.template objects for Template "/Eintrag"
|
||||||
for country in wikicode.ifilter_templates( matches="/Eintrag" ):
|
for country in wikicode.ifilter_templates( matches="/Eintrag" ):
|
||||||
@@ -118,8 +200,8 @@ class Charts:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
country.add( "Liste Revision", 0 )
|
country.add( "Liste Revision", 0 )
|
||||||
|
|
||||||
# Check if saved revid is unequal current revid
|
# Check if saved revid is unequal to current revid
|
||||||
if( int( str( country.get( "Liste Revision" ).value ) ) !=
|
if( str( country.get( "Liste Revision" ).value ) !=
|
||||||
list_page.latest_revision_id ):
|
list_page.latest_revision_id ):
|
||||||
|
|
||||||
country = self.update_overview( country, list_page )
|
country = self.update_overview( country, list_page )
|
||||||
@@ -127,7 +209,7 @@ class Charts:
|
|||||||
# If any param of any occurence of Template "/Eintrag" has changed,
|
# If any param of any occurence of Template "/Eintrag" has changed,
|
||||||
# Save new version
|
# Save new version
|
||||||
# We need to convert mwparser-objects to string before saving
|
# We need to convert mwparser-objects to string before saving
|
||||||
self.overview_text = str( wikicode )
|
return str( wikicode )
|
||||||
|
|
||||||
def open_overview( self ):
|
def open_overview( self ):
|
||||||
"""
|
"""
|
||||||
@@ -151,7 +233,6 @@ class Charts:
|
|||||||
data = self.parse_charts_list( list_page )
|
data = self.parse_charts_list( list_page )
|
||||||
|
|
||||||
# Update "Liste Revision" param
|
# Update "Liste Revision" param
|
||||||
self.changed = True
|
|
||||||
country.get( "Liste Revision" ).value = str(
|
country.get( "Liste Revision" ).value = str(
|
||||||
list_page.latest_revision_id )
|
list_page.latest_revision_id )
|
||||||
|
|
||||||
@@ -192,7 +273,6 @@ class Charts:
|
|||||||
# Check if date has changed
|
# Check if date has changed
|
||||||
if( date.strftime( "%d. %B" ).lstrip( "0" ) !=
|
if( date.strftime( "%d. %B" ).lstrip( "0" ) !=
|
||||||
country.get("Chartein").value ):
|
country.get("Chartein").value ):
|
||||||
self.changed = True
|
|
||||||
country.get("Chartein").value = date.strftime( "%d. %B"
|
country.get("Chartein").value = date.strftime( "%d. %B"
|
||||||
).lstrip( "0" )
|
).lstrip( "0" )
|
||||||
|
|
||||||
@@ -202,7 +282,6 @@ class Charts:
|
|||||||
|
|
||||||
# Check if Titel has changed
|
# Check if Titel has changed
|
||||||
if( data[1] != country.get( "Titel" ).value ):
|
if( data[1] != country.get( "Titel" ).value ):
|
||||||
self.changed = True
|
|
||||||
country.get( "Titel" ).value = data[1]
|
country.get( "Titel" ).value = data[1]
|
||||||
|
|
||||||
# Check if param "Intepret" is present
|
# Check if param "Intepret" is present
|
||||||
@@ -211,7 +290,6 @@ class Charts:
|
|||||||
|
|
||||||
# Check if Interpret has changed
|
# Check if Interpret has changed
|
||||||
if( data[2] != country.get( "Interpret" ).value ):
|
if( data[2] != country.get( "Interpret" ).value ):
|
||||||
self.changed = True
|
|
||||||
country.get( "Interpret" ).value = data[2]
|
country.get( "Interpret" ).value = data[2]
|
||||||
|
|
||||||
def save_overview( self ):
|
def save_overview( self ):
|
||||||
@@ -222,8 +300,45 @@ class Charts:
|
|||||||
fw.write( self.overview_text )
|
fw.write( self.overview_text )
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(*args):
|
||||||
Charts()
|
"""
|
||||||
|
Process command line arguments and invoke bot.
|
||||||
|
|
||||||
|
If args is an empty list, sys.argv is used.
|
||||||
|
|
||||||
|
@param args: command line arguments
|
||||||
|
@type args: list of unicode
|
||||||
|
"""
|
||||||
|
# Process global arguments to determine desired site
|
||||||
|
local_args = pywikibot.handle_args(args)
|
||||||
|
|
||||||
|
# This factory is responsible for processing command line arguments
|
||||||
|
# that are also used by other scripts and that determine on which pages
|
||||||
|
# to work on.
|
||||||
|
genFactory = pagegenerators.GeneratorFactory()
|
||||||
|
# The generator gives the pages that should be worked upon.
|
||||||
|
gen = None
|
||||||
|
# If dry is True, doesn't do any real changes, but only show
|
||||||
|
# what would have been changed.
|
||||||
|
dry = False
|
||||||
|
|
||||||
|
# Parse command line arguments
|
||||||
|
for arg in local_args:
|
||||||
|
if arg.startswith("-dry"):
|
||||||
|
dry = True
|
||||||
|
else:
|
||||||
|
genFactory.handleArg(arg)
|
||||||
|
|
||||||
|
if not gen:
|
||||||
|
gen = genFactory.getCombinedGenerator()
|
||||||
|
if gen:
|
||||||
|
# The preloading generator is responsible for downloading multiple
|
||||||
|
# pages from the wiki simultaneously.
|
||||||
|
gen = pagegenerators.PreloadingGenerator(gen)
|
||||||
|
bot = Charts(gen, dry)
|
||||||
|
bot.run()
|
||||||
|
else:
|
||||||
|
pywikibot.showHelp()
|
||||||
|
|
||||||
if( __name__ == "__main__" ):
|
if( __name__ == "__main__" ):
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user