news: html syntax error due to duplicate <p> tag, This breaks Feed Readers
[samba-web.git] / scripts / js_redirect.py
1 #! /usr/bin/python
2 #
3 # js_redirect.py -- setup by Deryck Hodge <deryck@samba.org>
4 # to run via cron every hour on dp.
5 #
6 # This does two things:
7 # 1) Create the list of options for the select-a-mirror drop-down menu, and 
8 # 2) generate the Javascript to redirect samba.org to a random US mirror.
9 #
10
11 import os, re
12
13 os.chdir('/data/httpd/html/samba')
14 hosts = open('web_hosts.html', 'r').readlines()
15 mirrors = []  
16 us_mirrors = []
17
18 for line in hosts:
19     if line[:4] == '<li>':
20         parts = re.match('<li><a href=(.*/samba/).*">(.*)</a>', line)
21         # Make list of mirror_name/url pairs to preserve web_hosts sort. 
22         mirrors.append((parts.group(2), parts.group(1)))
23         # While we're here, get the US mirrors on their own
24         if re.match('<li><a href="(.*/samba/).*">USA (.*)</a>', line) and not line.find('us2') > -1:
25             us_mirrors.append(re.match('<li><a href="(.*/samba/).*">USA (.*)</a>', line).group(1))
26
27 # Write all mirrors to drop-down menu
28 menu = open('menu_options.html', 'w')
29 for m in mirrors:
30     menu.write('<option value=' + m[1] + '">' + m[0] + '</option>\n')
31 menu.close()
32
33 us_mirrors.sort
34 # Write the js for a US mirror redirect
35 js = open('redirect_include.html', 'w')
36 js.write('<script language="Javascript" type="text/javascript">\n')
37 js.write('<!-- Hide from old browsers\n')
38 js.write('randomMirror = new Array;\n')
39 for i in range(len(us_mirrors)):
40     js.write('randomMirror[' + str(i) + '] = "' + us_mirrors[i] + '"\n')
41 js.write('\n')
42 js.write('n = Math.floor(Math.random()*' + str(len(us_mirrors)) + ')\n')
43 js.write('// end hide -->\n')
44 js.write('</script>')
45 js.close()
46