-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
executable file
·315 lines (268 loc) · 9.08 KB
/
scrape.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
# This program is in the public domain
# Author: Paul Kienzke, William Ratcliff
import sys
import smtplib
import traceback
import urllib2
import re
from datetime import datetime
# Get a time stamp for the start of today.
today = datetime.today()
today = datetime(year=today.year,month=today.month,day=today.day)
# ===== Configuration ======
WEBMASTER = ""
#WEBMASTER = "[email protected]"
#WEBMASTER = "[email protected]"
# ==================== Facilities =====================
# Facility sets
AMERICAS = []
EUROPE = []
EAST = []
class NCNR:
name = "NCNR"
url = "http://www.ncnr.nist.gov/call/current_call.html"
def scrape(self):
self.deadline = "activity suspended until Feb. 2012"
AMERICAS.append(NCNR())
class ORNL:
name = "ORNL"
url = "http://neutrons.ornl.gov/users/proposals.shtml"
def scrape(self):
data = fetchurl(self.url)
pat = re.compile(r'<td>[^,]+,\s+(.*?),\s+Midnight Eastern Time</td>\s*<td>(.*?)</td>',
flags=re.DOTALL)
match = list(pat.findall(data))
for deadline, period in match:
# TODO: this will fail if Locale indicates non-English month names
if datetime.strptime(deadline, '%B %d, %Y') >= today:
self.deadline, self.period = deadline, period
break
else:
raise ValueError("Can't find proposal data. Website layout changed?")
AMERICAS.append(ORNL())
class LANSCE:
name = "LANSCE"
url = "http://lansce.lanl.gov/"
def scrape(self):
data = fetchurl(self.url)
pat = re.compile('Lujan Center.*?Proposal Call.*?Deadline (.*?)<',
flags=re.DOTALL)
match = pat.search(data)
if match:
self.deadline = match.group(1)
else:
raise ValueError("Can't find proposal data. Website layout changed?")
AMERICAS.append(LANSCE())
class CNBC:
name = "CNBC, Chalk River"
url = "http://www.cins.ca/beam.html"
deadline = "continuous proposal system"
def scrape(self): pass
AMERICAS.append(CNBC())
class ANSTO:
name = "ANSTO"
url="https://neutron.ansto.gov.au/Bragg/proposal/index.jsp"
def scrape(self):
data = fetchurl(self.url)
match = re.search('Proposal Round for instrument time (.*?)<.*?>. Proposals are due by (.*?)[.]',
data)
if match:
self.deadline = match.group(2)
self.period = match.group(1)
else:
raise ValueError("Can't find proposal data. Website layout changed?")
EAST.append(ANSTO())
class JPARC:
name = "J-PARC"
url = "http://j-parc.jp/MatLife/en/applying/koubo.html"
def scrape(self): pass
EAST.append(JPARC())
class ISIS:
name = "ISIS"
url = "http://www.isis.stfc.ac.uk/apply-for-beamtime/"
def scrape(self):
data = fetchurl(self.url)
match = re.search('Proposal deadline:\W*?(.*?) for beamtime from (.*?)\W*?<',
data)
if match:
self.deadline = match.group(1)
self.period = match.group(2)
else:
raise ValueError("Can't find proposal data. Website layout changed?")
EUROPE.append(ISIS())
class ILL:
name = "ILL"
url = "http://club.ill.fr/cv/"
def scrape(self):
data = fetchurl(self.url)
match = re.search('Next deadline is\W*?(.*?)[.]?<', data)
if match:
self.deadline = match.group(1)
else:
raise ValueError("Can't find proposal data. Website layout changed?")
EUROPE.append(ILL())
class LLB:
name = "LLB"
url = "http://www-llb.cea.fr/en/fr-en/proposal.php"
deadline = "1 May and 1 November annually"
def scrape(self):
return # Show all rather than just the next
if (5 < today.month < 11
or (today.month == 5 and today.day > 1)
or (today.month == 11 and today.day == 1)):
self.deadline = "1 November %d"%today.year
elif today.month > 5:
self.deadline = "1 May %d"%(today.year+1)
else:
self.deadline = "1 May %d"%(today.year)
EUROPE.append(LLB())
class BENSC:
name = "BENSC"
url = "http://www.helmholtz-berlin.de/user/neutrons/user-info/call-for-proposals_en.html"
deadline = "1 March and 1 September annually"
def scrape(self):
return # Show all rather than just the next
if (3 < today.month < 9
or (today.month == 3 and today.day > 1)
or (today.month == 9 and today.day == 1)):
self.deadline = "1 September %d"%today.year
elif today.month > 3:
self.deadline = "1 March %d"%(today.year+1)
else:
self.deadline = "1 March %d"%(today.year)
EUROPE.append(BENSC())
class FRM_II:
name = "FRM II"
url = "http://www.frm2.tum.de/en/user-office/user-guide/index.html"
def scrape(self): pass
EUROPE.append(FRM_II())
class SINQ:
name = "SINQ"
url = "http://sinq.web.psi.ch/sinq/sinq_call.html"
def scrape(self):
data = fetchurl(self.url)
match = re.search('proposal\W*submission\W*deadline:\W*<font.*?>\W*(.*?)\W*</font', data)
if match:
self.deadline = match.group(1)
else:
raise ValueError("Can't find proposal data. Website layout changed?")
EUROPE.append(SINQ())
# ===== page layout ===
PAGE = """\
<html>
<link rel="stylesheet" type="text/css" href="site.css" />
<title>Beamtime Proposal Deadlines</title>
<body>
<h2>Beamtime proposal deadlines</h2>
%(table)s
* This information was obtained automatically from the respective
websites and we cannot guarantee that it is correct. Please verify dates
with the individual facilities.
</body>
</html>
"""
TABLE_HEADER = """\
<table class="pretty-table"><tbody>
<tr class="header">
<th scope="col">Institute</th>
<th scope="col">Submission Deadline*</th>
<th scope="col">Experiment Period</th>
</tr>"""
TABLE_REGION = """\
<tr class="region">
<th scope="row" colspan="3">%(region)s</td>
</tr>"""
TABLE_ROW = """\
<tr class="%(rowclass)s">
<th scope="row"><a href="%(url)s">%(name)s</a></td>
<td>%(deadline)s</td>
<td>%(period)s</td>
</tr>"""
TABLE_FOOTER = """</tbody></table>"""
def build_table(regions):
emit = [TABLE_HEADER]
for region,facilities in regions:
emit.append(TABLE_REGION%dict(region=region))
odd = True
for facility in facilities:
d = dict(name = facility.name,
url = facility.url,
deadline = facility.deadline,
period = facility.period,
rowclass = "odd" if odd else "even",
)
emit.append(TABLE_ROW%d)
odd=not odd
emit.append(TABLE_FOOTER)
return "\n".join(emit)
def scrape(facilities, limit=None):
errors = []
for facility in facilities:
try:
if not limit or facility.__class__.__name__ in limit:
facility.scrape()
except:
errors.append(facility.name+"\n"+traceback.format_exc())
if not hasattr(facility,'period'):
facility.period = ''
if not hasattr(facility,'deadline'):
facility.deadline = 'unknown'
if not testurl(facility.url):
errors.append("%s: broken link\n %s"%(facility.name,facility.url))
return errors
def testurl(url):
try:
f = urllib2.urlopen(urllib2.Request(url))
okay = True
except:
okay = False
return okay
def fetchurl(url):
return urllib2.urlopen(url).read()
def fetchdom(url):
import BeautifulSoup
return BeautifulSoup.BeautifulSoup(fetchurl(url))
def mail(sender, receivers, message,
subject='no subject', server='localhost'):
"""
Send an email message to a group of receivers
"""
if ':' in server:
host,port = server.split(':')
port = int(port)
else:
host,port = server,25
header="From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
header %= sender,", ".join(receivers),subject
#print "Sending the following mail message:\n"+header+message
#print "Trying to connect to",host,port
smtp = smtplib.SMTP(host,port)
#print "Connection established"
smtp.sendmail(sender,receivers,header+message)
#print "Mail sent from",sender,"to",", ".join(receivers)
smtp.quit()
def main():
if len(sys.argv) > 1:
limit = sys.argv[1:]
else:
limit = None
# Scrape all facility webpages, storing results in the facility objects.
# Return
errors = scrape(AMERICAS+EUROPE+EAST, limit)
table = build_table([('North America',AMERICAS),
('Europe',EUROPE),
('Asia and Australia',EAST),
])
page = PAGE%dict(table=table)
print page
if errors:
if limit is not None and WEBMASTER != "":
mail(sender=WEBMASTER,
receivers=[WEBMASTER],
message="\n".join(errors),
subject="proposal deadline scraper")
else:
print >>sys.stderr, "\n".join(errors)
if __name__ == "__main__":
main()