Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F88627930
bfe_webjournal_widget_seminars.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Oct 19, 20:28
Size
5 KB
Mime Type
text/x-python
Expires
Mon, Oct 21, 20:28 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21800462
Attached To
R3600 invenio-infoscience
bfe_webjournal_widget_seminars.py
View Options
# -*- coding: utf-8 -*-
## $Id$
##
## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
from
invenio.config
import
cachedir
from
urllib2
import
urlopen
from
xml.dom
import
minidom
import
time
Cached_Filename
=
"webjournal_widget_seminars.xml"
Indico_Seminar_Location
=
"http://indico.cern.ch/tools/export.py?fid=1l7&date=today&days=1&of=xml"
Update_Frequency
=
3600
# in seconds
def
format
(
bfo
):
"""
"""
out
=
get_widget_HTML
()
return
out
def
escape_values
(
bfo
):
"""
"""
return
0
def
get_widget_HTML
():
"""
Indico seminars of the day service
Gets seminars of the day from CERN Indico every 60 minutes and displays
them in a widget.
"""
try
:
seminar_xml
=
minidom
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
except
:
_update_seminars
()
seminar_xml
=
minidom
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
try
:
timestamp
=
seminar_xml
.
firstChild
.
getAttribute
(
"time"
)
except
:
timestamp
=
time
.
struct_time
()
last_update
=
time
.
mktime
(
time
.
strptime
(
timestamp
,
"%a,
%d
%b %Y %H:%M:%S %Z"
))
now
=
time
.
mktime
(
time
.
gmtime
())
if
last_update
+
Update_Frequency
<
now
:
_update_seminars
()
seminar_xml
=
minidom
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
html
=
""
seminars
=
seminar_xml
.
getElementsByTagName
(
"seminar"
)
if
len
(
seminars
)
==
0
:
return
"<li><i>no seminars today</i></li>"
for
seminar
in
seminars
:
html
+=
"<li>"
try
:
seminar_time
=
seminar
.
getElementsByTagName
(
"start_time"
)[
0
]
.
firstChild
.
toxml
()
except
:
seminar_time
=
""
try
:
category
=
seminar
.
getElementsByTagName
(
"category"
)[
0
]
.
firstChild
.
toxml
()
except
:
category
=
"Seminar"
html
+=
'
%s
%s
<br/>'
%
(
seminar_time
,
category
)
try
:
title
=
seminar
.
getElementsByTagName
(
"title"
)[
0
]
.
firstChild
.
toxml
()
except
:
title
=
""
try
:
url
=
seminar
.
getElementsByTagName
(
"url"
)[
0
]
.
firstChild
.
toxml
()
except
:
url
=
"#"
try
:
speaker
=
seminar
.
getElementsByTagName
(
"speaker"
)[
0
]
.
firstChild
.
toxml
()
except
:
speaker
=
""
if
(
title
!=
""
):
html
+=
'<strong><a href="
%s
">
%s
</a></strong>,
%s
<br/>'
%
(
url
,
title
,
speaker
)
try
:
room
=
seminar
.
getElementsByTagName
(
"room"
)[
0
]
.
firstChild
.
toxml
()
except
:
room
=
""
html
+=
room
html
+=
"</li>"
return
html
.
encode
(
'utf-8'
)
def
_update_seminars
():
"""
helper function that gets the xml data source from CERN Indico and creates
a dedicated xml file in the cache for easy use in the widget.
"""
indico_xml
=
urlopen
(
Indico_Seminar_Location
)
xml_file_handler
=
minidom
.
parseString
(
indico_xml
.
read
())
seminar_xml
=
[
'<Indico_Seminars time="
%s
">'
%
time
.
strftime
(
"%a,
%d
%b %Y %H:%M:%S GMT"
,
time
.
gmtime
()),
]
agenda_items
=
xml_file_handler
.
getElementsByTagName
(
"agenda_item"
)
for
item
in
agenda_items
:
seminar_xml
.
extend
([
"<seminar>"
,
])
try
:
start_time
=
item
.
getElementsByTagName
(
"start_time"
)[
0
]
.
firstChild
.
toxml
()
except
:
start_time
=
""
seminar_xml
.
extend
([
"<start_time>
%s
</start_time>"
%
start_time
,
])
try
:
category
=
item
.
getElementsByTagName
(
"category"
)[
0
]
.
firstChild
.
toxml
()
category
=
category
.
split
(
"/"
)[
-
1
]
category
=
category
.
replace
(
"&"
,
""
)
category
=
category
.
replace
(
"nbsp;"
,
""
)
category
=
category
.
replace
(
" "
,
""
)
except
:
category
=
""
seminar_xml
.
extend
([
"<category>
%s
</category>"
%
category
,
])
try
:
title
=
item
.
getElementsByTagName
(
"title"
)[
0
]
.
firstChild
.
toxml
()
except
:
title
=
""
seminar_xml
.
extend
([
"<title>
%s
</title>"
%
title
,
])
try
:
url
=
item
.
getElementsByTagName
(
"agenda_url"
)[
0
]
.
firstChild
.
toxml
()
except
:
url
=
"#"
seminar_xml
.
extend
([
"<url>
%s
</url>"
%
url
,
])
try
:
speaker
=
item
.
getElementsByTagName
(
"speaker"
)[
0
]
.
firstChild
.
toxml
()
except
:
speaker
=
""
seminar_xml
.
extend
([
"<speaker>
%s
</speaker>"
%
speaker
,
])
try
:
room
=
item
.
getElementsByTagName
(
"room"
)[
0
]
.
firstChild
.
toxml
()
except
:
room
=
""
seminar_xml
.
extend
([
"<room>
%s
</room>"
%
room
,
])
seminar_xml
.
extend
([
"</seminar>"
,
])
seminar_xml
.
extend
([
"</Indico_Seminars>"
,
])
# write the created file to cache
fptr
=
open
(
"
%s
/
%s
"
%
(
cachedir
,
Cached_Filename
),
"w"
)
fptr
.
write
((
"
\n
"
.
join
(
seminar_xml
))
.
encode
(
'utf-8'
))
fptr
.
close
()
if
__name__
==
"__main__"
:
get_widget_HTML
()
Event Timeline
Log In to Comment