Page MenuHomec4science

bfe_webjournal_widget_seminars.py
No OneTemporary

File Metadata

Created
Fri, Jun 7, 10:25

bfe_webjournal_widget_seminars.py

# -*- coding: utf-8 -*-
##
## This file is part of Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 CERN.
##
## 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.
##
## 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 Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""
WebJournal widget - Display Indico seminars
"""
from invenio.config import CFG_CACHEDIR, CFG_SITE_LANG
from urllib2 import urlopen
from xml.dom import minidom
import time
import socket
from invenio.webjournal_utils import \
parse_url_string
from invenio.messages import gettext_set_language
update_frequency = 3600 # in seconds
def format_element(bfo, indico_seminar_xml="http://indico.cern.ch/tools/export.py?fid=1l7&date=today&days=1&of=xml"):
"""
Display the list of seminar from the given Indico XML URL
@param indico_seminar_xml: the URL to the XML generated by an Indico instance
"""
args = parse_url_string(bfo.user_info['uri'])
journal_name = args["journal_name"]
cached_filename = "webjournal_widget_seminars_%s.xml" % journal_name
out = get_widget_html(bfo, indico_seminar_xml, cached_filename, args['ln'])
return out
def escape_values(bfo):
"""
Called by BibFormat in order to check if output of this element
should be escaped.
"""
return 0
def get_widget_html(bfo, indico_seminar_xml, cached_filename,
ln=CFG_SITE_LANG):
"""
Indico seminars of the day service
Gets seminars of the day from CERN Indico every 60 minutes and displays
them in a widget.
"""
_ = gettext_set_language(ln)
try:
seminar_xml = minidom.parse('%s/%s' % (CFG_CACHEDIR, cached_filename))
except:
try:
_update_seminars(indico_seminar_xml, cached_filename)
seminar_xml = minidom.parse('%s/%s' % (CFG_CACHEDIR, cached_filename))
except:
return "<ul><li><i>" + _("No information available") + "</i></li></ul>"
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:
try:
_update_seminars(indico_seminar_xml, cached_filename)
seminar_xml = minidom.parse('%s/%s' % (CFG_CACHEDIR, cached_filename))
except:
return "<ul><li><i>" + _("No information available") + "</i></li></ul>"
html = ""
seminars = seminar_xml.getElementsByTagName("seminar")
if len(seminars) == 0:
return "<ul><li><i>" + _("No seminars today") + "</i></li></ul>"
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>"
html = '<ul>' + html + '</ul>'
return html
def _update_seminars(indico_seminar_xml, cached_filename):
"""
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.
"""
default_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(2.0)
try:
try:
indico_xml = urlopen(indico_seminar_xml)
except:
return
finally:
socket.setdefaulttimeout(default_timeout)
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("&amp;", "")
category = category.replace("nbsp;", "")
category = category.replace("&nbsp;", "")
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" % (CFG_CACHEDIR, cached_filename), "w")
fptr.write("\n".join(seminar_xml))
fptr.close()
_ = gettext_set_language('en')
dummy = _("What's on today")
dummy = _("Seminars of the week")

Event Timeline