Wikiproyecto:Bots/Repositorio/IDDC.py

De Wikipedia, la enciclopedia libre

actualizar · discusión · código desprotegido  

Información de fichero
  • Nombre del fichero: IDDC.py
  • Lenguaje: Python
  • Estado: no protegido
Detalles de edición
  • Detalles:
Script de BOTijo (disc. · contr. · bloq.) para actualizar {{IDDC/Imagen}} y {{IDDC/Descripción}} para {{IDDC}}. Originalmente tarea009.py
# -*- coding: utf-8 -*-

# Copyright (C) 2009 emijrp
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime, os, re
import wikipedia

def parse_templates(page):
	tpls = dict(page.templatesWithParams())
	for tpl in tpls.copy():
		parameters =[]
		i=1
		for a in tpls[tpl]:
			if "=" not in a:
				key = "%i" % i
				val = a
			else:
				key, val = a.split("=", 1)
			parameters.append((key.strip(), val.strip()))
			i+=1
		tpls[tpl] = dict(parameters)
	return tpls

def main():
	date = datetime.date.today().isoformat()

	cwiki = wikipedia.getSite("commons", "commons")
	eswiki = wikipedia.getSite("es", "wikipedia")

	fname = None
	page = wikipedia.Page(cwiki, u"Template:Potd/%s" % date)
	if page.exists() and not page.isRedirectPage() and not page.isDisambig():
		templates = parse_templates(page)
		if templates.has_key("Potd filename"):
			fname = templates["Potd filename"]['1']
		else:
			return
		wikipedia.output(u"\3{lightblue}fname:\3{default} [[File:%s]]" % fname )
	else:
		return

	description = None
	for lang in ['es', 'en']:
		page=wikipedia.Page(cwiki, u"Template:Potd/%s (%s)" % (date, lang))
		if page.exists() and not page.isRedirectPage() and not page.isDisambig():
			templates = parse_templates(page)
			if templates.has_key("Potd description"):
				description = templates["Potd description"]['1']
				wikipedia.output(u"\3{lightblue}description:\3{default} %s" % description )
				break
			else:
				print "Error al parsear", page.get()

	page = wikipedia.Page(eswiki, u"Template:IDDC/Imagen")
	if fname and page.get()!= fname:
		if not test: page.put(fname, u"BOT - Actualizando imagen del día de Commons")

	page = wikipedia.Page(eswiki, u"Template:IDDC/Descripción")
	if description and page.get() != description:
		if not test: page.put(description, u"BOT - Actualizando descripción de la imagen del día de Commons")

if __name__ == "__main__":
	args = wikipedia.handleArgs()
	test = "-t" in args or "--test" in args
	if test: wikipedia.output(u"\3{lightred}Warning:\3{default} test mode is \3{lightgreen}on\3{default}. Changes can't be edited.")
	main()
	wikipedia.stopme()