Updated
[andrew-tools.git] / wqotd.py
blob5e8e7ccffffe62264c10490a32ecae2230a11b51
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
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
6 whatever way you want.
8 The following parameters are supported:
10 &params;
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.
17 """
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
27 import pagegenerators
28 import time
30 # This is required for the text that is shown when you run this script
31 # with the parameter -help.
32 docuReplacements = {
33 '&params;': pagegenerators.parameterHelp
36 class BasicBot:
37 # Edit summary message that should be used.
38 # NOTE: Put a good description here, and add translations, if possible!
39 msg = {
40 'en': u'Robot: Updating quote of the day',
43 def __init__(self, generator, dry):
44 """
45 Constructor. Parameters:
46 * generator - The page generator that determines on which pages
47 to work on.
48 * dry - If True, doesn't do any real changes, but only shows
49 what would have been changed.
50 """
51 self.generator = generator
52 self.dry = dry
53 # Set the edit summary message
54 self.summary = pywikibot.translate(pywikibot.getSite(), self.msg)
56 def run(self):
57 for page in self.generator:
58 self.treat(page)
60 def treat(self, page):
61 """
62 Loads the given page, does some changes, and saves it.
63 """
64 text = self.load(page)
65 if not text:
66 return
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)
75 q = page1.get()
77 text = q
79 if not self.save(text, page, self.summary):
80 pywikibot.output(u'Page %s not saved.' % page.aslink())
82 def load(self, page):
83 """
84 Loads the given page, does some changes, and saves it.
85 """
86 try:
87 # Load the page
88 text = page.get()
89 except pywikibot.NoPage:
90 pywikibot.output(u"Page %s does not exist; skipping."
91 % page.aslink())
92 except pywikibot.IsRedirectPage:
93 pywikibot.output(u"Page %s is a redirect; skipping."
94 % page.aslink())
95 else:
96 return text
97 return None
99 def save(self, text, page, comment, minorEdit=True, botflag=True):
100 # only save if something was changed
101 if text != page.get():
102 if not self.dry:
103 try:
104 # Save the page
105 page.put(text, comment=comment,
106 minorEdit=minorEdit, botflag=botflag)
107 except pywikibot.LockedPage:
108 pywikibot.output(u"Page %s is locked; skipping."
109 % page.aslink())
110 except pywikibot.EditConflict:
111 pywikibot.output(
112 u'Skipping %s because of edit conflict'
113 % (page.title()))
114 except pywikibot.SpamfilterError, error:
115 pywikibot.output(
116 u'Cannot change %s because of spam blacklist entry %s'
117 % (page.title(), error.url))
118 else:
119 return True
120 return False
122 def main():
123 # This factory is responsible for processing command line arguments
124 # that are also used by other scripts and that determine on which pages
125 # to work on.
126 genFactory = pagegenerators.GeneratorFactory()
127 # The generator gives the pages that should be worked upon.
128 gen = None
129 # This temporary array is used to read the page title if one single
130 # page to work on is specified by the arguments.
131 pageTitleParts = []
132 # If dry is True, doesn't do any real changes, but only show
133 # what would have been changed.
134 dry = False
136 # Parse command line arguments
137 for arg in pywikibot.handleArgs():
138 if arg.startswith("-dry"):
139 dry = True
140 else:
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)
150 gen = iter([page])
152 if not gen:
153 gen = genFactory.getCombinedGenerator()
154 if gen:
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)
159 bot.run()
160 else:
161 pywikibot.showHelp()
163 if __name__ == "__main__":
164 try:
165 main()
166 finally:
167 pywikibot.stopme()