2008年3月9日 星期日

在 kiba-feeder.py 改用 time 模組來處理 rss feed 的時間標記

在 kiba-feeder.py 中原本用 20081208182341 這個字串來代替
Sat, 08 Mar 2008 18:23:41 +0000
並選擇最大的放在 /tmp/Status.txt 作為最新 feed 的時間標記
但無法處理類似
2008-03-08T03:26:56.686+08:00 的時間字串
經過上次修改可以用 200803081823 和 200803080326 來處理
但是要將這一串字減掉 8 小時得到統一的 UTC 時間不大好處理(我不會而已啦)
所以再次修改用 python 的 time 模組中提供的功能來標記最新的 feed 時間
參考:
http://www.freebsd.org.hk/html/python/tut_tw/tut.html
http://docs.python.org/lib/module-time.html

--- kiba-feeder.py-orig    2008-03-05 20:38:37.000000000 +0800
+++ kiba-feeder.py 2008-03-09 07:56:05.000000000 +0800
@@ -12,6 +12,7 @@
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
+import time

PLUGIN_NAME = "kiba-feeder"

@@ -31,6 +32,10 @@
'http://www.neowin.net/fb.php?page=gamers', \
'http://www.neowin.net/fb.php?page=main', \
'http://www.neowin.net/fb.php?page=software', \
+ 'http://swyear.blogspot.com/feeds/posts/default', \
+ 'http://planet.linux.org.tw/atom.xml', \
+ 'http://hyperrate.com/rss.php?eid=1', \
+ 'http://planetsuse.org/rss20.xml', \
'')

#Set this if you want to use a custom Theme THEME_DIRECTORY
@@ -79,28 +84,34 @@
while True:
if FeedNumber == (len(FEED_URLS)-1):
break
-
+
+ def time_zone(time_string):#check Time Zone eg. +0080, +08:00;return time offset in seconds
+ if time_string[-1] in '1234567890':#Sat, 08 Mar 2008 23:55:02 +0800 or 2008-03-08T22:39:03+00:00
+ if time_string[-3] == ':':#2008-03-08T22:39:03+00:00
+ return int(time_string[-6] + '1') * (int(time_string[-5:-3])*3600 + int(time_string[-2:])*60)
+ else:#Sat, 08 Mar 2008 23:55:02 +0800
+ return int(time_string[-5] + '1') * (int(time_string[-4:-2])*3600 + int(time_string[-2:])*60)
+ else:#Sat, 08 Mar 2008 22:22:00 GMT or 2006-04-18T18:11:15Z;fix me for other time zone string
+ return 0
+
Feed = feedparser.parse(FEED_URLS[FeedNumber])

FeedTitle = Feed['channel']['title']
LatestItemTitle = Feed['items'][0]['title']
LatestItemURL = Feed['items'][0]['link']
LatestItemDate = Feed['items'][0]['date']
-
- Year = str(LatestItemDate.split(' ')[3])
- Month = str(12)
- Day = str(LatestItemDate.split(' ')[1])
- Time = str(LatestItemDate.split(' ')[4])
- Hour = str(Time.split(':')[0])
- Minute = str(Time.split(':')[1])
- Second = str(Time.split(':')[2])
-
- if int(Year + Month + Day + Hour + Minute + Second) > self.TimeCalc:
- self.TimeCalc = int(Year + Month + Day + Hour + Minute + Second)
-
+ time_offset = time_zone(LatestItemDate)
+ if LatestItemDate[1] in '1234567890':#2008-03-08T03:26:56.686+08:00 or 2008-03-08T22:39:03+00:00
+ time_string = LatestItemDate[:19]
+ local_time = time.strptime(time_string,"%Y-%m-%dT%H:%M:%S")
+ else:
+ time_string = LatestItemDate[:25]#Sat, 08 Mar 2008 23:55:02 +0800
+ local_time = time.strptime(time_string,"%a, %d %b %Y %H:%M:%S")
+ utc_time_stamp = time.mktime(local_time) - time_offset
+ if utc_time_stamp > self.TimeCalc:
+ self.TimeCalc = utc_time_stamp
self.description = ('%s : %s' % (FeedTitle,LatestItemTitle))
self.URL = LatestItemURL
-
FeedNumber = (FeedNumber + 1)

self.status = open(STATUS_DIR + '/Status.txt','r')


寫程式很有趣,但我如果是靠這個吃飯,應該會過勞死吧
(也因為對程式語言不熟的緣故)

沒有留言: