Wiktionary:Entries needing topical attention/bot code

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Copyright (c) 2009 Betacommand.

Modified.

Copyright (c) 2009 NicDumZ.

Modified.

Copyright (c) 2014 msh210.

The copyright holders have licensed the code under the following license:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

'''
import re
import sys
import catlib
import pagegenerators
import wikipedia
site = wikipedia.getSite()
 
def main():
  flib = 0
  msg = u'{{shortcut|WT:TA}}\n'
  msg += u'Sometimes [[/bot code|updated]] list of'
  msg += u'\n[[:Category:Entries needing topical attention]],'
  msg += u'\nlast updated ~~~~~.'
  msg += u'\n\nTo add an entry to this list, add the code'
  msg += u'\n<code>topic=\'\'[topic]\'\'</code> parameter to any of these templates:'
  msg += u'\n{{temp|attention}}, {{temp|look}}, {{temp|question}}, {{temp|rfc}}, {{temp|rfc-sense}},'
  msg += u'\n{{temp|rfv}}, {{temp|rfv-sense}}, {{temp|rfphoto}}, {{temp|rfdrawing}}, {{temp|rfdef}},'
  msg += u'\n{{temp|rfd-redundant}}, {{temp|rfex}}, {{temp|rfquote-sense}}, {{temp|rfexp}},'
  msg += u'\n{{temp|rfd}}, {{temp|rft}}, {{temp|rfr}}, {{temp|tearoom}}.'
  msg += u'[[Category:Requests]]\n\n{|class=\'wikitable sortable\'\n'
  msg += u'!page!!topic!!language code'
  cat = catlib.Category(site,'Category:Entries needing topical attention')
  gen = pagegenerators.CategorizedPageGenerator(cat)
  reTemplate = re.compile('\{\{((rf|question|tearoom|look)[^}]*?lang=(?P<langcode>[^}]*?)\|[^}]*?topic=(?P<topic>.*?)[|}]|(rf|question|tearoom|look)[^}]*?topic=(?P<topic4>.*?)(\|[^}]*?lang=(?P<langcode2>.*?))?[|}]|attention\|(?P<langcode3>[^}]*?)\|.*?topic=(?P<topic2>.*?)[|}]|attention\|topic=(?P<topic3>[^}]*?)\|(?P<langcode4>[^=]*?)[|}])', re.I)
  for page in pagegenerators.PreloadingGenerator(gen):
    try:
      text = page.get()
      for match in reTemplate.finditer(text):
        if match.group('topic'):
            topic = match.group('topic')
        elif match.group('topic2'):
            topic = match.group('topic2')
        elif match.group('topic3'):
            topic = match.group('topic3')
        elif match.group('topic4'):
            topic = match.group('topic4')
        else:
            topic = ' '
        if match.group('langcode'):
            langcode = match.group('langcode')
        elif match.group('langcode2'):
            langcode = match.group('langcode2')
        elif match.group('langcode3'):
            langcode = match.group('langcode3')
        elif match.group('langcode4'):
            langcode = match.group('langcode4')
        else:
            langcode = ''
        msg += u'\n|-\n|[['+page.title()+']]||'+topic+'||'+langcode
        flib += 1
    except:
      pass
  logpage = wikipedia.Page(site,'Wiktionary:Entries needing topical attention')
  msg += u'\n|}'
  try:
    oldtext = logpage.get()
  except:
    oldtext = u''
  logpage.put(msg,comment='Update, '+str(flib)+' lines (bot)')
if __name__ == "__main__":
  try:
    main()
  finally:
    wikipedia.stopme()
#
  1. Many thanks to Betacommand and NicDumZ.