-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw3po.py
118 lines (90 loc) · 3.37 KB
/
w3po.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script assurant la diffusion des notifications de W3PO à ses abonnés
# (C) Linedwell, 2011-2018
#
# Distribué sous licence GNU GPLv3
# Distributed under the terms of the GNU GPLv3 license
# http://www.gnu.org/licenses/gpl.html
import sys, getopt
sys.path.insert(1, '..') #ajoute au PYTHONPATH le répertoire parent
import mylogging
import cgi
import urllib2
import pywikibot
# Déclarations
site = pywikibot.Site('fr', 'wikipedia')
nbrModif = 0
nbrTotal = 0
# Retourne la liste des personnes à qui envoyer la notification
def getSubscribers(group, hs):
pageGroup = pywikibot.Page(site, u"Utilisateur:Orikrin1998/Blog/Abonnés/"+group.title()+"s")
pageTotal = pywikibot.Page(site, u"Utilisateur:Orikrin1998/Blog/Abonnés/Wikipédia seulement")
pageHS = pywikibot.Page(site, u"Utilisateur:Orikrin1998/Blog/Abonnés/Tous les articles")
if not hs:
subscribers = set(list(pageGroup.linkedPages(namespaces=3)) + list(pageTotal.linkedPages(namespaces=3)) + list(pageHS.linkedPages(namespaces=3)))
if hs:
subscribers = set(list(pageHS.linkedPages(namespaces=3)))
return subscribers
#Retourne le titre associé à l'URL
def getURLTitle(url):
webPage = urllib2.urlopen(url)
_, params = cgi.parse_header(webPage.headers.get('Content-Type', ''))
encoding = params.get('charset', 'utf-8')
text = webPage.read().decode(encoding)
tt = text.split("<title>") [1]
titleFull = tt.split("</title>") [0]
title = titleFull.split(" | Blog d'un geek polyglotte") [0]
return title
#Exécution
def main():
subtype = ''
link = ''
hs = False
try:
opts, args = getopt.getopt(sys.argv[1:], 'h', ['hs'])
except getopt.GetoptError:
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--hs'):
hs = True
if len(args) == 2:
if args[0] in ('article', 'sondage', 'interview'):
subtype = args[0]
else:
print "type incorrect : attendu 'article', 'sondage' ou 'interview'"
sys.exit(2)
if args[1] != '':
link = args[1]
else:
print "lien obligatoire"
sys.exit(2)
else:
print u"usage : python " + sys.argv[0] + " [--hs] type_de_billet lien"
sys.exit(2)
banner = "{{Utilisateur:Orikrin1998/Blog/Annonce|type=" + subtype + "|lien=" + link + "|date=~~~~~}}"
billTitle = getURLTitle(link)
if type == "interview":
billTitle = "Interview de " + billTitle
subscribers = getSubscribers(subtype,hs)
for sub in subscribers:
try:
sub.get()
except pywikibot.NoPage:
pywikibot.output(u"Page %s does not exist; skipping."
% sub.title(asLink=True))
except pywikibot.IsRedirectPage:
pywikibot.output(u"Page %s is a redirect; skipping."
% sub.title(asLink=True))
except pywikibot.LockedPage:
pywikibot.output(u"Page %s is locked; skipping."
% sub.title(asLink=True))
else:
message = u"\n\n== Du nouveau sur W3PO : " + billTitle + " ==\n" + banner
sub.text += message
sub.save(u"Du nouveau sur W3PO",minor=False)
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()