Wikipedia:Elenchi generati offline/Quote nella sezione iniziale/quote.py

# -*- coding: utf-8  -*-
import re, xmlreader, wikipedia, sys

def SezioneUnica(txt):
	#Voce con unica sezione?
	sections = re.findall('== *([^=]+) *==\s', txt)
	if len(sections) == 0 or re.search('(note|bibliografia|voci correlate|altri progetti|collegamenti esterni)', sections[0], re.IGNORECASE):
		return True
	else:
		return False
		
scelta = int(sys.argv[2])

if scelta == 1: #Prima dell'incipit
	regex = re.compile("^[^'=]*\{\{ *quote\s*\|[^}]+\}\}[^'=]*('''.*'''|\{\{bio)", re.IGNORECASE)
elif scelta == 2: #Dopo l'incipit
	regex = re.compile("^[^'=]*('''[^=]*'''|\{\{bio)[^=]*\{\{ *quote\s*\|[^}]+\}\}", re.IGNORECASE)

hasquote = re.compile("\{\{ *quote", re.IGNORECASE)

dump = xmlreader.XmlDump(sys.argv[1])
gen = dump.parse()
for p in gen:
	if re.match('^(Wikipedia|File|Template|Aiuto|Categoria|Portale|Progetto):', p.title, re.IGNORECASE) and not hasquote.search(p.text):
		continue

	#Rimuovo gli '=' singoli dei parametri dei template
	txt = re.sub("[^=]=[^=]", '', p.text)
	#Rimuovo corsivo
	txt = re.sub("[^']''[^']+?''[^']", '', txt)
	#Rimuovo gli apostrofi
	txt = re.sub("[^']'[^']", '', txt)
	#Rimuovo note
	txt = re.sub("<ref[^>]*>.+?</ref>", '', txt)
	
	
	if scelta in [1, 2]:
		if regex.search(txt):
			print '# [['+p.title.encode('utf8')+']]'
	elif scelta in [3, 4]: #Citazione lunghe
		
		#Rimuovo tutte le sezioni
		txt2 = re.sub('==(.|\s)+$', '', txt)
		
		#Cerco i template quote
		m = re.findall('\{\{ *quote\s*\|([^}]+)\}\}', txt2, re.IGNORECASE)
		
		if len(m) > 1 and ( ( not SezioneUnica(txt) and scelta == 4) or scelta == 3):	
			print '# [['+p.title.encode('utf8')+']]'
			continue
		for r in m:
			#Estraggo la citazione per ogni template
			cit = re.sub('\|.*$', '', r[0])
			if len(cit) > 300 and ( ( not SezioneUnica(txt) and scelta == 4) or scelta == 3):
				print '# [['+p.title.encode('utf8')+']]'
				continue