1 | """ Test internet site |
---|
2 | |
---|
3 | Ole Nielsen, ACSys 2000, GA 2006 |
---|
4 | """ |
---|
5 | |
---|
6 | |
---|
7 | import urllib |
---|
8 | import string |
---|
9 | #from caching import cache |
---|
10 | |
---|
11 | base_url = 'http://www.ga.gov.au:8500/oracle/flood/' |
---|
12 | servlet_base_url = 'http://www.ga.gov.au:8500/' |
---|
13 | |
---|
14 | def test_site(): |
---|
15 | """Test flood database |
---|
16 | """ |
---|
17 | |
---|
18 | top_url = 'flood_input_hide.jsp' |
---|
19 | |
---|
20 | url = base_url+top_url |
---|
21 | live = False |
---|
22 | print 'Testing %s. ' %url, |
---|
23 | T = get_page(url) |
---|
24 | |
---|
25 | for line in T: |
---|
26 | #print line.strip() |
---|
27 | if line.startswith('<title>Geoscience Australia: Online Flood Search'): |
---|
28 | live = True |
---|
29 | break |
---|
30 | |
---|
31 | if live is True: |
---|
32 | print 'OK: Page is live' |
---|
33 | else: |
---|
34 | msg = 'Page %s is not live' %url |
---|
35 | raise msg |
---|
36 | |
---|
37 | print |
---|
38 | |
---|
39 | |
---|
40 | def test_database(filename): |
---|
41 | """Read list of studies from html and try them one by one |
---|
42 | """ |
---|
43 | |
---|
44 | print 'Reading %s' %filename |
---|
45 | fid = open(filename) |
---|
46 | |
---|
47 | for link in get_individual_studies(fid.readlines()): |
---|
48 | url = base_url+htmlmap(link) |
---|
49 | |
---|
50 | live = False |
---|
51 | print 'Testing %s...' %link[:72], |
---|
52 | T = get_page(url) |
---|
53 | |
---|
54 | live = False |
---|
55 | for line in T: |
---|
56 | if line.startswith('<tr><th>Dataset Name</th>') or\ |
---|
57 | line.startswith('<h2>Study title only available.</h2>'): |
---|
58 | live = True |
---|
59 | break |
---|
60 | |
---|
61 | if live is True: |
---|
62 | print 'OK: Link is live' |
---|
63 | else: |
---|
64 | msg = 'FAIL: Link is not live: %s' %url |
---|
65 | raise msg |
---|
66 | |
---|
67 | |
---|
68 | # Secon tier links |
---|
69 | for link in get_second_tier_links(T): |
---|
70 | url = servlet_base_url+htmlmap(link) |
---|
71 | |
---|
72 | live = False |
---|
73 | print 'Testing %s...' %link[:80], |
---|
74 | T = get_page(url) |
---|
75 | |
---|
76 | live = False |
---|
77 | for line in T: |
---|
78 | #print line.strip() |
---|
79 | if line.startswith('<tr><td><h3>Participants in '): |
---|
80 | live = True |
---|
81 | break |
---|
82 | |
---|
83 | if live is True: |
---|
84 | print 'OK: Detail link is live' |
---|
85 | else: |
---|
86 | for line in T: |
---|
87 | print line.strip() |
---|
88 | msg = 'FAIL: Detail link is not live' |
---|
89 | print msg |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | def get_second_tier_links(lines): |
---|
94 | """Scan html lines for flood details and yield |
---|
95 | """ |
---|
96 | for line in lines: |
---|
97 | |
---|
98 | index = line.find('<a href="/servlet/FloodDetailServlet?sno') |
---|
99 | if index >= 0: |
---|
100 | start_index = line.find('servlet', index) |
---|
101 | end_index = line.find('">', start_index) |
---|
102 | yield line[start_index:end_index] |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | def get_individual_studies(lines): |
---|
107 | """Scan html lines for individual flood studies and yield links |
---|
108 | """ |
---|
109 | |
---|
110 | for line in lines: |
---|
111 | index = line.find('<a href="flood_infolist.jsp?sno') |
---|
112 | if index >= 0: |
---|
113 | start_index = line.find('flood_infolist', index) |
---|
114 | end_index = line.find('">', start_index) |
---|
115 | |
---|
116 | yield line[start_index:end_index] |
---|
117 | |
---|
118 | |
---|
119 | def get_page(URL): |
---|
120 | """General routine for getting and caching URL pages |
---|
121 | """ |
---|
122 | |
---|
123 | F = urllib.urlopen(URL) |
---|
124 | T = F.readlines() |
---|
125 | return(T) |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | def htmlmap(s): |
---|
130 | import types |
---|
131 | from string import replace, strip |
---|
132 | |
---|
133 | s = replace(s,' ','%20') |
---|
134 | return(strip(s)) |
---|
135 | |
---|
136 | |
---|
137 | test_site() |
---|
138 | test_database('flood_studies_all.html') |
---|
139 | |
---|
140 | |
---|
141 | |
---|