-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathafegeixAutoritat.py
91 lines (68 loc) · 2.21 KB
/
afegeixAutoritat.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pprint
import json
import mwclient
import argparse
import pprint
import time
# Import JSON configuration
parser = argparse.ArgumentParser(description="""Script for adding Autoritat template via API""")
parser.add_argument("-config",help="""Path to a JSON file with configuration options!""")
parser.add_argument("-file",help="""Path to a JSON file with configuration options!""")
args = parser.parse_args()
if "config" in args:
if args.config is not None:
with open(args.config) as json_data_file:
data = json.load(json_data_file)
if "mw" in data:
if "host" in data["mw"]:
host = data["mw"]["host"]
if "user" in data["mw"]:
user = data["mw"]["user"]
if "password" in data["mw"]:
pwd = data["mw"]["password"]
if "protocol" in data["mw"]:
protocol = data["mw"]["protocol"]
site = mwclient.Site(host, scheme=protocol)
if user and pwd :
# Login parameters
site.login(user, pwd)
def update_wiki(site, pagename, content):
page = site.pages[ pagename ]
summary = "Afegida plantilla Autoritat"
if page.can('edit'):
page.save( content, summary=summary, minor=False, bot=True)
def process_content(content):
done = 0
new_content = ""
plantilla = "{{Autoritat}}\n"
lines = content.split("\n")
for line in lines:
if line.find("[[Categor") >= 0 and done == 0:
new_content = new_content + plantilla + "\n"
done = done + 1
if line.find("{{ORDENA") >= 0 and done == 0:
new_content = new_content + plantilla + "\n"
done = done + 1
if line.find("utoritat}}") >= 0 and done == 0:
new_content = new_content
done = done + 1
return None
new_content = new_content + line + "\n"
return new_content
with open(args.file) as fp:
for line in fp:
pageline = line.strip()
page = site.Pages[pageline]
content = page.text()
# If enpty content (new page) exit
if not content :
continue
new_content = process_content( content )
if new_content is None :
continue
if ( new_content ) and ( content != new_content ) :
update_wiki(site, pageline, new_content)
print( pageline )
time.sleep( 30 )