Aller au contenu

Utilisateur:Botsolète/code

Une page de Wikipédia, l'encyclopédie libre.
Blocage pour les administrateurs
Blocage pour les non-administrateurs
Moi Me laisser un message Compteur d'éditions Contributions Code Dresseur

Pour lire la licence publique générale GNU, cliquez ici.


#!/usr/bin/python3
# -*- coding:Utf-8 -*-

#       Copyright (C) 2010 Marin Moulinier
#       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 2 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, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import urllib.request as url
import urllib.parse as urlparser
import json
import time
import sys
import gzip
import io
import re

name = 'Botsolète'
password = 'password'
useragent = 'Botsolete'

opener = url.build_opener(url.HTTPCookieProcessor())
opener.addheaders = [('User-Agent', useragent), ('Accept-encoding', 'gzip')]

def requeteAPI(params):
	params['format'] = 'json'
	
	try:
		reponseRequete = opener.open('http://fr.wikipedia.org/w/api.php', urlparser.urlencode(params))
	
	except urllib.error.HTTPError as erreur_retournee:
		print('Erreur de requête : ' + erreur_retournee + '.')
		time.sleep(20)
		reponseRequete = opener.open('http://fr.wikipedia.org/w/api.php', urlparser.urlencode(params))
	
	dezip = gzip.GzipFile(fileobj = io.BytesIO(reponseRequete.read())).read()
	reponse = json.loads(dezip.decode())
	reponseRequete.close()
	
	time.sleep(5)

def requetePage(article):
	reponseRequete = opener.open('http://fr.wikipedia.org/w/index.php?action=raw&title=' + url.quote(article))
	dezip = gzip.GzipFile(fileobj = io.BytesIO(reponseRequete.read())).read()
	reponse = urlparser.unquote(urlparser.quote_from_bytes(dezip))
	reponse = re.sub(r'[Mm] *{{ *[Ee]lle *}}', '{{Mlle}}', reponse)
	reponse = re.sub(r'[Mm] *{{ *[Ll]le *}}', '{{Mlle}}', reponse)
	reponse = re.sub(r'[Mm] *{{ *[Mm]e *}}', '{{Mme}}', reponse)
	reponse = re.sub(r'[Mm] *{{ *[Rr]s *}}', '{{MM.}}', reponse)
	reponse = re.sub(r'[Dd] *{{ *[Rr]s *}}', '{{Drs}}', reponse)
	reponse = re.sub(r'[Pp] *{{ *[Rr]s *}}', '{{Prs}}', reponse)
	reponse = re.sub(r'[Mm] *{{ *[Rr] *}}', '{{M.}}', reponse)
	reponse = re.sub(r'[Dd] *{{ *[Rr] *}}', '{{Dr}}', reponse)
	reponse = re.sub(r'[Pp] *{{ *[Rr] *}}', '{{Pr}}', reponse)
	reponse = re.sub(r'{{ *[Tt]ablabonita', '{{subst:Prettytable', reponse)
	reponse = re.sub(r'{{ *[Pp]rettytable', '{{subst:Prettytable', reponse)
	reponse = re.sub(r'{{ *[Ss]ommaire flottant', '{{Sommaire|align=right|flottant=1', reponse)
	reponse = re.sub(r'{{ *[Mm]édaille d\'or olympique *}}', '{{Médaille|or|JO}}', reponse)
	reponse = re.sub(r'{{ *[Mm]édaille d\'argent olympique *}}', '{{Médaille|argent|JO}}', reponse)
	reponse = re.sub(r'{{ *[Mm]édaille de bronze olympique *}}', '{{Médaille|bronze|JO}}', reponse)
	reponseRequete.close()
	time.sleep(5)
	
	return reponse

def obsolete(tpl):
	print('Vérification du nombre de pages incluant {{' + tpl + '}}...')
	cat = requeteAPI({'action': 'query', 'list': 'embeddedin', 'eititle': 'Modèle:' + tpl, 'eilimit': '2000'})
	
	for page in cat['query']['embeddedin']:
		print('Obtention de ' + page['title'] + '...')
		texteAMettreAJour = requetePage(page['title'])
		
		print('Mise à jour de ' + page['title'] + '...')
		edition = requeteAPI({'action': 'edit', 'title': page['title'], 'summary': 'Bot remplace {{' + tpl + '}}', 'text': texteAMettreAJour, 'token': token})
		
		if edition['edit']['result'] != 'Success':
			sys.exit('Erreur d\'édition de l\'article : ' + edition['edit']['result'] + '.')

print('Génération de la cookie de vérification...')
cookiePre = requeteAPI({'action': 'login', 'lgname': name, 'lgpassword': password})

if(cookiePre['login']['result'] != 'NeedToken'):
	sys.exit('Erreur de pré-connexion : ' + cookiePre['login']['result'] + '.')

print('Connexion...')
cookie = requeteAPI({'action': 'login', 'lgname': name, 'lgpassword': password, 'lgtoken': cookiePre['login']['token']})

if cookie['login']['result'] != 'Success':
	sys.exit('Erreur de connexion : ' + cookie['login']['result'] + '.')

print('Récupération du token...')

cat = requeteAPI({'action': 'query', 'titles': 'World of Goo', 'prop': 'info', 'intoken': 'edit'})
token = cat['query']['pages'][list(cat['query']['pages'].keys())[0]]['edittoken']

obsolete('Elle')
obsolete('Lle')
obsolete('Me')
obsolete('Rs')
obsolete('R')
obsolete('Sommaire flottant')
obsolete('Prettytable')
obsolete('Médaille d\'or olympique')
obsolete('Médaille d\'argent olympique')
obsolete('Médaille de bronze olympique')