-
Notifications
You must be signed in to change notification settings - Fork 0
/
zeusrules.py
62 lines (48 loc) · 1.79 KB
/
zeusrules.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
import yaml
import gdata.spreadsheet.service
import gdata.service
import gdata.spreadsheet
def main():
stream = file('config.yaml', 'r')
config = yaml.load(stream)
client = gdata.spreadsheet.service.SpreadsheetsService()
client.email = config['email']
client.password = config['password']
client.source = config['app_source']
client.ProgrammaticLogin()
DocKey = config['gdoc_key']
DocWorkSheetId = 'od6' # defaults to 'od6', don't ask why.
SourceDomain = 'http://www.mathsrevision.net'
rows = client.GetListFeed(DocKey, DocWorkSheetId)
# file opening (wraps the generated entries)
ZeusRules = '## Zeus web server rewrite rules.' + "\n"
ZeusRules += '## CAUTION: This file is generated automatically. Manual changes will be lost next time this file is regenerated.' + "\n"
ZeusRules += "\n\n"
ZeusRules += 'insensitive match IN:Host into $ with ^www\.mathsrevision\.net$ ' + "\n"
ZeusRules += 'if matched ' + "\n"
# generate entries
for row in rows.entry:
# remove the domain part from the url
src = row.custom['source'].text.split(SourceDomain)[1]
# escape the string (dots, question marks)
src = src.replace('.', '\.')
src = src.replace('?', '\?')
dest = row.custom['destination'].text
rule = "\n"
rule += ' insensitive match URL into $ with ^{source}$' + "\n"
rule += ' if matched ' + "\n"
rule += ' set OUT:Location = {destination}' + "\n"
rule += ' set OUT:Content-Type = text/html' + "\n"
rule += ' set RESPONSE = 301' + "\n"
rule += ' set BODY = Moved' + "\n"
rule += ' goto END' + "\n"
rule += ' endif' + "\n"
ZeusRules += rule.format(source=src, destination=dest)
# file closing
ZeusRules += 'endif'
# save to file
outfile = file("rewrite.script", 'w')
print >> outfile, ZeusRules
outfile.close()
if __name__ == "__main__":
main()