Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92036733
bfe_webjournal_widget_weather.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, Nov 16, 20:02
Size
3 KB
Mime Type
text/x-python
Expires
Mon, Nov 18, 20:02 (2 d)
Engine
blob
Format
Raw Data
Handle
22366910
Attached To
R3600 invenio-infoscience
bfe_webjournal_widget_weather.py
View Options
#!/usr/bin/env python
"""
"""
from
invenio
import
errorlib
from
invenio.config
import
cachedir
import
feedparser
import
time
from
urllib2
import
urlopen
from
invenio.errorlib
import
register_exception
import
re
Weather_Service
=
"Yahoo! Weather"
# rss feed on yahoo weather, check developer.yahoo.com/weather for details
RSS_Feed
=
"http://weather.yahooapis.com/forecastrss?p=SZXX0008&u=c"
# filename of the rss feed in cache
Cached_Filename
=
"webjournal_widget_YahooWeather.rss"
# filename of flat file in cache that holds the expire time
Expire_Time_Filename
=
"weather_RSS_expires"
image_pattern
=
re
.
compile
(
'''
<img\s*(class=["']imageScale["'])*?\s*src=(?P<image>\S*)\s*/>*
'''
,
re
.
DOTALL
|
re
.
IGNORECASE
|
re
.
VERBOSE
)
def
format
(
bfo
,
title
=
""
):
"""
wrapper function needed for BibFormat to route the widget HTML
"""
out
=
get_widget_HTML
()
if
title
!=
""
:
try
:
weather_image_match
=
image_pattern
.
findall
(
out
)[
0
]
weather_image
=
weather_image_match
[
1
]
out
=
re
.
sub
(
image_pattern
,
""
,
out
)
except
:
register_exception
(
req
=
bfo
.
req
)
weather_image
=
""
weather_image
=
weather_image
.
replace
(
"
\"
"
,
"
\'
"
)
out
=
'''<div id="weather" style="background: url(%s) left bottom no-repeat;" class="rmenuitem">
<h3 class="rmenutext">%s</h3>
</div>
<ul class="rmenulist">
%s
</ul>
'''
%
(
weather_image
,
title
,
out
)
return
out
def
escape_values
(
bfo
):
"""
"""
return
0
def
get_widget_HTML
():
"""
weather forecast using Yahoo! Weather service
we check and store the "expires" data from the rss feed to decide when
an update is needed.
there always resides a cached version in cds cachedir along with a flat
file that indicates the time when the feed expires.
"""
try
:
weather_feed
=
feedparser
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
except
:
_update_feed
()
weather_feed
=
feedparser
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
now_in_gmt
=
time
.
gmtime
()
now_time_string
=
time
.
strftime
(
"%a,
%d
%b %Y %H:%M:%S GMT"
,
now_in_gmt
)
try
:
expire_time_string
=
open
(
'
%s
/
%s
'
(
cachedir
,
Expire_Time_Filename
))
.
read
()
expire_time
=
time
.
strptime
(
open
(
Expire_Time_Filename
)
.
read
(),
"%a,
%d
%b %Y %H:%M:%S %Z"
)
#expire_time['tm_isdt'] = 0
expire_in_seconds
=
time
.
mktime
(
expire_time
)
now_in_seconds
=
time
.
mktime
(
now_in_gmt
)
diff
=
time
.
mktime
(
expire_time
)
-
time
.
mktime
(
now_in_gmt
)
except
:
diff
=
-
1
if
diff
<
0
:
_update_feed
()
weather_feed
=
feedparser
.
parse
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
))
# construct the HTML
html
=
weather_feed
.
entries
[
0
][
'summary'
]
return
html
def
_update_feed
():
"""
helper function that updates the feed by copying the new rss file to the
cache dir and resetting the time string on the expireTime flat file
"""
feed
=
urlopen
(
RSS_Feed
)
cached_file
=
open
(
'
%s
/
%s
'
%
(
cachedir
,
Cached_Filename
),
'w'
)
cached_file
.
write
(
feed
.
read
())
cached_file
.
close
()
feed_data
=
feedparser
.
parse
(
RSS_Feed
)
expire_time
=
feed_data
.
headers
[
'expires'
]
expire_file
=
open
(
'
%s
/
%s
'
%
(
cachedir
,
Expire_Time_Filename
),
'w'
)
expire_file
.
write
(
expire_time
)
expire_file
.
close
()
if
__name__
==
"__main__"
:
from
invenio.bibformat_engine
import
BibFormatObject
myrec
=
BibFormatObject
(
7
)
format
(
myrec
)
Event Timeline
Log In to Comment