Allegato "newsletter.py"
Scarica 1 #!/usr/bin/python
2 # -*- coding: ISO-8859-1 -*-
3
4 # Copyright (C) 2007 Milo Casagrande <milo@ubuntu.com>
5 #
6 # This program is free software; you can redistribuite it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; either version 2 of the License, or (at your option)
9 # any later version
10 #
11 # This program is distribuited in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA, 02110-1301 USA.
19
20 '''
21 Programma per la conversione della newsletter italiana in puro
22 formato testo, da usare per spedire il contenuto della newsletter
23 via mail.
24 '''
25
26 import sys, re, xmlrpclib
27 import codecs
28 import string
29
30 alpha = string.letters
31
32 PROGVER = "0.0.3"
33
34 WIKIBASE = "http://wiki.ubuntu-it.org/" # URL del wiki italiano
35 WIKIWORLD = "http://wiki.ubuntu.com/" # URL del wiki internazionale
36 NEWSBASE = "NewsletterItaliana/" # stringa di base per la newsletter
37 BREAK = "[[BR]]\n"
38 CATEGORY = "CategoryComunita"
39 TYPE = "<type 'dict'>"
40
41 ANNO = "" # anno della newsletter
42 NUMERO = "" # numero della nesletter
43
44
45 def get_newsletter():
46 """
47 Funzione per leggere il testo della newsletter direttamente online
48 """
49 global NEWSBASE, ANNO, NUMERO
50
51 wiki = xmlrpclib.ServerProxy(WIKIBASE+"?action=xmlrpc2")
52
53 pagina = NEWSBASE + ANNO + "." + NUMERO
54
55 pagedata = wiki.getPage(pagina)
56
57 tipo = str(type(pagedata))
58
59 if tipo == TYPE:
60 print "*** Errore: la pagina non esiste!"
61 sys.exit(2)
62 else:
63 read_newsletter(pagedata)
64
65 def read_newsletter(paginawiki):
66 """
67 Funzione per leggere la newsletter e per iniziare la magia
68 @paginawiki: stringa con la pagina recuperata online
69 """
70 global ANNO, NUMERO
71
72 inputfile = "/tmp/" + ANNO + NUMERO + ".txt"
73 outputfile = ANNO + NUMERO + ".txt"
74
75 try:
76 infile = codecs.open(inputfile, 'wr', 'utf-8')
77 except IOError, e:
78 print "*** Errore nell'aprire il file %s" % inputfile
79 sys.exit(2)
80
81 # apriamo e chiudiamo il file in codifica utf-8
82 infile.write(paginawiki)
83 infile.close()
84
85 try:
86 infile = open(inputfile, 'r')
87 except IOError, e:
88 print "*** Errore nell'aprire il file %s" % inputfile
89 sys.exit(2)
90
91 try:
92 outfile = open(outputfile, 'w')
93 except IOError, e:
94 print "*** Errore nella'aprire il file %s" % outputfile
95 sys.exit(2)
96
97 temp = ""
98 towrite = ""
99
100 while True:
101 string = infile.readline()
102
103 if string == "": # siamo alla fine del file
104 False
105 break
106
107 towrite = check_string(string)
108
109 if towrite == None:
110 pass
111 else:
112 temp += towrite
113
114
115 outfile.write(temp)
116 infile.close()
117 outfile.close()
118
119 print "Newsletter creata nel file %s" % outputfile
120
121 def check_string(string):
122 u"""Qui c'รจ tutta la magia...
123 Ritorna la stringa analizzata e possibilmente a posto
124
125 @string: stringa da anilizzare
126 """
127 exp = []
128 nexp = []
129 nnexp = []
130
131 # la riga dei permessi viene tolta
132 if re.findall('\#acl',string) != []:
133 return None
134 # la riga iniziale viene tolta
135 elif re.findall('\#LANGUAGE', string) != []:
136 return None
137 # qualsiasi tabella viene tolta
138 elif re.findall('\|\|\<table', string) != []:
139 return None
140 # tolte le linee orizzontali
141 elif re.findall('\-{4,6}', string) != []:
142 return None
143 # qualsiasi immagine
144 elif re.findall('attachment', string) != []:
145 return None
146 elif re.findall('\[\[Immagine\(.*?\]\]', string) != []:
147 return None
148 # tutti i titoli vengono mantenuti
149 elif re.findall('\={1,3}\s.*?\s\={1,3}', string) != []:
150 return string
151 # un qualsiasi a capo viene tolto e sostituito
152 elif re.findall('\[\[BR\]\]\\n', string) != []:
153 string = string.replace(BREAK, "\n")
154 elif re.match(CATEGORY, string):
155 return None
156
157 # tutti gli apici - qui c'รจ il problema degli apostrofi, anche quelli vengono tolti
158 exp = re.findall('\'{2,5}.*?\'{2,5}', string)
159
160 if exp != []:
161 for word in exp:
162 nexp.append(word.replace("'", ""))
163
164 for i in range(len(exp)):
165 string = string.replace(exp[i], nexp[i])
166
167 # tutti gli apici inversi
168 exp = re.findall('\`{1,2}', string)
169
170 if exp != []:
171 string = string.replace("`", "")
172
173 exp = []
174 nexp = []
175
176 # link esterni
177 exp = re.findall('\[[^wiki:][^0-9][^\.][^\:][^0-9].*?[^:]\]', string)
178
179 if exp != []:
180 nnexp = replace_square(exp)
181
182 newstring = ""
183
184 for word in nnexp:
185 splitted = word.split()
186 for split in splitted[1:]:
187 newstring += split + " "
188
189 newstring += "(" + splitted[0] + " )"
190 nexp.append(newstring)
191 newstring = ""
192
193 for i in range(len(exp)):
194 string = string.replace(exp[i], nexp[i])
195
196 exp = []
197 nexp = []
198 nnexp = []
199
200 # link interni al wiki tipo [:Pagina:Nome]
201 exp = re.findall('\[\:+.*?\:+.*?\]', string)
202
203 if exp != []:
204 nnexp = replace_square(exp)
205
206 newstring = ""
207
208 for word in nnexp:
209 splitted = word.split(":")
210 for split in splitted[2:]:
211 newstring += split + " "
212
213 newstring += "(" + WIKIBASE + splitted[1] + " )"
214 nexp.append(newstring)
215 newstring = ""
216
217 for i in range(len(exp)):
218 string = string.replace(exp[i], nexp[i])
219
220 exp = []
221 nexp = []
222 nnexp = []
223
224 # link interni al wiki tipo [:Pagina]
225 exp = re.findall('\[\:.*?\]', string)
226
227 if exp != []:
228 nnexp = replace_square(exp)
229
230 newstring = ""
231
232 for word in nnexp:
233 splitted = word.split(":")
234 for split in splitted[1:]:
235 newstring += split + " "
236
237 newstring += "(" + WIKIBASE + splitted[1] + " )"
238 nexp.append(newstring)
239 newstring = ""
240
241 for i in range(len(exp)):
242 string = string.replace(exp[i], nexp[i])
243
244
245 exp = []
246 nexp = []
247 nnexp = []
248
249 # link al wiki internazionale tipo [wiki:Ubuntu/Pagina Nome]
250 exp = re.findall('\[wiki:Ubuntu/.*?\s.*?\]', string)
251
252 if exp != []:
253 nnexp = replace_square(exp)
254
255 newstring = ""
256
257 for word in nnexp:
258 splitted = word.split()
259 for split in splitted[1:]:
260 newstring += split + " "
261
262 # separazione del vero nome della pagina
263 base = splitted[0].split("/")
264
265 newstring += "(" + WIKIWORLD + base[1] + " )"
266 nexp.append(newstring)
267 newstring = ""
268
269 for i in range(len(exp)):
270 string = string.replace(exp[i], nexp[i])
271
272
273 return string
274
275 def replace_square(exp):
276 """
277 Funzione per togliere le parentesi quadre
278 @exp: la lista contenente le stringhe da cui togliere le parentesi
279 """
280 nsq = []
281 nnsq = []
282
283 for word in exp:
284 nsq.append(word.replace("[", ""))
285
286 for word in nsq:
287 nnsq.append(word.replace("]", ""))
288
289 return nnsq
290
291 def main():
292 global ANNO, NUMERO, BUGINFO
293
294 print "Welcome to the Jungle version %s!" % PROGVER
295
296 while True:
297 ANNO = raw_input("Inserisci l'anno della newsletter: ")
298
299 for i in ANNO:
300 if i in alpha:
301 print "Dato errato!"
302
303 if len(ANNO) > 4 or len(ANNO) < 4:
304 print "Hai inserito un anno sbagliato!"
305 else:
306 break
307
308 while True:
309 NUMERO = raw_input("Inserisci il numero della newsletter: ")
310
311 # controllare se รจ vuoto?
312
313 for i in NUMERO:
314 if i in alpha:
315 print "Dato errato!"
316
317 lung = len(NUMERO)
318
319 if lung > 3:
320 print "Numero della newsletter inesistente!"
321 True
322 continue
323 elif lung == 2:
324 NUMERO = "0" + NUMERO
325 False
326 break
327 elif lung == 1:
328 NUMERO = "00" + NUMERO
329 False
330 break
331
332
333 get_newsletter()
334
335
336 if __name__ == "__main__":
337 main()
338 sys.exit(0)
Allegati
Per riferirsi agli allegati di una pagina, usare attachment:NOME_FILE, come mostrato qui sotto nell'elenco degli allegati. NON usare l'URL che si trova in corrispondenza del collegamento [scarica], potrebbe cambiare in futuro.Non รจ consentito inserire allegati su questa pagina.