2 # -*- coding: utf-8 -*-
4 This is not a complete bot; rather, it is a template from which simple
5 bots can be made. You can rename it to mybot.py, then edit it in
8 The following parameters are supported:
12 -dry If given, doesn't do any real changes, but only shows
13 what would have been changed.
15 All other parameters will be regarded as part of the title of a single page,
16 and the bot will only work on that single page.
19 # (C) Pywikipedia bot team, 2006-2010
21 # Distributed under the terms of the MIT license.
23 __version__
= '$Id: basic.py 8278 2010-06-11 17:01:24Z xqt $'
26 import wikipedia
as pywikibot
30 # This is required for the text that is shown when you run this script
31 # with the parameter -help.
33 '¶ms;': pagegenerators
.parameterHelp
37 # Edit summary message that should be used.
38 # NOTE: Put a good description here, and add translations, if possible!
40 'en': u
'Robot: Updating quote of the day',
43 def __init__(self
, generator
, dry
):
45 Constructor. Parameters:
46 * generator - The page generator that determines on which pages
48 * dry - If True, doesn't do any real changes, but only shows
49 what would have been changed.
51 self
.generator
= generator
53 # Set the edit summary message
54 self
.summary
= pywikibot
.translate(pywikibot
.getSite(), self
.msg
)
57 for page
in self
.generator
:
60 def treat(self
, page
):
62 Loads the given page, does some changes, and saves it.
64 text
= self
.load(page
)
68 d1
= time
.strftime("%B", time
.gmtime())
69 d2
= str(int(time
.strftime("%d", time
.gmtime())))
70 d3
= time
.strftime("%Y", time
.gmtime())
71 pywikibot
.output("I think it's " + d1
+ " " + d2
+ ", " + d3
)
72 s
= pywikibot
.getSite('en', 'wikiquote')
73 page1
= pywikibot
.Page(s
, "Wikiquote:Quote of the day/" + d1
+ " " + d2
+ ", " + d3
)
79 if not self
.save(text
, page
, self
.summary
):
80 pywikibot
.output(u
'Page %s not saved.' % page
.aslink())
84 Loads the given page, does some changes, and saves it.
89 except pywikibot
.NoPage
:
90 pywikibot
.output(u
"Page %s does not exist; skipping."
92 except pywikibot
.IsRedirectPage
:
93 pywikibot
.output(u
"Page %s is a redirect; skipping."
99 def save(self
, text
, page
, comment
, minorEdit
=True, botflag
=True):
100 # only save if something was changed
101 if text
!= page
.get():
105 page
.put(text
, comment
=comment
,
106 minorEdit
=minorEdit
, botflag
=botflag
)
107 except pywikibot
.LockedPage
:
108 pywikibot
.output(u
"Page %s is locked; skipping."
110 except pywikibot
.EditConflict
:
112 u
'Skipping %s because of edit conflict'
114 except pywikibot
.SpamfilterError
, error
:
116 u
'Cannot change %s because of spam blacklist entry %s'
117 % (page
.title(), error
.url
))
123 # This factory is responsible for processing command line arguments
124 # that are also used by other scripts and that determine on which pages
126 genFactory
= pagegenerators
.GeneratorFactory()
127 # The generator gives the pages that should be worked upon.
129 # This temporary array is used to read the page title if one single
130 # page to work on is specified by the arguments.
132 # If dry is True, doesn't do any real changes, but only show
133 # what would have been changed.
136 # Parse command line arguments
137 for arg
in pywikibot
.handleArgs():
138 if arg
.startswith("-dry"):
141 # check if a standard argument like
142 # -start:XYZ or -ref:Asdf was given.
143 if not genFactory
.handleArg(arg
):
144 pageTitleParts
.append(arg
)
146 if pageTitleParts
!= []:
147 # We will only work on a single page.
148 pageTitle
= ' '.join(pageTitleParts
)
149 page
= pywikibot
.Page(pywikibot
.getSite(), pageTitle
)
153 gen
= genFactory
.getCombinedGenerator()
155 # The preloading generator is responsible for downloading multiple
156 # pages from the wiki simultaneously.
157 gen
= pagegenerators
.PreloadingGenerator(gen
)
158 bot
= BasicBot(gen
, dry
)
163 if __name__
== "__main__":